Skip to content

Commit d0fc0a6

Browse files
authored
openapi sample (#44025)
* Add OpenAPI samples for weather data and TripAdvisor API integration - Introduced `weather_openapi.json` to define an OpenAPI specification for retrieving current weather data from wttr.in. - Created `sample_agent_openapi.py` to demonstrate the creation of an AI agent that utilizes the weather OpenAPI tool. - Developed `sample_agent_openapi_with_project_connection.py` to showcase an AI agent using the TripAdvisor API with project connection authentication. - Updated documentation for openapi. - Remove indentaiton from doc since it isn't supported by code snipper * added change log * Resolved comment * resolve comment
1 parent be8f299 commit d0fc0a6

File tree

8 files changed

+2129
-222
lines changed

8 files changed

+2129
-222
lines changed

sdk/ai/azure-ai-projects/.env.template

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,5 @@ BING_CUSTOM_SEARCH_PROJECT_CONNECTION_ID=
5454
BING_CUSTOM_SEARCH_INSTANCE_NAME=
5555
SHAREPOINT_PROJECT_CONNECTION_ID=
5656
A2A_PROJECT_CONNECTION_ID=
57-
BROWSER_AUTOMATION_PROJECT_CONNECTION_ID=
57+
BROWSER_AUTOMATION_PROJECT_CONNECTION_ID=
58+
OPENAPI_PROJECT_CONNECTION_ID=

sdk/ai/azure-ai-projects/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111
### Sample updates
1212

13+
* Added OpenAPI tool sample. See `sample_agent_openapi.py`.
14+
* Added OpenAPI with Project Connection sample. See `sample_agent_openapi_with_project_connection.py`.
15+
* Added SharePoint grounding tool sample. See `sample_agent_sharepoint.py`.
16+
17+
1318
## 2.0.0b2 (2025-11-14)
1419

1520
### Features Added

sdk/ai/azure-ai-projects/README.md

Lines changed: 273 additions & 220 deletions
Large diffs are not rendered by default.

sdk/ai/azure-ai-projects/samples/agents/assets/tripadvisor_openapi.json

Lines changed: 1606 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"openapi": "3.1.0",
3+
"info": {
4+
"title": "get weather data",
5+
"description": "Retrieves current weather data for a location based on wttr.in.",
6+
"version": "v1.0.0"
7+
},
8+
"servers": [
9+
{
10+
"url": "https://wttr.in"
11+
}
12+
],
13+
"auth": [],
14+
"paths": {
15+
"/{location}": {
16+
"get": {
17+
"description": "Get weather information for a specific location",
18+
"operationId": "GetCurrentWeather",
19+
"parameters": [
20+
{
21+
"name": "location",
22+
"in": "path",
23+
"description": "City or location to retrieve the weather for",
24+
"required": true,
25+
"schema": {
26+
"type": "string"
27+
}
28+
},
29+
{
30+
"name": "format",
31+
"in": "query",
32+
"description": "Always use j1 value for this parameter",
33+
"required": true,
34+
"schema": {
35+
"type": "string",
36+
"default": "j1"
37+
}
38+
}
39+
],
40+
"responses": {
41+
"200": {
42+
"description": "Successful response",
43+
"content": {
44+
"text/plain": {
45+
"schema": {
46+
"type": "string"
47+
}
48+
}
49+
}
50+
},
51+
"404": {
52+
"description": "Location not found"
53+
}
54+
},
55+
"deprecated": false
56+
}
57+
}
58+
},
59+
"components": {
60+
"schemes": {}
61+
}
62+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
6+
"""
7+
DESCRIPTION:
8+
This sample demonstrates how to create an AI agent with OpenAPI tool capabilities
9+
using the OpenApiAgentTool and synchronous Azure AI Projects client. The agent can
10+
call external APIs defined by OpenAPI specifications.
11+
12+
USAGE:
13+
python sample_agent_openapi.py
14+
15+
Before running the sample:
16+
17+
pip install "azure-ai-projects>=2.0.0b1" azure-identity openai python-dotenv jsonref
18+
19+
Set these environment variables with your own values:
20+
1) AZURE_AI_PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
21+
page of your Microsoft Foundry portal.
22+
2) AZURE_AI_MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
23+
the "Models + endpoints" tab in your Microsoft Foundry project.
24+
"""
25+
26+
import os
27+
import jsonref
28+
from dotenv import load_dotenv
29+
30+
from azure.identity import DefaultAzureCredential
31+
from azure.ai.projects import AIProjectClient
32+
from azure.ai.projects.models import (
33+
PromptAgentDefinition,
34+
OpenApiAgentTool,
35+
OpenApiFunctionDefinition,
36+
OpenApiAnonymousAuthDetails,
37+
)
38+
39+
load_dotenv()
40+
41+
endpoint = os.environ["AZURE_AI_PROJECT_ENDPOINT"]
42+
43+
with (
44+
DefaultAzureCredential() as credential,
45+
AIProjectClient(endpoint=endpoint, credential=credential) as project_client,
46+
project_client.get_openai_client() as openai_client,
47+
):
48+
49+
weather_asset_file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../assets/weather_openapi.json"))
50+
51+
# [START tool_declaration]
52+
with open(weather_asset_file_path, "r") as f:
53+
openapi_weather = jsonref.loads(f.read())
54+
55+
tool = OpenApiAgentTool(
56+
openapi=OpenApiFunctionDefinition(
57+
name="get_weather",
58+
spec=openapi_weather,
59+
description="Retrieve weather information for a location",
60+
auth=OpenApiAnonymousAuthDetails(),
61+
)
62+
)
63+
# [END tool_declaration]
64+
65+
agent = project_client.agents.create_version(
66+
agent_name="MyAgent",
67+
definition=PromptAgentDefinition(
68+
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
69+
instructions="You are a helpful assistant.",
70+
tools=[tool],
71+
),
72+
)
73+
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
74+
75+
response = openai_client.responses.create(
76+
input="What is the name and population of the country that uses currency with abbreviation THB?",
77+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
78+
)
79+
print(f"Response created: {response.output_text}")
80+
81+
print("\nCleaning up...")
82+
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
83+
print("Agent deleted")
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
6+
"""
7+
DESCRIPTION:
8+
This sample demonstrates how to create an AI agent with OpenAPI tool capabilities
9+
using the OpenApiAgentTool with project connection authentication. The agent can
10+
call external APIs defined by OpenAPI specifications, using credentials stored in
11+
an Azure AI Project connection.
12+
13+
USAGE:
14+
python sample_agent_openapi_with_project_connection.py
15+
16+
Before running the sample:
17+
18+
pip install "azure-ai-projects>=2.0.0b1" azure-identity openai python-dotenv jsonref
19+
20+
Set these environment variables with your own values:
21+
1) AZURE_AI_PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
22+
page of your Microsoft Foundry portal.
23+
2) AZURE_AI_MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
24+
the "Models + endpoints" tab in your Microsoft Foundry project.
25+
3) OPENAPI_PROJECT_CONNECTION_ID - The OpenAPI project connection ID,
26+
as found in the "Connections" tab in your Microsoft Foundry project.
27+
"""
28+
29+
import os
30+
import jsonref
31+
from dotenv import load_dotenv
32+
33+
from azure.identity import DefaultAzureCredential
34+
from azure.ai.projects import AIProjectClient
35+
from azure.ai.projects.models import (
36+
PromptAgentDefinition,
37+
OpenApiAgentTool,
38+
OpenApiFunctionDefinition,
39+
OpenApiAnonymousAuthDetails,
40+
OpenApiManagedAuthDetails,
41+
OpenApiProjectConnectionAuthDetails,
42+
OpenApiProjectConnectionSecurityScheme,
43+
)
44+
45+
load_dotenv()
46+
47+
endpoint = os.environ["AZURE_AI_PROJECT_ENDPOINT"]
48+
49+
with (
50+
DefaultAzureCredential() as credential,
51+
AIProjectClient(endpoint=endpoint, credential=credential) as project_client,
52+
project_client.get_openai_client() as openai_client,
53+
):
54+
55+
tripadvisor_asset_file_path = os.path.abspath(
56+
os.path.join(os.path.dirname(__file__), "../assets/tripadvisor_openapi.json")
57+
)
58+
59+
# [START tool_declaration]
60+
with open(tripadvisor_asset_file_path, "r") as f:
61+
openapi_tripadvisor = jsonref.loads(f.read())
62+
63+
tool = OpenApiAgentTool(
64+
openapi=OpenApiFunctionDefinition(
65+
name="tripadvisor",
66+
spec=openapi_tripadvisor,
67+
description="Trip Advisor API to get travel information",
68+
auth=OpenApiProjectConnectionAuthDetails(
69+
security_scheme=OpenApiProjectConnectionSecurityScheme(
70+
project_connection_id=os.environ["OPENAPI_PROJECT_CONNECTION_ID"]
71+
)
72+
),
73+
)
74+
)
75+
# [END tool_declaration]
76+
77+
agent = project_client.agents.create_version(
78+
agent_name="MyAgent",
79+
definition=PromptAgentDefinition(
80+
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
81+
instructions="You are a helpful assistant.",
82+
tools=[tool],
83+
),
84+
)
85+
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
86+
87+
response = openai_client.responses.create(
88+
input="Recommend me 5 top hotels in paris, France",
89+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
90+
)
91+
print(f"Response created: {response.output_text}")
92+
93+
print("\nCleaning up...")
94+
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
95+
print("Agent deleted")

sdk/ai/azure-ai-projects/samples/agents/tools/sample_agent_sharepoint.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@
6767
)
6868
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
6969

70+
user_input = input("Enter your question corresponded to the documents in SharePoint:\n")
71+
7072
# Send initial request that will trigger the SharePoint tool
7173
stream_response = openai_client.responses.create(
7274
stream=True,
73-
input="Please summarize the last meeting notes stored in SharePoint.",
75+
input=user_input,
7476
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
7577
)
7678

0 commit comments

Comments
 (0)