import asynciofrom autogen_agentchat.agents import AssistantAgent, CodeExecutorAgentfrom autogen_agentchat.teams import RoundRobinGroupChatfrom autogen_agentchat.ui import Consolefrom autogen_ext.code_executors import DockerCommandLineCodeExecutorfrom autogen_ext.models.openai import OpenAIChatCompletionClientasync def main() -> None: model_client = OpenAIChatCompletionClient(model="gpt-4o") # Create code executor with Docker async with DockerCommandLineCodeExecutor() as executor: # Assistant that writes code assistant = AssistantAgent( "code_writer", model_client=model_client, system_message="""You are a helpful AI assistant that can write Python code. When asked to perform calculations or data analysis, write Python code to solve the problem. Always explain what your code does.""", ) # Agent that executes code code_executor = CodeExecutorAgent( "code_executor", code_executor=executor, ) # Create team with both agents team = RoundRobinGroupChat( [assistant, code_executor], max_turns=10, ) # Run analysis task await Console( team.run_stream( task="""Calculate the first 10 Fibonacci numbers and plot them. Save the plot as 'fibonacci.png'.""" ) ) await model_client.close()asyncio.run(main())
---------- code_writer ----------I'll write Python code to calculate the Fibonacci numbers and create a plot.```pythonimport matplotlib.pyplot as plt# Calculate first 10 Fibonacci numbersfib = [0, 1]for i in range(8): fib.append(fib[-1] + fib[-2])# Create plotplt.figure(figsize=(10, 6))plt.plot(fib, marker='o')plt.title('First 10 Fibonacci Numbers')plt.xlabel('Index')plt.ylabel('Value')plt.grid(True)plt.savefig('fibonacci.png')
## How It Works1. **Code Writer**: AssistantAgent generates Python code based on the task2. **Code Executor**: CodeExecutorAgent runs the code in an isolated Docker container3. **Round Robin**: Agents take turns - writer creates code, executor runs it4. **Feedback Loop**: Executor reports results back to the writer for iteration## Data Analysis Example```pythonimport asynciofrom autogen_agentchat.agents import AssistantAgent, CodeExecutorAgentfrom autogen_agentchat.teams import RoundRobinGroupChatfrom autogen_agentchat.ui import Consolefrom autogen_ext.code_executors import DockerCommandLineCodeExecutorfrom autogen_ext.models.openai import OpenAIChatCompletionClientasync def main() -> None: model_client = OpenAIChatCompletionClient(model="gpt-4o") async with DockerCommandLineCodeExecutor( work_dir="./workspace", # Mount local directory image="python:3.11-slim", ) as executor: assistant = AssistantAgent( "data_analyst", model_client=model_client, system_message="""You are a data analyst. Write Python code to analyze data. Use pandas, numpy, and matplotlib for analysis and visualization. Always explain your findings.""", ) code_executor = CodeExecutorAgent( "executor", code_executor=executor, ) team = RoundRobinGroupChat([assistant, code_executor], max_turns=15) await Console( team.run_stream( task="""Analyze the sales_data.csv file: 1. Load the data 2. Calculate basic statistics 3. Find the top 5 products by revenue 4. Create a bar chart of top products 5. Save insights to analysis_report.txt""" ) ) await model_client.close()asyncio.run(main())