基础学习链接:【多Agent】AgentScope学习指南
速通攻略:零基础做个多智能体游戏
Agentscope入门文档:AgentScope 初探
应用开发进阶:AgentScope应用开发入门
AgentScope官方文档:AgentScope官方文档
AgentScope开源仓库(觉得不错,Star一下):AgentScope
视频课程合集:从零开始学多智能体应用开发
Multi-Agent分享:Multi-Agent最新开源进展分享
AgentScope框架
安装AgentScope
为了安装AgentScope,您需要安装Python 3.9或更高版本。
1、创建虚拟环境
(1)使用Conda
# 使用Python 3.9创建一个名为"agentscope"的新虚拟环境
conda create -n agentscope python=3.9
# 激活虚拟环境
conda activate agentscope
(2)使用Virtualenv
先安装Virtualenv,然后再创建一个虚拟环境
# 如果尚未安装virtualenv,请先安装它
pip install virtualenv
# 使用Python 3.9创建一个名为"agentscope"的新虚拟环境
virtualenv agentscope --python=python3.9
# 激活虚拟环境
source agentscope/bin/activate # 在Windows上使用`agentscope\Scripts\activate`
2、安装AgentScope
(1) 源码安装【强烈建议!!!】
# 从GitHub上拉取AgentScope的源代码
git clone https://github.com/modelscope/agentscope.git
cd agentscope
# 针对本地化的multi-agent应用
pip install -e .
# 为分布式multi-agent应用
pip install -e .[distribute] # 在Mac上使用`pip install -e .\[distribute\]`
注意:[distribute]
选项安装了分布式应用程序所需的额外依赖项。在运行这些命令之前,请激活您的虚拟环境。
(2)pip安装
# 针对本地化的multi-agent应用
pip install agentscope --pre
# 为分布式multi-agent应用
pip install agentscope[distribute] --pre # 在Mac上使用`pip install agentscope\[distribute\] --pre`
配置model_config
1、基础参数
(1)model_type
对应ModelWrapper
的类型,不同model_type
需要填的model_config
内容不同
举例:如果model_type
为openai
,那么它内部会找到agentscope\src\agentscope\models\openai_model.py
文件里面的class
(2)config_name
相当于自己定义的一个配置的名字,后面设置Agent的model_config_name
会与model进行绑定
2、通过model_configs.json初始化AgentScope
model_configs.json文件
[
{
"model_type": "dashscope_chat",
"config_name": "qwen",
"model_name": "qwen-72b-chat",
"api_key": "sk-",
"generate_args": {
"temperature": 0.5
}
},
{
"model_type": "openai",
"config_name": "deepseek",
"model_name": "deepseek-chat",
"api_key": "sk-",
"generate_args": {
"temperature": 0.5
},
"client_args": {
"base_url": "https://api.deepseek.com/v1"
}
},
{
"model_type": "openai",
"config_name": "gpt-3.5",
"model_name": "gpt-3.5-turbo-0125",
"api_key": "sk-",
"generate_args": {
"temperature": 0.5
}
}
]
import agentscope
# 初始化了多个大模型
agentscope.init(
model_configs="./model_configs.json"
)
配置agent_config
1、直接定义一个对话Agent(DialogAgent)
from agentscope.agents import DialogAgent
import agentscope
# 初始化了多个大模型
agentscope.init(
model_configs="./model_configs.json"
)
# 使用qwen大模型初始化一个对话agent
dialog_agent_qwen = DialogAgent(
name="Assistant_qwen",
sys_prompt="You're a helpful assistant.", # sys_prompt可以自行定义,不能为空
model_config_name="qwen", # 这里的qwen和前面的model_config.json文件中的config_name要一一对应,否则Agent找不到大模型会报错
)
# 使用deepseek大模型初始化一个对话agent
dialog_agent_deepseek = DialogAgent(
name="Assistant_deepseek",
sys_prompt="You're a helpful assistant.",
model_config_name="deepseek",
)
# 使用gpt-3.5大模型初始化一个对话agent
dialog_agent_gpt = DialogAgent(
name="Assistant_deepseek",
sys_prompt="You're a helpful assistant.",
model_config_name="gpt-3.5",
)
# 简单实用 可以当作一个api
from agentscope.message import Msg
msg = Msg(name = "贾维斯",content = "你好")
dialog_agent_deepseek (msg)
dialog_agent_gpt(msg)
2、通过配置文件创建多个Agent
agent_configs.json
文件
[
{
"class": "DialogAgent",
"args": {
"name": "Assistant_qwen",
"sys_prompt": "You're a helpful assistant.",
"model_config_name": "qwen",
"use_memory": true
}
},
{
"class": "DialogAgent",
"args": {
"name": "Assistant_deepseek",
"sys_prompt": "You're a helpful assistant.",
"model_config_name": "deepseek",
"use_memory": true
}
},
{
"class": "DialogAgent",
"args": {
"name": "Assistant_gpt",
"sys_prompt": "You're a helpful assistant.",
"model_config_name": "gpt-3.5",
"use_memory": true
}
}
]
import agentscope
# 初始化了多个大模型和多个Agent
agents = agentscope.init(
model_configs="./model_configs.json", # 前面创建的model_configs.json文件
agent_configs="./agent_configs.json",
)
# 对应配置文件agent_configs.json中的顺序
dialog_agent_qwen = agents[0]
dialog_agent_deepseek = agents[1]
dialog_agent_gpt = agents[2]
Agent之间相互对话
1、Pipeline 方式(按顺序一个接着一个讨论)
举例:成语接龙
[
{
"class": "DialogAgent",
"args": {
"name": "player_qwen",
"sys_prompt": "作为成语接龙的参与者,您的任务是根据前一个玩家提供的成语的最后一个字来提出一个新的成语。限制:你仅需说出一个四字中文成语,其他的话不需要说;请确保您的成语是四字成语。",
"model_config_name": "qwen",
"use_memory": true
}
},
{
"class": "DialogAgent",
"args": {
"name": "player_deepseek",
"sys_prompt": "作为成语接龙的参与者,您的任务是根据前一个玩家提供的成语的最后一个字来提出一个新的成语。限制:你仅需说出一个四字中文成语,其他的话不需要说;请确保您的成语是四字成语。",
"model_config_name": "deepseek",
"use_memory": true
}
},
{
"class": "DialogAgent",
"args": {
"name": "player_gpt",
"sys_prompt": "作为成语接龙的参与者,您的任务是根据前一个玩家提供的成语的最后一个字来提出一个新的成语。限制:你仅需说出一个四字中文成语,其他的话不需要说;请确保您的成语是四字成语。",
"model_config_name": "gpt-3.5",
"use_memory": true
}
}
]
import agentscope
from agentscope.pipelines import SequentialPipeline
from agentscope.message import Msg
# 初始化了多个大模型和多个Agent
agents = agentscope.init(
model_configs="./model_configs.json", # 前面创建的model_configs.json文件
agent_configs="./agent_configs.json",
)
# 对应配置文件agent_configs.json中的顺序
player_agent_qwen = agents[0]
player_agent_deepseek = agents[1]
player_agent_gpt = agents[2]
# 创建一个SequentialPipeline
pipe = SequentialPipeline([player_agent_qwen,
player_agent_deepseek,
player_agent_gpt])
msg = Msg("Moderator", "游戏规则:请回答一个四字中文成语,不要添加其他文字。请按规则接龙。本轮成语接龙起始词为「水落石出」")
# 启动Pipeline
pipe(msg)
结果展现:
2、MsgHub 方式(群聊讨论)
举例:座谈会
#初始化四个Agent
import agentscope
from agentscope.pipelines import SequentialPipeline
from agentscope.message import Msg
agent_configs = [
{
"class": "DialogAgent",
"args": {
"name": "agent1",
"sys_prompt": "你是一个得力帮手,你是agent1。",
"model_config_name": "qwen",
"use_memory": True
}
},
{
"class": "DialogAgent",
"args": {
"name": "agent2",
"sys_prompt": "你是一个得力帮手,你是agent2。",
"model_config_name": "qwen",
"use_memory": True
}
},
{
"class": "DialogAgent",
"args": {
"name": "agent3",
"sys_prompt": "你是一个得力帮手,你是agent3。",
"model_config_name": "qwen",
"use_memory": True
}
},
{
"class": "DialogAgent",
"args": {
"name": "agent4",
"sys_prompt": "你是一个得力帮手,你是agent4。",
"model_config_name": "qwen",
"use_memory": True
}
},
{
"class": "DialogAgent",
"args": {
"name": "HostAgent",
"sys_prompt": "你是一个主持agent,需要做一个决策。除了决策外不要输出任何东西。",
"model_config_name": "qwen",
"use_memory": True
}
}
]
# 初始化了多个大模型和多个Agent
agents = agentscope.init(
model_configs="./model_configs.json", # 前面创建的model_configs.json文件
agent_configs=agent_configs,
)
agent1 = agents[0]
agent2 = agents[1]
agent3 = agents[2]
agent4 = agents[3]
#四类通信方式
from agentscope.message import Msg
msg = Msg(name = "贾维斯",content = "请带上自己的名字进行传话,传话内容“你好”")
# 顺序执行
msg = agent1(msg)
print(msg)
msg = agent2(msg)
print(msg)
msg = agent3(msg)
print(msg)
msg = agent4(msg)
print(msg)
##{……'name': 'agent1','content': '我是来自阿里云的超大规模语言模型,我的名字是通义千问。现在帮您传话:“你好”'}
##{……'name': 'agent2','content': '您好!我是通义千问,来自阿里云的超大规模语言模型,很高兴为您服务,并向您问候:“你好”。'}
##{……'name': 'agent3','content': '您好!作为阿里云的超大规模语言模型通义千问,我非常荣幸能与您交流并提供服务。同样向您致以亲切的问候:“你好”!'}
##{…… 'name': 'agent4', 'content': '您好!非常高兴能与您交流互动。作为阿里云的超大规模语言模型通义千问,我的确具备强大的自然语言处理能力,可以为您提供广泛的知识问答、技术咨询、场景应用等各类服务。请随时告诉我您需要了解的内容或遇到的问题,我会全力以赴为您提供帮助!'}
# loop情况
msg = Msg("system","选择是否退出")
msg1 = Msg("system","none")
def ifBreak(msg):
return "退出" not in msg.content
while ifBreak(msg1):
msg1 = hostAgent(msg)
print(msg1.content)
msg = agent1(msg)
print(msg.content)
msg = agent2(msg)
print(msg.content)
# 选择情况
msg = hostAgent(Msg("system","agent1 和 agent2 应该选择谁退出?我建议 agent1 退出。"))
print(msg)
if "agent1" in msg.content :
msg = agent1(msg)
else:
msg = agent2(msg)
print(msg)
msg_send = Msg("system","重新选择退出或继续运行,你只需要回答肯定的指令")
def ifBreak(msg):
return ("退出" or "结束") not in msg.content
# 复杂形势
msg = hostAgent(msg_send)
print(msg)
while ifBreak(msg):
msg1 = hostAgent(Msg("system","agent1 和 agent2 应该选择谁发言?,你只需要回答是谁的名字"))
msg2 = Msg("system","请发言,发言后请告诉主持人退出")
if "agent1" in msg1.content :
msg2 = agent1(msg2)
else:
msg2 = agent2(msg2)
print(msg2)
msg = hostAgent(msg_send)
print(msg)
hahaha都看到这里了,要是觉得有用的话那就辛苦动动小手点个赞吧!
标签:name,AI,Agent,AgentScope,agent,msg,model,agentscope,configs From: https://blog.csdn.net/qq_74869852/article/details/140939723