From 9a2c60d5954ecbc172d09e9955d52a07d135dcbc Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sat, 21 Mar 2026 17:12:33 -0500 Subject: [PATCH] refac --- backend/open_webui/retrieval/utils.py | 55 ++++++++++++++----------- backend/open_webui/routers/retrieval.py | 1 + backend/open_webui/tools/builtin.py | 1 + 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/backend/open_webui/retrieval/utils.py b/backend/open_webui/retrieval/utils.py index dfb38a9659..dcd0671cd5 100644 --- a/backend/open_webui/retrieval/utils.py +++ b/backend/open_webui/retrieval/utils.py @@ -404,11 +404,34 @@ def get_all_items_from_collections(collection_names: list[str]) -> dict: async def query_collection( + request, collection_names: list[str], queries: list[str], embedding_function, k: int, ) -> dict: + # When request is provided, try hybrid search + reranking if enabled + if request and request.app.state.config.ENABLE_RAG_HYBRID_SEARCH: + try: + reranking_function = ( + (lambda query, documents: request.app.state.RERANKING_FUNCTION(query, documents)) + if request.app.state.RERANKING_FUNCTION + else None + ) + return await query_collection_with_hybrid_search( + collection_names=collection_names, + queries=queries, + embedding_function=embedding_function, + k=k, + reranking_function=reranking_function, + k_reranker=request.app.state.config.TOP_K_RERANKER, + r=request.app.state.config.RELEVANCE_THRESHOLD, + hybrid_bm25_weight=request.app.state.config.HYBRID_BM25_WEIGHT, + enable_enriched_texts=request.app.state.config.ENABLE_RAG_HYBRID_SEARCH_ENRICHED_TEXTS, + ) + except Exception as e: + log.debug(f'Hybrid search failed, falling back to vector search: {e}') + results = [] error = False @@ -1126,31 +1149,13 @@ async def get_sources_from_items( if full_context: query_result = get_all_items_from_collections(collection_names) else: - query_result = None # Initialize to None - if hybrid_search: - try: - query_result = await query_collection_with_hybrid_search( - collection_names=collection_names, - queries=queries, - embedding_function=embedding_function, - k=k, - reranking_function=reranking_function, - k_reranker=k_reranker, - r=r, - hybrid_bm25_weight=hybrid_bm25_weight, - enable_enriched_texts=request.app.state.config.ENABLE_RAG_HYBRID_SEARCH_ENRICHED_TEXTS, - ) - except Exception as e: - log.debug('Error when using hybrid search, using non hybrid search as fallback.') - - # fallback to non-hybrid search - if not hybrid_search and query_result is None: - query_result = await query_collection( - collection_names=collection_names, - queries=queries, - embedding_function=embedding_function, - k=k, - ) + query_result = await query_collection( + request, + collection_names=collection_names, + queries=queries, + embedding_function=embedding_function, + k=k, + ) except Exception as e: log.exception(e) diff --git a/backend/open_webui/routers/retrieval.py b/backend/open_webui/routers/retrieval.py index 30b69ee041..c0ae4803dc 100644 --- a/backend/open_webui/routers/retrieval.py +++ b/backend/open_webui/routers/retrieval.py @@ -2464,6 +2464,7 @@ async def query_collection_handler( ) else: return await query_collection( + request, collection_names=form_data.collection_names, queries=[form_data.query], embedding_function=lambda query, prefix: request.app.state.EMBEDDING_FUNCTION( diff --git a/backend/open_webui/tools/builtin.py b/backend/open_webui/tools/builtin.py index 73589810d6..70ce20a98f 100644 --- a/backend/open_webui/tools/builtin.py +++ b/backend/open_webui/tools/builtin.py @@ -1874,6 +1874,7 @@ async def query_knowledge_files( # Query vector collections if any if collection_names: query_results = await query_collection( + __request__, collection_names=collection_names, queries=[query], embedding_function=embedding_function,