.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorial/workflow_multiagent_debate.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorial_workflow_multiagent_debate.py: .. _multiagent-debate: 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. .. GENERATED FROM PYTHON SOURCE LINES 15-131 .. code-block:: Python 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()) .. rst-class:: sphx-glr-script-out .. code-block:: none Alice: As the affirmative side, I will present the argument that when a smaller circle A, whose radius is 1/3 of the larger circle B, rolls around circle B without sliding, it will revolve 4 times in total by the time it returns to its starting point. To understand this, let's consider the following: - The circumference of a circle is given by \( C = 2\pi r \), where \( r \) is the radius of the circle. - If the radius of circle A is 1/3 of the radius of circle B, then the ratio of their circumferences is also 1:3. This means if the radius of circle B is \( R \), the radius of circle A is \( \frac{R}{3} \). - When circle A rolls around the outside of circle B, it travels a distance equal to the circumference of the path it follows, which is the circumference of a circle with a radius equal to the sum of the radii of circles A and B, i.e., \( R + \frac{R}{3} = \frac{4R}{3} \). - The total distance that circle A travels is the circumference of this combined circle, which is \( 2\pi \times \frac{4R}{3} = \frac{8\pi R}{3} \). - Since the circumference of circle A is \( 2\pi \times \frac{R}{3} = \frac{2\pi R}{3} \), the number of times circle A revolves is the total distance traveled divided by the circumference of circle A, which is \( \frac{\frac{8\pi R}{3}}{\frac{2\pi R}{3}} = 4 \). Therefore, circle A will revolve 4 times as it rolls around circle B once. Bob: I will take the negative side of this argument. While Alice's explanation is mathematically sound, there is a subtle point that needs to be considered: the relative rotation between the two circles. When circle A rolls around circle B, it does not only revolve around circle B but also rotates about its own center. To find out how many times circle A revolves in total, we need to consider both the number of rotations due to its path around circle B and the number of rotations due to its own spinning. Let's break it down: - As Alice correctly pointed out, the circumference of the path that circle A follows is \( \frac{8\pi R}{3} \). - The distance that circle A travels along this path is equal to 4 circumferences of circle A, which suggests that if circle A were sliding without rotating, it would have to rotate 4 times to cover the distance. - However, since there is no sliding, circle A must also rotate once for each full circuit around circle B due to the contact point always being at rest relative to circle B (this is the additional rotation due to the rolling without slipping condition). So, for every complete revolution around circle B, circle A makes an extra rotation. This means that on top of the 4 revolutions due to the distance traveled, there is 1 additional revolution due to the rolling motion. Therefore, the total number of revolutions is 4 (from the distance) + 1 (from the rolling) = 5. Hence, I argue that circle A will revolve 5 times as it rolls around circle B once. Aggregator: The debate is indeed finished, and we can now determine the correct answer. Alice's argument correctly calculates the number of times circle A would revolve based on the distance it travels around circle B. However, Bob pointed out a crucial point about the relative rotation between the two circles. When circle A rolls without slipping around circle B, it will have an additional revolution due to its own spinning as it maintains contact with circle B. Therefore, the total number of revolutions is the 4 revolutions from the path distance plus 1 additional revolution from the rolling motion, which gives us 5 total revolutions. Hence, the correct answer is that circle A will revolve 5 times in total as it rolls around circle B once. The debate is finished, and the correct answer is: 5 .. GENERATED FROM PYTHON SOURCE LINES 132-140 Further Reading ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - :ref:`pipeline` .. _EMNLP 2024: Encouraging Divergent Thinking in Large Language Models through Multi-Agent Debate. EMNLP 2024. .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 2.736 seconds) .. _sphx_glr_download_tutorial_workflow_multiagent_debate.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: workflow_multiagent_debate.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: workflow_multiagent_debate.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: workflow_multiagent_debate.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_