Autogen Agents Evaluator Optimizer
AutoGen Agents: Evaluator-Optimizer
In this tutorial, we'll explore the Evaluator-Optimizer agent pattern using AutoGen GroupChats and demonstrate how to trace the process using Phoenix.
The Evaluator-Optimizer pattern employs a loop where one agent acts as a generator, creating an initial output (like text or code), while a second agent serves as an evaluator, providing critical feedback against criteria. This feedback guides the generator through successive revisions, enabling iterative refinement and significant quality improvement for complex tasks. This approach trades increased interactions for a more polished & accurate final result.
AutoGen's GroupChat architecture is good for implementing this pattern becuase it can manage the conversational turns between the generator and evaluator agents. The GroupChatManager facilitates the dialogue, allowing the agents to exchange the evolving outputs and feedback.
With Phoenix tracing, we can gain full visibility into each refinement cycle, tracking the feedback provided, the revisions made, and the overall progress towards the desired quality standard, which aids in debugging and analysis.
By the end of this tutorial, you’ll learn how to:
- Set up generator and evaluator AutoGen agents with specific roles and criteria.
- Implement the Evaluator-Optimizer pattern within an AutoGen GroupChat.
- Manage the iterative refinement loop between agents.
- Trace and visualize the iterative feedback and revision process using Phoenix.
⚠️ You'll need an OpenAI Key for this tutorial.
Set up Keys and Dependencies
Configure Tracing
Example Evaluator-Optimizer Task: Code Generation & Evaluation
This Evaluator-Optimizer Agent pattern uses a loop where one AI agent generates an output, like code, and another agent evaluates it against specific criteria to drive improvement.
In this specific example, we'll utilize a Code_Generator agent to write Python code based on requirements. A dedicated Code_Reviewer agent will then assess this code for correctness, style, and documentation, providing targeted feedback. Through this iterative cycle of generation and critique within an AutoGen GroupChat, we'll demonstrate how to produce higher-quality, reviewed code that meets defined standards.

Define Agent
The llm_config specifies the configuration used for each AssistantAgent.
We use a different model for code generation and evaluation.
This section initializes two AutoGen AssisstantAgents designed for an iterative code generation and review workflow.
The Code_Generator agent is responsible for writing and revising Python code based on instructions and feedback, while the Code_Reviewer agent evaluates the generated code against detailed criteria like correctness, style, and readability, providing specific feedback or a precise termination signal: "TERMINATE!".
Next, we define a function,check_reviewer_approval, to specifically detect if a message contains only "TERMINATE!".
Then, we initialize the AutoGen UserProxyAgent to act as the human user. We use check_reviewer_approval as the termination message check for this Agent.
Finally, we initialize the agent framework by first defining an AutoGen GroupChat that includes the user proxy, coder, and reviewer agents, limiting the interaction to a maximum of 10 rounds.
It then creates a GroupChatManager to orchestrate the conversation within this group, assigning it an LLM configuration and making it aware of the termination condition by passing in the check_reviewer_approval function. The GroupChatManager uses existing context to determine which agent to call next.
Run Agent
We are now ready to run our agent using this sample coding_task.
View Results in Phoenix
Phoenix tracing shows us the Evaluator-Optimizer workflow by visualizing the iterative loop between the Code_Generator and Code_Reviewer agents within the GroupChat.
Within the trace, you can see the entire refinement process, inspecting the specific prompts fed to the agents at each step and seeing the output progressively change and improve prior to the termination decision.
Run the cell below to see the full tracing results.