Ideation processes often require time-consuming analysis and debate. What if we make two LLMs come up with ideas and then make them debate about those ideas? Sounds interesting right? This tutorial exactly shows how to create an AI-powered solution using two LLM agents that collaborate through structured conversation. For achieving this we will be using AutoGen for building the agent and ChatGPT as LLM for our agent.
1. Setup and Installation
First install required packages:
pip install -U autogen-agentchat
pip install autogen-ext[openai]
2. Core Components
Let’s explore the key components of AutoGen that make this ideation system work. Understanding these components will help you customize and extend the system for your specific needs.
1. RoundRobinGroupChat
- Manages a team of agents in a turn-based manner.
- Agents take turns responding, and all messages are shared for context.
- Ensures structured and fair interaction.
2. TextMentionTermination
- Stops the conversation when a specific keyword (e.g., “FINALIZE”) is detected.
- Useful for ending discussions when agents reach consensus or complete a task.
3. AssistantAgent
- Represents an LLM-powered team member with a specific role.
- Each agent is defined by a system message that guides its behavior.
- Agents use the conversation history to generate context-aware responses.
These components work together to create a structured, collaborative system where agents brainstorm, debate, and reach decisions efficiently.
3. Building the Agent Team
Create two specialized agents with distinct roles:
import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.base import TaskResult
from autogen_agentchat.conditions import ExternalTermination, TextMentionTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_core import CancellationToken
from autogen_ext.models.openai import OpenAIChatCompletionClient
from apikey import API_KEY
# Create an OpenAI model client.
model_client = OpenAIChatCompletionClient(
model="gpt-4o-mini",
api_key=API_KEY,
)
# Create the primary agent.
primary_agent = AssistantAgent(
"participant1",
model_client=model_client,
system_message="You are a participant in an ideation and feedback session. You will be provided with a problem statement and asked to generate ideas. Your ideas will be\
reviwed by another participant and then you together will narrow down ideas by debating over them. Respond with 'FINALIZE' when you have a final idea.",
)
# Create the critic agent.
critic_agent = AssistantAgent(
"participant2",
model_client=model_client,
system_message="You are a participant in an ideation and feedback session. Your teammate will be provide some ideas that you need to review with your \
teammate and narrow down ideas by debating over them. Respond with 'FINALIZE' when you have a final idea.",
)
# Define a termination condition that stops the task if the critic approves.
text_termination = TextMentionTermination("FINALIZE")
# Create a team with the primary and critic agents.
team = RoundRobinGroupChat([primary_agent, critic_agent], termination_condition=text_termination)
4. Running the Team
Execute with asynchronous processing:
result = await team.run(task="Generate ideas for an applications of AI in healthcare.")
print(result)
5. Monitoring Interactions
You can also track the debate in real-time:
# When running inside a script, use a async main function and call it from `asyncio.run(...)`.
await team.reset() # Reset the team for a new task.
async for message in team.run_stream(task="Generate ideas for an applications of AI in healthcare."): # type: ignore
if isinstance(message, TaskResult):
print("Stop Reason:", message.stop_reason)
else:
print(message)
AutoGen also provides us with a function to visualize the interactions in a prettier ways using console function:
await team.reset() # Reset the team for a new task.
await Console(team.run_stream(task="Generate ideas for an applications of AI in healthcare.")) # Stream the messages to the console.
Now the system is complete. But there is a-lot to play around with, but I will leave that to you. Here are few ideas to enhance your system:
- Adding domain-specific agents (medical experts, technical validators)
- Implementing custom termination conditions
- Making a simple UI using streamlit
- Adding more players to the team
References:
Leave a comment