From 35d385e9cc7e3aa7000a4c3b413135177e598dcb Mon Sep 17 00:00:00 2001 From: Tim Baek Date: Wed, 7 Jan 2026 10:21:05 -0500 Subject: [PATCH] refac --- backend/open_webui/tools/builtin.py | 17 ++++++++++------- backend/open_webui/utils/middleware.py | 6 +++--- backend/open_webui/utils/tools.py | 4 ++-- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/backend/open_webui/tools/builtin.py b/backend/open_webui/tools/builtin.py index 257805f38..8f3c2a2df 100644 --- a/backend/open_webui/tools/builtin.py +++ b/backend/open_webui/tools/builtin.py @@ -15,7 +15,7 @@ from typing import Optional from fastapi import Request from open_webui.models.users import UserModel -from open_webui.routers.retrieval import search_web +from open_webui.routers.retrieval import search_web as _search_web from open_webui.retrieval.utils import get_content_from_url from open_webui.routers.images import ( image_generations, @@ -142,14 +142,16 @@ async def calculate_timestamp( # ============================================================================= -async def web_search( +async def search_web( query: str, count: int = 5, __request__: Request = None, __user__: dict = None, ) -> str: """ - Search the web for information on a given topic. + Search the public web for information. Best for current events, external references, + or topics not covered in internal documents. If knowledge base tools are available, + consider checking those first for internal information. :param query: The search query to look up :param count: Number of results to return (default: 5) @@ -162,7 +164,7 @@ async def web_search( engine = __request__.app.state.config.WEB_SEARCH_ENGINE user = UserModel(**__user__) if __user__ else None - results = search_web(__request__, engine, query, user) + results = _search_web(__request__, engine, query, user) # Limit results results = results[:count] if results else [] @@ -172,7 +174,7 @@ async def web_search( ensure_ascii=False, ) except Exception as e: - log.exception(f"web_search error: {e}") + log.exception(f"search_web error: {e}") return json.dumps({"error": str(e)}) @@ -1416,8 +1418,9 @@ async def query_knowledge_bases( __model_knowledge__: list[dict] = None, ) -> str: """ - Search knowledge bases using semantic/vector search to find relevant content chunks. - Handles collections (KBs), individual files, and notes. + Search internal knowledge bases using semantic/vector search. This should be your first + choice for finding information before searching the web. Searches across collections (KBs), + individual files, and notes that the user has access to. :param query: The search query to find semantically relevant content :param knowledge_ids: Optional list of KB ids to limit search to specific knowledge bases diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index c8751bba2..edbf86626 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -159,7 +159,7 @@ def get_citation_source_from_tool_result( Returns a list of sources (usually one, but query_knowledge_bases may return multiple). """ try: - if tool_name == "web_search": + if tool_name == "search_web": # Parse JSON array: [{"title": "...", "link": "...", "snippet": "..."}] results = json.loads(tool_result) documents = [] @@ -178,7 +178,7 @@ def get_citation_source_from_tool_result( }) return [{ - "source": {"name": "web_search", "id": "web_search"}, + "source": {"name": "search_web", "id": "search_web"}, "document": documents, "metadata": metadata, }] @@ -3233,7 +3233,7 @@ async def process_chat_response( ) # Extract citation sources from tool results - if tool_function_name in ["web_search", "view_knowledge_file", "query_knowledge_bases"] and tool_result: + if tool_function_name in ["search_web", "view_knowledge_file", "query_knowledge_bases"] and tool_result: try: citation_sources = get_citation_source_from_tool_result( tool_name=tool_function_name, diff --git a/backend/open_webui/utils/tools.py b/backend/open_webui/utils/tools.py index 8d30b6ee8..75b2b9aea 100644 --- a/backend/open_webui/utils/tools.py +++ b/backend/open_webui/utils/tools.py @@ -44,7 +44,7 @@ from open_webui.env import ( AIOHTTP_CLIENT_SESSION_TOOL_SERVER_SSL, ) from open_webui.tools.builtin import ( - web_search, + search_web, fetch_url, generate_image, edit_image, @@ -389,7 +389,7 @@ def get_builtin_tools( if getattr(request.app.state.config, "ENABLE_WEB_SEARCH", False) and get_model_capability( "web_search" ): - builtin_functions.extend([web_search, fetch_url]) + builtin_functions.extend([search_web, fetch_url]) # Add image generation/edit tools if enabled globally AND model has image_generation capability if getattr(