Skip to content

Commit e6bc225

Browse files
committed
openapi
1 parent 29abc01 commit e6bc225

3 files changed

Lines changed: 252 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"openapi": "3.1.0",
3+
"info": {
4+
"title": "RestCountries.NET API",
5+
"description": "Web API version 3.1 for managing country items, based on previous implementations from restcountries.eu and restcountries.com.",
6+
"version": "v3.1"
7+
},
8+
"servers": [
9+
{ "url": "https://restcountries.net" }
10+
],
11+
"auth": [],
12+
"paths": {
13+
"/v3.1/currency": {
14+
"get": {
15+
"description": "Search by currency.",
16+
"operationId": "LookupCountryByCurrency",
17+
"parameters": [
18+
{
19+
"name": "currency",
20+
"in": "query",
21+
"description": "The currency to search for.",
22+
"required": true,
23+
"schema": {
24+
"type": "string"
25+
}
26+
}
27+
],
28+
"responses": {
29+
"200": {
30+
"description": "Success",
31+
"content": {
32+
"text/plain": {
33+
"schema": {
34+
"type": "string"
35+
}
36+
}
37+
}
38+
}
39+
}
40+
}
41+
}
42+
},
43+
"components": {
44+
"schemes": {}
45+
}
46+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# pylint: disable=line-too-long,useless-suppression
2+
# ------------------------------------
3+
# Copyright (c) Microsoft Corporation.
4+
# Licensed under the MIT License.
5+
# ------------------------------------
6+
7+
"""
8+
DESCRIPTION:
9+
This sample demonstrates how to use agent operations with the
10+
OpenAPI tool from the Azure Agents service using a synchronous client.
11+
To learn more about OpenAPI specs, visit https://learn.microsoft.com/openapi
12+
13+
USAGE:
14+
python openapi.py
15+
16+
Before running the sample:
17+
18+
pip install azure-ai-agents azure-identity jsonref
19+
20+
Set these environment variables with your own values:
21+
1) PROJECT_ENDPOINT - the Azure AI Agents endpoint.
22+
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
23+
the "Models + endpoints" tab in your Azure AI Foundry project.
24+
"""
25+
# <initialization>
26+
# Import necessary libraries
27+
import os
28+
import jsonref
29+
from azure.ai.agents import AgentsClient
30+
from azure.identity import DefaultAzureCredential
31+
from azure.ai.agents.models import OpenApiTool, OpenApiAnonymousAuthDetails
32+
from dotenv import load_dotenv
33+
load_dotenv()
34+
35+
# Initialize the Agents Client using the endpoint and default credentials
36+
agents_client = AgentsClient(
37+
endpoint=os.environ["PROJECT_ENDPOINT"],
38+
credential=DefaultAzureCredential(),
39+
)
40+
# </initialization>
41+
42+
# <weather_tool_setup>
43+
# --- Weather OpenAPI Tool Setup ---
44+
# Load the OpenAPI specification for the weather service from a local JSON file using jsonref to handle references
45+
with open(os.path.join(os.path.dirname(__file__), "weather_openapi.json"), "r") as f:
46+
openapi_weather = jsonref.loads(f.read())
47+
# </weather_tool_setup>
48+
49+
# <countries_tool_setup>
50+
# --- Countries OpenAPI Tool Setup ---
51+
# Load the OpenAPI specification for the countries service from a local JSON file
52+
with open(os.path.join(os.path.dirname(__file__), "countries.json"), "r") as f:
53+
openapi_countries = jsonref.loads(f.read())
54+
55+
# Create Auth object for the OpenApiTool (note: using anonymous auth here; connection or managed identity requires additional setup)
56+
auth = OpenApiAnonymousAuthDetails()
57+
58+
# Initialize the main OpenAPI tool definition for weather
59+
openapi_tool = OpenApiTool(
60+
name="get_weather", spec=openapi_weather, description="Retrieve weather information for a location", auth=auth
61+
)
62+
# Add the countries API definition to the same tool object
63+
openapi_tool.add_definition(
64+
name="get_countries", spec=openapi_countries, description="Retrieve a list of countries", auth=auth
65+
)
66+
# </countries_tool_setup>
67+
68+
# Use the agents client context manager
69+
with agents_client:
70+
# <agent_creation>
71+
# --- Agent Creation ---
72+
# Create an agent configured with the combined OpenAPI tool definitions
73+
agent = agents_client.create_agent(
74+
model=os.environ["MODEL_DEPLOYMENT_NAME"], # Specify the model deployment
75+
name="my-agent", # Give the agent a name
76+
instructions="You are a helpful agent", # Define agent's role
77+
tools=openapi_tool.definitions, # Provide the list of tool definitions
78+
)
79+
print(f"Created agent, ID: {agent.id}")
80+
# </agent_creation>
81+
82+
# <thread_management>
83+
# --- Thread Management ---
84+
# Create a new conversation thread for the interaction
85+
thread = agents_client.create_thread()
86+
print(f"Created thread, ID: {thread.id}")
87+
88+
# Create the initial user message in the thread
89+
message = agents_client.create_message(
90+
thread_id=thread.id,
91+
role="user",
92+
content="What's the weather in Seattle and What is the name and population of the country that uses currency with abbreviation THB?",
93+
)
94+
print(f"Created message, ID: {message.id}")
95+
# </thread_management>
96+
97+
# <message_processing>
98+
# --- Message Processing (Run Creation and Auto-processing) ---
99+
# Create and automatically process the run, handling tool calls internally
100+
# Note: This differs from the function_tool example where tool calls are handled manually
101+
run = agents_client.create_and_process_run(thread_id=thread.id, agent_id=agent.id)
102+
print(f"Run finished with status: {run.status}")
103+
# </message_processing>
104+
105+
# <tool_execution_loop> # Note: This section now processes completed steps, as create_and_process_run handles execution
106+
# --- Post-Run Step Analysis ---
107+
if run.status == "failed":
108+
print(f"Run failed: {run.last_error}")
109+
110+
# Retrieve the steps taken during the run for analysis
111+
run_steps = agents_client.list_run_steps(thread_id=thread.id, run_id=run.id)
112+
113+
# Loop through each step to display information
114+
for step in run_steps.data:
115+
print(f"Step {step['id']} status: {step['status']}")
116+
117+
# Check if there are tool calls recorded in the step details
118+
step_details = step.get("step_details", {})
119+
tool_calls = step_details.get("tool_calls", [])
120+
121+
if tool_calls:
122+
print(" Tool calls:")
123+
for call in tool_calls:
124+
print(f" Tool Call ID: {call.get('id')}")
125+
print(f" Type: {call.get('type')}")
126+
127+
function_details = call.get("function", {})
128+
if function_details:
129+
print(f" Function name: {function_details.get('name')}")
130+
print() # Add an extra newline between steps for readability
131+
# </tool_execution_loop>
132+
133+
# <cleanup>
134+
# --- Cleanup ---
135+
# Delete the agent resource to clean up
136+
agents_client.delete_agent(agent.id)
137+
print("Deleted agent")
138+
139+
# Fetch and log all messages exchanged during the conversation thread
140+
messages = agents_client.list_messages(thread_id=thread.id)
141+
print(f"Messages: {messages}")
142+
# </cleanup>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
{
3+
"openapi": "3.1.0",
4+
"info": {
5+
"title": "get weather data",
6+
"description": "Retrieves current weather data for a location based on wttr.in.",
7+
"version": "v1.0.0"
8+
},
9+
"servers": [
10+
{
11+
"url": "https://wttr.in"
12+
}
13+
],
14+
"auth": [],
15+
"paths": {
16+
"/{location}": {
17+
"get": {
18+
"description": "Get weather information for a specific location",
19+
"operationId": "GetCurrentWeather",
20+
"parameters": [
21+
{
22+
"name": "location",
23+
"in": "path",
24+
"description": "City or location to retrieve the weather for",
25+
"required": true,
26+
"schema": {
27+
"type": "string"
28+
}
29+
},
30+
{
31+
"name": "format",
32+
"in": "query",
33+
"description": "Always use j1 value for this parameter",
34+
"required": true,
35+
"schema": {
36+
"type": "string",
37+
"default": "j1"
38+
}
39+
}
40+
],
41+
"responses": {
42+
"200": {
43+
"description": "Successful response",
44+
"content": {
45+
"text/plain": {
46+
"schema": {
47+
"type": "string"
48+
}
49+
}
50+
}
51+
},
52+
"404": {
53+
"description": "Location not found"
54+
}
55+
},
56+
"deprecated": false
57+
}
58+
}
59+
},
60+
"components": {
61+
"schemes": {}
62+
}
63+
}
64+

0 commit comments

Comments
 (0)