AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


聊聊AutoGen多Agent框架
发布日期:2024-08-27 07:37:18 浏览次数: 1646


概述

AutoGen是一个开源编程框架,用于构建AI Agent和促进 多个Agent之间的合作解决任务。AutoGen的目标是提供一个易于使用的 加快人工智能开发研究的灵活框架; 比如用于深度学习的PyTorch。它提供了可以交谈的Agent等功能 与其他Agent,LLM和Tools 支持,自主和人在循环工作流程, 以及多Agent对话模式。

参考:

官方文档

Multi-Agent开发框架AutoGen

功能点

ReAct

ReAct = Reasoning(协同推理) + Acting(行动)

提供完备的提示词模板,支持消息函数。示例:

# NOTE: this ReAct prompt is adapted from Langchain's ReAct agent: https://github.com/langchain-ai/langchain/blob/master/libs/langchain/langchain/agents/react/agent.py#L79
ReAct_prompt = """
Answer the following questions as best you can. You have access to tools provided.

Use the following format:

Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take
Action Input: the input to the action
Observation: the result of the action
... (this process can repeat multiple times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question

Begin!
Question: {input}
"
""

# Define the ReAct prompt message. Assuming a "question" field is present in the context


def react_prompt_message(sender, recipient, context):
    return ReAct_prompt.format(input=context["question"])

RAG

对于自我认知的场景下或是一些专业知识解释的场景下,可以直接使用RAG+Agent实现。示例:

assistant = RetrieveAssistantAgent(
    name="assistant",
    system_message="You are a helpful assistant.",
    llm_config={
        "timeout"600,
        "cache_seed"42,
        "config_list": config_list,
    },
)
ragproxyagent = RetrieveUserProxyAgent(
    name="ragproxyagent",
    human_input_mode="NEVER",
    max_consecutive_auto_reply=3,
    retrieve_config={
        "task""code",
        "docs_path": [
            "https://raw.githubusercontent.com/microsoft/FLAML/main/website/docs/Examples/Integrate%20-%20Spark.md",
            "https://raw.githubusercontent.com/microsoft/FLAML/main/website/docs/Research.md",
            os.path.join(os.path.abspath(""), "..""website""docs"),
        ],
        "custom_text_types": ["mdx"],
        "chunk_token_size"2000,
        "model": config_list[0]["model"],
        "client": chromadb.PersistentClient(path="/tmp/chromadb"),
        "embedding_model""all-mpnet-base-v2",
        "get_or_create": True,  # set to False if you don't want to reuse an existing collection, but you'll need to remove the collection manually
    },
    code_execution_config=False,  # set to False if you don't want to execute the code
)

Termination

根据指定的条件终止agent。示例:

  1. 通过初始化聊天时设置参数:在启动对话时(使用 **initiate_chat** 方法),可以设定特定的参数来定义何时结束对话。例如,通过设置 **max_turns** 参数来限制对话的回合数,这样对话在达到指定回合后自动终止。
  2. 通过配置代理触发终止:在定义每个代理时,可以指定让代理根据特定条件自动终止对话。这包括设置如 **max_consecutive_auto_reply**(连续自动回复的最大次数)和 **is_termination_msg**(是否接收到特定的终止消息)等参数。

Human Feedback

入人类干预的主要方式是通过设置 **ConversableAgent****human_input_mode** 参数。这个参数支持三种模式:**NEVER**(从不请求人类输入)、**TERMINATE**(仅在满足终止条件时请求人类输入)、和 **ALWAYS**(总是请求人类输入)。这允许代理在需要时包含人类反馈,例如,通过人类输入来引导代理或校正其行为。

Tools

"Tool Use"指的是在多代理对话中,代理能够使用各种工具来执行特定的任务。这些工具可以是函数、代码执行器,或者其他可以通过 AutoGen 注册并调用的资源。代理可以通过这些工具来增强它们的功能性,比如进行信息检索、执行计算任务或者其他需要特定能力的操作。

Conversation Patterns

对话模式:双Agent对话、顺序Agent对话、群聊对话。

双Agent聊天:最简单的对话模式 两个Agent互相聊天。

顺序聊天:两个Agent之间的聊天序列,链接在一起 一起通过一种结转机制,从而带来总结 上一个聊天到下一个聊天的上下文。summary_method 参数指定结转逻辑。 群聊:多Agent聊天对话。下一个Agent的选择方式:round_robin、random、manual(人为选择)、auto。

Nested Chat

AutoGen允许在一个群聊中,调用另外一个Agent群聊来执行对话(嵌套对话Nested Chats)。这样做可以把一个群聊封装成单一的Agent,从而实现更加复杂的工作流。

具体实现方法是先利用上面的方法定义一个群聊,然后将这个群聊在另外一个群聊中,注册成为nested chat,并且设定一个trigger的表达式,当trigger表达式为"真"时,就会调用nested chat包装好的群聊来解决问题,将结果返回。

参考示例:

Solving Complex Tasks with Nested Chats[1]

LLM Reflection[2]

问题

  1. tools的调用,是如何识别的?——可观测性与Nested Chat。
  2. 按Agent类别区分的话(虽然只是参数设置不同),RAG Agent可以独立于普通的Agent,如何调用该Agent?并衔接其它Agent?AutoGen又是如何识别到User消息并基于RAG回复,而又切换为正常不走RAG的回复?——Nested Chat

思考与总结

AutoGen框架的agent实现非常灵活,功能强大,几乎是可以覆盖90%上的业务场景。而且通过推理部署框架的接入,隔离了基座LLM的差异性。相比较其他的agent框架,不管是在易用性还是文档、功能上都要更好更强。


53AI,企业落地应用大模型首选服务商

产品:大模型应用平台+智能体定制开发+落地咨询服务

承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询