Skip to content

Commit 2fa8a79

Browse files
committed
First sample
1 parent dfa92c7 commit 2fa8a79

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# ------------------------------------
2+
# Copyright (c) Microsoft Corporation.
3+
# Licensed under the MIT License.
4+
# ------------------------------------
5+
6+
"""
7+
DESCRIPTION:
8+
This sample demonstrates how to run basic Prompt Agent operations
9+
using the synchronous AIProjectClient, while defining a desired
10+
JSON schema for the response ("structured output").
11+
12+
The OpenAI compatible Responses and Conversation calls in this sample are made using
13+
the OpenAI client from the `openai` package. See https://platform.openai.com/docs/api-reference
14+
for more information.
15+
16+
This sample is inspired from the OpenAI example here:
17+
https://platform.openai.com/docs/guides/structured-outputs/supported-schemas
18+
19+
USAGE:
20+
python sample_agent_structured_output.py
21+
22+
Before running the sample:
23+
24+
pip install "azure-ai-projects>=2.0.0b1" openai azure-identity python-dotenv pydantic
25+
26+
Set these environment variables with your own values:
27+
1) AZURE_AI_PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
28+
page of your Azure AI Foundry portal.
29+
2) AZURE_AI_MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
30+
the "Models + endpoints" tab in your Azure AI Foundry project.
31+
"""
32+
33+
import os
34+
from dotenv import load_dotenv
35+
from azure.identity import DefaultAzureCredential
36+
from azure.ai.projects import AIProjectClient
37+
from azure.ai.projects.models import PromptAgentDefinition, PromptAgentDefinitionText, ResponseTextFormatConfigurationJsonSchema
38+
from pydantic import BaseModel
39+
40+
load_dotenv()
41+
42+
class CalendarEvent(BaseModel):
43+
model_config = {"extra": "forbid"}
44+
name: str
45+
date: str
46+
participants: list[str]
47+
48+
project_client = AIProjectClient(
49+
endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
50+
credential=DefaultAzureCredential(),
51+
)
52+
53+
with project_client:
54+
55+
openai_client = project_client.get_openai_client()
56+
57+
agent = project_client.agents.create_version(
58+
agent_name="MyAgent",
59+
definition=PromptAgentDefinition(
60+
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
61+
#BUG? text=PromptAgentDefinitionText(format=ResponseTextFormatConfigurationJsonSchema(name="CalendarEvent", schema=CalendarEvent.model_json_schema())),
62+
text=PromptAgentDefinitionText(format={"type": "json_schema", "name": "CalendarEvent", "schema": CalendarEvent.model_json_schema()}),
63+
instructions="""
64+
You are a helpful assistant that extracts calendar event information from the input user messages,
65+
and returns it in the desired structured output format.
66+
""",
67+
),
68+
)
69+
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
70+
71+
conversation = openai_client.conversations.create(
72+
items=[{"type": "message", "role": "user", "content": "Alice and Bob are going to a science fair on Friday."}],
73+
)
74+
print(f"Created conversation with initial user message (id: {conversation.id})")
75+
76+
response = openai_client.responses.create(
77+
conversation=conversation.id,
78+
extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
79+
input="",
80+
)
81+
print(f"Response output: {response.output_text}")
82+
83+
openai_client.conversations.delete(conversation_id=conversation.id)
84+
print("Conversation deleted")
85+
86+
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
87+
print("Agent deleted")

0 commit comments

Comments
 (0)