File size: 1,039 Bytes
26754ce
 
 
 
 
 
 
 
 
247039e
26754ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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.")