Skip to content

Build and run agents you can see, understand and trust.

Add an example for multi-agent conversation (#673)

DavdGaoc9c23e42e96f
diff --git a/examples/multi_agent_conversation/main.py b/examples/multi_agent_conversation/main.py
deleted file mode 100644
index a57ac03bb..000000000
--- a/examples/multi_agent_conversation/main.py
+++ /dev/null
@@ -1,6 +0,0 @@
-# -*- coding: utf-8 -*-
-"""The multi-agent conversation example."""
-
-
-async def main() -> None:
- """The main entry point for the multi-agent conversation example."""
diff --git a/examples/workflows/multiagent_conversation/README.md b/examples/workflows/multiagent_conversation/README.md
new file mode 100644
index 000000000..a712d1f9a
--- /dev/null
+++ b/examples/workflows/multiagent_conversation/README.md
@@ -0,0 +1,20 @@
+# MultiAgent Conversation
+
+This example demonstrates how to build a multi-agent conversation workflow using ``MsgHub`` in AgentScope,
+where multiple agents broadcast messages to each other in a shared conversation space.
+
+## Setup
+
+The example is built upon the DashScope LLM API in [main.py](https://github.com/agentscope-ai/agentscope/blob/main/examples/workflows/multiagent_conversation/main.py). You can switch to other LLMs by modifying the ``model`` and ``formatter`` parameters in the code.
+
+To run the example, first install the latest version of AgentScope, then run:
+
+```bash
+python examples/workflows/multiagent_conversation/main.py
+```
+
+## Main Workflow
+
+- Create multiple participant agents with different attributes (e.g., Alice, Bob, Charlie).
+- Agents introduce themselves and interact in the message hub.
+- Supports dynamic addition and removal of agents, as well as broadcasting messages.
diff --git a/examples/workflows/multiagent_conversation/main.py b/examples/workflows/multiagent_conversation/main.py
new file mode 100644
index 000000000..06ca7a593
--- /dev/null
+++ b/examples/workflows/multiagent_conversation/main.py
@@ -0,0 +1,80 @@
+# -*- coding: utf-8 -*-
+"""The example of how to construct multi-agent conversation with MsgHub and
+pipeline in AgentScope."""
+import asyncio
+import os
+
+from agentscope.agent import ReActAgent
+from agentscope.formatter import DashScopeMultiAgentFormatter
+from agentscope.message import Msg
+from agentscope.model import DashScopeChatModel
+from agentscope.pipeline import MsgHub, sequential_pipeline
+
+
+def create_participant_agent(
+ name: str,
+ age: int,
+ career: str,
+ character: str,
+) -> ReActAgent:
+ """Create a participant agent with a specific name, age, and character."""
+ return ReActAgent(
+ name=name,
+ sys_prompt=(
+ f"You're a {age}-year-old {career} named {name} and you're "
+ f"a {character} person."
+ ),
+ model=DashScopeChatModel(
+ model_name="qwen-max",
+ api_key=os.environ["DASHSCOPE_API_KEY"],
+ stream=True,
+ ),
+ # Use multiagent formatter because the multiple entities will
+ # occur in the prompt of the LLM API call
+ formatter=DashScopeMultiAgentFormatter(),
+ )
+
+
+async def main() -> None:
+ """Run a multi-agent conversation workflow."""
+
+ # Create multiple participant agents with different characteristics
+ alice = create_participant_agent("Alice", 30, "teacher", "friendly")
+ bob = create_participant_agent("Bob", 14, "student", "rebellious")
+ charlie = create_participant_agent("Charlie", 28, "doctor", "thoughtful")
+
+ # Create a conversation where participants introduce themselves within
+ # a message hub
+ async with MsgHub(
+ participants=[alice, bob, charlie],
+ # The greeting message will be sent to all participants at the start
+ announcement=Msg(
+ "system",
+ "Now you meet each other with a brief self-introduction.",
+ "system",
+ ),
+ ) as hub:
+ # Quick construct a pipeline to run the conversation
+ await sequential_pipeline([alice, bob, charlie])
+ # Or by the following way:
+ # await alice()
+ # await bob()
+ # await charlie()
+
+ # Delete a participant agent from the hub and fake a broadcast message
+ print("##### We fake Bob's departure #####")
+ hub.delete(bob)
+ await hub.broadcast(
+ Msg(
+ "bob",
+ "I have to start my homework now, see you later!",
+ "assistant",
+ ),
+ )
+ await alice()
+ await charlie()
+
+ # ...
+
+
+asyncio.run(main())
diff --git a/examples/workflows/multiagent_debate/README.md b/examples/workflows/multiagent_debate/README.md
new file mode 100644
index 000000000..2b8190efd
--- /dev/null
+++ b/examples/workflows/multiagent_debate/README.md
@@ -0,0 +1,19 @@
+# MultiAgent 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](https://aclanthology.org/2024.emnlp-main.992/), 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.
+
+## Setup
+
+The example is built upon DashScope LLM API in [main.py](https://github.com/agentscope-ai/agentscope/blob/main/examples/workflows/multiagent_debate/main.py).
+You can also change to the other LLMs by modifying the ``model`` and ``formatter`` parameters in the code.
+
+To run the example, first install the latest version of AgentScope, then run:
+
+```bash
+python examples/workflows/multiagent_debate/main.py
+```