| from langchain.tools import BaseTool | |
| import requests | |
| from bs4 import BeautifulSoup | |
| class WebSearchTool(BaseTool): | |
| """ | |
| Инструмент для простого веб-поиска (Google) — возвращает топ-5 результатов. | |
| """ | |
| name: str = "web_search" | |
| description: str = "Perform generic web searches (beyond SearchSummaryTool) with more flexible scraping and parsing. Use when you need custom page analysis or non-trivial web-data extraction." | |
| def _run(self, query: str) -> str: | |
| resp = requests.get( | |
| 'https://www.google.com/search', | |
| params={'q': query}, | |
| headers={'User-Agent': 'Mozilla/5.0'} | |
| ) | |
| soup = BeautifulSoup(resp.text, 'html.parser') | |
| results = [] | |
| for g in soup.select('div.yuRUbf')[:5]: | |
| a = g.a | |
| results.append(f"{a.text} - {a['href']}") | |
| return "\n".join(results) | |
| async def _arun(self, query: str) -> str: | |
| raise NotImplementedError("Async not supported.") | |