naman1102 commited on
Commit
5b43bea
·
1 Parent(s): a63d8c1
Files changed (2) hide show
  1. app.py +52 -32
  2. tools.py +1 -1
app.py CHANGED
@@ -18,7 +18,16 @@ You are a general AI assistant. I will ask you a question. Report your thoughts,
18
 
19
  IMPORTANT: When using tools that require file access (such as audio_transcriber_tool, excel_tool, analyze_code_tool, or image_tool), ALWAYS use the task_id parameter only. Do NOT use any file names mentioned by the user - ignore them completely and only pass the task_id.
20
 
21
- You MUST always provide a FINAL ANSWER. If you cannot find complete information, provide your best estimate based on available information.
 
 
 
 
 
 
 
 
 
22
  """
23
 
24
 
@@ -31,38 +40,49 @@ class BasicAgent:
31
  """Run the agent and return whatever FINAL_ANSWER the graph produces."""
32
  print(f"Agent received question: {question}")
33
 
34
- # Create system prompt with task_id included
35
- system_prompt_with_task = SYSTEM_PROMPT
36
- if task_id:
37
- system_prompt_with_task += f"\n\nIMPORTANT: Your current task_id is: {task_id}. When using any tools that require a task_id parameter (audio_transcriber_tool, excel_tool, analyze_code_tool, image_tool), use this exact task_id: {task_id}"
38
-
39
- # Initialize the state properly with all required fields
40
- init_state = {
41
- "messages": [
42
- SystemMessage(content=system_prompt_with_task),
43
- HumanMessage(content=question)
44
- ]
45
- }
46
-
47
- # Run the agent with increased steps
48
- out_state = self.graph.invoke(init_state, {"recursion_limit": 15})
49
-
50
- # Extract the final answer from the last message
51
- if out_state and "messages" in out_state:
52
- last_message = out_state["messages"][-1]
53
- if hasattr(last_message, 'content'):
54
- content = last_message.content
55
- print("content: ", content)
56
- print("\n\n\n\n")
57
-
58
- # Look for FINAL ANSWER: pattern
59
- if "FINAL ANSWER:" in content:
60
- final_answer = content.split("FINAL ANSWER:")[-1].strip()
61
- return final_answer
62
- else:
 
 
 
 
 
 
 
63
  return content
64
-
65
- return "No answer generated."
 
 
 
 
66
 
67
 
68
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
18
 
19
  IMPORTANT: When using tools that require file access (such as audio_transcriber_tool, excel_tool, analyze_code_tool, or image_tool), ALWAYS use the task_id parameter only. Do NOT use any file names mentioned by the user - ignore them completely and only pass the task_id.
20
 
21
+ SEARCH STRATEGY:
22
+ - If wikipedia_search_tool fails or returns insufficient/irrelevant results, try these fallback strategies:
23
+ 1. Try wikipedia_search_tool again with a broader, more general query (remove specific terms, use synonyms)
24
+ 2. If Wikipedia still doesn't help, try arxiv_search_tool for academic/research topics
25
+ 3. You can use multiple search attempts with different keywords to find better information
26
+ - Always evaluate if the search results are relevant and sufficient before proceeding to your final answer
27
+ - IMPORTANT: When you see [END_OF_SEARCH] in tool results, this means the search is complete and you have all available information
28
+ - Do NOT perform additional searches after seeing [END_OF_SEARCH] - immediately proceed to analyze the provided information and give your final answer
29
+
30
+ You MUST always provide a FINAL ANSWER. If you reach the recursion limit or tools fail, provide the best answer possible with available information.
31
  """
32
 
33
 
 
40
  """Run the agent and return whatever FINAL_ANSWER the graph produces."""
41
  print(f"Agent received question: {question}")
42
 
43
+ try:
44
+ # Create system prompt with task_id included
45
+ system_prompt_with_task = SYSTEM_PROMPT
46
+ if task_id:
47
+ system_prompt_with_task += f"\n\nIMPORTANT: Your current task_id is: {task_id}. When using any tools that require a task_id parameter (audio_transcriber_tool, excel_tool, analyze_code_tool, image_tool), use this exact task_id: {task_id}"
48
+
49
+ # Initialize the state properly with all required fields
50
+ init_state = {
51
+ "messages": [
52
+ SystemMessage(content=system_prompt_with_task),
53
+ HumanMessage(content=question)
54
+ ]
55
+ }
56
+
57
+ # Run the agent with increased steps
58
+ out_state = self.graph.invoke(init_state, {"recursion_limit": 15})
59
+
60
+ # Extract the final answer from the last message
61
+ if out_state and "messages" in out_state:
62
+ last_message = out_state["messages"][-1]
63
+ if hasattr(last_message, 'content') and last_message.content:
64
+ content = last_message.content
65
+ print("content: ", content)
66
+ print("\n\n\n\n")
67
+
68
+ # Look for FINAL ANSWER: pattern (case insensitive)
69
+ if "FINAL ANSWER:" in content.upper():
70
+ # Find the last occurrence of FINAL ANSWER
71
+ parts = content.upper().split("FINAL ANSWER:")
72
+ if len(parts) > 1:
73
+ # Get the original case version
74
+ answer_start = content.upper().rfind("FINAL ANSWER:") + len("FINAL ANSWER:")
75
+ final_answer = content[answer_start:].strip()
76
+ return final_answer
77
+
78
+ # If no FINAL ANSWER found, return the whole content
79
  return content
80
+
81
+ return "No valid response generated."
82
+
83
+ except Exception as e:
84
+ print(f"Agent execution error: {e}")
85
+ return f"Unable to process the question due to an error. Please try again."
86
 
87
 
88
  def run_and_submit_all( profile: gr.OAuthProfile | None):
tools.py CHANGED
@@ -316,7 +316,7 @@ def arxiv_search_tool(arxiv_query: str) -> str:
316
 
317
  from langchain_openai import ChatOpenAI
318
  from langchain.schema import SystemMessage, HumanMessage
319
- LLM = ChatOpenAI(model_name="gpt-4.1-mini", temperature=0.2)
320
 
321
  @tool
322
  def analyze_code_tool(task_id: str) -> str:
 
316
 
317
  from langchain_openai import ChatOpenAI
318
  from langchain.schema import SystemMessage, HumanMessage
319
+ LLM = ChatOpenAI(model_name="gpt-4o-mini", temperature=0.2)
320
 
321
  @tool
322
  def analyze_code_tool(task_id: str) -> str: