naman1102 commited on
Commit
bfa9267
Β·
1 Parent(s): 1294ec3

final_answer

Browse files
Files changed (2) hide show
  1. agent.py +7 -7
  2. app.py +22 -4
agent.py CHANGED
@@ -19,7 +19,7 @@ from tools import (
19
 
20
  # ─────────────────────────── Configuration ───────────────────────────────
21
  SYSTEM_PROMPT = """
22
- You are a general AI assistant. 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.
23
  """
24
  MAX_TOOL_CALLS = 5
25
 
@@ -36,18 +36,18 @@ MAX_TOOL_CALLS = 5
36
  # ─────────────────────────── Graph wiring ───────────────────────────────
37
 
38
  def build_graph():
39
-
40
-
41
- llm = ChatOpenAI(model_name="gpt-4.1-mini", temperature=0.3)
42
 
43
  llm_tools = [
44
  wikipedia_search_tool,
45
  arxiv_search_tool,
46
  audio_transcriber_tool,
47
  excel_tool,
48
- analyze_code_tool,
49
  ]
50
- # llm = llm.bind_tools(llm_tools)
51
- agent = create_react_agent(model=llm, tools=llm_tools, prompt=SYSTEM_PROMPT)
 
52
 
53
  return agent
 
19
 
20
  # ─────────────────────────── Configuration ───────────────────────────────
21
  SYSTEM_PROMPT = """
22
+ You are a general AI assistant. I will ask you a question. Don't 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.
23
  """
24
  MAX_TOOL_CALLS = 5
25
 
 
36
  # ─────────────────────────── Graph wiring ───────────────────────────────
37
 
38
  def build_graph():
39
+ """Build and return a create_react_agent."""
40
+ llm = ChatOpenAI(model_name="gpt-4o-mini", temperature=0.3)
 
41
 
42
  llm_tools = [
43
  wikipedia_search_tool,
44
  arxiv_search_tool,
45
  audio_transcriber_tool,
46
  excel_tool,
47
+ analyze_code_tool
48
  ]
49
+
50
+ # Create the react agent with proper system prompt
51
+ agent = create_react_agent(model=llm, tools=llm_tools, messages_modifier=SYSTEM_PROMPT)
52
 
53
  return agent
app.py CHANGED
@@ -26,14 +26,32 @@ class BasicAgent:
26
  """Run the agent and return whatever FINAL_ANSWER the graph produces."""
27
  print(f"Agent received question: {question}")
28
 
29
- # The user_question argument for AgentState is the question.
 
 
 
 
 
 
30
 
31
-
32
  # IMPORTANT: invoke() returns a **new** state instance (or an AddableValuesDict),
33
  # not the object we pass in. Use the returned value to fetch final_answer.
34
- out_state = self.graph.invoke({"messages": [HumanMessage(content=question)]})
35
  print("out_state: ", out_state)
36
- return out_state
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
 
39
  def run_and_submit_all( profile: gr.OAuthProfile | None):
 
26
  """Run the agent and return whatever FINAL_ANSWER the graph produces."""
27
  print(f"Agent received question: {question}")
28
 
29
+ # Initialize the state properly with all required fields
30
+ init_state = {
31
+ "messages": [
32
+ SystemMessage(content=SYSTEM_PROMPT),
33
+ HumanMessage(content=question)
34
+ ]
35
+ }
36
 
 
37
  # IMPORTANT: invoke() returns a **new** state instance (or an AddableValuesDict),
38
  # not the object we pass in. Use the returned value to fetch final_answer.
39
+ out_state = self.graph.invoke(init_state)
40
  print("out_state: ", out_state)
41
+
42
+ # Extract the final answer from the last message
43
+ if out_state and "messages" in out_state:
44
+ last_message = out_state["messages"][-1]
45
+ if hasattr(last_message, 'content'):
46
+ content = last_message.content
47
+ # Look for FINAL ANSWER: pattern
48
+ if "FINAL ANSWER:" in content:
49
+ final_answer = content.split("FINAL ANSWER:")[-1].strip()
50
+ return final_answer
51
+ else:
52
+ return content
53
+
54
+ return "No answer generated."
55
 
56
 
57
  def run_and_submit_all( profile: gr.OAuthProfile | None):