Paperbag commited on
Commit
9ad9538
·
1 Parent(s): 48a69ad

Enhance answer_message function to include tool execution and result integration. The function now processes tool calls from the AI response, executes them, and appends the results before generating the final response.

Browse files
Files changed (1) hide show
  1. agent.py +17 -2
agent.py CHANGED
@@ -96,9 +96,24 @@ def answer_message(state: AgentState) -> AgentState:
96
  {messages}
97
 
98
  """
99
- response = model_with_tools.invoke(prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  # Append the model's answer to the messages list
101
- return {"messages": messages + [response]}
102
 
103
 
104
 
 
96
  {messages}
97
 
98
  """
99
+ ai_msg = model_with_tools.invoke(prompt)
100
+ messages.append(ai_msg)
101
+
102
+ # Step 2: Execute tools and collect results
103
+ for tool_call in ai_msg.tool_calls:
104
+ # Execute the tool with the generated arguments
105
+ name = tool_call['name']
106
+ args = tool_call['args']
107
+ tool = tools_by_name[name]
108
+ tool_result = tool.invoke(args)
109
+ messages.append(tool_result)
110
+
111
+ # Step 3: Pass results back to model for final response
112
+ final_response = model_with_tools.invoke(messages)
113
+ print(final_response.text)
114
+
115
  # Append the model's answer to the messages list
116
+ return {"messages": messages + [final_response]}
117
 
118
 
119