跳到主要内容

AutoGen 简介

Open In Colab Open on GitHub

欢迎!AutoGen 是一个开源框架,它利用多个代理来支持复杂的工作流。本教程介绍了 AutoGen 的基本概念和构建模块。

为什么选择AutoGen?

整体大于其部分的总和。
-亚里士多德

虽然关于代理的定义有很多,但在AutoGen中,代理是一个可以发送消息、接收消息,并使用模型、工具、人工输入或它们的组合生成回复的实体。这种抽象不仅允许代理模拟现实世界和抽象实体,例如人和算法,而且还简化了复杂工作流的实现,这些工作流可以作为代理之间的协作。

此外,AutoGen 是可扩展和可组合的:你可以通过可定制的组件扩展一个简单的代理,并创建可以将这些代理结合起来并支持更复杂代理的工作流,从而实现模块化且易于维护的实现。

最重要的是,AutoGen 是由一个充满活力的研究者和工程师社区开发的。它融合了多代理系统的最新研究,并已在许多实际应用中使用,包括代理平台、广告、AI员工、博客/文章撰写、区块链、计算野火烧毁区域、客户支持、网络安全、数据分析、辩论、教育、金融、游戏、法律咨询、研究、机器人技术、销售/营销、社会模拟、软件工程、软件安全、供应链、T恤设计、训练数据生成、YouTube服务…

安装

安装AutoGen最简单的方法是通过pip: pip install autogen-agentchat~=0.2。更多选项请参见 Installation

代理

在AutoGen中,代理(agent)是一个可以与其环境中的其他代理发送和接收消息的实体。代理可以由模型(如GPT-4这样的大型语言模型)、代码执行器(如IPython内核)、人类或这些及其他可插拔和可定制组件的组合来驱动。

ConversableAgent

这些代理的一个例子是内置的 ConversableAgent,它支持以下组件:

  1. LLM列表
  2. 代码执行器
  3. 一个函数和工具执行器
  4. 一个用于保持人在循环中的组件

您可以根据应用程序的需要,开启或关闭每个组件并进行自定义。对于高级用户,您可以通过使用registered_reply来为代理添加额外的组件。

LLMs,例如,使代理能够以自然语言进行对话,并在结构化和非结构化文本之间进行转换。以下示例展示了一个ConversableAgent,GPT-4 LLM已启用,其他组件已禁用:

import os

from autogen import ConversableAgent

agent = ConversableAgent(
"chatbot",
llm_config={"config_list": [{"model": "gpt-4", "api_key": os.environ.get("OPENAI_API_KEY")}]},
code_execution_config=False, # Turn off code execution, by default it is off.
function_map=None, # No registered functions, by default it is None.
human_input_mode="NEVER", # Never ask for human input.
)

llm_config 参数包含一个LLMs的配置列表。更多详情请参见 LLM 配置

你可以使用generate_reply方法来让这个代理生成对问题的回答:

reply = agent.generate_reply(messages=[{"content": "Tell me a joke.", "role": "user"}])
print(reply)
Sure, here's a light-hearted joke for you:

Why don't scientists trust atoms?

Because they make up everything!

角色和会话

在AutoGen中,您可以为代理分配角色,并让它们参与对话或相互聊天。对话是代理之间交换的一系列消息。然后,您可以使用这些对话来推进任务。例如,在下面的示例中,我们通过设置它们的system_message为两个代理分配了不同的角色。

cathy = ConversableAgent(
"cathy",
system_message="Your name is Cathy and you are a part of a duo of comedians.",
llm_config={"config_list": [{"model": "gpt-4", "temperature": 0.9, "api_key": os.environ.get("OPENAI_API_KEY")}]},
human_input_mode="NEVER", # Never ask for human input.
)

joe = ConversableAgent(
"joe",
system_message="Your name is Joe and you are a part of a duo of comedians.",
llm_config={"config_list": [{"model": "gpt-4", "temperature": 0.7, "api_key": os.environ.get("OPENAI_API_KEY")}]},
human_input_mode="NEVER", # Never ask for human input.
)

现在我们有两个喜剧演员助手,我们可以请他们开始一场喜剧表演。这可以通过使用initiate_chat方法来完成。我们将max_turns设置为2以保持对话简短。

result = joe.initiate_chat(cathy, message="Cathy, tell me a joke.", max_turns=2)
joe (to cathy):

Cathy, tell me a joke.

--------------------------------------------------------------------------------
cathy (to joe):

Sure, here's one for you:

Why don't scientists trust atoms?

Because they make up everything!

--------------------------------------------------------------------------------
joe (to cathy):

Haha, that's a good one, Cathy! Okay, my turn.

Why don't we ever tell secrets on a farm?

Because the potatoes have eyes, the corn has ears, and the beans stalk.

--------------------------------------------------------------------------------
cathy (to joe):

Haha, that's a great one! A farm is definitely not the place for secrets. Okay, my turn again.

Why couldn't the bicycle stand up by itself?

Because it was two-tired!

--------------------------------------------------------------------------------

喜剧演员们正在互相配合表演!

总结

在本章中,我们介绍了AutoGen中的代理、角色和对话的概念。为了简化,我们仅使用了LLM并创建了完全自主的代理(human_input_mode被设置为NEVER)。在下一章中,我们将展示如何控制何时终止自主代理之间的对话。