From ed2a1e7db960fa6aa4c1c298272164c6f48f5bc0 Mon Sep 17 00:00:00 2001 From: thiswillbeyourgithub <26625900+thiswillbeyourgithub@users.noreply.github.com> Date: Thu, 12 Sep 2024 15:58:26 +0200 Subject: [PATCH] enh: use non hybrid search as fallback if hybrid search failed --- backend/open_webui/apps/rag/utils.py | 30 ++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/backend/open_webui/apps/rag/utils.py b/backend/open_webui/apps/rag/utils.py index c179f47c0..dd4cdd007 100644 --- a/backend/open_webui/apps/rag/utils.py +++ b/backend/open_webui/apps/rag/utils.py @@ -167,6 +167,7 @@ def query_collection_with_hybrid_search( r: float, ): results = [] + failed = 0 for collection_name in collection_names: try: result = query_doc_with_hybrid_search( @@ -183,6 +184,10 @@ def query_collection_with_hybrid_search( "Error when querying the collection with " 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) @@ -265,19 +270,25 @@ def get_rag_context( continue try: + context = None if file["type"] == "text": context = file["content"] else: if hybrid_search: - context = query_collection_with_hybrid_search( - collection_names=collection_names, - query=query, - embedding_function=embedding_function, - k=k, - reranking_function=reranking_function, - r=r, - ) - else: + try: + context = query_collection_with_hybrid_search( + collection_names=collection_names, + query=query, + embedding_function=embedding_function, + k=k, + reranking_function=reranking_function, + r=r, + ) + 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( collection_names=collection_names, query=query, @@ -286,7 +297,6 @@ def get_rag_context( ) except Exception as e: log.exception(e) - context = None if context: relevant_contexts.append({**context, "source": file})