This commit is contained in:
CakeCN
2024-12-03 16:21:19 +08:00
commit 4198ca63b1
975 changed files with 333413 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
# Distributed Debate Competition
This example demonstrates:
- How to simulate a debate competition with three participant agents
- How to allow human participation in the debate
## Background
This example simulates a debate competition with three participant agents:
1. The affirmative side (**Pro**)
2. The negative side (**Con**)
3. The adjudicator (**Judge**)
The debate topic is whether AGI can be achieved using the GPT model framework. Pro argues in favor, while Con contests it. Judge listens to both sides' arguments and provides an analytical judgment on which side presented a more compelling case.
Each agent is an independent process and can run on different machines. Human participants can join as Pro or Con.
## Tested Models
These models are tested in this example. For other models, some modifications may be needed.
- Ollama Chat (qwen2:1.5b)
- Dashscope Chat (qwen-Max)
- Gemini Chat (gemini-pro)
## Prerequisites
Before running the example:
- Install the distributed version of AgentScope by running
```bash
# On windows
pip install -e .[distribute]
# On mac / linux
pip install -e .\[distribute\]
```
- Fill in your model configuration correctly in `configs/model_configs.json`
- Modify the `model_config_name` field in `configs/debate_agent_configs.json` accordingly
- Ensure the specified ports are available and IP addresses are accessible
## Setup and Execution
### Step 1: Setup Pro and Con agent servers
For an LLM-based Pro:
```shell
cd examples/distributed_debate
python distributed_debate.py --role pro --pro-host localhost --pro-port 12011
```
(Alternatively) for human participation as Pro:
```shell
python distributed_debate.py --role pro --pro-host localhost --pro-port 12011 --is-human
```
For an LLM-based Con:
```shell
python distributed_debate.py --role con --con-host localhost --con-port 12012
```
(Alternatively) for human participation as Con:
```shell
python distributed_debate.py --role con --con-host localhost --con-port 12012 --is-human
```
### Step 2: Run the main process
```shell
python distributed_debate.py --role main --pro-host localhost --pro-port 12011 --con-host localhost --con-port 12012
```
### Step 3: Watch or join the debate in your terminal
If you join as Con, you'll see something like:
```text
System: Welcome to the debate on whether Artificial General Intelligence (AGI) can be achieved
...
Pro: Thank you. I argue that AGI can be achieved using the GPT model framework.
...
User Input:
```

View File

@@ -0,0 +1,29 @@
[
{
"class": "DialogAgent",
"args": {
"name": "Pro",
"sys_prompt": "Assume the role of a debater who is arguing in favor of the proposition that AGI (Artificial General Intelligence) can be achieved using the GPT model framework. Construct a coherent and persuasive argument, including scientific, technological, and theoretical evidence, to support the statement that GPT models are a viable path to AGI. Highlight the advancements in language understanding, adaptability, and scalability of GPT models as key factors in progressing towards AGI.",
"model_config_name": "gpt-3.5-turbo",
"use_memory": true
}
},
{
"class": "DialogAgent",
"args": {
"name": "Con",
"sys_prompt": "Assume the role of a debater who is arguing against the proposition that AGI can be achieved using the GPT model framework. Construct a coherent and persuasive argument, including scientific, technological, and theoretical evidence, to support the statement that GPT models, while impressive, are insufficient for reaching AGI. Discuss the limitations of GPT models such as lack of understanding, consciousness, ethical reasoning, and general problem-solving abilities that are essential for true AGI.",
"model_config_name": "gpt-3.5-turbo",
"use_memory": true
}
},
{
"class": "DialogAgent",
"args": {
"name": "Judge",
"sys_prompt": "Assume the role of an impartial judge in a debate where the affirmative side argues that AGI can be achieved using the GPT model framework, and the negative side contests this. Listen to both sides' arguments and provide an analytical judgment on which side presented a more compelling and reasonable case. Consider the strength of the evidence, the persuasiveness of the reasoning, and the overall coherence of the arguments presented by each side.",
"model_config_name": "gpt-3.5-turbo",
"use_memory": true
}
}
]

View File

@@ -0,0 +1,21 @@
[
{
"config_name": "gpt-4",
"model_type": "openai_chat",
"model_name": "gpt-4",
"api_key": "xxx",
"organization": "xxx",
"generate_args": {
"temperature": 0.5
}
},
{
"config_name": "qwen",
"model_type": "dashscope_chat",
"model_name": "qwen-max",
"api_key": "xxx",
"generate_args": {
"temperature": 0.5
}
}
]

View File

