mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: [BREAKING] Python: Provider-leading client design & OpenAI package extraction (#4818)
* Python: Provider-leading client design & OpenAI package extraction Major refactoring of the Python Agent Framework client architecture: - Extract OpenAI clients into new `agent-framework-openai` package - Core package no longer depends on openai, azure-identity, azure-ai-projects - Rename clients for discoverability: OpenAIResponsesClient → OpenAIChatClient, OpenAIChatClient → OpenAIChatCompletionClient - Unify `model_id`/`deployment_name`/`model_deployment_name` → `model` param - New FoundryChatClient for Azure AI Foundry Responses API - New FoundryAgent/FoundryAgentClient for connecting to pre-configured Foundry agents - Remove OpenAIBase/OpenAIConfigMixin from non-deprecated client MRO - Deprecate AzureOpenAI* clients, AzureAIClient, OpenAIAssistantsClient - Reorganize samples: azure_openai+azure_ai+azure_ai_agent → azure/ - ADR-0020: Provider-Leading Client Design Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix: missing Agent imports in samples, .model_id → .model in foundry_local sample Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix: CI failures — mypy errors, coverage targets, sample imports - azure-ai mypy: add type ignores for TypedDict total=, model arg, forward ref - Coverage: replace core.azure/openai targets with openai package target - project_provider: add type annotation for opts dict Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix: populate openai .pyi stub, fix broken README links, coverage targets Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fixes * updated observabilitty * reset azure init.pyi * fix errors * updated adr number * fix foundry local * fixed not renamed docstrings and comments, and added deprecated markers to old classes * fix tests and pyprojects * fix test vars * updated function tests * update durable * updated test setup for functions * Fix Foundry auth in workflow samples Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Stabilize Python integration workflows Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Update hosting samples for Foundry Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Trigger full CI rerun Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Trigger CI rerun again Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * trigger rerun * trigger rerun * fix for litellm * undo durabletask changes * Move Foundry APIs into foundry namespace Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix Foundry pyproject formatting Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Split provider samples by Foundry surface Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Restore hosting sample requirements Also fix the Foundry Local sample link after the provider sample move. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * updated tests * udpated foundry integration tests * removed dist from azurefunctions tests * Use separate Foundry clients for concurrent agents Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix client setup in azfunc and durable * disabled two tests * updated setup for some function and durable tests * improved azure openai setup with new clients * ignore deprecated * fixes * skip 11 * remove openai assistants int tests --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot
parent
4b533608b6
commit
5e056b672e
@@ -1,8 +1,9 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
from agent_framework.azure import AzureOpenAIChatClient
|
||||
from agent_framework import Agent
|
||||
from agent_framework.foundry import FoundryChatClient
|
||||
from azure.ai.agentserver.agentframework import from_agent_framework # pyright: ignore[reportUnknownVariableType]
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from azure.identity import AzureCliCredential
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment variables from .env file
|
||||
@@ -16,14 +17,13 @@ def main():
|
||||
"server_label": "Microsoft_Learn_MCP",
|
||||
"server_url": "https://learn.microsoft.com/api/mcp",
|
||||
}
|
||||
|
||||
# Create an Agent using the Azure OpenAI Chat Client with a MCP Tool that connects to Microsoft Learn MCP
|
||||
agent = AzureOpenAIChatClient(credential=DefaultAzureCredential()).as_agent(
|
||||
agent = Agent(
|
||||
client=FoundryChatClient(credential=AzureCliCredential()),
|
||||
name="DocsAgent",
|
||||
instructions="You are a helpful assistant that can help with microsoft documentation questions.",
|
||||
tools=mcp_tool,
|
||||
)
|
||||
|
||||
# Run the agent as a hosted agent
|
||||
from_agent_framework(agent).run()
|
||||
|
||||
|
||||
@@ -11,18 +11,14 @@ import os
|
||||
from datetime import datetime
|
||||
from typing import Annotated
|
||||
|
||||
from agent_framework.azure import AzureOpenAIResponsesClient
|
||||
from agent_framework import Agent
|
||||
from agent_framework.foundry import FoundryChatClient
|
||||
from azure.ai.agentserver.agentframework import from_agent_framework
|
||||
from azure.identity.aio import AzureCliCredential, ManagedIdentityCredential
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv(override=True)
|
||||
|
||||
# Configure these for your Foundry project
|
||||
# Read the explicit variables present in the .env file
|
||||
PROJECT_ENDPOINT = os.getenv(
|
||||
"PROJECT_ENDPOINT"
|
||||
) # e.g., "https://<project>.services.ai.azure.com"
|
||||
PROJECT_ENDPOINT = os.getenv("PROJECT_ENDPOINT") # e.g., "https://<project>.services.ai.azure.com"
|
||||
MODEL_DEPLOYMENT_NAME = os.getenv(
|
||||
"MODEL_DEPLOYMENT_NAME", "gpt-4.1-mini"
|
||||
) # Your model deployment name e.g., "gpt-4.1-mini"
|
||||
@@ -90,14 +86,10 @@ def get_available_hotels(
|
||||
nights = (check_out - check_in).days
|
||||
|
||||
# Filter hotels by price
|
||||
available_hotels = [
|
||||
hotel for hotel in SEATTLE_HOTELS if hotel["price_per_night"] <= max_price
|
||||
]
|
||||
available_hotels = [hotel for hotel in SEATTLE_HOTELS if hotel["price_per_night"] <= max_price]
|
||||
|
||||
if not available_hotels:
|
||||
return (
|
||||
f"No hotels found in Seattle within your budget of ${max_price}/night."
|
||||
)
|
||||
return f"No hotels found in Seattle within your budget of ${max_price}/night."
|
||||
|
||||
# Build response
|
||||
result = f"Available hotels in Seattle from {check_in_date} to {check_out_date} ({nights} nights):\n\n"
|
||||
@@ -117,22 +109,19 @@ def get_available_hotels(
|
||||
|
||||
def get_credential():
|
||||
"""Will use Managed Identity when running in Azure, otherwise falls back to Azure CLI Credential."""
|
||||
return (
|
||||
ManagedIdentityCredential()
|
||||
if os.getenv("MSI_ENDPOINT")
|
||||
else AzureCliCredential()
|
||||
)
|
||||
return ManagedIdentityCredential() if os.getenv("MSI_ENDPOINT") else AzureCliCredential()
|
||||
|
||||
|
||||
async def main():
|
||||
"""Main function to run the agent as a web server."""
|
||||
async with get_credential() as credential:
|
||||
client = AzureOpenAIResponsesClient(
|
||||
client = FoundryChatClient(
|
||||
project_endpoint=PROJECT_ENDPOINT,
|
||||
deployment_name=MODEL_DEPLOYMENT_NAME,
|
||||
model=MODEL_DEPLOYMENT_NAME,
|
||||
credential=credential,
|
||||
)
|
||||
agent = client.as_agent(
|
||||
agent = Agent(
|
||||
client=client,
|
||||
name="SeattleHotelAgent",
|
||||
instructions="""You are a helpful travel assistant specializing in finding hotels in Seattle, Washington.
|
||||
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
azure-ai-agentserver-agentframework==1.0.0b16
|
||||
agent-framework-azure-ai
|
||||
agent-framework-foundry
|
||||
@@ -5,8 +5,8 @@ import sys
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from agent_framework import AgentSession, BaseContextProvider, Message, SessionContext
|
||||
from agent_framework.azure import AzureOpenAIChatClient
|
||||
from agent_framework import Agent, AgentSession, BaseContextProvider, Message, SessionContext
|
||||
from agent_framework.foundry import FoundryChatClient
|
||||
from azure.ai.agentserver.agentframework import from_agent_framework # pyright: ignore[reportUnknownVariableType]
|
||||
from azure.identity import DefaultAzureCredential
|
||||
from dotenv import load_dotenv
|
||||
@@ -105,7 +105,8 @@ class TextSearchContextProvider(BaseContextProvider):
|
||||
|
||||
def main():
|
||||
# Create an Agent using the Azure OpenAI Chat Client
|
||||
agent = AzureOpenAIChatClient(credential=DefaultAzureCredential()).as_agent(
|
||||
agent = Agent(
|
||||
client=FoundryChatClient(credential=DefaultAzureCredential()),
|
||||
name="SupportSpecialist",
|
||||
instructions=(
|
||||
"You are a helpful support specialist for Contoso Outdoors. "
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
from agent_framework.azure import AzureOpenAIChatClient
|
||||
from agent_framework import Agent
|
||||
from agent_framework.foundry import FoundryChatClient
|
||||
from agent_framework_orchestrations import ConcurrentBuilder
|
||||
from azure.ai.agentserver.agentframework import from_agent_framework
|
||||
from azure.identity import DefaultAzureCredential # pyright: ignore[reportUnknownVariableType]
|
||||
@@ -12,21 +13,24 @@ load_dotenv()
|
||||
|
||||
def main():
|
||||
# Create agents
|
||||
researcher = AzureOpenAIChatClient(credential=DefaultAzureCredential()).as_agent(
|
||||
researcher = Agent(
|
||||
client=FoundryChatClient(credential=DefaultAzureCredential()),
|
||||
instructions=(
|
||||
"You're an expert market and product researcher. "
|
||||
"Given a prompt, provide concise, factual insights, opportunities, and risks."
|
||||
),
|
||||
name="researcher",
|
||||
)
|
||||
marketer = AzureOpenAIChatClient(credential=DefaultAzureCredential()).as_agent(
|
||||
marketer = Agent(
|
||||
client=FoundryChatClient(credential=DefaultAzureCredential()),
|
||||
instructions=(
|
||||
"You're a creative marketing strategist. "
|
||||
"Craft compelling value propositions and target messaging aligned to the prompt."
|
||||
),
|
||||
name="marketer",
|
||||
)
|
||||
legal = AzureOpenAIChatClient(credential=DefaultAzureCredential()).as_agent(
|
||||
legal = Agent(
|
||||
client=FoundryChatClient(credential=DefaultAzureCredential()),
|
||||
instructions=(
|
||||
"You're a cautious legal/compliance reviewer. "
|
||||
"Highlight constraints, disclaimers, and policy concerns based on the prompt."
|
||||
@@ -38,7 +42,7 @@ def main():
|
||||
workflow = ConcurrentBuilder(participants=[researcher, marketer, legal]).build()
|
||||
|
||||
# Convert the workflow to an agent
|
||||
workflow_agent = workflow.as_agent()
|
||||
workflow_agent = Agent(client=workflow)
|
||||
|
||||
# Run the agent as a hosted agent
|
||||
from_agent_framework(workflow_agent).run()
|
||||
|
||||
+12
-12
@@ -4,8 +4,8 @@ import asyncio
|
||||
import os
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from agent_framework import WorkflowBuilder
|
||||
from agent_framework.azure import AzureOpenAIResponsesClient
|
||||
from agent_framework import Agent, WorkflowBuilder
|
||||
from agent_framework.foundry import FoundryChatClient
|
||||
from azure.ai.agentserver.agentframework import from_agent_framework
|
||||
from azure.identity.aio import AzureCliCredential, ManagedIdentityCredential
|
||||
from dotenv import load_dotenv
|
||||
@@ -24,26 +24,24 @@ MODEL_DEPLOYMENT_NAME = os.getenv(
|
||||
|
||||
def get_credential():
|
||||
"""Will use Managed Identity when running in Azure, otherwise falls back to Azure CLI Credential."""
|
||||
return (
|
||||
ManagedIdentityCredential()
|
||||
if os.getenv("MSI_ENDPOINT")
|
||||
else AzureCliCredential()
|
||||
)
|
||||
return ManagedIdentityCredential() if os.getenv("MSI_ENDPOINT") else AzureCliCredential()
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def create_agents():
|
||||
async with get_credential() as credential:
|
||||
client = AzureOpenAIResponsesClient(
|
||||
client = FoundryChatClient(
|
||||
project_endpoint=PROJECT_ENDPOINT,
|
||||
deployment_name=MODEL_DEPLOYMENT_NAME,
|
||||
model=MODEL_DEPLOYMENT_NAME,
|
||||
credential=credential,
|
||||
)
|
||||
writer = client.as_agent(
|
||||
writer = Agent(
|
||||
client=client,
|
||||
name="Writer",
|
||||
instructions="You are an excellent content writer. You create new content and edit contents based on the feedback.",
|
||||
)
|
||||
reviewer = client.as_agent(
|
||||
reviewer = Agent(
|
||||
client=client,
|
||||
name="Reviewer",
|
||||
instructions="You are an excellent content reviewer. Provide actionable feedback to the writer about the provided content in the most concise manner possible.",
|
||||
)
|
||||
@@ -52,7 +50,9 @@ async def create_agents():
|
||||
|
||||
def create_workflow(writer, reviewer):
|
||||
workflow = WorkflowBuilder(start_executor=writer).add_edge(writer, reviewer).build()
|
||||
return workflow.as_agent()
|
||||
return Agent(
|
||||
client=workflow,
|
||||
)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
azure-ai-agentserver-agentframework==1.0.0b16
|
||||
agent-framework-azure-ai
|
||||
agent-framework-foundry
|
||||
|
||||
Reference in New Issue
Block a user