from langchain.tools import BaseTool from config import config from langchain_openai.chat_models import ChatOpenAI from tools.web_search_tool import WebSearchTool class SearchSummaryTool(BaseTool): """ Инструмент-обёртка для web_search: сначала получает топ-5 результатов поиска, затем синтезирует краткий фактологический ответ с помощью LLM. """ name: str = "search_summary" description: str = ( "Perform a web search and extract concise factual answers. Use for online facts not in GAIA/Wikipedia—e.g. sports stats, Olympic participation, published papers, museum specimen locations, competition winners, and other up-to-date info." ) def _run(self, query: str) -> str: # 1) Получаем сырые результаты поиска scraper = WebSearchTool() raw_results = scraper._run(query) # 2) Формируем подсказку для LLM system_prompt = ( f"You are a knowledgeable assistant. The user asked: '{query}'.\n" f"Below are the top 5 search results (title — URL) from Google:\n{raw_results}\n" "Please provide a concise factual answer to the user's question in one sentence, " "citing the best source if appropriate" "and return exactly the requested fact—no apologies, no filler, " "just the concrete answer ready for exact-match checking. " "Be precise, concise, and follow any formatting instructions." ) # 3) Инициализируем LLM и генерируем ответ llm = ChatOpenAI( model_name=config.OPENAI_MODEL, openai_api_key=config.OPENAI_API_KEY, temperature = 0.2 ) answer = llm.invoke(system_prompt) return answer async def _arun(self, query: str) -> str: raise NotImplementedError("Async not supported.")