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.

  1. import asyncio
  2. import os
  3. from pydantic import Field, BaseModel
  4. from agentscope.agent import ReActAgent
  5. from agentscope.formatter import (
  6. DashScopeMultiAgentFormatter,
  7. DashScopeChatFormatter,
  8. )
  9. from agentscope.message import Msg
  10. from agentscope.model import DashScopeChatModel
  11. from agentscope.pipeline import MsgHub
  12. # Prepare a topic
  13. topic = (
  14. "The two circles are externally tangent and there is no relative sliding. "
  15. "The radius of circle A is 1/3 the radius of circle B. Circle A rolls "
  16. "around circle B one trip back to its starting point. How many times will "
  17. "circle A revolve in total?"
  18. )
  19. # Create two debater agents, Alice and Bob, who will discuss the topic.
  20. def create_solver_agent(name: str) -> ReActAgent:
  21. """Get a solver agent."""
  22. return ReActAgent(
  23. name=name,
  24. sys_prompt=f"You're a debater named {name}. Hello and welcome to the "
  25. "debate competition. It's unnecessary to fully agree with "
  26. "each other's perspectives, as our objective is to find "
  27. "the correct answer. The debate topic is stated as "
  28. f"follows: {topic}.",
  29. model=DashScopeChatModel(
  30. model_name="qwen-max",
  31. api_key=os.environ["DASHSCOPE_API_KEY"],
  32. stream=False,
  33. ),
  34. formatter=DashScopeMultiAgentFormatter(),
  35. )
  36. alice, bob = [create_solver_agent(name) for name in ["Alice", "Bob"]]
  37. # Create a moderator agent
  38. moderator = ReActAgent(
  39. name="Aggregator",
  40. 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:
  41. ``````
  42. {topic}
  43. ``````
  44. At the end of each round, you will evaluate both sides' answers and decide which one is correct.""",
  45. model=DashScopeChatModel(
  46. model_name="qwen-max",
  47. api_key=os.environ["DASHSCOPE_API_KEY"],
  48. stream=False,
  49. ),
  50. # Use multiagent formatter because the moderator will receive messages from more than a user and an assistant
  51. formatter=DashScopeMultiAgentFormatter(),
  52. )
  53. # A structured output model for the moderator
  54. class JudgeModel(BaseModel):
  55. """The structured output model for the moderator."""
  56. finished: bool = Field(
  57. description="Whether the debate is finished.",
  58. )
  59. correct_answer: str | None = Field(
  60. description="The correct answer to the debate topic, only if the debate is finished. Otherwise, leave it as None.",
  61. default=None,
  62. )
  63. async def run_multiagent_debate() -> None:
  64. """Run the multi-agent debate workflow."""
  65. while True:
  66. # The reply messages in MsgHub from the participants will be broadcasted to all participants.
  67. async with MsgHub(participants=[alice, bob, moderator]):
  68. await alice(
  69. Msg(
  70. "user",
  71. "You are affirmative side, Please express your viewpoints.",
  72. "user",
  73. ),
  74. )
  75. await bob(
  76. Msg(
  77. "user",
  78. "You are negative side. You disagree with the affirmative side. Provide your reason and answer.",
  79. "user",
  80. ),
  81. )
  82. # Alice and Bob doesn't need to know the moderator's message, so moderator is called outside the MsgHub.
  83. msg_judge = await moderator(
  84. Msg(
  85. "user",
  86. "Now you have heard the answers from the others, have the debate finished, and can you get the correct answer?",
  87. "user",
  88. ),
  89. structured_model=JudgeModel,
  90. )
  91. if msg_judge.metadata.get("finished"):
  92. print(
  93. "\nThe debate is finished, and the correct answer is: ",
  94. msg_judge.metadata.get("correct_answer"),
  95. )
  96. break
  97. asyncio.run(run_multiagent_debate())
  1. Alice: As the affirmative side, I will argue that when circle A, with a radius 1/3 of circle B, rolls around circle B without sliding, it will revolve 4 times in total to return to its starting point.
  2. To understand this, let's consider the following:
  3. - The circumference of a circle is given by \( C = 2\pi r \), where \( r \) is the radius.
  4. - If the radius of circle B is \( R \), then the radius of circle A is \( \frac{R}{3} \).
  5. - The circumference of circle B is \( 2\pi R \) and the circumference of circle A is \( 2\pi \frac{R}{3} \).
  6. When circle A rolls around circle B, it will travel a distance equal to the circumference of the path it follows. This path is essentially the circumference of a circle with a radius equal to the sum of the radii of circles A and B, which is \( R + \frac{R}{3} = \frac{4R}{3} \). Therefore, the length of the path is \( 2\pi \times \frac{4R}{3} = \frac{8\pi R}{3} \).
  7. Since circle A does not slide, it will rotate once for every circumference it travels along. The number of rotations can be calculated by dividing the total distance traveled by the circumference of circle A:
  8. \[ \text{Number of revolutions} = \frac{\text{Total distance}}{\text{Circumference of circle A}} = \frac{\frac{8\pi R}{3}}{2\pi \frac{R}{3}} = \frac{8\pi R}{3} \times \frac{3}{2\pi R} = 4 \]
  9. Thus, circle A will make 4 complete revolutions as it rolls around circle B one time.
  10. Bob: As the negative side, I will argue that the number of revolutions circle A makes is not 4, but 3. Here's the reasoning behind this:
  11. - When circle A rolls around the outside of circle B without sliding, it does indeed travel a distance equal to the circumference of the path with a radius of \( R + \frac{R}{3} = \frac{4R}{3} \), which is \( \frac{8\pi R}{3} \).
  12. - However, we must also consider the relative motion between the two circles. As circle A rolls around circle B, it will also rotate due to its own movement around its center. This means that for every full rotation around circle B, circle A will have an additional rotation because it has to roll over the entire circumference of circle B.
  13. - The total number of rotations can be found by adding the number of times circle A rotates around its own axis (which is 1 for each full trip around circle B) to the number of times it travels its own circumference along the path. Since the path is \( \frac{8\pi R}{3} \) and the circumference of circle A is \( \frac{2\pi R}{3} \), we would expect:
  14. \[ ext{Number of revolutions} = \frac{ ext{Total distance}}{ ext{Circumference of circle A}} = \frac{\frac{8\pi R}{3}}{\frac{2\pi R}{3}} = 4 \]
  15. - But here's the key point: one of these 'revolutions' is simply the result of circle A going around circle B once, which is the same as rotating on its own axis. Therefore, we need to subtract this one revolution from the total count, leaving us with 3 actual revolutions of circle A on its own axis.
  16. Hence, the correct number of revolutions circle A makes while rolling around circle B is 3.
  17. Aggregator: The debate has concluded, and we can now determine the correct answer. The key to this problem is understanding that when circle A rolls around the outside of circle B, it will make one full rotation due to its travel around the larger circle, plus additional rotations based on the distance traveled relative to its own circumference.
  18. - The total path length for circle A is \( \frac{8\pi R}{3} \).
  19. - The number of times circle A's circumference fits into this path is 4, as calculated by both debaters.
  20. - However, one of these 4 'revolutions' is not a true revolution of circle A on its own axis; it is simply the result of circle A going around circle B once.
  21. Therefore, the number of true revolutions (rotations on its own axis) made by circle A is 3, since the 4th revolution is just the result of the path around circle B.
  22. The correct answer is that circle A will revolve 3 times in total as it rolls around circle B one trip back to its starting point.
  23. The debate is finished, and the correct answer is: 3

Further Reading

Encouraging Divergent Thinking in Large Language Models through Multi-Agent Debate. EMNLP 2024.

Total running time of the script: (1 minutes 2.489 seconds)

Download Jupyter notebook: workflow_multiagent_debate.ipynb

Download Python source code: workflow_multiagent_debate.py

Download zipped: workflow_multiagent_debate.zip

Gallery generated by Sphinx-Gallery