Mohammad Haghir commited on
Commit
e34a8fa
·
1 Parent(s): 10c7cb1

tavily ret

Browse files
Files changed (3) hide show
  1. agent_utils.py +22 -0
  2. app.py +2 -2
  3. requirements.txt +2 -1
agent_utils.py CHANGED
@@ -1,5 +1,7 @@
1
  from langchain_community.document_loaders import WikipediaLoader
2
  import arxiv
 
 
3
 
4
  def wiki_ret(question: str) -> str:
5
  """ Retrieve docs from wikipedia """
@@ -37,3 +39,23 @@ def arxiv_ret(query: str, max_results: int = 3) -> str:
37
  return "No relevant papers found."
38
 
39
  return "\n\n".join(results)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from langchain_community.document_loaders import WikipediaLoader
2
  import arxiv
3
+ import os
4
+ from tavily import TavilyClient
5
 
6
  def wiki_ret(question: str) -> str:
7
  """ Retrieve docs from wikipedia """
 
39
  return "No relevant papers found."
40
 
41
  return "\n\n".join(results)
42
+
43
+
44
+ def tavily_ret(query: str, max_results: int = 3) -> str:
45
+ """Use Tavily to retrieve web-based information about a topic."""
46
+ api_key = os.getenv("TAVILY_API_KEY")
47
+ if not api_key:
48
+ return "Tavily API key not found."
49
+
50
+ client = TavilyClient(api_key=api_key)
51
+
52
+ results = client.search(query=query, search_depth="basic", max_results=max_results)
53
+
54
+ if not results["results"]:
55
+ return "No relevant information found."
56
+
57
+ summaries = []
58
+ for item in results["results"]:
59
+ summaries.append(f"Title: {item['title']}\nURL: {item['url']}\nSnippet: {item['content']}\n")
60
+
61
+ return "\n\n".join(summaries)
app.py CHANGED
@@ -17,7 +17,7 @@ from langchain_core.messages import HumanMessage
17
  from langgraph.graph import START, END, StateGraph
18
  from langgraph.prebuilt import ToolNode, tools_condition
19
 
20
- from agent_utils import wiki_ret, arxiv_ret
21
 
22
  # (Keep Constants as is)
23
  # --- Constants ---
@@ -82,7 +82,7 @@ class BasicAgent:
82
  def create_graph(self):
83
  builder = StateGraph(GraphState)
84
  builder.add_node("agent", self.agent)
85
- builder.add_node("tools", ToolNode(tools = [wiki_ret, arxiv_ret]))
86
 
87
  builder.add_edge(START, "agent")
88
  builder.add_conditional_edges("agent", tools_condition)
 
17
  from langgraph.graph import START, END, StateGraph
18
  from langgraph.prebuilt import ToolNode, tools_condition
19
 
20
+ from agent_utils import wiki_ret, arxiv_ret, tavily_ret
21
 
22
  # (Keep Constants as is)
23
  # --- Constants ---
 
82
  def create_graph(self):
83
  builder = StateGraph(GraphState)
84
  builder.add_node("agent", self.agent)
85
+ builder.add_node("tools", ToolNode(tools = [wiki_ret, arxiv_ret, tavily_ret]))
86
 
87
  builder.add_edge(START, "agent")
88
  builder.add_conditional_edges("agent", tools_condition)
requirements.txt CHANGED
@@ -5,4 +5,5 @@ langchain-community
5
  langchain-core
6
  wikipedia
7
  langgraph
8
- arxiv
 
 
5
  langchain-core
6
  wikipedia
7
  langgraph
8
+ arxiv
9
+ tavily-python