mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: openai updates (#388)
* openai updates * rebuild of openai structure * updated responses structure * renamed sample * added file id support to code interpreter * added hosted file ids to code interpretor * mypy fixes * removed default az cred from codebase * updated agent name setup * added kwargs to entra methods * and further kwargs * extra comment * updated all samples * readded custom get methods for responses * updated int tests with ad credential * missed one
This commit is contained in:
committed by
GitHub
Unverified
parent
19676978e9
commit
df9d85d1f0
+3
-5
@@ -4,8 +4,8 @@ import asyncio
|
||||
from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework import ChatClientAgent
|
||||
from agent_framework.azure import AzureAssistantsClient
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -23,8 +23,7 @@ async def non_streaming_example() -> None:
|
||||
|
||||
# Since no assistant ID is provided, the assistant will be automatically created
|
||||
# and deleted after getting a response
|
||||
async with ChatClientAgent(
|
||||
chat_client=AzureAssistantsClient(),
|
||||
async with AzureAssistantsClient(ad_credential=DefaultAzureCredential()).create_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
) as agent:
|
||||
@@ -40,8 +39,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 ChatClientAgent(
|
||||
chat_client=AzureAssistantsClient(),
|
||||
async with AzureAssistantsClient(ad_credential=DefaultAzureCredential()).create_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
) as agent:
|
||||
|
||||
+2
-1
@@ -4,6 +4,7 @@ import asyncio
|
||||
|
||||
from agent_framework import AgentRunResponseUpdate, ChatClientAgent, ChatResponseUpdate, HostedCodeInterpreterTool
|
||||
from agent_framework.azure import AzureAssistantsClient
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from openai.types.beta.threads.runs import (
|
||||
CodeInterpreterToolCallDelta,
|
||||
RunStepDelta,
|
||||
@@ -37,7 +38,7 @@ async def main() -> None:
|
||||
print("=== Azure OpenAI Assistants Agent with Code Interpreter Example ===")
|
||||
|
||||
async with ChatClientAgent(
|
||||
chat_client=AzureAssistantsClient(),
|
||||
chat_client=AzureAssistantsClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful assistant that can write and execute Python code to solve problems.",
|
||||
tools=HostedCodeInterpreterTool(),
|
||||
) as agent:
|
||||
|
||||
+4
-3
@@ -7,6 +7,7 @@ from typing import Annotated
|
||||
|
||||
from agent_framework import ChatClientAgent
|
||||
from agent_framework.azure import AzureAssistantsClient
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -31,7 +32,7 @@ async def tools_on_agent_level() -> None:
|
||||
# Tools are provided when creating the agent
|
||||
# The agent can use these tools for any query during its lifetime
|
||||
async with ChatClientAgent(
|
||||
chat_client=AzureAssistantsClient(),
|
||||
chat_client=AzureAssistantsClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful assistant that can provide weather and time information.",
|
||||
tools=[get_weather, get_time], # Tools defined at agent creation
|
||||
) as agent:
|
||||
@@ -60,7 +61,7 @@ async def tools_on_run_level() -> None:
|
||||
|
||||
# Agent created without tools
|
||||
async with ChatClientAgent(
|
||||
chat_client=AzureAssistantsClient(),
|
||||
chat_client=AzureAssistantsClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful assistant.",
|
||||
# No tools defined here
|
||||
) as agent:
|
||||
@@ -89,7 +90,7 @@ async def mixed_tools_example() -> None:
|
||||
|
||||
# Agent created with some base tools
|
||||
async with ChatClientAgent(
|
||||
chat_client=AzureAssistantsClient(),
|
||||
chat_client=AzureAssistantsClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a comprehensive assistant that can help with various information requests.",
|
||||
tools=[get_weather], # Base tool available for all queries
|
||||
) as agent:
|
||||
|
||||
+5
-4
@@ -6,6 +6,7 @@ from typing import Annotated
|
||||
|
||||
from agent_framework import ChatClientAgent, ChatClientAgentThread
|
||||
from agent_framework.azure import AzureAssistantsClient
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -22,7 +23,7 @@ async def example_with_automatic_thread_creation() -> None:
|
||||
print("=== Automatic Thread Creation Example ===")
|
||||
|
||||
async with ChatClientAgent(
|
||||
chat_client=AzureAssistantsClient(),
|
||||
chat_client=AzureAssistantsClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
) as agent:
|
||||
@@ -46,7 +47,7 @@ async def example_with_thread_persistence() -> None:
|
||||
print("Using the same thread across multiple conversations to maintain context.\n")
|
||||
|
||||
async with ChatClientAgent(
|
||||
chat_client=AzureAssistantsClient(),
|
||||
chat_client=AzureAssistantsClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
) as agent:
|
||||
@@ -82,7 +83,7 @@ async def example_with_existing_thread_id() -> None:
|
||||
existing_thread_id = None
|
||||
|
||||
async with ChatClientAgent(
|
||||
chat_client=AzureAssistantsClient(),
|
||||
chat_client=AzureAssistantsClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
) as agent:
|
||||
@@ -102,7 +103,7 @@ async def example_with_existing_thread_id() -> None:
|
||||
|
||||
# Create a new agent instance but use the existing thread ID
|
||||
async with ChatClientAgent(
|
||||
chat_client=AzureAssistantsClient(thread_id=existing_thread_id),
|
||||
chat_client=AzureAssistantsClient(thread_id=existing_thread_id, ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
) as agent:
|
||||
|
||||
@@ -4,8 +4,8 @@ import asyncio
|
||||
from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework import ChatClientAgent
|
||||
from agent_framework.azure import AzureChatClient
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -22,8 +22,7 @@ async def non_streaming_example() -> None:
|
||||
print("=== Non-streaming Response Example ===")
|
||||
|
||||
# Create agent with Azure Chat Client
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureChatClient(),
|
||||
agent = AzureChatClient(ad_credential=DefaultAzureCredential()).create_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
@@ -39,8 +38,7 @@ async def streaming_example() -> None:
|
||||
print("=== Streaming Response Example ===")
|
||||
|
||||
# Create agent with Azure Chat Client
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureChatClient(),
|
||||
agent = AzureChatClient(ad_credential=DefaultAzureCredential()).create_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
|
||||
+4
-3
@@ -7,6 +7,7 @@ from typing import Annotated
|
||||
|
||||
from agent_framework import ChatClientAgent
|
||||
from agent_framework.azure import AzureChatClient
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -31,7 +32,7 @@ async def tools_on_agent_level() -> None:
|
||||
# Tools are provided when creating the agent
|
||||
# The agent can use these tools for any query during its lifetime
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureChatClient(),
|
||||
chat_client=AzureChatClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful assistant that can provide weather and time information.",
|
||||
tools=[get_weather, get_time], # Tools defined at agent creation
|
||||
)
|
||||
@@ -61,7 +62,7 @@ async def tools_on_run_level() -> None:
|
||||
|
||||
# Agent created without tools
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureChatClient(),
|
||||
chat_client=AzureChatClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful assistant.",
|
||||
# No tools defined here
|
||||
)
|
||||
@@ -91,7 +92,7 @@ async def mixed_tools_example() -> None:
|
||||
|
||||
# Agent created with some base tools
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureChatClient(),
|
||||
chat_client=AzureChatClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a comprehensive assistant that can help with various information requests.",
|
||||
tools=[get_weather], # Base tool available for all queries
|
||||
)
|
||||
|
||||
+5
-4
@@ -6,6 +6,7 @@ from typing import Annotated
|
||||
|
||||
from agent_framework import ChatClientAgent, ChatClientAgentThread
|
||||
from agent_framework.azure import AzureChatClient
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -22,7 +23,7 @@ async def example_with_automatic_thread_creation() -> None:
|
||||
print("=== Automatic Thread Creation Example ===")
|
||||
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureChatClient(),
|
||||
chat_client=AzureChatClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
@@ -47,7 +48,7 @@ async def example_with_thread_persistence() -> None:
|
||||
print("Using the same thread across multiple conversations to maintain context.\n")
|
||||
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureChatClient(),
|
||||
chat_client=AzureChatClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
@@ -80,7 +81,7 @@ async def example_with_existing_thread_messages() -> None:
|
||||
print("=== Existing Thread Messages Example ===")
|
||||
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureChatClient(),
|
||||
chat_client=AzureChatClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
@@ -102,7 +103,7 @@ async def example_with_existing_thread_messages() -> None:
|
||||
|
||||
# Create a new agent instance but use the existing thread with its message history
|
||||
new_agent = ChatClientAgent(
|
||||
chat_client=AzureChatClient(),
|
||||
chat_client=AzureChatClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
|
||||
+3
-5
@@ -4,8 +4,8 @@ import asyncio
|
||||
from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework import ChatClientAgent
|
||||
from agent_framework.azure import AzureResponsesClient
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -21,8 +21,7 @@ async def non_streaming_example() -> None:
|
||||
"""Example of non-streaming response (get the complete result at once)."""
|
||||
print("=== Non-streaming Response Example ===")
|
||||
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureResponsesClient(),
|
||||
agent = AzureResponsesClient(ad_credential=DefaultAzureCredential()).create_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
@@ -37,8 +36,7 @@ async def streaming_example() -> None:
|
||||
"""Example of streaming response (get results as they are generated)."""
|
||||
print("=== Streaming Response Example ===")
|
||||
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureResponsesClient(),
|
||||
agent = AzureResponsesClient(ad_credential=DefaultAzureCredential()).create_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
|
||||
+2
-1
@@ -4,6 +4,7 @@ import asyncio
|
||||
|
||||
from agent_framework import ChatClientAgent, ChatResponse, HostedCodeInterpreterTool
|
||||
from agent_framework.azure import AzureResponsesClient
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from openai.types.responses.response import Response as OpenAIResponse
|
||||
from openai.types.responses.response_code_interpreter_tool_call import ResponseCodeInterpreterToolCall
|
||||
|
||||
@@ -13,7 +14,7 @@ async def main() -> None:
|
||||
print("=== Azure OpenAI Responses Agent with Code Interpreter Example ===")
|
||||
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureResponsesClient(),
|
||||
chat_client=AzureResponsesClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful assistant that can write and execute Python code to solve problems.",
|
||||
tools=HostedCodeInterpreterTool(),
|
||||
)
|
||||
|
||||
+4
-3
@@ -7,6 +7,7 @@ from typing import Annotated
|
||||
|
||||
from agent_framework import ChatClientAgent
|
||||
from agent_framework.azure import AzureResponsesClient
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -31,7 +32,7 @@ async def tools_on_agent_level() -> None:
|
||||
# Tools are provided when creating the agent
|
||||
# The agent can use these tools for any query during its lifetime
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureResponsesClient(),
|
||||
chat_client=AzureResponsesClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful assistant that can provide weather and time information.",
|
||||
tools=[get_weather, get_time], # Tools defined at agent creation
|
||||
)
|
||||
@@ -61,7 +62,7 @@ async def tools_on_run_level() -> None:
|
||||
|
||||
# Agent created without tools
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureResponsesClient(),
|
||||
chat_client=AzureResponsesClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful assistant.",
|
||||
# No tools defined here
|
||||
)
|
||||
@@ -91,7 +92,7 @@ async def mixed_tools_example() -> None:
|
||||
|
||||
# Agent created with some base tools
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureResponsesClient(),
|
||||
chat_client=AzureResponsesClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a comprehensive assistant that can help with various information requests.",
|
||||
tools=[get_weather], # Base tool available for all queries
|
||||
)
|
||||
|
||||
+5
-4
@@ -6,6 +6,7 @@ from typing import Annotated
|
||||
|
||||
from agent_framework import ChatClientAgent, ChatClientAgentThread
|
||||
from agent_framework.azure import AzureResponsesClient
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -22,7 +23,7 @@ async def example_with_automatic_thread_creation() -> None:
|
||||
print("=== Automatic Thread Creation Example ===")
|
||||
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureResponsesClient(),
|
||||
chat_client=AzureResponsesClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
@@ -49,7 +50,7 @@ async def example_with_thread_persistence_in_memory() -> None:
|
||||
print("=== Thread Persistence Example (In-Memory) ===")
|
||||
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureResponsesClient(),
|
||||
chat_client=AzureResponsesClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
@@ -92,7 +93,7 @@ async def example_with_existing_thread_id() -> None:
|
||||
existing_thread_id = None
|
||||
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureResponsesClient(),
|
||||
chat_client=AzureResponsesClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
@@ -116,7 +117,7 @@ async def example_with_existing_thread_id() -> None:
|
||||
print("\n--- Continuing with the same thread ID in a new agent instance ---")
|
||||
|
||||
agent = ChatClientAgent(
|
||||
chat_client=AzureResponsesClient(),
|
||||
chat_client=AzureResponsesClient(ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
)
|
||||
|
||||
@@ -5,6 +5,7 @@ from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework.foundry import FoundryChatClient
|
||||
from azure.identity.aio import DefaultAzureCredential
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -22,7 +23,7 @@ async def non_streaming_example() -> None:
|
||||
|
||||
# Since no Agent ID is provided, the agent will be automatically created
|
||||
# and deleted after getting a response
|
||||
async with FoundryChatClient().create_agent(
|
||||
async with FoundryChatClient(async_ad_credential=DefaultAzureCredential()).create_agent(
|
||||
name="WeatherAgent",
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
@@ -39,7 +40,7 @@ async def streaming_example() -> None:
|
||||
|
||||
# Since no Agent ID is provided, the agent will be automatically created
|
||||
# and deleted after getting a response
|
||||
async with FoundryChatClient().create_agent(
|
||||
async with FoundryChatClient(async_ad_credential=DefaultAzureCredential()).create_agent(
|
||||
name="WeatherAgent",
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
|
||||
@@ -11,6 +11,7 @@ from azure.ai.agents.models import (
|
||||
RunStepDeltaCodeInterpreterToolCall,
|
||||
RunStepDeltaToolCallObject,
|
||||
)
|
||||
from azure.identity.aio import DefaultAzureCredential
|
||||
|
||||
|
||||
def get_code_interpreter_chunk(chunk: AgentRunResponseUpdate) -> str | None:
|
||||
@@ -37,7 +38,7 @@ async def main() -> None:
|
||||
print("=== Foundry Agent with Code Interpreter Example ===")
|
||||
|
||||
async with ChatClientAgent(
|
||||
chat_client=FoundryChatClient(),
|
||||
chat_client=FoundryChatClient(async_ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful assistant that can write and execute Python code to solve problems.",
|
||||
tools=HostedCodeInterpreterTool(),
|
||||
) as agent:
|
||||
|
||||
@@ -28,7 +28,7 @@ async def main() -> None:
|
||||
chat_client=FoundryChatClient(
|
||||
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
|
||||
model_deployment_name=os.environ["FOUNDRY_MODEL_DEPLOYMENT_NAME"],
|
||||
credential=AzureCliCredential(),
|
||||
async_ad_credential=AzureCliCredential(),
|
||||
agent_name="WeatherAgent",
|
||||
),
|
||||
instructions="You are a helpful weather agent.",
|
||||
|
||||
@@ -7,6 +7,7 @@ from typing import Annotated
|
||||
|
||||
from agent_framework import ChatClientAgent
|
||||
from agent_framework.foundry import FoundryChatClient
|
||||
from azure.identity.aio import DefaultAzureCredential
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -31,7 +32,7 @@ async def tools_on_agent_level() -> None:
|
||||
# Tools are provided when creating the agent
|
||||
# The agent can use these tools for any query during its lifetime
|
||||
async with ChatClientAgent(
|
||||
chat_client=FoundryChatClient(),
|
||||
chat_client=FoundryChatClient(async_ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful assistant that can provide weather and time information.",
|
||||
tools=[get_weather, get_time], # Tools defined at agent creation
|
||||
) as agent:
|
||||
@@ -60,7 +61,7 @@ async def tools_on_run_level() -> None:
|
||||
|
||||
# Agent created without tools
|
||||
async with ChatClientAgent(
|
||||
chat_client=FoundryChatClient(),
|
||||
chat_client=FoundryChatClient(async_ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful assistant.",
|
||||
# No tools defined here
|
||||
) as agent:
|
||||
@@ -89,7 +90,7 @@ async def mixed_tools_example() -> None:
|
||||
|
||||
# Agent created with some base tools
|
||||
async with ChatClientAgent(
|
||||
chat_client=FoundryChatClient(),
|
||||
chat_client=FoundryChatClient(async_ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a comprehensive assistant that can help with various information requests.",
|
||||
tools=[get_weather], # Base tool available for all queries
|
||||
) as agent:
|
||||
|
||||
@@ -6,6 +6,7 @@ from typing import Annotated
|
||||
|
||||
from agent_framework import ChatClientAgent, ChatClientAgentThread
|
||||
from agent_framework.foundry import FoundryChatClient
|
||||
from azure.identity.aio import DefaultAzureCredential
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -22,7 +23,7 @@ async def example_with_automatic_thread_creation() -> None:
|
||||
print("=== Automatic Thread Creation Example ===")
|
||||
|
||||
async with ChatClientAgent(
|
||||
chat_client=FoundryChatClient(),
|
||||
chat_client=FoundryChatClient(async_ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
) as agent:
|
||||
@@ -46,7 +47,7 @@ async def example_with_thread_persistence() -> None:
|
||||
print("Using the same thread across multiple conversations to maintain context.\n")
|
||||
|
||||
async with ChatClientAgent(
|
||||
chat_client=FoundryChatClient(),
|
||||
chat_client=FoundryChatClient(async_ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
) as agent:
|
||||
@@ -82,7 +83,7 @@ async def example_with_existing_thread_id() -> None:
|
||||
existing_thread_id = None
|
||||
|
||||
async with ChatClientAgent(
|
||||
chat_client=FoundryChatClient(),
|
||||
chat_client=FoundryChatClient(async_ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
) as agent:
|
||||
@@ -102,7 +103,7 @@ async def example_with_existing_thread_id() -> None:
|
||||
|
||||
# Create a new agent instance but use the existing thread ID
|
||||
async with ChatClientAgent(
|
||||
chat_client=FoundryChatClient(thread_id=existing_thread_id),
|
||||
chat_client=FoundryChatClient(thread_id=existing_thread_id, async_ad_credential=DefaultAzureCredential()),
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
) as agent:
|
||||
|
||||
+2
-5
@@ -4,7 +4,6 @@ import asyncio
|
||||
from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework import ChatClientAgent
|
||||
from agent_framework.openai import OpenAIAssistantsClient
|
||||
from pydantic import Field
|
||||
|
||||
@@ -23,8 +22,7 @@ async def non_streaming_example() -> None:
|
||||
|
||||
# Since no assistant ID is provided, the assistant will be automatically created
|
||||
# and deleted after getting a response
|
||||
async with ChatClientAgent(
|
||||
chat_client=OpenAIAssistantsClient(),
|
||||
async with OpenAIAssistantsClient().create_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
) as agent:
|
||||
@@ -40,8 +38,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 ChatClientAgent(
|
||||
chat_client=OpenAIAssistantsClient(),
|
||||
async with OpenAIAssistantsClient().create_agent(
|
||||
instructions="You are a helpful weather agent.",
|
||||
tools=get_weather,
|
||||
) as agent:
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework import HostedCodeInterpreterTool, TextContent, TextReasoningContent, UsageContent
|
||||
from agent_framework.openai import OpenAIResponsesClient
|
||||
|
||||
|
||||
async def reasoning_example() -> None:
|
||||
"""Example of reasoning response (get results as they are generated)."""
|
||||
print("=== Reasoning Example ===")
|
||||
|
||||
agent = OpenAIResponsesClient(ai_model_id="o4-mini").create_agent(
|
||||
name="MathHelper",
|
||||
instructions="You are a personal math tutor. When asked a math question, "
|
||||
"write and run code using the python tool to answer the question.",
|
||||
tools=HostedCodeInterpreterTool(),
|
||||
reasoning={"effort": "medium"},
|
||||
)
|
||||
|
||||
query = "I need to solve the equation 3x + 11 = 14. Can you help me?"
|
||||
print(f"User: {query}")
|
||||
print(f"{agent.name}: ", end="", flush=True)
|
||||
usage = None
|
||||
async for chunk in agent.run_streaming(query):
|
||||
if chunk.contents:
|
||||
for content in chunk.contents:
|
||||
if isinstance(content, TextReasoningContent):
|
||||
print(f"\033[97m{content.text}\033[0m", end="", flush=True)
|
||||
if isinstance(content, TextContent):
|
||||
print(content.text, end="", flush=True)
|
||||
if isinstance(content, UsageContent):
|
||||
usage = content
|
||||
print("\n")
|
||||
if usage:
|
||||
print(f"Usage: {usage.details}")
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
print("=== Basic OpenAI Responses Reasoning Agent Example ===")
|
||||
|
||||
await reasoning_example()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -5,6 +5,7 @@ from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework.azure import AzureAssistantsClient
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -17,7 +18,7 @@ def get_weather(
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
async with AzureAssistantsClient() as client:
|
||||
async with AzureAssistantsClient(ad_credential=DefaultAzureCredential()) as client:
|
||||
message = "What's the weather in Amsterdam and in Paris?"
|
||||
stream = False
|
||||
print(f"User: {message}")
|
||||
|
||||
@@ -5,6 +5,7 @@ from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework.azure import AzureChatClient
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -17,7 +18,7 @@ def get_weather(
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
client = AzureChatClient()
|
||||
client = AzureChatClient(ad_credential=DefaultAzureCredential())
|
||||
message = "What's the weather in Amsterdam and in Paris?"
|
||||
stream = False
|
||||
print(f"User: {message}")
|
||||
|
||||
@@ -5,6 +5,7 @@ from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework.azure import AzureResponsesClient
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -17,7 +18,7 @@ def get_weather(
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
client = AzureResponsesClient()
|
||||
client = AzureResponsesClient(ad_credential=DefaultAzureCredential())
|
||||
message = "What's the weather in Amsterdam and in Paris?"
|
||||
stream = False
|
||||
print(f"User: {message}")
|
||||
|
||||
@@ -5,6 +5,7 @@ from random import randint
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework.foundry import FoundryChatClient
|
||||
from azure.identity.aio import DefaultAzureCredential
|
||||
from pydantic import Field
|
||||
|
||||
|
||||
@@ -17,7 +18,7 @@ def get_weather(
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
async with FoundryChatClient() as client:
|
||||
async with FoundryChatClient(async_ad_credential=DefaultAzureCredential()) as client:
|
||||
message = "What's the weather in Amsterdam and in Paris?"
|
||||
stream = False
|
||||
print(f"User: {message}")
|
||||
|
||||
@@ -19,13 +19,13 @@ def get_weather(
|
||||
async def main() -> None:
|
||||
client = OpenAIChatClient()
|
||||
message = "What's the weather in Amsterdam and in Paris?"
|
||||
stream = False
|
||||
stream = True
|
||||
print(f"User: {message}")
|
||||
if stream:
|
||||
print("Assistant: ", end="")
|
||||
async for chunk in client.get_streaming_response(message, tools=get_weather):
|
||||
if str(chunk):
|
||||
print(str(chunk), end="")
|
||||
if chunk.text:
|
||||
print(chunk.text, end="")
|
||||
print("")
|
||||
else:
|
||||
response = await client.get_response(message, tools=get_weather)
|
||||
|
||||
@@ -17,15 +17,15 @@ def get_weather(
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
client = OpenAIResponsesClient(ai_model_id="gpt-4o-mini")
|
||||
client = OpenAIResponsesClient()
|
||||
message = "What's the weather in Amsterdam and in Paris?"
|
||||
stream = False
|
||||
print(f"User: {message}")
|
||||
if stream:
|
||||
print("Assistant: ", end="")
|
||||
async for chunk in client.get_streaming_response(message, tools=get_weather):
|
||||
if str(chunk):
|
||||
print(str(chunk), end="")
|
||||
if chunk.text:
|
||||
print(chunk.text, end="")
|
||||
print("")
|
||||
else:
|
||||
response = await client.get_response(message, tools=get_weather)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework import ChatClientAgent, ChatMessage, ChatRole
|
||||
from agent_framework import ChatMessage, ChatRole
|
||||
from agent_framework.azure import AzureChatClient
|
||||
from agent_framework.workflow import (
|
||||
AgentExecutor,
|
||||
@@ -15,6 +15,7 @@ from agent_framework.workflow import (
|
||||
WorkflowContext,
|
||||
handler,
|
||||
)
|
||||
from azure.identity import DefaultAzureCredential
|
||||
|
||||
"""
|
||||
The following sample demonstrates a basic workflow that simulates
|
||||
@@ -90,10 +91,9 @@ async def main():
|
||||
"""Main function to run the group chat workflow."""
|
||||
|
||||
# Step 1: Create the executors.
|
||||
chat_client = AzureChatClient()
|
||||
chat_client = AzureChatClient(ad_credential=DefaultAzureCredential())
|
||||
writer = AgentExecutor(
|
||||
ChatClientAgent(
|
||||
chat_client,
|
||||
chat_client.create_agent(
|
||||
instructions=(
|
||||
"You are an excellent content writer. You create new content and edit contents based on the feedback."
|
||||
),
|
||||
@@ -101,8 +101,7 @@ async def main():
|
||||
id="writer",
|
||||
)
|
||||
reviewer = AgentExecutor(
|
||||
ChatClientAgent(
|
||||
chat_client,
|
||||
chat_client.create_agent(
|
||||
instructions=(
|
||||
"You are an excellent content reviewer. You review the content and provide feedback to the writer."
|
||||
),
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import asyncio
|
||||
|
||||
from agent_framework import ChatClientAgent, ChatMessage, ChatRole
|
||||
from agent_framework import ChatMessage, ChatRole
|
||||
from agent_framework.azure import AzureChatClient
|
||||
from agent_framework.workflow import (
|
||||
AgentExecutor,
|
||||
@@ -17,6 +17,7 @@ from agent_framework.workflow import (
|
||||
WorkflowContext,
|
||||
handler,
|
||||
)
|
||||
from azure.identity import DefaultAzureCredential
|
||||
|
||||
"""
|
||||
The following sample demonstrates a basic workflow that simulates
|
||||
@@ -135,9 +136,9 @@ class CriticGroupChatManager(Executor):
|
||||
async def main():
|
||||
"""Main function to run the group chat workflow."""
|
||||
# Step 1: Create the executors.
|
||||
chat_client = AzureChatClient(ad_credential=DefaultAzureCredential())
|
||||
writer = AgentExecutor(
|
||||
ChatClientAgent(
|
||||
AzureChatClient(),
|
||||
chat_client.create_agent(
|
||||
instructions=(
|
||||
"You are an excellent content writer. You create new content and edit contents based on the feedback."
|
||||
),
|
||||
@@ -146,8 +147,7 @@ async def main():
|
||||
),
|
||||
)
|
||||
reviewer = AgentExecutor(
|
||||
ChatClientAgent(
|
||||
AzureChatClient(),
|
||||
chat_client.create_agent(
|
||||
instructions=(
|
||||
"You are an excellent content reviewer. You review the content and provide feedback to the writer. "
|
||||
"You do not address user requests. Only provide feedback to the writer."
|
||||
|
||||
@@ -7,7 +7,13 @@ from collections import defaultdict
|
||||
from dataclasses import dataclass
|
||||
|
||||
import aiofiles
|
||||
from agent_framework.workflow import Executor, WorkflowBuilder, WorkflowCompletedEvent, WorkflowContext, handler
|
||||
from agent_framework.workflow import (
|
||||
Executor,
|
||||
WorkflowBuilder,
|
||||
WorkflowCompletedEvent,
|
||||
WorkflowContext,
|
||||
handler,
|
||||
)
|
||||
|
||||
"""
|
||||
The following sample demonstrates a basic map reduce workflow that
|
||||
|
||||
Reference in New Issue
Block a user