-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathtools.py
More file actions
47 lines (36 loc) · 1.42 KB
/
Copy pathtools.py
File metadata and controls
47 lines (36 loc) · 1.42 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Tavily-backed web search tools."""
import os
from typing import Any
from langchain_core.tools import tool
from tavily import TavilyClient
def _search_tavily(query: str, max_results: int = 5) -> list[dict[str, Any]]:
"""Search Tavily and normalize its results."""
print(f"[TOOL] web_search: query='{query}', max_results={max_results}")
tavily_key = os.environ.get("TAVILY_API_KEY")
if not tavily_key:
raise RuntimeError("TAVILY_API_KEY not set")
try:
client = TavilyClient(api_key=tavily_key)
results = client.search(
query=query,
max_results=max_results,
include_raw_content=False,
topic="general",
)
formatted_results = []
for r in results.get("results", []):
formatted_results.append(
{
"url": r.get("url", ""),
"title": r.get("title", ""),
"content": (r.get("content") or "")[:3000],
}
)
print(f"[TOOL] web_search: found {len(formatted_results)} results")
return formatted_results
except Exception as error:
raise RuntimeError("Tavily search failed") from error
@tool
def web_search(query: str, max_results: int = 5) -> list[dict[str, Any]]:
"""Search the live web and return source URLs with concise content snippets."""
return _search_tavily(query, max_results)