|
| 1 | +""" |
| 2 | +Agent Framework MagenticOne Example - Travel Planning with Multiple Agents |
| 3 | +""" |
| 4 | +import asyncio |
| 5 | +import os |
| 6 | + |
| 7 | +from agent_framework import ( |
| 8 | + ChatAgent, |
| 9 | + MagenticAgentMessageEvent, |
| 10 | + MagenticBuilder, |
| 11 | + MagenticCallbackEvent, |
| 12 | + MagenticCallbackMode, |
| 13 | + MagenticOrchestratorMessageEvent, |
| 14 | + WorkflowOutputEvent, |
| 15 | +) |
| 16 | +from agent_framework.azure import AzureOpenAIChatClient |
| 17 | +from agent_framework.openai import OpenAIChatClient |
| 18 | +from azure.identity import DefaultAzureCredential |
| 19 | +from dotenv import load_dotenv |
| 20 | +from rich.console import Console |
| 21 | +from rich.markdown import Markdown |
| 22 | +from rich.panel import Panel |
| 23 | + |
| 24 | +# Setup the client to use either Azure OpenAI or GitHub Models |
| 25 | +load_dotenv(override=True) |
| 26 | +API_HOST = os.getenv("API_HOST", "github") |
| 27 | + |
| 28 | +if API_HOST == "azure": |
| 29 | + client = AzureOpenAIChatClient( |
| 30 | + credential=DefaultAzureCredential(), |
| 31 | + deployment_name=os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT"), |
| 32 | + endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"), |
| 33 | + api_version=os.environ.get("AZURE_OPENAI_VERSION"), |
| 34 | + ) |
| 35 | +elif API_HOST == "github": |
| 36 | + client = OpenAIChatClient( |
| 37 | + base_url="https://models.github.ai/inference", |
| 38 | + api_key=os.environ["GITHUB_TOKEN"], |
| 39 | + model_id=os.getenv("GITHUB_MODEL", "openai/gpt-4o"), |
| 40 | + ) |
| 41 | +elif API_HOST == "ollama": |
| 42 | + client = OpenAIChatClient( |
| 43 | + base_url=os.environ.get("OLLAMA_ENDPOINT", "http://localhost:11434/v1"), |
| 44 | + api_key="none", |
| 45 | + model_id=os.environ.get("OLLAMA_MODEL", "llama3.1:latest"), |
| 46 | + ) |
| 47 | +else: |
| 48 | + client = OpenAIChatClient(api_key=os.environ.get("OPENAI_API_KEY"), model_id=os.environ.get("OPENAI_MODEL", "gpt-4o")) |
| 49 | + |
| 50 | +# Initialize rich console |
| 51 | +console = Console() |
| 52 | + |
| 53 | +# Create the agents |
| 54 | +local_agent = ChatAgent( |
| 55 | + chat_client=client, |
| 56 | + instructions=("You are a helpful assistant that can suggest authentic and interesting local activities " "or places to visit for a user and can utilize any context information provided."), |
| 57 | + name="local_agent", |
| 58 | + description="A local assistant that can suggest local activities or places to visit.", |
| 59 | +) |
| 60 | + |
| 61 | +language_agent = ChatAgent( |
| 62 | + chat_client=client, |
| 63 | + instructions=( |
| 64 | + "You are a helpful assistant that can review travel plans, providing feedback on important/critical " |
| 65 | + "tips about how best to address language or communication challenges for the given destination. " |
| 66 | + "If the plan already includes language tips, you can mention that the plan is satisfactory, with rationale." |
| 67 | + ), |
| 68 | + name="language_agent", |
| 69 | + description="A helpful assistant that can provide language tips for a given destination.", |
| 70 | +) |
| 71 | + |
| 72 | +travel_summary_agent = ChatAgent( |
| 73 | + chat_client=client, |
| 74 | + instructions=( |
| 75 | + "You are a helpful assistant that can take in all of the suggestions and advice from the other agents " |
| 76 | + "and provide a detailed final travel plan. You must ensure that the final plan is integrated and complete. " |
| 77 | + "YOUR FINAL RESPONSE MUST BE THE COMPLETE PLAN. Provide a comprehensive summary when all perspectives " |
| 78 | + "from other agents have been integrated." |
| 79 | + ), |
| 80 | + name="travel_summary_agent", |
| 81 | + description="A helpful assistant that can summarize the travel plan.", |
| 82 | +) |
| 83 | + |
| 84 | + |
| 85 | +# Event callback for streaming output with rich formatting |
| 86 | +async def on_event(event: MagenticCallbackEvent) -> None: |
| 87 | + if isinstance(event, MagenticOrchestratorMessageEvent): |
| 88 | + emoji = "✅" if event.kind == "task_ledger" else "🦠" |
| 89 | + console.print( |
| 90 | + Panel( |
| 91 | + Markdown(event.message.text), |
| 92 | + title=f"{emoji} orchestrator: {event.kind}", |
| 93 | + border_style="bold green", |
| 94 | + padding=(1, 2), |
| 95 | + ) |
| 96 | + ) |
| 97 | + elif isinstance(event, MagenticAgentMessageEvent): |
| 98 | + console.print( |
| 99 | + Panel( |
| 100 | + Markdown(event.message.text), |
| 101 | + title=f"🤖 {event.agent_id}", |
| 102 | + border_style="bold blue", |
| 103 | + padding=(1, 2), |
| 104 | + ) |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +magentic_orchestrator = ( |
| 109 | + MagenticBuilder() |
| 110 | + .participants( |
| 111 | + local_agent=local_agent, |
| 112 | + language_agent=language_agent, |
| 113 | + travel_summary_agent=travel_summary_agent, |
| 114 | + ) |
| 115 | + .on_event(on_event, mode=MagenticCallbackMode.NON_STREAMING) |
| 116 | + .with_standard_manager( |
| 117 | + chat_client=client, |
| 118 | + max_round_count=20, |
| 119 | + max_stall_count=3, |
| 120 | + max_reset_count=2, |
| 121 | + ) |
| 122 | + .build() |
| 123 | +) |
| 124 | + |
| 125 | + |
| 126 | +async def main(): |
| 127 | + async for event in magentic_orchestrator.run_stream("Plan a half-day trip to Costa Rica"): |
| 128 | + if isinstance(event, WorkflowOutputEvent): |
| 129 | + final_result = event.data |
| 130 | + console.print( |
| 131 | + Panel( |
| 132 | + Markdown(final_result.text), |
| 133 | + title="🌎 final travel plan", |
| 134 | + border_style="bold green", |
| 135 | + padding=(1, 2), |
| 136 | + ) |
| 137 | + ) |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + asyncio.run(main()) |
0 commit comments