mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: [BREAKING] Renamed create_agent to as_agent (#3249)
* Renamed create_agent to as_agent * Override for as_agent * Added override
This commit is contained in:
committed by
GitHub
Unverified
parent
a151f10cc2
commit
5687e13221
@@ -17,7 +17,7 @@ This sample demonstrates using Anthropic with:
|
||||
|
||||
async def main() -> None:
|
||||
"""Example of streaming response (get results as they are generated)."""
|
||||
agent = AnthropicClient[AnthropicChatOptions]().create_agent(
|
||||
agent = AnthropicClient[AnthropicChatOptions]().as_agent(
|
||||
name="DocsAgent",
|
||||
instructions="You are a helpful agent for both Microsoft docs questions and general questions.",
|
||||
tools=[
|
||||
|
||||
@@ -26,7 +26,7 @@ async def non_streaming_example() -> None:
|
||||
print("=== Non-streaming Response Example ===")
|
||||
|
||||
agent = AnthropicClient(
|
||||
).create_agent(
|
||||
).as_agent(
|
||||
name="WeatherAgent",
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
@@ -43,7 +43,7 @@ async def streaming_example() -> None:
|
||||
print("=== Streaming Response Example ===")
|
||||
|
||||
agent = AnthropicClient(
|
||||
).create_agent(
|
||||
).as_agent(
|
||||
name="WeatherAgent",
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
|
||||
@@ -28,7 +28,7 @@ To use the Foundry integration ensure you have the following environment variabl
|
||||
|
||||
async def main() -> None:
|
||||
"""Example of streaming response (get results as they are generated)."""
|
||||
agent = AnthropicClient(anthropic_client=AsyncAnthropicFoundry()).create_agent(
|
||||
agent = AnthropicClient(anthropic_client=AsyncAnthropicFoundry()).as_agent(
|
||||
name="DocsAgent",
|
||||
instructions="You are a helpful agent for both Microsoft docs questions and general questions.",
|
||||
tools=[
|
||||
|
||||
@@ -31,7 +31,7 @@ async def main() -> None:
|
||||
|
||||
# Create a agent with the pptx skill enabled
|
||||
# Skills also need the code interpreter tool to function
|
||||
agent = client.create_agent(
|
||||
agent = client.as_agent(
|
||||
name="DocsAgent",
|
||||
instructions="You are a helpful agent for creating powerpoint presentations.",
|
||||
tools=HostedCodeInterpreterTool(),
|
||||
|
||||
@@ -145,49 +145,6 @@ async def get_agent_by_reference_example() -> None:
|
||||
)
|
||||
|
||||
|
||||
async def get_agent_by_details_example() -> None:
|
||||
"""Example of using provider.get_agent(details=...) with pre-fetched AgentDetails.
|
||||
|
||||
This method uses pre-fetched AgentDetails to get the latest version.
|
||||
Use this when you already have AgentDetails from a previous API call.
|
||||
"""
|
||||
print("=== provider.get_agent(details=...) Example ===")
|
||||
|
||||
async with (
|
||||
AzureCliCredential() as credential,
|
||||
AIProjectClient(endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], credential=credential) as project_client,
|
||||
):
|
||||
# First, create an agent using the SDK directly
|
||||
created_agent = await project_client.agents.create_version(
|
||||
agent_name="TestAgentByDetails",
|
||||
description="Test agent for get_agent by details example.",
|
||||
definition=PromptAgentDefinition(
|
||||
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
|
||||
instructions="You are a helpful assistant. Always include an emoji in your response.",
|
||||
),
|
||||
)
|
||||
|
||||
try:
|
||||
# Fetch AgentDetails separately (simulating a previous API call)
|
||||
agent_details = await project_client.agents.get(agent_name=created_agent.name)
|
||||
|
||||
# Get the agent using the pre-fetched details (sync - no HTTP call)
|
||||
provider = AzureAIProjectAgentProvider(project_client=project_client)
|
||||
agent = provider.as_agent(agent_details.versions.latest)
|
||||
|
||||
print(f"Retrieved agent: {agent.name} (from pre-fetched details)")
|
||||
|
||||
query = "How are you today?"
|
||||
print(f"User: {query}")
|
||||
result = await agent.run(query)
|
||||
print(f"Agent: {result}\n")
|
||||
finally:
|
||||
# Clean up the agent
|
||||
await project_client.agents.delete_version(
|
||||
agent_name=created_agent.name, agent_version=created_agent.version
|
||||
)
|
||||
|
||||
|
||||
async def multiple_agents_example() -> None:
|
||||
"""Example of using a single provider to spawn multiple agents.
|
||||
|
||||
@@ -284,7 +241,6 @@ async def main() -> None:
|
||||
await create_agent_example()
|
||||
await get_agent_by_name_example()
|
||||
await get_agent_by_reference_example()
|
||||
await get_agent_by_details_example()
|
||||
await as_agent_example()
|
||||
await multiple_agents_example()
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ async def non_streaming_example() -> None:
|
||||
# and deleted after getting a response
|
||||
# For authentication, run `az login` command in terminal or replace AzureCliCredential with preferred
|
||||
# authentication option.
|
||||
async with AzureOpenAIAssistantsClient(credential=AzureCliCredential()).create_agent(
|
||||
async with AzureOpenAIAssistantsClient(credential=AzureCliCredential()).as_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
) as agent:
|
||||
@@ -48,7 +48,7 @@ async def streaming_example() -> None:
|
||||
|
||||
# Since no assistant ID is provided, the assistant will be automatically created
|
||||
# and deleted after getting a response
|
||||
async with AzureOpenAIAssistantsClient(credential=AzureCliCredential()).create_agent(
|
||||
async with AzureOpenAIAssistantsClient(credential=AzureCliCredential()).as_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
) as agent:
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ async def main() -> None:
|
||||
endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
|
||||
deployment_name=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"],
|
||||
credential=AzureCliCredential(),
|
||||
).create_agent(
|
||||
).as_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
) as agent:
|
||||
|
||||
@@ -31,7 +31,7 @@ async def non_streaming_example() -> None:
|
||||
# Create agent with Azure Chat Client
|
||||
# For authentication, run `az login` command in terminal or replace AzureCliCredential with preferred
|
||||
# authentication option.
|
||||
agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
|
||||
agent = AzureOpenAIChatClient(credential=AzureCliCredential()).as_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
@@ -49,7 +49,7 @@ async def streaming_example() -> None:
|
||||
# Create agent with Azure Chat Client
|
||||
# For authentication, run `az login` command in terminal or replace AzureCliCredential with preferred
|
||||
# authentication option.
|
||||
agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
|
||||
agent = AzureOpenAIChatClient(credential=AzureCliCredential()).as_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ async def main() -> None:
|
||||
deployment_name=os.environ["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"],
|
||||
endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
|
||||
credential=AzureCliCredential(),
|
||||
).create_agent(
|
||||
).as_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
|
||||
@@ -30,7 +30,7 @@ async def non_streaming_example() -> None:
|
||||
|
||||
# For authentication, run `az login` command in terminal or replace AzureCliCredential with preferred
|
||||
# authentication option.
|
||||
agent = AzureOpenAIResponsesClient(credential=AzureCliCredential()).create_agent(
|
||||
agent = AzureOpenAIResponsesClient(credential=AzureCliCredential()).as_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
@@ -47,7 +47,7 @@ async def streaming_example() -> None:
|
||||
|
||||
# For authentication, run `az login` command in terminal or replace AzureCliCredential with preferred
|
||||
# authentication option.
|
||||
agent = AzureOpenAIResponsesClient(credential=AzureCliCredential()).create_agent(
|
||||
agent = AzureOpenAIResponsesClient(credential=AzureCliCredential()).as_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ async def main():
|
||||
print("=== Azure Responses Agent with Image Analysis ===")
|
||||
|
||||
# 1. Create an Azure Responses agent with vision capabilities
|
||||
agent = AzureOpenAIResponsesClient(credential=AzureCliCredential()).create_agent(
|
||||
agent = AzureOpenAIResponsesClient(credential=AzureCliCredential()).as_agent(
|
||||
name="VisionAgent",
|
||||
instructions="You are a helpful agent that can analyze images.",
|
||||
)
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@ async def main() -> None:
|
||||
deployment_name=os.environ["AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME"],
|
||||
endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
|
||||
credential=AzureCliCredential(),
|
||||
).create_agent(
|
||||
).as_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@ async def main():
|
||||
credential=credential,
|
||||
)
|
||||
|
||||
agent: ChatAgent = responses_client.create_agent(
|
||||
agent: ChatAgent = responses_client.as_agent(
|
||||
name="DocsAgent",
|
||||
instructions=("You are a helpful assistant that can help with Microsoft documentation questions."),
|
||||
)
|
||||
|
||||
@@ -125,7 +125,7 @@ async def main() -> None:
|
||||
print(f"Direct response: {direct_response.messages[0].text}")
|
||||
|
||||
# Create an agent using the custom chat client
|
||||
echo_agent = echo_client.create_agent(
|
||||
echo_agent = echo_client.as_agent(
|
||||
name="EchoAgent",
|
||||
instructions="You are a helpful assistant that echoes back what users say.",
|
||||
)
|
||||
|
||||
@@ -27,7 +27,7 @@ async def non_streaming_example() -> None:
|
||||
"""Example of non-streaming response (get the complete result at once)."""
|
||||
print("=== Non-streaming Response Example ===")
|
||||
|
||||
agent = OllamaChatClient().create_agent(
|
||||
agent = OllamaChatClient().as_agent(
|
||||
name="TimeAgent",
|
||||
instructions="You are a helpful time agent answer in one sentence.",
|
||||
tools=get_time,
|
||||
@@ -43,7 +43,7 @@ async def streaming_example() -> None:
|
||||
"""Example of streaming response (get results as they are generated)."""
|
||||
print("=== Streaming Response Example ===")
|
||||
|
||||
agent = OllamaChatClient().create_agent(
|
||||
agent = OllamaChatClient().as_agent(
|
||||
name="TimeAgent",
|
||||
instructions="You are a helpful time agent answer in one sentence.",
|
||||
tools=get_time,
|
||||
|
||||
@@ -21,7 +21,7 @@ https://ollama.com/
|
||||
async def reasoning_example() -> None:
|
||||
print("=== Response Reasoning Example ===")
|
||||
|
||||
agent = OllamaChatClient().create_agent(
|
||||
agent = OllamaChatClient().as_agent(
|
||||
name="TimeAgent",
|
||||
instructions="You are a helpful agent answer in one sentence.",
|
||||
default_options={"think": True}, # Enable Reasoning on agent level
|
||||
|
||||
@@ -36,7 +36,7 @@ async def non_streaming_example() -> None:
|
||||
api_key="ollama", # Just a placeholder, Ollama doesn't require API key
|
||||
base_url=os.getenv("OLLAMA_ENDPOINT"),
|
||||
model_id=os.getenv("OLLAMA_MODEL"),
|
||||
).create_agent(
|
||||
).as_agent(
|
||||
name="WeatherAgent",
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
@@ -56,7 +56,7 @@ async def streaming_example() -> None:
|
||||
api_key="ollama", # Just a placeholder, Ollama doesn't require API key
|
||||
base_url=os.getenv("OLLAMA_ENDPOINT"),
|
||||
model_id=os.getenv("OLLAMA_MODEL"),
|
||||
).create_agent(
|
||||
).as_agent(
|
||||
name="WeatherAgent",
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
|
||||
@@ -26,7 +26,7 @@ async def non_streaming_example() -> None:
|
||||
"""Example of non-streaming response (get the complete result at once)."""
|
||||
print("=== Non-streaming Response Example ===")
|
||||
|
||||
agent = OpenAIChatClient().create_agent(
|
||||
agent = OpenAIChatClient().as_agent(
|
||||
name="WeatherAgent",
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
@@ -42,7 +42,7 @@ async def streaming_example() -> None:
|
||||
"""Example of streaming response (get results as they are generated)."""
|
||||
print("=== Streaming Response Example ===")
|
||||
|
||||
agent = OpenAIChatClient().create_agent(
|
||||
agent = OpenAIChatClient().as_agent(
|
||||
name="WeatherAgent",
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ async def main() -> None:
|
||||
agent = OpenAIChatClient(
|
||||
model_id=os.environ["OPENAI_CHAT_MODEL_ID"],
|
||||
api_key=os.environ["OPENAI_API_KEY"],
|
||||
).create_agent(
|
||||
).as_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
|
||||
@@ -55,7 +55,7 @@ async def mcp_tools_on_agent_level() -> None:
|
||||
# Tools are provided when creating the agent
|
||||
# The agent can use these tools for any query during its lifetime
|
||||
# The agent will connect to the MCP server through its context manager.
|
||||
async with OpenAIChatClient().create_agent(
|
||||
async with OpenAIChatClient().as_agent(
|
||||
name="DocsAgent",
|
||||
instructions="You are a helpful assistant that can help with microsoft documentation questions.",
|
||||
tools=MCPStreamableHTTPTool( # Tools defined at agent creation
|
||||
|
||||
+2
-2
@@ -32,7 +32,7 @@ runtime_schema = {
|
||||
async def non_streaming_example() -> None:
|
||||
print("=== Non-streaming runtime JSON schema example ===")
|
||||
|
||||
agent = OpenAIChatClient[OpenAIChatOptions]().create_agent(
|
||||
agent = OpenAIChatClient[OpenAIChatOptions]().as_agent(
|
||||
name="RuntimeSchemaAgent",
|
||||
instructions="Return only JSON that matches the provided schema. Do not add commentary.",
|
||||
)
|
||||
@@ -65,7 +65,7 @@ async def non_streaming_example() -> None:
|
||||
async def streaming_example() -> None:
|
||||
print("=== Streaming runtime JSON schema example ===")
|
||||
|
||||
agent = OpenAIChatClient().create_agent(
|
||||
agent = OpenAIChatClient().as_agent(
|
||||
name="RuntimeSchemaAgent",
|
||||
instructions="Return only JSON that matches the provided schema. Do not add commentary.",
|
||||
)
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ async def main():
|
||||
print("=== OpenAI Responses Agent with Image Analysis ===")
|
||||
|
||||
# 1. Create an OpenAI Responses agent with vision capabilities
|
||||
agent = OpenAIResponsesClient().create_agent(
|
||||
agent = OpenAIResponsesClient().as_agent(
|
||||
name="VisionAgent",
|
||||
instructions="You are a helpful agent that can analyze images.",
|
||||
)
|
||||
|
||||
+1
-1
@@ -48,7 +48,7 @@ async def main() -> None:
|
||||
print("=== OpenAI Responses Image Generation Agent Example ===")
|
||||
|
||||
# Create an agent with customized image generation options
|
||||
agent = OpenAIResponsesClient().create_agent(
|
||||
agent = OpenAIResponsesClient().as_agent(
|
||||
instructions="You are a helpful AI that can generate images.",
|
||||
tools=[
|
||||
HostedImageGenerationTool(
|
||||
|
||||
@@ -19,7 +19,7 @@ In this case they are here: https://platform.openai.com/docs/api-reference/respo
|
||||
"""
|
||||
|
||||
|
||||
agent = OpenAIResponsesClient[OpenAIResponsesOptions](model_id="gpt-5").create_agent(
|
||||
agent = OpenAIResponsesClient[OpenAIResponsesOptions](model_id="gpt-5").as_agent(
|
||||
name="MathHelper",
|
||||
instructions="You are a personal math tutor. When asked a math question, "
|
||||
"reason over how best to approach the problem and share your thought process.",
|
||||
|
||||
+1
-1
@@ -42,7 +42,7 @@ async def main():
|
||||
print("=== OpenAI Streaming Image Generation Example ===\n")
|
||||
|
||||
# Create agent with streaming image generation enabled
|
||||
agent = OpenAIResponsesClient().create_agent(
|
||||
agent = OpenAIResponsesClient().as_agent(
|
||||
instructions="You are a helpful agent that can generate images.",
|
||||
tools=[
|
||||
HostedImageGenerationTool(
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ async def main() -> None:
|
||||
agent = OpenAIResponsesClient(
|
||||
model_id=os.environ["OPENAI_RESPONSES_MODEL_ID"],
|
||||
api_key=os.environ["OPENAI_API_KEY"],
|
||||
).create_agent(
|
||||
).as_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
|
||||
+2
-2
@@ -32,7 +32,7 @@ runtime_schema = {
|
||||
async def non_streaming_example() -> None:
|
||||
print("=== Non-streaming runtime JSON schema example ===")
|
||||
|
||||
agent = OpenAIResponsesClient().create_agent(
|
||||
agent = OpenAIResponsesClient().as_agent(
|
||||
name="RuntimeSchemaAgent",
|
||||
instructions="Return only JSON that matches the provided schema. Do not add commentary.",
|
||||
)
|
||||
@@ -65,7 +65,7 @@ async def non_streaming_example() -> None:
|
||||
async def streaming_example() -> None:
|
||||
print("=== Streaming runtime JSON schema example ===")
|
||||
|
||||
agent = OpenAIResponsesClient().create_agent(
|
||||
agent = OpenAIResponsesClient().as_agent(
|
||||
name="RuntimeSchemaAgent",
|
||||
instructions="Return only JSON that matches the provided schema. Do not add commentary.",
|
||||
)
|
||||
|
||||
+2
-2
@@ -25,7 +25,7 @@ async def non_streaming_example() -> None:
|
||||
print("=== Non-streaming example ===")
|
||||
|
||||
# Create an OpenAI Responses agent
|
||||
agent = OpenAIResponsesClient().create_agent(
|
||||
agent = OpenAIResponsesClient().as_agent(
|
||||
name="CityAgent",
|
||||
instructions="You are a helpful agent that describes cities in a structured format.",
|
||||
)
|
||||
@@ -51,7 +51,7 @@ async def streaming_example() -> None:
|
||||
print("=== Streaming example ===")
|
||||
|
||||
# Create an OpenAI Responses agent
|
||||
agent = OpenAIResponsesClient().create_agent(
|
||||
agent = OpenAIResponsesClient().as_agent(
|
||||
name="CityAgent",
|
||||
instructions="You are a helpful agent that describes cities in a structured format.",
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user