【笔记】Handy Multi-Agent Tutorial 第三章: CAMEL框架简介及实践(实践部分)
正文详见:
Handy Multi-Agent Tutorial - 飞书云文档https://fmhw1n4zpn.feishu.cn/docx/AF4XdOZpIo6TOaxzDK8cxInNnCe
3.2 创建个Agent Society
多智能体在CAMEL中的实现类似于角色扮演,所以他的类的名字也是RolyPlaying()。
RolePlaying机制是利用两个Agent之间的交互来完成任务。
首先你要设置好你的初步的想法、每名角色的任务(每个智能体的功能)。多智能体接收到这些信息后就可以通过“一问一答”的方式慢慢丰富你的想法,然后合理安排任务实现这个想法。
详细的步骤说明见下文。
参数名称 | 类型 | 默认值 | 描述 |
assistant_role_name | str | 无 | 助手智能体所扮演角色的名称(合理的名称设置有利于提高agent的能力)。 |
user_role_name | str | 无 | 用户智能体所扮演角色的名称(合理的名称设置有利于提高agent的能力)。 |
critic_role_name | str, optional | "critic" | 评审者智能体所扮演角色的名称。如果名称为 "human",则评审者将被设置为人类Agent,否则将创建一个 CriticAgent。 |
task_prompt | str, optional | "" | 要执行任务的提示。 |
with_task_specify | bool, optional | True | 是否使用任务明确化Agent。 |
with_task_planner | bool, optional | False | 是否使用任务规划Agent。 |
with_critic_in_the_loop | bool, optional | False | 是否在循环中包含一个评审者。 |
critic_criteria | str, optional | None | 评审者Agent的评审标准。如果没有指定,则设置为提高任务性能的标准。 |
model_type | ModelType, optional | None | 用于角色扮演的模型类型。如果指定,它将覆盖所有Agent中的模型。 |
task_type | TaskType, optional | TaskType.AI_SOCIETY | 要执行的任务类型。 |
output_language | str, optional | None | Agent输出的语言。 |
3.2.2 配置Role-Playing会话
设置参数
- 通过model我们设置好使用的大模型。
- task_kwargs设置了任务提示(task_prompt)、通过with_task_specify初始化了一个任务明确化agent,通过我们的任务提示丰富实现内容。
-
user_role_kwargs和assistant_role_kwargs初始化了两个角色扮演智能体,并赋予了他们一个详细的身份。前者是用户智能体(user),后者是助手智能体(assistant)。
通过user和assistant,我们可以知道,我么需要让物理学家帮助这个时间旅行者指定一个去过去的计划,具体的实现细节则有任务明确化agent产生。
from camel.societies import RolePlaying
from camel.types import TaskType, ModelType, ModelPlatformType
from camel.models import ModelFactoryimport os# 设置代理
#os.environ["http_proxy"] = "http://127.0.0.1:7897"
#os.environ["https_proxy"] = "http://127.0.0.1:7897"model = ModelFactory.create(model_platform=ModelPlatformType.OPENAI_COMPATIBLE_MODEL,model_type="Qwen/Qwen2.5-72B-Instruct",url='https://api-inference.modelscope.cn/v1/',api_key='你的api密钥'
)task_kwargs = {'task_prompt': '制定一个计划去过去并进行改变。','with_task_specify': True,#开启后,将会有一个agent将我们的初始prompt进一步明确化'task_specify_agent_kwargs': {'model': model}
}user_role_kwargs = {'user_role_name': '一个雄心勃勃的渴望成为时间旅行者的人','user_agent_kwargs': {'model': model}
}assistant_role_kwargs = {'assistant_role_name': '最优秀的实验物理学家','assistant_agent_kwargs': {'model': model}
}
组建AI-Society
society = RolePlaying(**task_kwargs, # 任务参数**user_role_kwargs, # 指令发送者的参数**assistant_role_kwargs, # 指令接收者的参数
)
在这里我们可以在日志里观察到CAMEL对每个智能体的system_prompt的设定:
camel.agents.chat_agent - INFO - Model Qwen/Qwen2.5-72B-Instruct, index 0, processed these messages:[{'role': 'system', 'content': 'You can make a task more specific.'}, {'role': 'user', 'content': 'Here is a task that 最优秀的实验物理学家 will help 一个雄心勃勃的渴望成为时间旅行者的人 to complete: 制定一个计划去过去并进行改变。.\nPlease make it more specific. Be creative and imaginative.\nPlease reply with the specified task in 50 words or less. Do not add anything else.'}]
解决任务
定义一个helper 函数,使得多智能体对话拥有终止机制,如果意外终止,这个函数可以为我们展现RolePlaying的终止原因:
def is_terminated(response):"""当会话应该终止时给出对应信息。"""if response.terminated:role = response.msg.role_type.namereason = response.info['termination_reasons']print(f'AI {role} 因为 {reason} 而终止')return response.terminated
为AI-Society编写一个简单的循环来继续前进:
def run(society, round_limit: int=10):# 获取AI助手到AI用户的初始消息input_msg = society.init_chat()# 开始互动会话for _ in range(round_limit):# 获取这一轮的两个响应assistant_response, user_response = society.step(input_msg)# 检查终止条件if is_terminated(assistant_response) or is_terminated(user_response):break# 获取结果print(f'[AI 用户] {user_response.msg.content}.\n')# 检查任务是否结束if 'CAMEL_TASK_DONE' in user_response.msg.content:breakprint(f'[AI 助手] {assistant_response.msg.content}.\n')# 获取下一轮的输入消息input_msg = assistant_response.msgreturn None
run(society)
效果:
进阶学习
接下来我们来研究一下critic_role_name参数。
我们在组建AI-Society加入这些参数。
with_critic_in_the_loop设置为True
的时候将会在循环中引入CrticAgen。
society = RolePlaying(**task_kwargs, # 任务参数**user_role_kwargs, # 指令发送者的参数**assistant_role_kwargs, # 指令接收者的参数critic_role_name='human',with_critic_in_the_loop=True,output_language="中文",
)
当critic_role_name赋值为human时,我们可以亲自动手去调整和优化智能体的对话。
如果,将"human"以外的参数赋值给critic_role_name
,则将创建一个 CriticAgent,自动与其交互。
3.3 创建你的Workforce
报错太多,暂时没有解决,等解决的再补充