Skip to content

Commit 22e4689

Browse files
authored
use agent endpoints (#47983)
* upse agent endpoints * Refactor print statements for improved readability and remove unnecessary comments in sample agent scripts * recording * Update agent name retrieval to use environment variable with fallback to default value "MyAgent" across multiple sample scripts and tests. * Update asset tag in assets.json for versioning * Update asset tag in assets.json for versioning * sample update * recording * update comments * recording * recording * recording * recording * recording * sample update * recording * recording * fix mypy * fix sample * mypy * fix mypy * remove get agent call * update asset tag in assets.json * update asset tag in assets.json * clean up
1 parent d7a899c commit 22e4689

66 files changed

Lines changed: 1294 additions & 1141 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

sdk/ai/azure-ai-projects/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "python",
44
"TagPrefix": "python/ai/azure-ai-projects",
5-
"Tag": "python/ai/azure-ai-projects_c84bf9341a"
5+
"Tag": "python/ai/azure-ai-projects_875c4f833f"
66
}

sdk/ai/azure-ai-projects/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ pytyped = ["py.typed"]
6868
verifytypes = false
6969

7070
[tool.mypy]
71-
exclude = '.*samples[\\/]hosted_agents[\\/]assets[\\/].*main\.py$'
71+
mypy_path = ["samples"]
72+
exclude = '(^|.*[\\/])samples[\\/]hosted_agents[\\/]assets[\\/].*main\.py$|(^|.*[\\/])samples[\\/].+[\\/]utils?\.py$'
7273

7374
[tool.azure-sdk-conda]
7475
in_bundle = false

sdk/ai/azure-ai-projects/samples/agents/agent_retrieve_helper.py

Lines changed: 0 additions & 69 deletions
This file was deleted.

