Skip to content

Commit b200fe7

Browse files
committed
updating code
1 parent fb7af30 commit b200fe7

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

articles/ai-services/agents/how-to/tools/function-calling.md

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -110,34 +110,28 @@ print(f"Created run, ID: {run.id}")
110110

111111
# Poll the run status until it is completed or requires action
112112
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")
141135
```
142136

143137
::: zone-end

0 commit comments

Comments
 (0)