mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
5e056b672e
* 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>
124 lines
3.7 KiB
Python
124 lines
3.7 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
"""Client application for interacting with a Durable Task hosted agent.
|
|
|
|
This client connects to the Durable Task Scheduler and sends requests to
|
|
registered agents, demonstrating how to interact with agents from external processes.
|
|
|
|
Prerequisites:
|
|
- The worker must be running with the agent registered
|
|
- Set FOUNDRY_PROJECT_ENDPOINT and FOUNDRY_MODEL
|
|
- Sign in with Azure CLI for AzureCliCredential authentication
|
|
- Durable Task Scheduler must be running
|
|
"""
|
|
|
|
import asyncio
|
|
import logging
|
|
import os
|
|
|
|
from agent_framework.azure import DurableAIAgentClient
|
|
from azure.identity import AzureCliCredential
|
|
from dotenv import load_dotenv
|
|
from durabletask.azuremanaged.client import DurableTaskSchedulerClient
|
|
|
|
# Load environment variables from .env file
|
|
load_dotenv()
|
|
|
|
# Configure logging
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def get_client(
|
|
taskhub: str | None = None, endpoint: str | None = None, log_handler: logging.Handler | None = None
|
|
) -> DurableAIAgentClient:
|
|
"""Create a configured DurableAIAgentClient.
|
|
|
|
Args:
|
|
taskhub: Task hub name (defaults to TASKHUB env var or "default")
|
|
endpoint: Scheduler endpoint (defaults to ENDPOINT env var or "http://localhost:8080")
|
|
log_handler: Optional logging handler for client logging
|
|
|
|
Returns:
|
|
Configured DurableAIAgentClient instance
|
|
"""
|
|
taskhub_name = taskhub or os.getenv("TASKHUB", "default")
|
|
endpoint_url = endpoint or os.getenv("ENDPOINT", "http://localhost:8080")
|
|
|
|
logger.debug(f"Using taskhub: {taskhub_name}")
|
|
logger.debug(f"Using endpoint: {endpoint_url}")
|
|
|
|
credential = None if endpoint_url == "http://localhost:8080" else AzureCliCredential()
|
|
|
|
dts_client = DurableTaskSchedulerClient(
|
|
host_address=endpoint_url,
|
|
secure_channel=endpoint_url != "http://localhost:8080",
|
|
taskhub=taskhub_name,
|
|
token_credential=credential,
|
|
log_handler=log_handler,
|
|
)
|
|
|
|
return DurableAIAgentClient(dts_client)
|
|
|
|
|
|
def run_client(agent_client: DurableAIAgentClient) -> None:
|
|
"""Run client interactions with the Joker agent.
|
|
|
|
Args:
|
|
agent_client: The DurableAIAgentClient instance
|
|
"""
|
|
# Get a reference to the Joker agent
|
|
logger.debug("Getting reference to Joker agent...")
|
|
joker = agent_client.get_agent("Joker")
|
|
|
|
# Create a new session for the conversation
|
|
session = joker.create_session()
|
|
logger.debug(f"Session ID: {session.session_id}")
|
|
logger.info("Start chatting with the Joker agent! (Type 'exit' to quit)")
|
|
|
|
# Interactive conversation loop
|
|
while True:
|
|
# Get user input
|
|
try:
|
|
user_message = input("You: ").strip()
|
|
except (EOFError, KeyboardInterrupt):
|
|
logger.info("\nExiting...")
|
|
break
|
|
|
|
# Check for exit command
|
|
if user_message.lower() == "exit":
|
|
logger.info("Goodbye!")
|
|
break
|
|
|
|
# Skip empty messages
|
|
if not user_message:
|
|
continue
|
|
|
|
# Send message to agent and get response
|
|
try:
|
|
response = joker.run(user_message, session=session)
|
|
logger.info(f"Joker: {response.text} \n")
|
|
except Exception as e:
|
|
logger.error(f"Error getting response: {e}")
|
|
|
|
logger.info("Conversation completed.")
|
|
|
|
|
|
async def main() -> None:
|
|
"""Main entry point for the client application."""
|
|
logger.debug("Starting Durable Task Agent Client...")
|
|
|
|
# Create client using helper function
|
|
agent_client = get_client()
|
|
|
|
try:
|
|
run_client(agent_client)
|
|
except Exception as e:
|
|
logger.exception(f"Error during agent interaction: {e}")
|
|
finally:
|
|
logger.debug("Client shutting down")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|