Skip to content

Commit f206b47

Browse files
authored
Merge pull request #5012 from lindazqli/patch-61
Update openapi-spec-samples.md
2 parents ba985b1 + a51c24f commit f206b47

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

articles/ai-services/agents/how-to/tools/openapi-spec-samples.md

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ import os
5353
import jsonref
5454
from azure.ai.projects import AIProjectClient
5555
from azure.identity import DefaultAzureCredential
56+
# import the folloing
5657
from azure.ai.agents.models import OpenApiTool, OpenApiAnonymousAuthDetails
58+
# use the following for connection auth
59+
# from azure.ai.agents.models import OpenApiTool, OpenApiConnectionAuthDetails, OpenApiConnectionSecurityScheme
60+
# use the following for managed identity auth
61+
# from azure.ai.agents.models import OpenApiTool, OpenApiManagedAuthDetails, OpenApiManagedSecurityScheme
5762

5863
endpoint = os.environ["PROJECT_ENDPOINT"]
5964
model_deployment_name = os.environ["MODEL_DEPLOYMENT_NAME"]
@@ -64,25 +69,24 @@ with AIProjectClient(
6469
) as project_client:
6570
```
6671

67-
## Weather Tool Setup
68-
The OpenAPI specification for the weather service is loaded from `weather_openapi.json`.
69-
70-
```python
71-
# Load the OpenAPI specification for the weather service from a local JSON file using jsonref to handle references
72-
with open(os.path.join(os.path.dirname(__file__), "weather_openapi.json"), "r") as f:
73-
openapi_weather = jsonref.loads(f.read())
74-
```
75-
7672
## Countries Tool Setup
7773
Similarly, the OpenAPI specification for the countries service is loaded from `countries.json`. An anonymous authentication object (`OpenApiAnonymousAuthDetails`) is created, as this specific API doesn't require authentication in this example.
7874

7975
```python
76+
# Load the OpenAPI specification for the countries service from a local JSON file
77+
with open(os.path.join(os.path.dirname(__file__), "weather.json"), "r") as f:
78+
openapi_weather = jsonref.loads(f.read())
79+
8080
# Load the OpenAPI specification for the countries service from a local JSON file
8181
with open(os.path.join(os.path.dirname(__file__), "countries.json"), "r") as f:
8282
openapi_countries = jsonref.loads(f.read())
8383

8484
# Create Auth object for the OpenApiTool (note: using anonymous auth here; connection or managed identity requires additional setup)
8585
auth = OpenApiAnonymousAuthDetails()
86+
# for connection setup
87+
# auth = OpenApiConnectionAuthDetails(security_scheme=OpenApiConnectionSecurityScheme(connection_id=os.environ["CONNECTION_ID"]))
88+
# for managed identity set up
89+
# auth = OpenApiManagedAuthDetails(security_scheme=OpenApiManagedSecurityScheme(audience="https://your_identity_scope.com"))
8690

8791
# Initialize the main OpenAPI tool definition for weather
8892
openapi_tool = OpenApiTool(
@@ -113,11 +117,11 @@ Create the thread and add the initial user message.
113117

114118
```python
115119
# Create a new conversation thread for the interaction
116-
thread = project_client.threads.create_thread()
120+
thread = project_client.agents.threads.create()
117121
print(f"Created thread, ID: {thread.id}")
118122

119123
# Create the initial user message in the thread
120-
message = project_client.messages.create_message(
124+
message = project_client.agents.messages.create(
121125
thread_id=thread.id,
122126
role="user",
123127
content="What's the weather in Seattle and What is the name and population of the country that uses currency with abbreviation THB?",
@@ -131,32 +135,29 @@ Create the run, check the output, and examine what tools were called during the
131135

132136
```python
133137
# Create and automatically process the run, handling tool calls internally
134-
run = project_client.runs.create_and_process_run(thread_id=thread.id, agent_id=agent.id)
135-
print(f"Run finished with status: {run.status}")
136-
if run.status == "failed":
137-
print(f"Run failed: {run.last_error}")
138-
139-
# Retrieve the steps taken during the run for analysis
140-
run_steps = agents_client.list_run_steps(thread_id=thread.id, run_id=run.id)
141-
142-
# Loop through each step to display information
143-
for step in run_steps.data:
144-
print(f"Step {step['id']} status: {step['status']}")
145-
146-
# Check if there are tool calls recorded in the step details
147-
step_details = step.get("step_details", {})
148-
tool_calls = step_details.get("tool_calls", [])
149-
150-
if tool_calls:
151-
print(" Tool calls:")
152-
for call in tool_calls:
153-
print(f" Tool Call ID: {call.get('id')}")
154-
print(f" Type: {call.get('type')}")
155-
156-
function_details = call.get("function", {})
157-
if function_details:
158-
print(f" Function name: {function_details.get('name')}")
159-
print()
138+
run = project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id)
139+
print(f"Run finished with status: {run.status}")
140+
141+
if run.status == "failed":
142+
print(f"Run failed: {run.last_error}")
143+
144+
# Retrieve the steps taken during the run for analysis
145+
run_steps = project_client.agents.run_steps.list(thread_id=thread.id, run_id=run.id)
146+
147+
# Loop through each step to display information
148+
for step in run_steps:
149+
print(f"Step {step['id']} status: {step['status']}")
150+
151+
tool_calls = step.get("step_details", {}).get("tool_calls", [])
152+
for call in tool_calls:
153+
print(f" Tool Call ID: {call.get('id')}")
154+
print(f" Type: {call.get('type')}")
155+
function_details = call.get("function", {})
156+
if function_details:
157+
print(f" Function name: {function_details.get('name')}")
158+
print(f" function output: {function_details.get('output')}")
159+
160+
print()
160161
```
161162

162163

@@ -165,12 +166,13 @@ After the interaction is complete, the script performs cleanup by deleting the c
165166

166167
```python
167168
# Delete the agent resource to clean up
168-
agents_client.delete_agent(agent.id)
169+
project_client.agents.delete_agent(agent.id)
169170
print("Deleted agent")
170171

171172
# Fetch and log all messages exchanged during the conversation thread
172-
messages = agents_client.list_messages(thread_id=thread.id)
173-
print(f"Messages: {messages}")
173+
messages = project_client.agents.messages.list(thread_id=thread.id)
174+
for msg in messages:
175+
print(f"Message ID: {msg.id}, Role: {msg.role}, Content: {msg.content}")
174176
```
175177

176178
:::zone-end

0 commit comments

Comments
 (0)