wishwakankanamg commited on
Commit
767d298
·
1 Parent(s): dd5b40c
Files changed (2) hide show
  1. __pycache__/agent.cpython-310.pyc +0 -0
  2. agent.py +53 -9
__pycache__/agent.cpython-310.pyc CHANGED
Binary files a/__pycache__/agent.cpython-310.pyc and b/__pycache__/agent.cpython-310.pyc differ
 
agent.py CHANGED
@@ -69,19 +69,58 @@ def modulus(a: int, b: int) -> int:
69
  """
70
  return a % b
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  @tool
73
- def wiki_search(query: str) -> str:
74
- """Search Wikipedia for a query and return maximum 2 results.
75
 
76
  Args:
77
  query: The search query."""
78
- search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
79
- formatted_search_docs = "\n\n---\n\n".join(
80
- [
81
- f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
82
- for doc in search_docs
83
- ])
84
- return {"wiki_results": formatted_search_docs}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  @tool
87
  def web_search(query: str) -> str:
@@ -154,6 +193,11 @@ hf_token = os.environ.get('HF_TOKEN')
154
  if not hf_token:
155
  raise ValueError("Hugging Face API token (HF_TOKEN) not found in environment variables.")
156
 
 
 
 
 
 
157
  # Build graph function
158
  def build_graph(provider: str = "huggingface"):
159
 
 
69
  """
70
  return a % b
71
 
72
+ # @tool
73
+ # def wiki_search(query: str) -> str:
74
+ # """Search Wikipedia for a query and return maximum 2 results.
75
+
76
+ # Args:
77
+ # query: The search query."""
78
+ # search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
79
+ # formatted_search_docs = "\n\n---\n\n".join(
80
+ # [
81
+ # f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
82
+ # for doc in search_docs
83
+ # ])
84
+ # return {"wiki_results": formatted_search_docs}
85
  @tool
86
+ def web_search(query: str) -> str:
87
+ """Search Tavily for a query and return maximum 3 results.
88
 
89
  Args:
90
  query: The search query."""
91
+ try:
92
+ # Initialize the tool
93
+ tavily_tool = TavilySearchResults(max_results=3)
94
+ # Invoke it correctly
95
+ search_results = tavily_tool.invoke(input=query) # <--- CORRECTED LINE
96
+
97
+ # The result of TavilySearchResults.invoke is usually a list of strings or a single string.
98
+ # Let's check its type and format accordingly.
99
+ # Typically, TavilySearchResults directly returns a list of Document objects
100
+ # or a list of dictionaries if you've configured it differently.
101
+ # For the default, it's often a list of strings or a single concatenated string.
102
+ # If it returns a list of Document objects (which is common for loaders/retrievers):
103
+ if isinstance(search_results, list) and all(hasattr(doc, 'metadata') and hasattr(doc, 'page_content') for doc in search_results):
104
+ formatted_search_docs = "\n\n---\n\n".join(
105
+ [
106
+ f'<Document source="{doc.metadata.get("source", "N/A")}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
107
+ for doc in search_results
108
+ ])
109
+ # If it returns a list of strings (less likely for Tavily in recent versions, but good to check)
110
+ elif isinstance(search_results, list) and all(isinstance(item, str) for item in search_results):
111
+ formatted_search_docs = "\n\n---\n\n".join(search_results)
112
+ # If it returns a single string
113
+ elif isinstance(search_results, str):
114
+ formatted_search_docs = search_results
115
+ else:
116
+ # Fallback or handle unexpected format
117
+ print(f"Unexpected Tavily search result format: {type(search_results)}")
118
+ formatted_search_docs = str(search_results)
119
+
120
+ return {"web_results": formatted_search_docs}
121
+ except Exception as e:
122
+ print(f"Error during Tavily search for query '{query}': {e}")
123
+ return {"web_results": f"Error performing web search: {e}"}
124
 
125
  @tool
126
  def web_search(query: str) -> str:
 
193
  if not hf_token:
194
  raise ValueError("Hugging Face API token (HF_TOKEN) not found in environment variables.")
195
 
196
+ tavili_key = os.environ.get('TAVILY_API_KEY')
197
+ if not tavili_key:
198
+ raise ValueError("Hugging Face API token (HF_TOKEN) not found in environment variables.")
199
+
200
+
201
  # Build graph function
202
  def build_graph(provider: str = "huggingface"):
203