Welcome back to the series!
So far, we’ve learned what AG-2 is and created a basic single-agent app. Now we’re moving to the fun part: collaboration between agents.
In this post, we’ll build a simple multi-agent pipeline:
- A Researcher agent that finds information
- A Writer agent that turns that info into a summary
This will show you how to chain agents, pass messages between them, and use AG-2's orchestration features.
What We’re Building
We’ll simulate this conversation:
You: “Write a summary about climate change.”
Researcher: “Climate change refers to long-term shifts…”
Writer: “Sure! Here's a concise summary of that…”
Step 1: Install or Update AG-2 (if needed)
Make sure you're using the latest AG-2:
pip install --upgrade ag2
Step 2: Define Two Agents
Create a file called multi_agent_pipeline.py
:
from ag2 import Agent, Orchestrator, Conversation import os os.environ["OPENAI_API_KEY"] = "your-api-key-here" # Researcher agent researcher = Agent( name="researcher", llm="openai/gpt-4", system_message="You are a helpful researcher. Given a topic, find relevant and accurate information.", ) # Writer agent writer = Agent( name="writer", llm="openai/gpt-4", system_message="You are a professional writer. Based on research findings, write clear summaries.", )
Step 3: Orchestrate the Conversation
Now we’ll define the flow between them.
# Define orchestrator logic orchestrator = Orchestrator( agents=[researcher, writer], rules=[ {"from": "user", "to": "researcher"}, {"from": "researcher", "to": "writer"}, {"from": "writer", "to": "user"}, ] ) # Start the conversation conv = Conversation(orchestrator=orchestrator) conv.send("Please write a short summary about climate change.")
This defines a simple linear message flow from the user to Researcher → Writer → back to user.
Step 4: Run It
python multi_agent_pipeline.py
You should see something like:
User: Please write a short summary about climate change.
Researcher: Climate change refers to...
Writer: Sure! Here's a summary: ...
Success! You just built a multi-agent reasoning chain.
What’s Next?
In the next post, we’ll:
- Explore Patterns in AG-2 (like debate, delegation, human review)
- Add tool use in multi-agent chains
- Start designing more dynamic workflows
Keep coding
Top comments (0)