mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
b05fc9e849
* Initial plan * Add load_dotenv() to 303 Python samples for environment variable loading Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Update SAMPLE_GUIDELINES.md to document load_dotenv() requirement Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Update samples README.md to document .env file usage Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Run ruff format on all changed sample files Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Clarify load_dotenv() usage in README - local dev vs production Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Remove deprecated getting_started folder as requested Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Document env_file_path parameter for per-client configuration Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Merge main branch to resolve conflicts Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Fix run_evaluation.py file that was empty in merge commit Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Remove dotnet changes from merge - out of scope for this PR Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Remove package and test changes from merge - only sample changes needed Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Remove test_func_utils.py - only sample changes needed Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Revert sample files not in original changeset - keep only load_dotenv additions Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Move load_dotenv() outside snippet tag in 06_host_your_agent.py Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Fix comment placement - move load_dotenv before code comments Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Fix load_dotenv() placement across all samples - after docstring, before code comments Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Merge latest main branch with load_dotenv changes Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Remove non-sample changes from merge - keep only load_dotenv additions Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Revert non-load_dotenv sample changes from merge Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Fix run_evaluation.py - use main's improved version (file already had load_dotenv) Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> * Manual update * Manual update 2 * Fix Role usage and load_dotenv placement per PR review feedback Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> * Fix Role usage - use string literals not enum attributes Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> * Fix SAMPLE_GUIDELINES.md example - load_dotenv before docstring per guidance Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> * Move load_dotenv() before docstrings in all samples per SAMPLE_GUIDELINES ordering Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> * Address PR review: rename files, fix placement, add session usage, remove note Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> * Update Redis README to reference renamed file redis_history_provider.py Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: TaoChenOSU <12570346+TaoChenOSU@users.noreply.github.com> Co-authored-by: Tao Chen <taochen@microsoft.com> Co-authored-by: eavanvalkenburg <13749212+eavanvalkenburg@users.noreply.github.com> Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com>
121 lines
3.8 KiB
Python
121 lines
3.8 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
"""Client application for interacting with multiple hosted agents.
|
|
|
|
This client connects to the Durable Task Scheduler and interacts with two different
|
|
agents (WeatherAgent and MathAgent), demonstrating how to work with multiple agents
|
|
each with their own specialized capabilities and tools.
|
|
|
|
Prerequisites:
|
|
- The worker must be running with both agents registered
|
|
- Set AZURE_OPENAI_ENDPOINT and AZURE_OPENAI_CHAT_DEPLOYMENT_NAME
|
|
(plus AZURE_OPENAI_API_KEY or Azure CLI authentication)
|
|
- Durable Task Scheduler must be running
|
|
"""
|
|
|
|
import asyncio
|
|
import logging
|
|
import os
|
|
|
|
from agent_framework.azure import DurableAIAgentClient
|
|
from azure.identity import DefaultAzureCredential
|
|
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 DefaultAzureCredential()
|
|
|
|
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 both WeatherAgent and MathAgent.
|
|
|
|
Args:
|
|
agent_client: The DurableAIAgentClient instance
|
|
"""
|
|
logger.debug("Testing WeatherAgent")
|
|
|
|
# Get reference to WeatherAgent
|
|
weather_agent = agent_client.get_agent("WeatherAgent")
|
|
weather_session = weather_agent.create_session()
|
|
|
|
logger.debug(f"Created weather conversation session: {weather_session.session_id}")
|
|
|
|
# Test WeatherAgent
|
|
weather_message = "What is the weather in Seattle?"
|
|
logger.info(f"User: {weather_message}")
|
|
|
|
weather_response = weather_agent.run(weather_message, session=weather_session)
|
|
logger.info(f"WeatherAgent: {weather_response.text} \n")
|
|
|
|
logger.debug("Testing MathAgent")
|
|
|
|
# Get reference to MathAgent
|
|
math_agent = agent_client.get_agent("MathAgent")
|
|
math_session = math_agent.create_session()
|
|
|
|
logger.debug(f"Created math conversation session: {math_session.session_id}")
|
|
|
|
# Test MathAgent
|
|
math_message = "Calculate a 20% tip on a $50 bill"
|
|
logger.info(f"User: {math_message}")
|
|
|
|
math_response = math_agent.run(math_message, session=math_session)
|
|
logger.info(f"MathAgent: {math_response.text} \n")
|
|
|
|
logger.debug("Both agents completed successfully!")
|
|
|
|
|
|
async def main() -> None:
|
|
"""Main entry point for the client application."""
|
|
logger.debug("Starting Durable Task Multi-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())
|