From 63039cb7489d687697453599a3e136c2d20babfc Mon Sep 17 00:00:00 2001 From: Tao Chen Date: Mon, 23 Mar 2026 15:51:17 -0700 Subject: [PATCH] Update autogen-migration samples --- .../01_round_robin_group_chat.py | 26 ++++++------------- .../orchestrations/02_selector_group_chat.py | 22 +++++----------- .../orchestrations/03_swarm.py | 21 +++++---------- .../orchestrations/04_magentic_one.py | 21 +++++---------- .../single_agent/01_basic_assistant_agent.py | 19 ++++---------- .../02_assistant_agent_with_tool.py | 20 ++++---------- .../03_assistant_agent_thread_and_stream.py | 19 ++++---------- .../single_agent/04_agent_as_tool.py | 20 +++++--------- 8 files changed, 48 insertions(+), 120 deletions(-) diff --git a/python/samples/autogen-migration/orchestrations/01_round_robin_group_chat.py b/python/samples/autogen-migration/orchestrations/01_round_robin_group_chat.py index e5c6bd09f8..8b883a07b9 100644 --- a/python/samples/autogen-migration/orchestrations/01_round_robin_group_chat.py +++ b/python/samples/autogen-migration/orchestrations/01_round_robin_group_chat.py @@ -1,25 +1,17 @@ -# /// script -# requires-python = ">=3.10" -# dependencies = [ -# "autogen-agentchat", -# "autogen-ext[openai]", -# ] -# /// -# Run with any PEP 723 compatible runner, e.g.: -# uv run samples/autogen-migration/orchestrations/01_round_robin_group_chat.py - # Copyright (c) Microsoft. All rights reserved. -"""AutoGen RoundRobinGroupChat vs Agent Framework GroupChatBuilder/SequentialBuilder. -Demonstrates sequential agent orchestration where agents take turns processing -the task in a round-robin fashion. -""" import asyncio from agent_framework import Message from dotenv import load_dotenv +"""AutoGen RoundRobinGroupChat vs Agent Framework GroupChatBuilder/SequentialBuilder. + +Demonstrates sequential agent orchestration where agents take turns processing +the task in a round-robin fashion. +""" + # Load environment variables from .env file load_dotenv() @@ -98,7 +90,7 @@ async def run_agent_framework() -> None: print("[Agent Framework] Sequential conversation:") async for event in workflow.run("Create a brief summary about electric vehicles", stream=True): if event.type == "output" and isinstance(event.data, list): - for message in event.data: + for message in event.data: # type: ignore if isinstance(message, Message) and message.role == "assistant" and message.text: print(f"---------- {message.author_name} ----------") print(message.text) @@ -144,9 +136,7 @@ async def run_agent_framework_with_cycle() -> None: if last_message and "APPROVED" in last_message.text: await context.yield_output("Content approved.") else: - await context.send_message( - AgentExecutorRequest(messages=response.full_conversation, should_respond=True) - ) + await context.send_message(AgentExecutorRequest(messages=response.full_conversation, should_respond=True)) workflow = ( WorkflowBuilder(start_executor=researcher) diff --git a/python/samples/autogen-migration/orchestrations/02_selector_group_chat.py b/python/samples/autogen-migration/orchestrations/02_selector_group_chat.py index 6f16e1dea9..485f3793e5 100644 --- a/python/samples/autogen-migration/orchestrations/02_selector_group_chat.py +++ b/python/samples/autogen-migration/orchestrations/02_selector_group_chat.py @@ -1,25 +1,17 @@ -# /// script -# requires-python = ">=3.10" -# dependencies = [ -# "autogen-agentchat", -# "autogen-ext[openai]", -# ] -# /// -# Run with any PEP 723 compatible runner, e.g.: -# uv run samples/autogen-migration/orchestrations/02_selector_group_chat.py - # Copyright (c) Microsoft. All rights reserved. -"""AutoGen SelectorGroupChat vs Agent Framework GroupChatBuilder. -Demonstrates LLM-based speaker selection where an orchestrator decides -which agent should speak next based on the conversation context. -""" import asyncio from agent_framework import Message from dotenv import load_dotenv +"""AutoGen SelectorGroupChat vs Agent Framework GroupChatBuilder. + +Demonstrates LLM-based speaker selection where an orchestrator decides +which agent should speak next based on the conversation context. +""" + # Load environment variables from .env file load_dotenv() @@ -113,7 +105,7 @@ async def run_agent_framework() -> None: print("[Agent Framework] Group chat conversation:") async for event in workflow.run("How do I connect to a PostgreSQL database using Python?", stream=True): if event.type == "output" and isinstance(event.data, list): - for message in event.data: + for message in event.data: # type: ignore if isinstance(message, Message) and message.role == "assistant" and message.text: print(f"---------- {message.author_name} ----------") print(message.text) diff --git a/python/samples/autogen-migration/orchestrations/03_swarm.py b/python/samples/autogen-migration/orchestrations/03_swarm.py index a178ffcffe..e2a8688b10 100644 --- a/python/samples/autogen-migration/orchestrations/03_swarm.py +++ b/python/samples/autogen-migration/orchestrations/03_swarm.py @@ -1,19 +1,4 @@ -# /// script -# requires-python = ">=3.10" -# dependencies = [ -# "autogen-agentchat", -# "autogen-ext[openai]", -# ] -# /// -# Run with any PEP 723 compatible runner, e.g.: -# uv run samples/autogen-migration/orchestrations/03_swarm.py - # Copyright (c) Microsoft. All rights reserved. -"""AutoGen Swarm pattern vs Agent Framework HandoffBuilder. - -Demonstrates agent handoff coordination where agents can transfer control -to other specialized agents based on the task requirements. -""" import asyncio from typing import Any @@ -21,6 +6,12 @@ from typing import Any from agent_framework import AgentResponseUpdate, WorkflowEvent from dotenv import load_dotenv +"""AutoGen Swarm pattern vs Agent Framework HandoffBuilder. + +Demonstrates agent handoff coordination where agents can transfer control +to other specialized agents based on the task requirements. +""" + # Load environment variables from .env file load_dotenv() diff --git a/python/samples/autogen-migration/orchestrations/04_magentic_one.py b/python/samples/autogen-migration/orchestrations/04_magentic_one.py index b6728b0e46..58ec95e492 100644 --- a/python/samples/autogen-migration/orchestrations/04_magentic_one.py +++ b/python/samples/autogen-migration/orchestrations/04_magentic_one.py @@ -1,19 +1,4 @@ -# /// script -# requires-python = ">=3.10" -# dependencies = [ -# "autogen-agentchat", -# "autogen-ext[openai]", -# ] -# /// -# Run with any PEP 723 compatible runner, e.g.: -# uv run samples/autogen-migration/orchestrations/04_magentic_one.py - # Copyright (c) Microsoft. All rights reserved. -"""AutoGen MagenticOneGroupChat vs Agent Framework MagenticBuilder. - -Demonstrates orchestrated multi-agent workflows with a central coordinator -managing specialized agents for complex tasks. -""" import asyncio import json @@ -27,6 +12,12 @@ from agent_framework import ( from agent_framework.orchestrations import MagenticProgressLedger from dotenv import load_dotenv +"""AutoGen MagenticOneGroupChat vs Agent Framework MagenticBuilder. + +Demonstrates orchestrated multi-agent workflows with a central coordinator +managing specialized agents for complex tasks. +""" + # Load environment variables from .env file load_dotenv() diff --git a/python/samples/autogen-migration/single_agent/01_basic_assistant_agent.py b/python/samples/autogen-migration/single_agent/01_basic_assistant_agent.py index 73a3caba02..fad39f7719 100644 --- a/python/samples/autogen-migration/single_agent/01_basic_assistant_agent.py +++ b/python/samples/autogen-migration/single_agent/01_basic_assistant_agent.py @@ -1,14 +1,9 @@ -# /// script -# requires-python = ">=3.10" -# dependencies = [ -# "autogen-agentchat", -# "autogen-ext[openai]", -# ] -# /// -# Run with any PEP 723 compatible runner, e.g.: -# uv run samples/autogen-migration/single_agent/01_basic_assistant_agent.py - # Copyright (c) Microsoft. All rights reserved. + +import asyncio + +from dotenv import load_dotenv + """Basic AutoGen AssistantAgent vs Agent Framework Agent. Both samples expect OpenAI-compatible environment variables (OPENAI_API_KEY or @@ -16,10 +11,6 @@ Azure OpenAI configuration). Update the prompts or client wiring to match your model of choice before running. """ -import asyncio - -from dotenv import load_dotenv - # Load environment variables from .env file load_dotenv() diff --git a/python/samples/autogen-migration/single_agent/02_assistant_agent_with_tool.py b/python/samples/autogen-migration/single_agent/02_assistant_agent_with_tool.py index aca868b9f2..af7ebaf03b 100644 --- a/python/samples/autogen-migration/single_agent/02_assistant_agent_with_tool.py +++ b/python/samples/autogen-migration/single_agent/02_assistant_agent_with_tool.py @@ -1,24 +1,14 @@ -# /// script -# requires-python = ">=3.10" -# dependencies = [ -# "autogen-agentchat", -# "autogen-core", -# "autogen-ext[openai]", -# ] -# /// -# Run with any PEP 723 compatible runner, e.g.: -# uv run samples/autogen-migration/single_agent/02_assistant_agent_with_tool.py - # Copyright (c) Microsoft. All rights reserved. -"""AutoGen AssistantAgent vs Agent Framework Agent with function tools. - -Demonstrates how to create and attach tools to agents in both frameworks. -""" import asyncio from dotenv import load_dotenv +"""AutoGen AssistantAgent vs Agent Framework Agent with function tools. + +Demonstrates how to create and attach tools to agents in both frameworks. +""" + # Load environment variables from .env file load_dotenv() diff --git a/python/samples/autogen-migration/single_agent/03_assistant_agent_thread_and_stream.py b/python/samples/autogen-migration/single_agent/03_assistant_agent_thread_and_stream.py index c544880cb1..9610f47ad2 100644 --- a/python/samples/autogen-migration/single_agent/03_assistant_agent_thread_and_stream.py +++ b/python/samples/autogen-migration/single_agent/03_assistant_agent_thread_and_stream.py @@ -1,23 +1,14 @@ -# /// script -# requires-python = ">=3.10" -# dependencies = [ -# "autogen-agentchat", -# "autogen-ext[openai]", -# ] -# /// -# Run with any PEP 723 compatible runner, e.g.: -# uv run samples/autogen-migration/single_agent/03_assistant_agent_thread_and_stream.py - # Copyright (c) Microsoft. All rights reserved. -"""AutoGen vs Agent Framework: Thread management and streaming responses. - -Demonstrates conversation state management and streaming in both frameworks. -""" import asyncio from dotenv import load_dotenv +"""AutoGen vs Agent Framework: Thread management and streaming responses. + +Demonstrates conversation state management and streaming in both frameworks. +""" + # Load environment variables from .env file load_dotenv() diff --git a/python/samples/autogen-migration/single_agent/04_agent_as_tool.py b/python/samples/autogen-migration/single_agent/04_agent_as_tool.py index 489ec74c01..74a9fb3463 100644 --- a/python/samples/autogen-migration/single_agent/04_agent_as_tool.py +++ b/python/samples/autogen-migration/single_agent/04_agent_as_tool.py @@ -1,24 +1,15 @@ -# /// script -# requires-python = ">=3.10" -# dependencies = [ -# "autogen-agentchat", -# "autogen-ext[openai]", -# ] -# /// -# Run with any PEP 723 compatible runner, e.g.: -# uv run samples/autogen-migration/single_agent/04_agent_as_tool.py - # Copyright (c) Microsoft. All rights reserved. + +import asyncio + +from dotenv import load_dotenv + """AutoGen vs Agent Framework: Agent-as-a-Tool pattern. Demonstrates hierarchical agent architectures where one agent delegates work to specialized sub-agents wrapped as tools. """ -import asyncio - -from dotenv import load_dotenv - # Load environment variables from .env file load_dotenv() @@ -107,6 +98,7 @@ async def run_agent_framework() -> None: if content.type == "function_call": # Accumulate function call content as it streams in call_id = content.call_id + assert call_id is not None, "Function call content must have a call_id" if call_id in accumulated_calls: # Add to existing call (arguments stream in gradually) accumulated_calls[call_id] = accumulated_calls[call_id] + content