Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/ai/azure-ai-projects/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "python",
"TagPrefix": "python/ai/azure-ai-projects",
"Tag": "python/ai/azure-ai-projects_c84bf9341a"
"Tag": "python/ai/azure-ai-projects_f3cdb78ac6"
}
1 change: 1 addition & 0 deletions sdk/ai/azure-ai-projects/samples/agents/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Package marker for sample type-checking.

This file was deleted.

93 changes: 61 additions & 32 deletions sdk/ai/azure-ai-projects/samples/agents/sample_agent_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,87 @@
page of your Microsoft Foundry portal.
2) FOUNDRY_MODEL_NAME - The deployment name of the AI model, as found under the "Name" column in
the "Models + endpoints" tab in your Microsoft Foundry project.
3) FOUNDRY_AGENT_NAME - Optional. The name of the AI agent. If not set, defaults to "MyAgent".
"""

import os
from dotenv import load_dotenv
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import PromptAgentDefinition
from azure.ai.projects.models import (
AgentEndpointConfig,
FixedRatioVersionSelectionRule,
PromptAgentDefinition,
ProtocolConfiguration,
ResponsesProtocolConfiguration,
VersionSelector,
)

load_dotenv()

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

with (
DefaultAzureCredential() as credential,
AIProjectClient(endpoint=endpoint, credential=credential) as project_client,
):
created_version = None
original_agent_endpoint = None

with project_client.get_openai_client() as openai_client:
agent = project_client.agents.create_version(
agent_name="MyAgent",
try:
created_version = project_client.agents.create_version(
agent_name=agent_name,
definition=PromptAgentDefinition(
model=os.environ["FOUNDRY_MODEL_NAME"],
instructions="You are a helpful assistant that answers general questions",
),
)
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.version})")

conversation = openai_client.conversations.create(
items=[{"type": "message", "role": "user", "content": "What is the size of France in square miles?"}],
)
print(f"Created conversation with initial user message (id: {conversation.id})")

response = openai_client.responses.create(
conversation=conversation.id,
extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}},
)
print(f"Response output: {response.output_text}")

openai_client.conversations.items.create(
conversation_id=conversation.id,
items=[{"type": "message", "role": "user", "content": "And what is the capital city?"}],
)
print("Added a second user message to the conversation")

response = openai_client.responses.create(
conversation=conversation.id,
extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}},
original_agent_endpoint = project_client.agents.get(agent_name=agent_name).agent_endpoint
endpoint_config = AgentEndpointConfig(
version_selector=VersionSelector(
version_selection_rules=[
FixedRatioVersionSelectionRule(agent_version=created_version.version, traffic_percentage=100),
]
),
protocol_configuration=ProtocolConfiguration(responses=ResponsesProtocolConfiguration()),
)
print(f"Response output: {response.output_text}")

openai_client.conversations.delete(conversation_id=conversation.id)
print("Conversation deleted")

project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
print("Agent deleted")
project_client.agents.update_details(agent_name=agent_name, agent_endpoint=endpoint_config)
print(f"Agent endpoint configured for version {created_version.version}")

with project_client.get_openai_client(agent_name=agent_name) as openai_client:
agent = project_client.agents.get(agent_name=agent_name)
print(f"Agent created (id: {agent.id}, name: {agent.name}, version: {agent.versions.latest.version})")

conversation = openai_client.conversations.create(
items=[{"type": "message", "role": "user", "content": "What is the size of France in square miles?"}],
)
print(f"Created conversation with initial user message (id: {conversation.id})")

response = openai_client.responses.create(
conversation=conversation.id,
)
print(f"Response output: {response.output_text}")

openai_client.conversations.items.create(
conversation_id=conversation.id,
items=[{"type": "message", "role": "user", "content": "And what is the capital city?"}],
)
print("Added a second user message to the conversation")

response = openai_client.responses.create(
conversation=conversation.id,
)
print(f"Response output: {response.output_text}")

openai_client.conversations.delete(conversation_id=conversation.id)
print("Conversation deleted")
finally:
if original_agent_endpoint is not None:
project_client.agents.update_details(agent_name=agent_name, agent_endpoint=original_agent_endpoint)

if created_version is not None:
project_client.agents.delete_version(
agent_name=agent_name, agent_version=created_version.version, force=True
)
112 changes: 72 additions & 40 deletions sdk/ai/azure-ai-projects/samples/agents/sample_agent_basic_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,64 +25,96 @@
page of your Microsoft Foundry portal.
2) FOUNDRY_MODEL_NAME - The deployment name of the AI model, as found under the "Name" column in
the "Models + endpoints" tab in your Microsoft Foundry project.
3) FOUNDRY_AGENT_NAME - Optional. The name of the AI agent. If not set, defaults to "MyAgent".
"""

import asyncio
import os
from dotenv import load_dotenv
from azure.identity.aio import DefaultAzureCredential
from azure.ai.projects.aio import AIProjectClient
from azure.ai.projects.models import PromptAgentDefinition
from azure.ai.projects.models import (
AgentEndpointConfig,
FixedRatioVersionSelectionRule,
PromptAgentDefinition,
ProtocolConfiguration,
ResponsesProtocolConfiguration,
VersionSelector,
)

load_dotenv()

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


async def main() -> None:
async with (
DefaultAzureCredential() as credential,
AIProjectClient(endpoint=endpoint, credential=credential) as project_client,
project_client.get_openai_client() as openai_client,
):

