Skip to content

Commit 23580d4

Browse files
authored
Update openapi-spec.md
1 parent 814c19a commit 23580d4

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,78 @@ work together, generate client code, create tests, apply design standards, and m
3434

3535
::: zone pivot="code-example"
3636
## Step 1: Create an agent with OpenAPI Spec tool
37+
Create a client object, which will contain the connection string for connecting to your AI project and other resources.
38+
```python
39+
import os
40+
import jsonref
41+
from azure.ai.projects import AIProjectClient
42+
from azure.identity import DefaultAzureCredential
43+
from azure.ai.projects.models import OpenApiTool, OpenApiAnonymousAuthDetails
44+
45+
46+
# Create an Azure AI Client from a connection string, copied from your AI Studio project.
47+
# At the moment, it should be in the format "<HostName>;<AzureSubscriptionId>;<ResourceGroup>;<HubName>"
48+
# Customer needs to login to Azure subscription via Azure CLI and set the environment variables
49+
50+
project_client = AIProjectClient.from_connection_string(
51+
credential=DefaultAzureCredential(),
52+
conn_str=os.environ["PROJECT_CONNECTION_STRING"],
53+
)
54+
```
3755

3856
## Step 2: Enable the OpenAPI Spec tool
57+
You may want to store the OpenAPI specification in another file and import the content to initialize the tool. Please note the sample code is using `anonymous` as authentication type.
58+
```python
59+
with open('./weather_openapi.json', 'r') as f:
60+
openapi_spec = jsonref.loads(f.read())
61+
62+
# Create Auth object for the OpenApiTool (note that connection or managed identity auth setup requires additional setup in Azure)
63+
auth = OpenApiAnonymousAuthDetails()
64+
65+
# Initialize agent OpenApi tool using the read in OpenAPI spec
66+
openapi = OpenApiTool(name="get_weather", spec=openapi_spec, description="Retrieve weather information for a location", auth=auth)
67+
```
3968

4069
## Step 3: Create a thread
70+
```python
71+
# Create agent with OpenApi tool and process assistant run
72+
with project_client:
73+
agent = project_client.agents.create_agent(
74+
model="gpt-4o-mini",
75+
name="my-assistant",
76+
instructions="You are a helpful assistant",
77+
tools=openapi.definitions
78+
)
79+
print(f"Created agent, ID: {agent.id}")
80+
81+
# Create thread for communication
82+
thread = project_client.agents.create_thread()
83+
print(f"Created thread, ID: {thread.id}")
84+
```
4185

4286
## Step 4: Create a run and check the output
87+
Create a run and observe that the model uses the OpenAPI Spec tool to provide a response to the user's question.
88+
```python
89+
# Create message to thread
90+
message = project_client.agents.create_message(
91+
thread_id=thread.id,
92+
role="user",
93+
content="What's the weather in Seattle?",
94+
)
95+
print(f"Created message, ID: {message.id}")
96+
97+
# Create and process agent run in thread with tools
98+
run = project_client.agents.create_and_process_run(thread_id=thread.id, assistant_id=agent.id)
99+
print(f"Run finished with status: {run.status}")
100+
101+
if run.status == "failed":
102+
print(f"Run failed: {run.last_error}")
103+
104+
# Delete the assistant when done
105+
project_client.agents.delete_agent(agent.id)
106+
print("Deleted agent")
107+
108+
# Fetch and log all messages
109+
messages = project_client.agents.list_messages(thread_id=thread.id)
110+
print(f"Messages: {messages}")
111+
```

0 commit comments

Comments
 (0)