Note
Go to the end to download the full example code.
Multi-Agent Debate¶
Debate workflow simulates a multi-turn discussion between different agents, mostly several solvers and an aggregator. Typically, the solvers generate and exchange their answers, while the aggregator collects and summarizes the answers.
We implement the examples in EMNLP 2024, where two debater agents will discuss a topic in a fixed order, and express their arguments based on the previous debate history. At each round a moderator agent will decide whether the correct answer can be obtained in the current iteration.
import asyncio
import os
from pydantic import Field, BaseModel
from agentscope.agent import ReActAgent
from agentscope.formatter import (
DashScopeMultiAgentFormatter,
DashScopeChatFormatter,
)
from agentscope.message import Msg
from agentscope.model import DashScopeChatModel
from agentscope.pipeline import MsgHub
# Prepare a topic
topic = (
"The two circles are externally tangent and there is no relative sliding. "
"The radius of circle A is 1/3 the radius of circle B. Circle A rolls "
"around circle B one trip back to its starting point. How many times will "
"circle A revolve in total?"
)
# Create two debater agents, Alice and Bob, who will discuss the topic.
def create_solver_agent(name: str) -> ReActAgent:
"""Get a solver agent."""
return ReActAgent(
name=name,
sys_prompt=f"You're a debater named {name}. Hello and welcome to the "
"debate competition. It's unnecessary to fully agree with "
"each other's perspectives, as our objective is to find "
"the correct answer. The debate topic is stated as "
f"follows: {topic}.",
model=DashScopeChatModel(
model_name="qwen-max",
api_key=os.environ["DASHSCOPE_API_KEY"],
stream=False,
),
formatter=DashScopeMultiAgentFormatter(),
)
alice, bob = [create_solver_agent(name) for name in ["Alice", "Bob"]]
# Create a moderator agent
moderator = ReActAgent(
name="Aggregator",
sys_prompt=f"""You're a moderator. There will be two debaters involved in a debate competition. They will present their answer and discuss their perspectives on the topic:
``````
{topic}
``````
At the end of each round, you will evaluate both sides' answers and decide which one is correct.""",
model=DashScopeChatModel(
model_name="qwen-max",
api_key=os.environ["DASHSCOPE_API_KEY"],
stream=False,
),
# Use multiagent formatter because the moderator will receive messages from more than a user and an assistant
formatter=DashScopeMultiAgentFormatter(),
)
# A structured output model for the moderator
class JudgeModel(BaseModel):
"""The structured output model for the moderator."""
finished: bool = Field(
description="Whether the debate is finished.",
)
correct_answer: str | None = Field(
description="The correct answer to the debate topic, only if the debate is finished. Otherwise, leave it as None.",
default=None,
)
async def run_multiagent_debate() -> None:
"""Run the multi-agent debate workflow."""
while True:
# The reply messages in MsgHub from the participants will be broadcasted to all participants.
async with MsgHub(participants=[alice, bob, moderator]):
await alice(
Msg(
"user",
"You are affirmative side, Please express your viewpoints.",
"user",
),
)
await bob(
Msg(
"user",
"You are negative side. You disagree with the affirmative side. Provide your reason and answer.",
"user",
),
)
# Alice and Bob doesn't need to know the moderator's message, so moderator is called outside the MsgHub.
msg_judge = await moderator(
Msg(
"user",
"Now you have heard the answers from the others, have the debate finished, and can you get the correct answer?",
"user",
),
structured_model=JudgeModel,
)
if msg_judge.metadata.get("finished"):
print(
"\nThe debate is finished, and the correct answer is: ",
msg_judge.metadata.get("correct_answer"),
)
break
asyncio.run(run_multiagent_debate())
Alice: Thank you. As the affirmative side, I will argue that when circle A, with a radius 1/3 that of circle B, rolls around the outside of circle B without sliding, it will complete exactly 4 revolutions in total by the time it returns to its starting point.
To understand this, let's break down the problem:
- Let the radius of circle B be \( r \).
- Then, the radius of circle A is \( \frac{1}{3}r \).
When circle A rolls around circle B, the path it follows is the circumference of a larger circle whose radius is the sum of the radii of circles A and B. This larger circle has a radius of \( r + \frac{1}{3}r = \frac{4}{3}r \). The circumference of this larger circle, which is the distance circle A travels, is:
\[ C_{\text{path}} = 2\pi \times \frac{4}{3}r = \frac{8}{3}\pi r \]
Now, the number of revolutions circle A makes is the distance it travels (the circumference of the path) divided by the circumference of circle A itself. The circumference of circle A is:
\[ C_{A} = 2\pi \times \frac{1}{3}r = \frac{2}{3}\pi r \]
Therefore, the number of revolutions, \( N \), that circle A makes as it rolls around circle B is:
\[ N = \frac{C_{\text{path}}}{C_{A}} = \frac{\frac{8}{3}\pi r}{\frac{2}{3}\pi r} = \frac{8}{3} \div \frac{2}{3} = 4 \]
Thus, circle A will revolve 4 times in total.
Bob: Thank you. As the negative side, I will argue that the number of revolutions circle A makes is not 4 but rather 3.
To clarify, let's revisit the problem and consider the relative motion between the two circles. When circle A rolls around the outside of circle B, it does indeed travel a distance equal to the circumference of a larger circle with radius \( \frac{4}{3}r \), which is \( \frac{8}{3}\pi r \). However, this calculation alone does not fully account for the relative rotation between the two circles.
As circle A rolls along the circumference of circle B, it must also rotate to maintain contact without sliding. For every point of contact, circle A has to rotate in such a way that its point of contact aligns with the next point on circle B. Since the radius of circle A is \( \frac{1}{3}r \), it means that for every full rotation of circle B (which is \( 2\pi r \)), circle A would have to rotate 3 times to cover the same arc length (since \( 3 \times \frac{2}{3}\pi r = 2\pi r \)).
Therefore, as circle A travels around the circumference of the path, which is \( \frac{8}{3}\pi r \), it needs to make 3 complete rotations to match the curvature of the path and maintain no sliding. This is because the relative motion between the two circles requires circle A to rotate an additional time to "catch up" with the path it is following around circle B.
In conclusion, circle A will revolve 3 times in total as it rolls around circle B.
/home/runner/work/agentscope/agentscope/src/agentscope/model/_dashscope_model.py:182: DeprecationWarning: 'required' is not supported by DashScope API. It will be converted to 'auto'.
warnings.warn(
Aggregator: {
"type": "tool_use",
"name": "generate_response",
"input": {
"correct_answer": "4",
"finished": true
},
"id": "call_f74b67617759436bb2803c"
}
system: {
"type": "tool_result",
"id": "call_f74b67617759436bb2803c",
"name": "generate_response",
"output": [
{
"type": "text",
"text": "Successfully generated response."
}
]
}
Aggregator: The debate has concluded, and after evaluating the arguments presented by both Alice and Bob, the correct answer is that circle A will revolve 4 times in total as it rolls around circle B.
Alice's argument correctly accounted for the path length that circle A travels, which is the circumference of a larger circle with radius \( \frac{4}{3}r \), and then divided this by the circumference of circle A to find the number of revolutions. This calculation accurately reflects the total number of revolutions circle A makes.
Bob's argument suggested that circle A would only make 3 revolutions, based on the idea that circle A must rotate to maintain contact without sliding. However, this reasoning does not fully account for the additional rotation due to the path length traveled by circle A.
Therefore, the correct number of revolutions is 4.
The debate is finished, and the correct answer is: 4
Further Reading¶
Encouraging Divergent Thinking in Large Language Models through Multi-Agent Debate. EMNLP 2024.
Total running time of the script: (0 minutes 31.606 seconds)