Spaces:
Sleeping
Sleeping
Owadokun Tosin Tobi commited on
Update research.py
Browse files- src/tools/research.py +22 -48
src/tools/research.py
CHANGED
|
@@ -1,53 +1,27 @@
|
|
| 1 |
import os
|
| 2 |
-
from dotenv import load_dotenv
|
| 3 |
from tavily import TavilyClient
|
| 4 |
-
from
|
| 5 |
-
from langgraph.prebuilt import ToolNode
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
tavily_client = TavilyClient(api_key=os.getenv("TAVILY_API_KEY"))
|
| 10 |
-
|
| 11 |
-
def perform_research(state: Dict) -> Dict:
|
| 12 |
"""
|
| 13 |
-
|
| 14 |
-
Useful for gathering comprehensive context from multiple angles.
|
| 15 |
"""
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
max_results=6,
|
| 35 |
-
search_depth="advanced",
|
| 36 |
-
include_raw_content=False
|
| 37 |
-
)
|
| 38 |
-
for result in response.get("results", []):
|
| 39 |
-
if result["url"] not in seen_urls:
|
| 40 |
-
seen_urls.add(result["url"])
|
| 41 |
-
all_results.append({
|
| 42 |
-
"title": result["title"],
|
| 43 |
-
"url": result["url"],
|
| 44 |
-
"content": result["content"][:2000]
|
| 45 |
-
})
|
| 46 |
-
except Exception as e:
|
| 47 |
-
print(f"Tavily error on query '{query}': {e}")
|
| 48 |
-
continue
|
| 49 |
-
|
| 50 |
-
return {"research_data": all_results[:20]} # Cap to avoid token blowup
|
| 51 |
-
|
| 52 |
-
# LangGraph ToolNode
|
| 53 |
-
research_tool = ToolNode(tools=[perform_research])
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from tavily import TavilyClient
|
| 3 |
+
from langchain_core.tools import tool
|
|
|
|
| 4 |
|
| 5 |
+
@tool
|
| 6 |
+
def perform_research(topic: str):
|
|
|
|
|
|
|
|
|
|
| 7 |
"""
|
| 8 |
+
Searches the web for recent news using Tavily.
|
|
|
|
| 9 |
"""
|
| 10 |
+
api_key = os.getenv("TAVILY_API_KEY")
|
| 11 |
+
if not api_key:
|
| 12 |
+
return "Error: TAVILY_API_KEY not found."
|
| 13 |
+
|
| 14 |
+
client = TavilyClient(api_key=api_key)
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
# Search specifically for news
|
| 18 |
+
response = client.search(query=topic, topic="news", days=2)
|
| 19 |
+
results = response.get("results", [])
|
| 20 |
+
|
| 21 |
+
context = []
|
| 22 |
+
for r in results[:4]:
|
| 23 |
+
context.append(f"Title: {r['title']}\nURL: {r['url']}\nSummary: {r['content']}")
|
| 24 |
+
|
| 25 |
+
return "\n\n".join(context)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"Search failed: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|