@@ -110,34 +110,28 @@ print(f"Created run, ID: {run.id}")
110
110
111
111
# Poll the run status until it is completed or requires action
112
112
while run.status in [" queued" , " in_progress" , " requires_action" ]:
113
- time.sleep(1 )
114
- run = agents_client.runs.get(thread_id = thread.id, run_id = run.id)
115
-
116
- if run.status == " requires_action" and isinstance (run.required_action, SubmitToolOutputsAction):
117
- tool_calls = run.required_action.submit_tool_outputs.tool_calls
118
- if not tool_calls:
119
- print (" No tool calls provided - cancelling run" )
120
- agents_client.runs.cancel(thread_id = thread.id, run_id = run.id)
121
- break
122
-
123
- tool_outputs = []
124
- for tool_call in tool_calls:
125
- if isinstance (tool_call, RequiredFunctionToolCall):
126
- try :
127
- print (f " Executing tool call: { tool_call} " )
128
- output = functions.execute(tool_call)
129
- tool_outputs.append(
130
- ToolOutput(
131
- tool_call_id = tool_call.id,
132
- output = output,
133
- )
134
- )
135
- except Exception as e:
136
- print (f " Error executing tool_call { tool_call.id} : { e} " )
137
-
138
- print (f " Tool outputs: { tool_outputs} " )
139
- if tool_outputs:
140
- agents_client.runs.submit_tool_outputs(thread_id = thread.id, run_id = run.id, tool_outputs = tool_outputs)
113
+ time.sleep(1 )
114
+ run = project_client.agents.runs.get(thread_id = thread.id, run_id = run.id)
115
+
116
+ if run.status == " requires_action" :
117
+ tool_calls = run.required_action.submit_tool_outputs.tool_calls
118
+ tool_outputs = []
119
+ for tool_call in tool_calls:
120
+ if tool_call.name == " fetch_weather" :
121
+ output = fetch_weather(" New York" )
122
+ tool_outputs.append({" tool_call_id" : tool_call.id, " output" : output})
123
+ project_client.agents.runs.submit_tool_outputs(thread_id = thread.id, run_id = run.id, tool_outputs = tool_outputs)
124
+
125
+ print (f " Run completed with status: { run.status} " )
126
+
127
+ # Fetch and log all messages from the thread
128
+ messages = project_client.agents.messages.list(thread_id = thread.id)
129
+ for message in messages:
130
+ print (f " Role: { message[' role' ]} , Content: { message[' content' ]} " )
131
+
132
+ # Delete the agent after use
133
+ project_client.agents.delete_agent(agent.id)
134
+ print (" Deleted agent" )
141
135
```
142
136
143
137
::: zone-end
0 commit comments