Owadokun Tosin Tobi commited on
Commit
2a8d0e3
·
unverified ·
1 Parent(s): 76b50fb

Update research.py

Browse files
Files changed (1) hide show
  1. 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 typing import List, Dict
5
- from langgraph.prebuilt import ToolNode
6
 
7
- load_dotenv()
8
-
9
- tavily_client = TavilyClient(api_key=os.getenv("TAVILY_API_KEY"))
10
-
11
- def perform_research(state: Dict) -> Dict:
12
  """
13
- Research Tool: Performs multi-query search on Tavily based on the topic.
14
- Useful for gathering comprehensive context from multiple angles.
15
  """
16
- topic = state.topic
17
-
18
- # Smart query planning (multi-perspective)
19
- base_queries = [
20
- topic,
21
- f"{topic} latest news 2025 OR 2026",
22
- f"{topic} controversies OR criticisms OR debates",
23
- f"{topic} statistics OR data OR impact",
24
- f"{topic} implications OR future outlook"
25
- ]
26
-
27
- all_results: List[Dict] = []
28
- seen_urls = set()
29
-
30
- for query in base_queries:
31
- try:
32
- response = tavily_client.search(
33
- query=query,
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)}"