社会#

1. 概念#

society模块是CAMEL的核心模块之一。通过模拟信息交换过程,该模块研究智能体之间的社会行为。

目前,社会模块的目标是使代理能够自主协作完成任务,同时保持与人类意图一致,并需要最少的人为干预。它包括两个框架:RolePlayingBabyAGI,用于运行代理的交互行为以实现目标。

RolePlaying为例,该框架采用指令跟随的方式设计。这些角色分别独立承担执行和规划任务的职责。对话通过轮转机制持续推进,从而协作完成任务。主要概念包括:

  • 任务:一个任务可以简单到一个想法,由初始提示初始化。

  • AI用户:预期提供指令的代理。

  • AI助手:该代理预期会提供满足指令要求的解决方案。

2. 类型#

2.1 RolePlaying#

RolePlaying 是CAMEL独有的协作智能体框架。通过该框架,CAMEL中的智能体克服了诸多挑战,例如角色反转助手重复指令敷衍回复消息无限循环以及对话终止条件

在使用CAMEL中的RolePlaying框架时,会使用预定义的提示为不同智能体创建独特的初始设置。例如,如果用户想要初始化一个助手智能体,该智能体将使用以下提示进行初始化。

  • 永远记住你是一个而我是

    这将为助手智能体分配所选角色,并提供有关用户角色的信息。

  • 永远不要互换角色!不要对我发号施令!

    这可以防止智能体互换角色。在某些情况下,我们观察到助手和用户会互换角色,助手突然掌控局面并开始指挥用户,而用户则遵循这些指令。

  • 如果由于身体、道德、法律原因或您的能力所限无法执行指令,您必须诚实地拒绝我的指示并说明原因。

    这禁止代理产生有害、虚假、非法和误导性信息。

  • 除非我明确说明任务已完成,否则你始终应该以以下格式开始回答:解决方案:<你的解决方案>。<你的解决方案>应当具体明确,最好能提供实现方法和示例来解决问题。

    这能确保助手始终以一致的格式进行回复,避免偏离对话结构,防止出现模糊或不完整的回应(我们称之为敷衍式回应),例如"我会做某事"这类回答。

  • 始终以以下内容结束您的解决方案:下一个请求。

    这确保助手通过请求新的解决指令来保持对话继续。

RolePlaying 属性#

属性

类型

描述

assistant_role_name

str

助手扮演的角色的名称。

user_role_name

str

用户所扮演角色的名称。

critic_role_name

str

评论者所扮演角色的名称。

task_prompt

str

要执行的任务提示。

with_task_specify

bool

是否使用任务指定代理。

with_task_planner

bool

是否使用任务规划器代理。

with_critic_in_the_loop

bool

是否在循环中包含一个评论者。

critic_criteria

str

critic代理的评判标准。

model

BaseModelBackend

用于生成响应的模型后端。

task_type

TaskType

要执行的任务类型。

assistant_agent_kwargs

Dict

传递给助手代理的额外参数。

user_agent_kwargs

Dict

传递给用户代理的额外参数。

task_specify_agent_kwargs

Dict

传递给任务指定代理的额外参数。

task_planner_agent_kwargs

Dict

传递给任务规划代理的额外参数。

critic_kwargs

Dict

传递给critic的额外参数。

sys_msg_generator_kwargs

Dict

传递给系统消息生成器的额外参数。

extend_sys_msg_meta_dicts

List[Dict]

用于扩展系统消息元数据字典的字典列表。

extend_task_specify_meta_dict

Dict

用于扩展任务指定元数据字典的字典。

output_language

str

代理输出的语言。

2.2 BabyAGI#

Babyagi 是源自“任务驱动自主代理”框架 yoheinakajima/babyagi

3. 开始使用#

3.1. 使用 RolePlaying#

from colorama import Fore

from camel.societies import RolePlaying
from camel.utils import print_text_animated

def main(model=None, chat_turn_limit=50) -> None:
# Initial the role-playing session on developing a trading bot task with default model (`GPT_4O_MINI`)
    task_prompt = "Develop a trading bot for the stock market"
    role_play_session = RolePlaying(
        assistant_role_name="Python Programmer",
        assistant_agent_kwargs=dict(model=model),
        user_role_name="Stock Trader",
        user_agent_kwargs=dict(model=model),
        task_prompt=task_prompt,
        with_task_specify=True,
        task_specify_agent_kwargs=dict(model=model),
    )

# Output initial message with different colors.
    print(
        Fore.GREEN
        + f"AI Assistant sys message:\n{role_play_session.assistant_sys_msg}\n"
    )
    print(
        Fore.BLUE + f"AI User sys message:\n{role_play_session.user_sys_msg}\n"
    )

    print(Fore.YELLOW + f"Original task prompt:\n{task_prompt}\n")
    print(
        Fore.CYAN
        + "Specified task prompt:"
        + f"\n{role_play_session.specified_task_prompt}\n"
    )
    print(Fore.RED + f"Final task prompt:\n{role_play_session.task_prompt}\n")

    n = 0
    input_msg = role_play_session.init_chat()

# Output response step by step with different colors.
# Keep output until detect the terminate content or reach the loop limit.
    while n < chat_turn_limit:
        n += 1
        assistant_response, user_response = role_play_session.step(input_msg)

        if assistant_response.terminated:
            print(
                Fore.GREEN
                + (
                    "AI Assistant terminated. Reason: "
                    f"{assistant_response.info['termination_reasons']}."
                )
            )
            break
        if user_response.terminated:
            print(
                Fore.GREEN
                + (
                    "AI User terminated. "
                    f"Reason: {user_response.info['termination_reasons']}."
                )
            )
            break

        print_text_animated(
            Fore.BLUE + f"AI User:\n\n{user_response.msg.content}\n"
        )
        print_text_animated(
            Fore.GREEN + "AI Assistant:\n\n"
            f"{assistant_response.msg.content}\n"
        )

        if "CAMEL_TASK_DONE" in user_response.msg.content:
            break

        input_msg = assistant_response.msg

if __name__ == "__main__":
    main()