Paperbag commited on
Commit
1be319c
·
1 Parent(s): ea7a488

Refactor agent.py to remove the get_image_file tool and update app copy.py to process and print multiple questions from the API response.

Browse files
Files changed (3) hide show
  1. __pycache__/agent.cpython-39.pyc +0 -0
  2. agent.py +24 -18
  3. app copy.py +8 -3
__pycache__/agent.cpython-39.pyc CHANGED
Binary files a/__pycache__/agent.cpython-39.pyc and b/__pycache__/agent.cpython-39.pyc differ
 
agent.py CHANGED
@@ -81,7 +81,7 @@ def web_search(keywords: str, max_results:int = 5) -> str:
81
 
82
 
83
  class AgentState(TypedDict):
84
- messages: List[Union[HumanMessage, AIMessage]]
85
 
86
 
87
  def read_message(state: AgentState) -> AgentState:
@@ -107,7 +107,7 @@ def read_message(state: AgentState) -> AgentState:
107
  # return {"messages": messages + [response]}
108
 
109
  # Augment the LLM with tools
110
- tools = [web_search,get_image_file]
111
  tools_by_name = {tool.name: tool for tool in tools}
112
  model_with_tools = model.bind_tools(tools)
113
 
@@ -124,34 +124,40 @@ def answer_message(state: AgentState) -> AgentState:
124
  Response: 24
125
  """)]
126
  messages = prompt + messages
 
 
127
  ai_msg = model_with_tools.invoke(messages)
128
  messages.append(ai_msg)
129
 
130
- # Step 2: Execute tools and collect results
131
- for tool_call in ai_msg.tool_calls:
132
- # Execute the tool with the generated arguments
133
- name = tool_call['name']
134
- args = tool_call['args']
 
 
 
 
 
135
  tool = tools_by_name[name]
136
- tool_result = tool.invoke(args)
137
- messages.append(tool_result)
138
-
 
139
  final_instruction = HumanMessage(
140
- content=(
141
- "Using the tool results above, provide the FINAL answer now. "
142
  "Do not call any tools. Respond with only the answer."
143
  )
144
  )
145
  messages.append(final_instruction)
146
 
147
- final_response = model_with_tools.invoke(messages)
148
-
149
- # final_response = model_with_tools.invoke(messages)
150
  print(f"Final response: {final_response}")
151
- final_response = final_response.content.split('</think>')[1].strip()
152
 
153
- # Append the model's answer to the messages list
154
- return {"messages": [final_response]}
 
155
 
156
 
157
 
 
81
 
82
 
83
  class AgentState(TypedDict):
84
+ messages: List[Union[HumanMessage, AIMessage, SystemMessage]]
85
 
86
 
87
  def read_message(state: AgentState) -> AgentState:
 
107
  # return {"messages": messages + [response]}
108
 
109
  # Augment the LLM with tools
110
+ tools = [web_search]
111
  tools_by_name = {tool.name: tool for tool in tools}
112
  model_with_tools = model.bind_tools(tools)
113
 
 
124
  Response: 24
125
  """)]
126
  messages = prompt + messages
127
+
128
+ # First pass: let model decide whether to call web_search
129
  ai_msg = model_with_tools.invoke(messages)
130
  messages.append(ai_msg)
131
 
132
+ # If the model didn't request any tools, its content is already the answer
133
+ tool_calls = getattr(ai_msg, "tool_calls", None) or []
134
+ if not tool_calls:
135
+ print(f"Final response: {ai_msg}")
136
+ return {"messages": messages}
137
+
138
+ # Execute requested tools and append their text output into the conversation
139
+ for tool_call in tool_calls:
140
+ name = tool_call["name"]
141
+ args = tool_call["args"]
142
  tool = tools_by_name[name]
143
+ tool_result = tool.invoke(args) # this is a plain string from web_search
144
+ messages.append(HumanMessage(content=f"Tool result ({name}):\n{tool_result}"))
145
+
146
+ # Second pass: force a plain-text final answer (no tool calls expected)
147
  final_instruction = HumanMessage(
148
+ content=(
149
+ "Using the tool results above, provide the FINAL numeric/text answer now. "
150
  "Do not call any tools. Respond with only the answer."
151
  )
152
  )
153
  messages.append(final_instruction)
154
 
155
+ final_response = model.invoke(messages)
 
 
156
  print(f"Final response: {final_response}")
 
157
 
158
+ # Return messages including the final AIMessage so BasicAgent reads .content
159
+ messages.append(final_response)
160
+ return {"messages": messages}
161
 
162
 
163
 
app copy.py CHANGED
@@ -30,9 +30,14 @@ questions_url = f"{DEFAULT_API_URL}/questions"
30
  response = requests.get(questions_url, timeout=15)
31
  response.raise_for_status()
32
  questions_data = response.json()
33
- question_text = questions_data[3].get("question")
34
- output = agent(question_text)
35
- print(output)
 
 
 
 
 
36
 
37
 
38
  # def run_and_submit_all( profile: gr.OAuthProfile | None):
 
30
  response = requests.get(questions_url, timeout=15)
31
  response.raise_for_status()
32
  questions_data = response.json()
33
+ for item in questions_data[:5]:
34
+ question_text = item.get("question")
35
+ if question_text is None:
36
+ continue
37
+ output = agent(question_text)
38
+ print("Q:", question_text)
39
+ print("A:", output)
40
+ print("-" * 40)
41
 
42
 
43
  # def run_and_submit_all( profile: gr.OAuthProfile | None):