3.2 创建你的第一个Agent Society 3.2.1 准备工作 在 CAMEL 中主要是多智能体的实现主要是通过角色扮演 的方式,让智能体扮演特定的角色,并拥有相应角色的专业知识背景。这些智能体通过对话和合作来共同完成任务。在多智能体系统接收到人类用户的初步想法和角色分配后,任务指定智能体将提供详细的描述,使想法更加具体化。然后,AI助理和AI用户将通过多轮对话合作完成指定的任务,直到AI用户确定任务完成为止。一方面,AI用户负责向AI助理提供指令,并引导对话朝着任务完成的方向进行;另一方面,AI助理则需要遵循AI用户的指示,做出回答并提供具体的解决方案。 面向任务的 类。我们以指令跟随的方式设计这个类。其本质是,要解决复杂任务,可以让两个交流智能体一步一步地协作,共同寻找解决方案。
在 CAMEL 中主要是多智能体的实现主要是通过角色扮演Role-Playing的方式,让智能体扮演特定的角色,并拥有相应角色的专业知识背景。这些智能体通过对话和合作来共同完成任务。在多智能体系统接收到人类用户的初步想法和角色分配后,任务指定智能体将提供详细的描述,使想法更加具体化。然后,AI助理和AI用户将通过多轮对话合作完成指定的任务,直到AI用户确定任务完成为止。一方面,AI用户负责向AI助理提供指令,并引导对话朝着任务完成的方向进行;另一方面,AI助理则需要遵循AI用户的指示,做出回答并提供具体的解决方案。
面向任务的 RolyPlaying() 类。我们以指令跟随的方式设计这个类。其本质是,要解决复杂任务,可以让两个交流智能体一步一步地协作,共同寻找解决方案。主要概念包括:
任务:任务可以简单到一个想法,由初始提示启动。
AI 用户:预期提供指令的智能体。
AI 助手:预期提供满足指令的解决方案的智能体。
以下展示了 RolePlaying 对象的主要参数配置及其默认值和描述:
| 参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| 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输出的语言。 |
下边用一个具体的例子一步步展示我们的RolePlaying 案例。
首先我们先导入相关模块及设置相关参数:
from camel.societies import RolePlaying from camel.types import TaskType, ModelType, ModelPlatformType from camel.models import ModelFactory import 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} }
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机制是利用两个Agent之间的交互来完成任务,为了不让Agent陷入无限循环的输入输出,CAMEL在设计的时候就引入了终止机制,如果意外终止,这个函数可以为我们展现RolePlaying的终止原因:
def is_terminated(response): """ 当会话应该终止时给出对应信息。 """ if response.terminated: role = response.msg.role_type.name reason = response.info['termination_reasons'] print(f'AI {role} 因为 {reason} 而终止') return response.terminated
OK!准备工作都已经完成了,是时候规划我们的路线了——为我们的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: break print(f'[AI 助手] {assistant_response.msg.content}.\n') # 获取下一轮的输入消息 input_msg = assistant_response.msg return None
run(society)

可以看到对于这样一个有趣的任务,我们的AI-Society首先会将我们的初始prompt给进一步明确化:"制定一个计划去过去并进行改变。">>>"设计一台利用量子纠缠和虫洞效应的时间机器,制定详细的时间旅行计划,包括安全返回机制,以确保能回到特定历史时刻并实施微小但关键的改变,从而影响未来。"然后再由AI_User一步步指导AI_Assistant完成整个任务。
进阶学习
引入 CrticAgent,with_critic_in_the_loop 设置为True的时候将会在循环中引入CrticAgent,如果我们将"human"赋值给critic_role_name ,我们人类将可以在与智能体之间交互中掌握主动权,去选择优化和调整,并且提升角色表现。
from camel.societies import RolePlaying from camel.types import TaskType, ModelType, ModelPlatformType from camel.models import ModelFactory import os from colorama import Fore # 设置代理 #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': '写一本关于AI社会的未来的书。', 'with_task_specify': True, 'task_specify_agent_kwargs': {'model': model} } user_role_kwargs = { 'user_role_name': 'AI专家', 'user_agent_kwargs': {'model': model} } assistant_role_kwargs = { 'assistant_role_name': '对AI感兴趣的作家', 'assistant_agent_kwargs': {'model': model} } society = RolePlaying( **task_kwargs, # 任务参数 **user_role_kwargs, # 指令发送者的参数 **assistant_role_kwargs, # 指令接收者的参数 critic_role_name='human', with_critic_in_the_loop=True, output_language="中文", ) def is_terminated(response): """ 当会话应该终止时给出对应信息。 """ if response.terminated: role = response.msg.role_type.name reason = response.info['termination_reasons'] print(f'AI {role} 因为 {reason} 而终止') return response.terminated 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(Fore.GREEN + f'[AI 用户] {user_response.msg.content}.\n') # 检查任务是否结束 if 'CAMEL_TASK_DONE' in user_response.msg.content: break print(Fore.BLUE + f'[AI 助手] {assistant_response.msg.content}.\n') # 获取下一轮的输入消息 input_msg = assistant_response.msg return None run(society)
上述 根据 “写一本关于AI社会的未来的书” 的任务,将会在更详细的选择中引入人工与其交互。

输入数字可以选择你想要让AI_Assistant去执行的选项,如果你选择第四个选项,则将由你亲自指导AI_Assistant,该AI-Society会要求你输入内容来指导AI_Assistant:

但是如果,将"human"以外的参数赋值给critic_role_name,则将创建一个 CriticAgent,自动与其交互。
