naman1102 commited on
Commit
919fd15
·
1 Parent(s): 7561460
Files changed (2) hide show
  1. agent.py +6 -6
  2. tools.py +37 -24
agent.py CHANGED
@@ -11,17 +11,13 @@ from tools import (
11
  SYSTEM_PROMPT = """
12
  You are a helpful AI assistant. You will answer questions by thinking step-by-step, taking actions using tools when necessary, and finishing with a final answer.
13
 
14
- When you want to use a tool, respond *exactly* in the following format:
15
 
16
- Thought: [your reasoning]
17
- Action: [tool_name]
18
  Action Input: [input]
19
 
20
  When you receive an observation, continue reasoning. Write:
21
 
22
- Thought: [final reasoning]
23
- FINAL ANSWER: [your answer]
24
-
25
  IMPORTANT:
26
  - 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.
27
  - If using tools that require a `task_id`, only use the value provided.
@@ -31,6 +27,10 @@ IMPORTANT:
31
  - Try to use the web search tool first before using wiki and arxiv tools.
32
  - If wiki tool doesnt give you enough information, try again with a shorter query, or a more genral query. Dont use a query that is too specific.
33
  - DOnt user wiki tool, web search tool and arxiv tool more than 3 times.
 
 
 
 
34
  """
35
 
36
  TOOLS = [
 
11
  SYSTEM_PROMPT = """
12
  You are a helpful AI assistant. You will answer questions by thinking step-by-step, taking actions using tools when necessary, and finishing with a final answer.
13
 
 
14
 
15
+
16
+
17
  Action Input: [input]
18
 
19
  When you receive an observation, continue reasoning. Write:
20
 
 
 
 
21
  IMPORTANT:
22
  - 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.
23
  - If using tools that require a `task_id`, only use the value provided.
 
27
  - Try to use the web search tool first before using wiki and arxiv tools.
28
  - If wiki tool doesnt give you enough information, try again with a shorter query, or a more genral query. Dont use a query that is too specific.
29
  - DOnt user wiki tool, web search tool and arxiv tool more than 3 times.
30
+
31
+ Return the final answer in the following format:
32
+ Thought: [final reasoning]
33
+ FINAL ANSWER: [your answer]
34
  """
35
 
36
  TOOLS = [
tools.py CHANGED
@@ -219,12 +219,21 @@ def wikipedia_search_tool(wiki_query: str) -> str:
219
  - "What is the capital of France?"
220
  - "Find information about quantum computing"
221
  - "What is the history of the internet?"
222
- If no valid wiki_query is provided, returns {}.
223
  """
224
  print("reached wikipedia search tool")
225
- query = wiki_query
 
 
 
 
 
226
  if not query:
227
- return {}
 
 
 
 
228
 
229
  try:
230
  # 1) Use the MediaWiki API to search for page titles matching the query
@@ -240,16 +249,17 @@ def wikipedia_search_tool(wiki_query: str) -> str:
240
  search_data = search_resp.json()
241
 
242
  search_results = search_data.get("query", {}).get("search", [])
243
- # print("wikipedia: search_results",search_results)
244
  if not search_results:
245
- print(f"No Wikipedia page found for '{query}'.")
246
- return f"No Wikipedia page found for '{query}'."
 
247
 
248
  # 2) Take the first search result's title
249
  first_title = search_results[0].get("title", "")
250
  if not first_title:
251
- print("Unexpected format from Wikipedia search.")
252
- return "Unexpected format from Wikipedia search."
 
253
 
254
  # 3) Fetch the page summary for that title via the REST summary endpoint
255
  title_for_url = requests.utils.requote_uri(first_title)
@@ -262,16 +272,18 @@ def wikipedia_search_tool(wiki_query: str) -> str:
262
  summary_text = summary_data.get("extract")
263
  if not summary_text:
264
  summary_text = summary_data.get("description", "No summary available.")
 
 
 
265
  print("Submitted wiki successfully")
266
- return f"Title: {first_title}\n\n{summary_text}"
267
-
268
 
269
  except requests.exceptions.RequestException as e:
270
  print("Wikipedia search error: ", e)
271
- return f"Wikipedia search error: {e}"
272
  except Exception as e:
273
  print("Unexpected error in wikipedia_search_tool: ", e)
274
- return f"Unexpected error in wikipedia_search_tool: {e}"
275
 
276
  @tool
277
  def arxiv_search_tool(query: str) -> str:
@@ -452,16 +464,20 @@ def web_search_tool(query: str) -> str:
452
  Purpose: When the user asks for current information, recent news, or topics not covered by Wikipedia, use this tool.
453
 
454
  Input: A string describing what to search for on the web.
455
-
456
- Example usage:
457
- - "What's the latest news about AI?"
458
- - "Current stock price of Tesla"
459
- - "Recent developments in renewable energy"
460
  """
461
  print("reached web_search_tool")
 
 
 
 
 
462
  if not query:
463
  return "No search query provided."
464
 
 
 
 
 
465
  ddg = DDGS()
466
  max_retries = 5
467
  result_text = ""
@@ -470,26 +486,23 @@ def web_search_tool(query: str) -> str:
470
  try:
471
  result_text = str(ddg.text(query, max_results=5))
472
  except Exception as e:
473
- # Network error or timeout—retry up to max_retries
474
  if attempt < max_retries:
475
  print(f"web_search_tool: exception '{e}', retrying in 4 seconds ({attempt}/{max_retries})")
476
  time.sleep(4)
477
  continue
478
  else:
479
- # Final attempt failed
480
- return f"Error during DuckDuckGo search: {e}"
481
 
482
- # Check for DuckDuckGo rate‐limit indicator
483
  if "202 Ratelimit" in result_text:
484
  if attempt < max_retries:
485
  print(f"web_search_tool: received '202 Ratelimit', retrying in 4 seconds ({attempt}/{max_retries})")
486
  time.sleep(4)
487
  continue
488
  else:
489
- # Final attempt still rate‐limited
490
  break
 
491
 
492
- # Successful response (no exception and no rate‐limit text)
493
- break
494
  print("Submitted web search successfully")
495
  return result_text
 
219
  - "What is the capital of France?"
220
  - "Find information about quantum computing"
221
  - "What is the history of the internet?"
222
+ If no valid wiki_query is provided, returns an empty string.
223
  """
224
  print("reached wikipedia search tool")
225
+
226
+ # --- Simple in-memory cache to avoid repeated look-ups in a single session
227
+ if not hasattr(wikipedia_search_tool, "_cache"):
228
+ wikipedia_search_tool._cache = {}
229
+
230
+ query = wiki_query.strip()
231
  if not query:
232
+ return ""
233
+
234
+ if query in wikipedia_search_tool._cache:
235
+ print("Returning cached Wikipedia result for query:", query)
236
+ return wikipedia_search_tool._cache[query]
237
 
238
  try:
239
  # 1) Use the MediaWiki API to search for page titles matching the query
 
249
  search_data = search_resp.json()
250
 
251
  search_results = search_data.get("query", {}).get("search", [])
 
252
  if not search_results:
253
+ msg = f"No Wikipedia page found for '{query}'. [END_OF_SEARCH]"
254
+ wikipedia_search_tool._cache[query] = msg
255
+ return msg
256
 
257
  # 2) Take the first search result's title
258
  first_title = search_results[0].get("title", "")
259
  if not first_title:
260
+ msg = "Unexpected format from Wikipedia search. [END_OF_SEARCH]"
261
+ wikipedia_search_tool._cache[query] = msg
262
+ return msg
263
 
264
  # 3) Fetch the page summary for that title via the REST summary endpoint
265
  title_for_url = requests.utils.requote_uri(first_title)
 
272
  summary_text = summary_data.get("extract")
273
  if not summary_text:
274
  summary_text = summary_data.get("description", "No summary available.")
275
+
276
+ result = f"Title: {first_title}\n\n{summary_text}\n\n[END_OF_SEARCH]"
277
+ wikipedia_search_tool._cache[query] = result
278
  print("Submitted wiki successfully")
279
+ return result
 
280
 
281
  except requests.exceptions.RequestException as e:
282
  print("Wikipedia search error: ", e)
283
+ return f"Wikipedia search error: {e} [END_OF_SEARCH]"
284
  except Exception as e:
285
  print("Unexpected error in wikipedia_search_tool: ", e)
286
+ return f"Unexpected error in wikipedia_search_tool: {e} [END_OF_SEARCH]"
287
 
288
  @tool
289
  def arxiv_search_tool(query: str) -> str:
 
464
  Purpose: When the user asks for current information, recent news, or topics not covered by Wikipedia, use this tool.
465
 
466
  Input: A string describing what to search for on the web.
 
 
 
 
 
467
  """
468
  print("reached web_search_tool")
469
+
470
+ if not hasattr(web_search_tool, "_cache"):
471
+ web_search_tool._cache = {}
472
+
473
+ query = query.strip()
474
  if not query:
475
  return "No search query provided."
476
 
477
+ if query in web_search_tool._cache:
478
+ print("Returning cached web search result for query:", query)
479
+ return web_search_tool._cache[query]
480
+
481
  ddg = DDGS()
482
  max_retries = 5
483
  result_text = ""
 
486
  try:
487
  result_text = str(ddg.text(query, max_results=5))
488
  except Exception as e:
 
489
  if attempt < max_retries:
490
  print(f"web_search_tool: exception '{e}', retrying in 4 seconds ({attempt}/{max_retries})")
491
  time.sleep(4)
492
  continue
493
  else:
494
+ return f"Error during DuckDuckGo search: {e} [END_OF_SEARCH]"
 
495
 
 
496
  if "202 Ratelimit" in result_text:
497
  if attempt < max_retries:
498
  print(f"web_search_tool: received '202 Ratelimit', retrying in 4 seconds ({attempt}/{max_retries})")
499
  time.sleep(4)
500
  continue
501
  else:
 
502
  break
503
+ break # Successful
504
 
505
+ result_text += "\n\n[END_OF_SEARCH]"
506
+ web_search_tool._cache[query] = result_text
507
  print("Submitted web search successfully")
508
  return result_text