sdk/ai/azure-ai-projects/samples/agents/sample_agent_basic.py

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,58 +25,88 @@
2525
page of your Microsoft Foundry portal.
2626
2) FOUNDRY_MODEL_NAME - The deployment name of the AI model, as found under the "Name" column in
2727
the "Models + endpoints" tab in your Microsoft Foundry project.
28+
3) FOUNDRY_AGENT_NAME - Optional. The name of the AI agent. If not set, defaults to "MyAgent".
2829
"""
2930

3031
import os
3132
from dotenv import load_dotenv
3233
from azure.identity import DefaultAzureCredential
3334
from azure.ai.projects import AIProjectClient
34-
from azure.ai.projects.models import PromptAgentDefinition
35+
from azure.ai.projects.models import (
36+
AgentEndpointConfig,
37+
FixedRatioVersionSelectionRule,
38+
PromptAgentDefinition,
39+
ProtocolConfiguration,
40+
ResponsesProtocolConfiguration,
41+
VersionSelector,
42+
)
3543

3644
load_dotenv()
3745

3846
endpoint = os.environ["FOUNDRY_PROJECT_ENDPOINT"]
47+
agent_name = os.environ.get("FOUNDRY_AGENT_NAME", "MyAgent")
3948

4049
with (
4150
DefaultAzureCredential() as credential,
4251
AIProjectClient(endpoint=endpoint, credential=credential) as project_client,
4352
):
53+
created_version = None
54+
original_agent_endpoint = None
4455

45-
with project_client.get_openai_client() as openai_client:
46-
agent = project_client.agents.create_version(
47-
agent_name="MyAgent",
56+
try:
57+
created_version = project_client.agents.create_version(
58+
agent_name=agent_name,
4859
definition=PromptAgentDefinition(
4960
model=os.environ["FOUNDRY_MODEL_NAME"],
5061
instructions="You are a helpful assistant that answers general questions",
5162
),
5263
)
53-
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")
54-
55-
conversation = openai_client.conversations.create(
56-
items=[{"type": "message", "role": "user", "content": "What is the size of France in square miles?"}],
57-
)
58-
print(f"Created conversation with initial user message (id: {conversation.id})")
59-
60-
response = openai_client.responses.create(
61-
conversation=conversation.id,
62-
extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}},
63-
)
64-
print(f"Response output: {response.output_text}")
65-
66-
openai_client.conversations.items.create(
67-
conversation_id=conversation.id,
68-
items=[{"type": "message", "role": "user", "content": "And what is the capital city?"}],
64+
print(
65+
f"Agent created (id: {created_version.id}, name: {created_version.name}, version: {created_version.version})"
6966
)
70-
print("Added a second user message to the conversation")
7167

72-
response = openai_client.responses.create(
73-
conversation=conversation.id,
74-
extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}},
68+
original_agent_endpoint = project_client.agents.get(agent_name=agent_name).agent_endpoint
69+
endpoint_config = AgentEndpointConfig(
70+
version_selector=VersionSelector(
71+
version_selection_rules=[
72+
FixedRatioVersionSelectionRule(agent_version=created_version.version, traffic_percentage=100),
73+
]
74+
),
75+
protocol_configuration=ProtocolConfiguration(responses=ResponsesProtocolConfiguration()),
7576
)
76-
print(f"Response output: {response.output_text}")
77-
78-
openai_client.conversations.delete(conversation_id=conversation.id)
79-
print("Conversation deleted")
80-
81-
project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
82-
print("Agent deleted")
77+
project_client.agents.update_details(agent_name=agent_name, agent_endpoint=endpoint_config)
78+
print(f"Agent endpoint configured for version {created_version.version}")
79+
80+
with project_client.get_openai_client(agent_name=agent_name) as openai_client:
81+
82+
conversation = openai_client.conversations.create(
83+
items=[{"type": "message", "role": "user", "content": "What is the size of France in square miles?"}],
84+
)
85+
print(f"Created conversation with initial user message (id: {conversation.id})")
86+
87+
response = openai_client.responses.create(
88+
conversation=conversation.id,
89+
)
90+
print(f"Response output: {response.output_text}")
91+
92+
openai_client.conversations.items.create(
93+
conversation_id=conversation.id,
94+
items=[{"type": "message", "role": "user", "content": "And what is the capital city?"}],
95+
)
96+
print("Added a second user message to the conversation")
97+
98+
response = openai_client.responses.create(
99+
conversation=conversation.id,
100+
)
101+
print(f"Response output: {response.output_text}")
102+
103+
openai_client.conversations.delete(conversation_id=conversation.id)
104+
print("Conversation deleted")
105+
finally:
106+
if original_agent_endpoint is not None:
107+
project_client.agents.update_details(agent_name=agent_name, agent_endpoint=original_agent_endpoint)
108+
109+
if created_version is not None:
110+
project_client.agents.delete_version(
111+
agent_name=agent_name, agent_version=created_version.version, force=True
112+
)

sdk/ai/azure-ai-projects/samples/agents/sample_agent_basic_async.py

Lines changed: 72 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,64 +25,96 @@
2525
page of your Microsoft Foundry portal.
2626
2) FOUNDRY_MODEL_NAME - The deployment name of the AI model, as found under the "Name" column in
2727
the "Models + endpoints" tab in your Microsoft Foundry project.
28+
3) FOUNDRY_AGENT_NAME - Optional. The name of the AI agent. If not set, defaults to "MyAgent".
2829
"""
2930

3031
import asyncio
3132
import os
3233
from dotenv import load_dotenv
3334
from azure.identity.aio import DefaultAzureCredential
3435
from azure.ai.projects.aio import AIProjectClient
35-
from azure.ai.projects.models import PromptAgentDefinition
36+
from azure.ai.projects.models import (
37+
AgentEndpointConfig,
38+
FixedRatioVersionSelectionRule,
39+
PromptAgentDefinition,
40+
ProtocolConfiguration,
41+
ResponsesProtocolConfiguration,
42+
VersionSelector,
43+
)
3644

3745
load_dotenv()
3846

3947
endpoint = os.environ["FOUNDRY_PROJECT_ENDPOINT"]
48+
agent_name = os.environ.get("FOUNDRY_AGENT_NAME", "MyAgent")
4049

4150

