add web_tools.py
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
"""Инструменты веб-поиска для research deep-агента."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
import httpx
|
||||
from dotenv import load_dotenv
|
||||
from langchain.tools import tool
|
||||
from markdownify import markdownify
|
||||
|
||||
load_dotenv()
|
||||
|
||||
_HEADERS = {
|
||||
"User-Agent": (
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
|
||||
),
|
||||
}
|
||||
_CLIENT = httpx.Client(timeout=30.0, follow_redirects=True, headers=_HEADERS)
|
||||
|
||||
|
||||
@tool
|
||||
def web_search(query: str, max_results: int = 5) -> str:
|
||||
"""Поиск информации в интернете (Tavily).
|
||||
|
||||
Args:
|
||||
query: поисковый запрос
|
||||
max_results: число результатов
|
||||
"""
|
||||
api_key = os.getenv("TAVILY_API_KEY", "")
|
||||
if not api_key:
|
||||
return "TAVILY_API_KEY не задан в .env — поиск недоступен."
|
||||
try:
|
||||
from tavily import TavilyClient
|
||||
|
||||
client = TavilyClient(api_key=api_key)
|
||||
resp = client.search(query, max_results=max_results)
|
||||
parts = []
|
||||
for item in resp.get("results", []):
|
||||
parts.append(
|
||||
f"**{item.get('title', '')}**\n"
|
||||
f"url: {item.get('url', '')}\n"
|
||||
f"snippet: {item.get('content', '')}\n"
|
||||
)
|
||||
return "\n".join(parts) if parts else "Ничего не найдено."
|
||||
except Exception as exc:
|
||||
return f"Ошибка web_search: {exc}"
|
||||
|
||||
|
||||
@tool
|
||||
def get_page_content(url: str) -> str:
|
||||
"""Загрузить и вернуть текст страницы по URL (markdown)."""
|
||||
try:
|
||||
resp = _CLIENT.get(url)
|
||||
if resp.status_code != 200:
|
||||
return f"HTTP {resp.status_code} для {url}"
|
||||
text = markdownify(resp.text)
|
||||
return text[:12000] + ("..." if len(text) > 12000 else "")
|
||||
except Exception as exc:
|
||||
return f"Ошибка загрузки страницы: {exc}"
|
||||
Reference in New Issue
Block a user