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: The problem involves two circles, A and B, that are externally tangent to each other with no relative sliding. The radius of circle A is 1/3 the radius of circle B. When circle A rolls around circle B for one complete trip, we need to determine how many times circle A will revolve in total.
Let's denote the radius of circle A as \( r \) and the radius of circle B as \( R \). According to the problem, \( r = \frac{1}{3}R \).
When circle A rolls around circle B, it follows a path along the circumference of an outer circle whose radius is the sum of the radii of A and B, which is \( R + r \). Therefore, the distance that the center of circle A travels is the circumference of this larger circle:
\[ \text{Distance traveled by center of A} = 2\pi(R + r) \]
Since \( r = \frac{1}{3}R \), we can substitute and get:
\[ \text{Distance traveled by center of A} = 2\pi\left(R + \frac{1}{3}R\right) = 2\pi\left(\frac{4}{3}R\right) = \frac{8\pi R}{3} \]
Now, the number of revolutions made by circle A is the distance traveled by its center divided by the circumference of circle A. The circumference of circle A is:
\[ \text{Circumference of A} = 2\pi r = 2\pi\left(\frac{1}{3}R\right) = \frac{2\pi R}{3} \]
Thus, the number of revolutions of circle A is:
\[ \text{Revolutions of A} = \frac{\text{Distance traveled by center of A}}{\text{Circumference of A}} = \frac{\frac{8\pi R}{3}}{\frac{2\pi R}{3}} = \frac{8\pi R}{3} \times \frac{3}{2\pi R} = 4 \]
So, my viewpoint is that circle A will revolve 4 times in total as it rolls around circle B once.
Bob: I'm going to play the role of the negative side, and I will provide a counter-argument to the affirmative side's solution. However, it is important to note that in this particular case, the mathematics and reasoning provided by the affirmative side (Alice) are correct. The number of revolutions circle A makes as it rolls around circle B without sliding is indeed 4.
The calculation is based on solid geometric principles, and the steps Alice has taken to arrive at the answer of 4 revolutions are mathematically sound. The distance traveled by the center of circle A is the circumference of the path it follows, which is the sum of the radii of both circles, and dividing this by the circumference of circle A gives us the number of revolutions.
However, if we were to find an alternative perspective or a potential point for debate, it could be in the interpretation of what constitutes a "revolution" for circle A. One might argue about whether the final position of circle A should be considered a full revolution if it aligns perfectly with its starting orientation, or if it should be counted as a fraction of a revolution. But in standard mathematical terms, a full trip around circle B does equate to 4 complete revolutions for circle A.
Since there is no error in the mathematical derivation, and the problem statement is clear, I cannot provide a valid counter-argument that would change the conclusion. The answer remains 4 revolutions. If you still wish to pursue a different line of argument, please let me know, and I can try to explore that further, but it would be outside the scope of the original problem statement.
Bob: While the calculations and logic presented by the affirmative side are mathematically accurate, the negative side can raise a conceptual question regarding the definition of a 'revolution.' In the context of the problem, one may consider a 'revolution' as a full 360-degree rotation of the circle A relative to a fixed point in space. When circle A rolls around circle B, it not only rotates due to its own motion along the circumference of circle B but also because of the need to maintain tangency, which adds to the total angle of rotation.
Let's revisit the concept of a revolution. If we take into account the additional rotation due to the tangential constraint, then for every full circuit around circle B, circle A must rotate an extra time to ensure that it remains tangent to circle B at all times. This means that after completing the path around circle B, circle A would have made 1 extra revolution beyond the 3 calculated from the distance traveled by its center.
Therefore, the total number of revolutions would be 3 (from the distance traveled) plus 1 (to maintain tangency), resulting in 4 revolutions. This is consistent with the previous calculation, but it introduces a different way of understanding the concept of a revolution in the context of rolling motion.
Aggregator: The debate has concluded with both Alice and Bob reaching the same numerical answer, that circle A will revolve 4 times in total as it rolls around circle B. While they approached the problem from slightly different perspectives, their conclusions align. Alice's approach was based on the distance traveled by the center of circle A and its circumference, whereas Bob emphasized the concept of a 'revolution' including the rotation necessary to maintain tangency. Both arguments are valid, and the correct number of revolutions is indeed 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: (1 minutes 18.790 seconds)