forked from modelscope/agentscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distributed_dialog.py
83 lines (73 loc) · 2.24 KB
/
distributed_dialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
""" An example of distributed dialog """
import argparse
from loguru import logger
import agentscope
from agentscope.agents.user_agent import UserAgent
from agentscope.agents.dialog_agent import DialogAgent
from agentscope.server import RpcAgentServerLauncher
def parse_args() -> argparse.Namespace:
"""Parse arguments"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--role",
choices=["assistant", "user"],
default="user",
)
parser.add_argument(
"--assistant-port",
type=int,
default=12010,
)
parser.add_argument(
"--assistant-host",
type=str,
default="localhost",
)
return parser.parse_args()
def setup_assistant_server(assistant_host: str, assistant_port: int) -> None:
"""Set up assistant rpc server"""
agentscope.init(
model_configs="configs/model_configs.json",
project="Distributed Conversation",
)
assistant_server_launcher = RpcAgentServerLauncher(
host=assistant_host,
port=assistant_port,
)
assistant_server_launcher.launch()
assistant_server_launcher.wait_until_terminate()
def run_main_process(assistant_host: str, assistant_port: int) -> None:
"""Run dialog main process"""
agentscope.init(
model_configs="configs/model_configs.json",
project="Distributed Conversation",
)
assistant_agent = DialogAgent(
name="Assistant",
sys_prompt="You are a helpful assistant.",
model_config_name="qwen",
use_memory=True,
).to_dist(
host=assistant_host,
port=assistant_port,
)
user_agent = UserAgent(
name="User",
require_url=False,
)
logger.info(
"Setup successfully, have fun chatting! (enter 'exit' to close the "
"agent)",
)
msg = user_agent()
while not msg.content.endswith("exit"):
msg = assistant_agent(msg)
logger.chat(msg)
msg = user_agent(msg)
if __name__ == "__main__":
args = parse_args()
if args.role == "assistant":
setup_assistant_server(args.assistant_host, args.assistant_port)
elif args.role == "user":
run_main_process(args.assistant_host, args.assistant_port)