wishwakankanamg commited on
Commit
835cee8
·
1 Parent(s): 4e1775f
__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
@@ -97,43 +97,64 @@ def wiki_search(query: str) -> str:
97
  # ])
98
  # return {"web_results": formatted_search_docs}
99
  @tool
100
- def web_search(query: str) -> str:
101
  """Search Tavily for a query and return maximum 3 results.
 
102
 
103
  Args:
104
- query: The search query."""
 
 
 
105
  try:
106
- # Initialize the tool
107
  tavily_tool = TavilySearchResults(max_results=3)
108
- # Invoke it correctly
109
- search_results = tavily_tool.invoke(input=query) # <--- CORRECTED LINE
110
-
111
- # The result of TavilySearchResults.invoke is usually a list of strings or a single string.
112
- # Let's check its type and format accordingly.
113
- # Typically, TavilySearchResults directly returns a list of Document objects
114
- # or a list of dictionaries if you've configured it differently.
115
- # For the default, it's often a list of strings or a single concatenated string.
116
- # If it returns a list of Document objects (which is common for loaders/retrievers):
117
- if isinstance(search_results, list) and all(hasattr(doc, 'metadata') and hasattr(doc, 'page_content') for doc in search_results):
118
- formatted_search_docs = "\n\n---\n\n".join(
119
- [
120
- f'<Document source="{doc.metadata.get("source", "N/A")}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
121
- for doc in search_results
122
- ])
123
- # If it returns a list of strings (less likely for Tavily in recent versions, but good to check)
124
- elif isinstance(search_results, list) and all(isinstance(item, str) for item in search_results):
125
- formatted_search_docs = "\n\n---\n\n".join(search_results)
126
- # If it returns a single string
127
- elif isinstance(search_results, str):
128
- formatted_search_docs = search_results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  else:
130
- # Fallback or handle unexpected format
131
- print(f"Unexpected Tavily search result format: {type(search_results)}")
132
- formatted_search_docs = str(search_results)
 
 
133
 
134
- return {"web_results": formatted_search_docs}
135
  except Exception as e:
136
  print(f"Error during Tavily search for query '{query}': {e}")
 
137
  return {"web_results": f"Error performing web search: {e}"}
138
 
139
  @tool
 
97
  # ])
98
  # return {"web_results": formatted_search_docs}
99
  @tool
100
+ def web_search(query: str) -> dict: # Changed return type annotation to dict
101
  """Search Tavily for a query and return maximum 3 results.
102
+ Each result will be formatted with its source URL and content.
103
 
104
  Args:
105
+ query: The search query.
106
+ """
107
+ print(f"\n--- Web Search Tool ---") # For debugging
108
+ print(f"Received query: {query}")
109
  try:
 
110
  tavily_tool = TavilySearchResults(max_results=3)
111
+ # .invoke() for TavilySearchResults typically expects 'input'
112
+ # and returns a list of dictionaries
113
+ search_results_list = tavily_tool.invoke(input=query)
114
+
115
+ print(f"Raw Tavily search results type: {type(search_results_list)}")
116
+ if isinstance(search_results_list, list):
117
+ print(f"Number of results: {len(search_results_list)}")
118
+ if search_results_list:
119
+ print(f"Type of first result: {type(search_results_list[0])}")
120
+ if isinstance(search_results_list[0], dict):
121
+ print(f"Keys in first result: {search_results_list[0].keys()}")
122
+
123
+ formatted_docs = []
124
+ if isinstance(search_results_list, list):
125
+ for doc_dict in search_results_list:
126
+ if isinstance(doc_dict, dict):
127
+ source = doc_dict.get("url", "N/A")
128
+ content = doc_dict.get("content", "")
129
+ # title = doc_dict.get("title", "") # Optionally include title
130
+ # score = doc_dict.get("score", "") # Optionally include score
131
+
132
+ # Constructing the XML-like format you desire
133
+ formatted_doc = (
134
+ f'<Document source="{source}">\n'
135
+ f'{content}\n'
136
+ f'</Document>'
137
+ )
138
+ formatted_docs.append(formatted_doc)
139
+ else:
140
+ # If an item in the list is not a dict, convert it to string
141
+ print(f"Warning: Unexpected item type in Tavily results list: {type(doc_dict)}")
142
+ formatted_docs.append(str(doc_dict))
143
+
144
+ final_formatted_string = "\n\n---\n\n".join(formatted_docs)
145
+
146
+ elif isinstance(search_results_list, str): # Less common, but for robustness
147
+ final_formatted_string = search_results_list
148
  else:
149
+ print(f"Unexpected Tavily search result format overall: {type(search_results_list)}")
150
+ final_formatted_string = str(search_results_list) # Fallback
151
+
152
+ print(f"Formatted search docs for LLM:\n{final_formatted_string[:500]}...") # Print a snippet
153
+ return {"web_results": final_formatted_string}
154
 
 
155
  except Exception as e:
156
  print(f"Error during Tavily search for query '{query}': {e}")
157
+ # It's good practice to return an error message in the expected dict format
158
  return {"web_results": f"Error performing web search: {e}"}
159
 
160
  @tool
langgraph_state_diagram.png CHANGED
system_prompt.txt CHANGED
@@ -1 +1,17 @@
1
- You are a helpful assistant tasked with answering questions using a set of tools. Now, I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.Your answer should only start with "FINAL ANSWER: ", then follows with the answer.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ You are a highly accurate assistant tasked with answering questions. Your primary goal is to provide 100% correct and verified answers using a set of tools.
2
+
3
+ Report your thoughts during the process.
4
+
5
+ When you have a high-confidence, verified answer, finish with the following template:
6
+ FINAL ANSWER: [YOUR FINAL ANSWER]
7
+
8
+ GUIDELINES FOR YOUR FINAL ANSWER:
9
+ 1. **ACCURACY IS PARAMOUNT:** Only provide an answer if you are highly confident in its factual correctness based on the information from the tools.
10
+ 2. **UNCERTAINTY:** If you cannot find a definitive answer, if the information is ambiguous/conflicting, or if you cannot be 100% certain, your FINAL ANSWER MUST explicitly state this (e.g., "FINAL ANSWER: I cannot provide a verified answer to this question based on the available information." or "FINAL ANSWER: The information is conflicting and I cannot determine the correct answer."). DO NOT GUESS.
11
+ 3. **CONCISENESS & COMPLETENESS:** Be as concise as possible, but ensure your answer is complete and contains all information necessary for it to be fully correct.
12
+ 4. **ANSWER TYPES:**
13
+ * **Numbers:** Use digits (e.g., 123, 4.56). Do not use commas as thousands separators (e.g., 1000 not 1,000). Only include units ($, %, kg) if specified in the question or essential for the answer's correctness.
14
+ * **Strings:** Be precise. Avoid abbreviations unless they are standard and unambiguous. Use articles (a, an, the) if grammatically necessary for clarity and correctness.
15
+ * **Lists:** For comma-separated lists, apply the relevant rules above to each element.
16
+
17
+ Your response should ONLY include your thought process (if any) followed by the "FINAL ANSWER: " line.