Spaces:
Runtime error
Runtime error
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -20,6 +20,10 @@ import re
|
|
| 20 |
from datetime import datetime, timedelta
|
| 21 |
from langchain_core.tools import tool
|
| 22 |
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Dictionary of known GAIA-style entities → canonical Wikipedia URLs
|
| 25 |
WIKIPEDIA_PAGES = {
|
|
@@ -32,6 +36,26 @@ WIKIPEDIA_PAGES = {
|
|
| 32 |
# ➕ add more GAIA topics here
|
| 33 |
}
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
@tool
|
| 36 |
def get_article_nominator_from_fac_page(title: str) -> str:
|
| 37 |
"""
|
|
@@ -196,6 +220,9 @@ def route_question(question: str) -> str:
|
|
| 196 |
"""
|
| 197 |
q = question.lower()
|
| 198 |
|
|
|
|
|
|
|
|
|
|
| 199 |
if "wikipedia" in q and any(k in q for k in ["how many", "list", "albums", "awards", "release"]):
|
| 200 |
return "extract_structured_facts_from_url"
|
| 201 |
|
|
@@ -417,6 +444,8 @@ def filter_vegetables(items: list[str]) -> list[str]:
|
|
| 417 |
|
| 418 |
# List of all tools
|
| 419 |
all_tools = [
|
|
|
|
|
|
|
| 420 |
route_question,
|
| 421 |
resolve_wikipedia_url,
|
| 422 |
handle_question,
|
|
|
|
| 20 |
from datetime import datetime, timedelta
|
| 21 |
from langchain_core.tools import tool
|
| 22 |
import logging
|
| 23 |
+
from tavily import TavilyClient
|
| 24 |
+
import os
|
| 25 |
+
|
| 26 |
+
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
|
| 27 |
|
| 28 |
# Dictionary of known GAIA-style entities → canonical Wikipedia URLs
|
| 29 |
WIKIPEDIA_PAGES = {
|
|
|
|
| 36 |
# ➕ add more GAIA topics here
|
| 37 |
}
|
| 38 |
|
| 39 |
+
@tool
|
| 40 |
+
def extract_number_from_snippets(snippets: list[str]) -> Optional[int]:
|
| 41 |
+
for s in snippets:
|
| 42 |
+
match = re.search(r"\b\d{1,4}\b", s)
|
| 43 |
+
if match:
|
| 44 |
+
return int(match.group())
|
| 45 |
+
return None
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
@tool
|
| 49 |
+
def tavily_search(query: str, k: int = 5) -> list[str]:
|
| 50 |
+
"""
|
| 51 |
+
Perform a web search using the Tavily API and return up to k relevant snippets.
|
| 52 |
+
"""
|
| 53 |
+
try:
|
| 54 |
+
response = client.search(query=query, search_depth="advanced", max_results=k)
|
| 55 |
+
return [r["content"] for r in response.get("results", [])]
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return [f"❌ Error during Tavily search: {str(e)}"]
|
| 58 |
+
|
| 59 |
@tool
|
| 60 |
def get_article_nominator_from_fac_page(title: str) -> str:
|
| 61 |
"""
|
|
|
|
| 220 |
"""
|
| 221 |
q = question.lower()
|
| 222 |
|
| 223 |
+
if "who" in q or "what" in q or "how many" in q or "when" in q:
|
| 224 |
+
return "tavily_search"
|
| 225 |
+
|
| 226 |
if "wikipedia" in q and any(k in q for k in ["how many", "list", "albums", "awards", "release"]):
|
| 227 |
return "extract_structured_facts_from_url"
|
| 228 |
|
|
|
|
| 444 |
|
| 445 |
# List of all tools
|
| 446 |
all_tools = [
|
| 447 |
+
extract_number_from_snippets,
|
| 448 |
+
tavily_search,
|
| 449 |
route_question,
|
| 450 |
resolve_wikipedia_url,
|
| 451 |
handle_question,
|