agent = await project_client.agents.create_version(
agent_name="MyAgent",
definition=PromptAgentDefinition(
model=os.environ["FOUNDRY_MODEL_NAME"],
instructions="You are a helpful assistant that answers general questions.",
),
)
print(f"Agent created (name: {agent.name}, id: {agent.id}, version: {agent.version})")

conversation = await openai_client.conversations.create(
items=[{"type": "message", "role": "user", "content": "What is the size of France in square miles?"}],
)
print(f"Created conversation with initial user message (id: {conversation.id})")

response = await openai_client.responses.create(
conversation=conversation.id,
extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}},
)
print(f"Response output: {response.output_text}")

await openai_client.conversations.items.create(
conversation_id=conversation.id,
items=[{"type": "message", "role": "user", "content": "And what is the capital city?"}],
)
print("Added a second user message to the conversation")

response = await openai_client.responses.create(
conversation=conversation.id,
extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}},
)
print(f"Response output: {response.output_text}")

await openai_client.conversations.delete(conversation_id=conversation.id)
print("Conversation deleted")

await project_client.agents.delete_version(agent_name=agent.name, agent_version=agent.version)
print("Agent deleted")
created_version = None
original_agent_endpoint = None

try:
created_version = await project_client.agents.create_version(
agent_name=agent_name,
definition=PromptAgentDefinition(
model=os.environ["FOUNDRY_MODEL_NAME"],
instructions="You are a helpful assistant that answers general questions.",
),
)

original_agent_endpoint = (await project_client.agents.get(agent_name=agent_name)).agent_endpoint
endpoint_config = AgentEndpointConfig(
version_selector=VersionSelector(
version_selection_rules=[
FixedRatioVersionSelectionRule(agent_version=created_version.version, traffic_percentage=100),
]
),
protocol_configuration=ProtocolConfiguration(responses=ResponsesProtocolConfiguration()),
)
await project_client.agents.update_details(agent_name=agent_name, agent_endpoint=endpoint_config)
print(f"Agent endpoint configured for version {created_version.version}")

openai_client = project_client.get_openai_client(agent_name=agent_name)

agent = await project_client.agents.get(agent_name=agent_name)
print(f"Agent created (name: {agent.name}, id: {agent.id}, version: {agent.versions.latest.version})")

conversation = await openai_client.conversations.create(
items=[{"type": "message", "role": "user", "content": "What is the size of France in square miles?"}],
)
print(f"Created conversation with initial user message (id: {conversation.id})")

response = await openai_client.responses.create(
conversation=conversation.id,
)
print(f"Response output: {response.output_text}")

await openai_client.conversations.items.create(
conversation_id=conversation.id,
items=[{"type": "message", "role": "user", "content": "And what is the capital city?"}],
)
print("Added a second user message to the conversation")

response = await openai_client.responses.create(
conversation=conversation.id,
)
print(f"Response output: {response.output_text}")

await openai_client.conversations.delete(conversation_id=conversation.id)
print("Conversation deleted")
finally:
if original_agent_endpoint is not None:
await project_client.agents.update_details(
agent_name=agent_name, agent_endpoint=original_agent_endpoint
)

if created_version is not None:
await project_client.agents.delete_version(
agent_name=agent_name, agent_version=created_version.version, force=True
)


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,43 @@
page of your Microsoft Foundry portal.
2) FOUNDRY_MODEL_NAME - The deployment name of the AI model, as found under the "Name" column in
the "Models + endpoints" tab in your Microsoft Foundry project.
3) FOUNDRY_AGENT_NAME - Optional. The name of the AI agent. If not set, defaults to "MyAgent".
"""

import os
from dotenv import load_dotenv
from agent_retrieve_helper import create_and_retrieve_agent_and_conversation # pylint: disable=import-error
from util import create_version_with_endpoint
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import PromptAgentDefinition

load_dotenv()

endpoint = os.environ["FOUNDRY_PROJECT_ENDPOINT"]
model = os.environ["FOUNDRY_MODEL_NAME"]

agent_name = os.environ.get("FOUNDRY_AGENT_NAME", "MyAgent")
with (
DefaultAzureCredential() as credential,
AIProjectClient(endpoint=endpoint, credential=credential) as project_client,
# Creates prerequisite resources and yields (agent_name, conversation_id).
# Then automatically deletes the created agent version when this context manager exits.
create_and_retrieve_agent_and_conversation(project_client=project_client, model=model) as (
agent_name,
conversation_id,
create_version_with_endpoint(
project_client=project_client,
agent_name=agent_name,
definition=PromptAgentDefinition(
model=model,
instructions="You are a helpful assistant.",
),
),
project_client.get_openai_client() as openai_client,
project_client.get_openai_client(agent_name=agent_name) as openai_client,
):
agent = project_client.agents.get(agent_name=agent_name)
conversation = openai_client.conversations.create()
print(f"Conversation created (id: {conversation.id})")

# Retrieve latest version for the prerequisite agent.
agent = project_client.agents.get(agent_name=agent_name)
print(f"Agent retrieved (id: {agent.id}, name: {agent.name}, version: {agent.versions.latest.version})")

# Retrieve the prerequisite conversation.
conversation = openai_client.conversations.retrieve(conversation_id=conversation_id)
conversation = openai_client.conversations.retrieve(conversation_id=conversation.id)
print(f"Retrieved conversation (id: {conversation.id})")

# Add a new user text message to the conversation
Expand All @@ -68,6 +74,5 @@

response = openai_client.responses.create(
conversation=conversation.id,
extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}},
)
print(f"Response output: {response.output_text}")
Loading
Loading