grantforge-api / backend /tools /web_search.py
GrantForge Bot
Deploy to Hugging Face
afd56bc
from tavily import TavilyClient
import os
from typing import List, Dict
def grant_search_tool(query: str) -> List[Dict]:
"""
Rzeczywiste wyszukiwanie nabor贸w w internecie przy u偶yciu Tavily.
"""
api_key = os.environ.get("TAVILY_API_KEY")
if not api_key:
return [
{
"title": "Brak klucza Tavily",
"description": "Dodaj TAVILY_API_KEY",
"url": "",
}
]
try:
client = TavilyClient(api_key=api_key)
response = client.search(
query=query + " dotacje unijne PARP NCBR",
search_depth="advanced",
max_results=10,
)
structured_results = []
for r in response.get("results", []):
structured_results.append(
{
"title": r.get("title", "Znaleziony dokument programowy"),
"description": r.get("content", ""),
"url": r.get("url", ""),
}
)
return (
structured_results
if structured_results
else [
{
"title": "Brak precyzyjnych wynik贸w",
"description": "Rozszerz zapytanie",
"url": "",
}
]
)
except Exception as e:
return [{"title": "B艂膮d wyszukiwania Tavily", "description": str(e), "url": ""}]