enh: use non hybrid search as fallback if hybrid search failed

This commit is contained in:
thiswillbeyourgithub 2024-09-12 15:58:26 +02:00
parent 53f03f6556
commit ed2a1e7db9

View File

@ -167,6 +167,7 @@ def query_collection_with_hybrid_search(
r: float, r: float,
): ):
results = [] results = []
failed = 0
for collection_name in collection_names: for collection_name in collection_names:
try: try:
result = query_doc_with_hybrid_search( result = query_doc_with_hybrid_search(
@ -183,6 +184,10 @@ def query_collection_with_hybrid_search(
"Error when querying the collection with " "Error when querying the collection with "
f"hybrid_search: {e}" f"hybrid_search: {e}"
) )
failed += 1
if failed == len(collection_names):
raise Exception("Hybrid search failed for all collections. Using "
"Non hybrid search as fallback.")
return merge_and_sort_query_results(results, k=k, reverse=True) return merge_and_sort_query_results(results, k=k, reverse=True)
@ -265,10 +270,12 @@ def get_rag_context(
continue continue
try: try:
context = None
if file["type"] == "text": if file["type"] == "text":
context = file["content"] context = file["content"]
else: else:
if hybrid_search: if hybrid_search:
try:
context = query_collection_with_hybrid_search( context = query_collection_with_hybrid_search(
collection_names=collection_names, collection_names=collection_names,
query=query, query=query,
@ -277,7 +284,11 @@ def get_rag_context(
reranking_function=reranking_function, reranking_function=reranking_function,
r=r, r=r,
) )
else: except Exception as e:
log.debug("Error when using hybrid search, using"
" non hybrid search as fallback.")
if (not hybrid_search) or (context is None):
context = query_collection( context = query_collection(
collection_names=collection_names, collection_names=collection_names,
query=query, query=query,
@ -286,7 +297,6 @@ def get_rag_context(
) )
except Exception as e: except Exception as e:
log.exception(e) log.exception(e)
context = None
if context: if context:
relevant_contexts.append({**context, "source": file}) relevant_contexts.append({**context, "source": file})