@@ -0,0 +1,129 @@
# -*- coding: utf-8 -*-
""" An example of distributed debate """
import argparse
from user_proxy_agent import UserProxyAgent
from loguru import logger
import agentscope
from agentscope.agents import DialogAgent
from agentscope.msghub import msghub
from agentscope.server import RpcAgentServerLauncher
from agentscope.message import Msg
FIRST_ROUND = """
Welcome to the debate on whether Artificial General Intelligence (AGI) can be achieved using the GPT model framework. This debate will consist of three rounds. In each round, the affirmative side will present their argument first, followed by the negative side. After both sides have presented, the adjudicator will summarize the key points and analyze the strengths of the arguments.
The rules are as follows:
Each side must present clear, concise arguments backed by evidence and logical reasoning.
No side may interrupt the other while they are presenting their case.
After both sides have presented, the adjudicator will have time to deliberate and will then provide a summary, highlighting the most persuasive points from both sides.
The adjudicator's summary will not declare a winner for the individual rounds but will focus on the quality and persuasiveness of the arguments.
At the conclusion of the three rounds, the adjudicator will declare the overall winner based on which side won two out of the three rounds, considering the consistency and strength of the arguments throughout the debate.
Let us begin the first round. The affirmative side: please present your argument for why AGI can be achieved using the GPT model framework.
""" # noqa
SECOND_ROUND = """
Let us begin the second round. It's your turn, the affirmative side.
"""
THIRD_ROUND = """
Next is the final round.
"""
END = """
Judge, please declare the overall winner now.
"""
def parse_args() -> argparse.Namespace:
"""Parse arguments"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--role",
choices=["pro", "con", "main"],
default="main",
)
parser.add_argument("--is-human", action="store_true")
parser.add_argument("--pro-host", type=str, default="localhost")
parser.add_argument(
"--pro-port",
type=int,
default=12011,
)
parser.add_argument("--con-host", type=str, default="localhost")
parser.add_argument(
"--con-port",
type=int,
default=12012,
)
parser.add_argument("--judge-host", type=str, default="localhost")
parser.add_argument(
"--judge-port",
type=int,
default=12013,
)
return parser.parse_args()
def setup_server(parsed_args: argparse.Namespace) -> None:
"""Setup rpc server for participant agent"""
agentscope.init(
model_configs="configs/model_configs.json",
project="Distributed Conversation",
)
host = getattr(parsed_args, f"{parsed_args.role}_host")
port = getattr(parsed_args, f"{parsed_args.role}_port")
server_launcher = RpcAgentServerLauncher(
host=host,
port=port,
custom_agents=[UserProxyAgent, DialogAgent],
)
server_launcher.launch(in_subprocess=False)
server_launcher.wait_until_terminate()
def run_main_process(parsed_args: argparse.Namespace) -> None:
"""Setup the main debate competition process"""
pro_agent, con_agent, judge_agent = agentscope.init(
model_configs="configs/model_configs.json",
agent_configs="configs/debate_agent_configs.json",
project="Distributed Conversation",
)
pro_agent = pro_agent.to_dist(
host=parsed_args.pro_host,
port=parsed_args.pro_port,
)
con_agent = con_agent.to_dist(
host=parsed_args.con_host,
port=parsed_args.con_port,
)
participants = [pro_agent, con_agent, judge_agent]
announcements = [
Msg(name="system", content=FIRST_ROUND, role="system"),
Msg(name="system", content=SECOND_ROUND, role="system"),
Msg(name="system", content=THIRD_ROUND, role="system"),
]
end = Msg(name="system", content=END, role="system")
with msghub(participants=participants) as hub:
for i in range(3):
hub.broadcast(announcements[i])
pro_resp = pro_agent()
logger.chat(pro_resp)
con_resp = con_agent()
logger.chat(con_resp)
judge_agent()
hub.broadcast(end)
judge_agent()
if __name__ == "__main__":
args = parse_args()
if args.role == "main":
run_main_process(args)
else:
setup_server(args)

View File

@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
"""User Proxy Agent class for distributed usage"""
from typing import Sequence, Union
from typing import Optional
from agentscope.agents import UserAgent
from agentscope.message import Msg
class UserProxyAgent(UserAgent):
"""User proxy agent class"""
def reply( # type: ignore[override]
self,
x: Optional[Union[Msg, Sequence[Msg]]] = None,
required_keys: Optional[Union[list[str], str]] = None,
) -> Msg:
"""
Reply with `self.speak(x)`
"""
if x is not None:
self.speak(x)
return super().reply(x, required_keys)
def observe(self, x: Union[dict, Sequence[dict]]) -> None:
"""
Observe with `self.speak(x)`
"""
if x is not None:
self.speak(x) # type: ignore[arg-type]
self.memory.add(x)