4251
async def main() -> None:
4352
async with (
4453
DefaultAzureCredential() as credential,
4554
AIProjectClient(endpoint=endpoint, credential=credential) as project_client,
46-
project_client.get_openai_client() as openai_client,
4755
):
48-
49-
agent = await project_client.agents.create_version(
50-
agent_name="MyAgent",
51-
definition=PromptAgentDefinition(
52-
model=os.environ["FOUNDRY_MODEL_NAME"],
53-
instructions="You are a helpful assistant that answers general questions.",
54-
),
55-
)
56-
print(f"Agent created (name: {agent.name}, id: {agent.id}, version: {agent.version})")
57-
58-
conversation = await openai_client.conversations.create(
59-
items=[{"type": "message", "role": "user", "content": "What is the size of France in square miles?"}],
60-
)
61-
print(f"Created conversation with initial user message (id: {conversation.id})")
62-
63-
response = await openai_client.responses.create(
64-
conversation=conversation.id,
65-
extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}},
66-
)
67-
print(f"Response output: {response.output_text}")
68-
69-
await openai_client.conversations.items.create(
70-
conversation_id=conversation.id,
71-
items=[{"type": "message", "role": "user", "content": "And what is the capital city?"}],
72-
)
73-
print("Added a second user message to the conversation")
74-
75-
response = await openai_client.responses.create(
76-
conversation=conversation.id,
77-
extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}},
78-
)
79-
print(f"Response output: {response.output_text}")
80-
81-
await openai_client.conversations.delete(conversation_id=conversation.id)
82-
print("Conversation deleted")
83-
84-
await project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
85-
print("Agent deleted")
56+
created_version = None
57+
original_agent_endpoint = None
58+
59+
try:
60+
created_version = await project_client.agents.create_version(
61+
agent_name=agent_name,
62+
definition=PromptAgentDefinition(
63+
model=os.environ["FOUNDRY_MODEL_NAME"],
64+
instructions="You are a helpful assistant that answers general questions.",
65+
),
66+
)
67+
68+
original_agent_endpoint = (await project_client.agents.get(agent_name=agent_name)).agent_endpoint
69+
endpoint_config = AgentEndpointConfig(
70+
version_selector=VersionSelector(
71+
version_selection_rules=[
72+
FixedRatioVersionSelectionRule(agent_version=created_version.version, traffic_percentage=100),
73+
]
74+
),
75+
protocol_configuration=ProtocolConfiguration(responses=ResponsesProtocolConfiguration()),
76+
)
77+
await project_client.agents.update_details(agent_name=agent_name, agent_endpoint=endpoint_config)
78+
print(f"Agent endpoint configured for version {created_version.version}")
79+
80+
openai_client = project_client.get_openai_client(agent_name=agent_name)
81+
82+
agent = await project_client.agents.get(agent_name=agent_name)
83+
print(f"Agent created (name: {agent.name}, id: {agent.id}, version: {agent.versions.latest.version})")
84+
85+
conversation = await openai_client.conversations.create(
86+
items=[{"type": "message", "role": "user", "content": "What is the size of France in square miles?"}],
87+
)
88+
print(f"Created conversation with initial user message (id: {conversation.id})")
89+
90+
response = await openai_client.responses.create(
91+
conversation=conversation.id,
92+
)
93+
print(f"Response output: {response.output_text}")
94+
95+
await openai_client.conversations.items.create(
96+
conversation_id=conversation.id,
97+
items=[{"type": "message", "role": "user", "content": "And what is the capital city?"}],
98+
)
99+
print("Added a second user message to the conversation")
100+
101+
response = await openai_client.responses.create(
102+
conversation=conversation.id,
103+
)
104+
print(f"Response output: {response.output_text}")
105+
106+
await openai_client.conversations.delete(conversation_id=conversation.id)
107+
print("Conversation deleted")
108+
finally:
109+
if original_agent_endpoint is not None:
110+
await project_client.agents.update_details(
111+
agent_name=agent_name, agent_endpoint=original_agent_endpoint
112+
)
113+
114+
if created_version is not None:
115+
await project_client.agents.delete_version(
116+
agent_name=agent_name, agent_version=created_version.version, force=True
117+
)
86118

87119

88120
if __name__ == "__main__":

0 commit comments

Comments
 (0)