pip install autogen-agentchat autogen-ext[openai]
export OPENAI_API_KEY=sk-...
Create a Coder and Reviewer in a team to collaboratively produce and review code.
1 import asyncio 2 from autogen_agentchat.agents import AssistantAgent 3 from autogen_agentchat.teams import RoundRobinGroupChat 4 from autogen_agentchat.conditions import TextMentionTermination 5 from autogen_ext.models.openai import OpenAIChatCompletionClient 6 7 model = OpenAIChatCompletionClient(model="gpt-4o") 8 9 coder = AssistantAgent( 10 name="Coder", 11 model_client=model, 12 system_message="""You are a Python expert. Write clean, documented code. 13 Always include type hints and a docstring. When the solution is complete and tested, write APPROVE.""", 14 ) 15 16 reviewer = AssistantAgent( 17 name="Reviewer", 18 model_client=model, 19 system_message="""You are a senior code reviewer. Check for: 20 1. Correctness and edge cases 21 2. Type hints and documentation 22 3. Pythonic style 23 If all checks pass, write APPROVE. Otherwise provide specific improvement feedback.""", 24 ) 25 26 termination = TextMentionTermination("APPROVE") 27 28 team = RoundRobinGroupChat( 29 participants=[coder, reviewer], 30 termination_condition=termination, 31 max_turns=8, 32 ) 33 34 async def main(): 35 result = await team.run( 36 task="Write a Python function that finds all prime numbers up to n using the Sieve of Eratosthenes." 37 ) 38 for msg in result.messages: 39 print(f"\n--- {msg.source} ---") 40 print(msg.content[:400]) 41 print(f"\nStop reason: {result.stop_reason}") 42 43 asyncio.run(main()) 44
Coder writes function, Reviewer checks it, terminates with APPROVE
Sign in to share your feedback and join the discussion.