在过去的一年中,大模型领域呈现出了迅猛的发展态势,相关文献数量庞大。这给希望了解该领域的同学带来困扰:直接深入研读众多论文比较耗费精力,而综述文章则可能缺乏必要的细节。在此背景下,希望在自我学习的过程中能够总结出一些核心要点,与大家共同学习。鉴于个人的精力和能力均有限,如有错误或疏漏,还望不吝赐教。文中大量观点和图表都出自最后的参考文献,因内容较多,不在原文一一标注出来了。

1. 背景

1.1. 大模型基础能力

1.1.1. 语言生成

语言生成有三个典型的任务:

  1. 语言模型任务(Language Modeling). 根据之前提供的tokens预测下个token,输入法提示是其典型应用。该任务没有对输出进行约束,因此是无条件的文本生成。
img
  1. 条件文本生成(Conditional Text Generation). 基于给定条件生成满足特定任务要求的文本,典型任务包括机器翻译、文本摘要和问答等。
img
  1. 代码合成(Code Synthesis). 代码合成是一种特殊的文本生成,根据特定要求生成编程语言。
img

1.1.2. 知识利用

知识利用是智能系统完成知识密集型任务的重要能力,这依赖于支持的事实证据。具体而言,它要求大模型(LLMs)正确利用来自预训练语料库的丰富事实知识,或在必要时检索外部数据。常见的知识利用的任务有问答和知识补全(抽取三元组等)。

img

1.1.3. 复杂推理

复杂推理指的是理解和使用支持性证据或逻辑来得出结论或做出决策的能力。可分为以下几种类型:

  1. 知识推理(Knowledge Reasoning). 知识推理任务依赖于逻辑关系和关于事实知识的证据来回答给定的问题。
img
  1. 符号推理(Symbolic Reasoning). 符号推理任务主要集中在在形式规则设置中操作符号以实现某些特定目标上,其中操作和规则可能是LLMs在预训练期间从未见过的。
img
  1. 数学推理(Mathematical Reasoning). 数学推理任务需要综合运用数学知识、逻辑和计算来解决问题或生成证明语句。现有的数学推理任务主要可以分为数学问题求解和自动定理证明两大类。
img

1.2. 大模型能力钥匙Prompt

1.2.1. Prompt基础

提示(prompting)是利用大模型(LLMs)解决各种任务的主要方法,提示的质量将在很大程度上影响LLMs在特定任务中的表现。一个好的提示一般包含下面几个关键要素:

  • 任务描述. 任务描述通常是期望LLMs遵循的具体指令。通常,应当用自然语言清晰描述任务目标。
  • 输入数据. 要让大模型处理的数据,一般用自然语言描述,也可以采用一些结构化的数据描述。
  • 上下文信息. 典型的上下文信息是用户和Chatbot的历史对话,有了这些信息大模型才能了解用户意图。
  • 提示风格. 对于不同的LLMs,设计一个适合激发它们解决特定任务能力的提示风格是非常重要的。总体上,应该将提示表达为清晰的问题或可以被很好理解和回答的详细指令。例如使用前缀“你是这个任务(或这个领域)的专家”,可以提高LLMs在某些特定任务中的表现。

有很多资料可以帮助你设计一个好的prompt,例如Prompt Engineering(吴恩达)

1.2.2. 上下文学习

上下文学习(In-Context Learning, ICL),又被称为情景学习,是GPT-3引入的一个非常重要的特性。简单来讲,ICL就是在prompt中增加示例,能让大模型更好理解任务并从中学习,对效果有明显增益。示例选取需要具备相关性和多样性。

img

例如在短标题生成任务中,下面是引入ICL的prompt:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
Your task is to generate a short title for product.
...
The requirements are as follows:
1. The short title should not exceed ${max_len} words.
2. xxx
...

Here are some short title examples:
original title: High Quality iPhone Case For Apple iPhone 14 13 12 11 Pro Max Plus XS MAX X XS XR Fashion Softcase Shockproof Cover Blue
short title: Shockproof Blue iPhone Softcase Cover

original title: COD GRATIS ONGKIR Baju Batik Wanita Blus Kancing Kualitas Terbaik Cocok Buat Seragam, pelandang Dan Kantor, kuliah. Banyak pilihan motif ready M, L, Xl, XXL, 3XL, 4XL
short title: Baju Batik Wanita Kualitas Terbaik

original title: Nike basketball shoes
short  title: Nike basketball shoes

...

1.2.3. 思维链

思维链(Chain-of-Thought, CoT)用于提高大型语言模型在复杂推理任务上的性能。CoT提示不像ICL(输入-输出对)那样简单地构建提示,而是进一步整合了中间推理步骤,这些步骤作为输入和输出之间的桥梁。

img

在Lazada业务中,CoT常用于处理小语种问题。原任务input和output都是local语言,但GPT对小语种理解能力较差,因此通过CoT将任务拆解为local input-> 翻译成en input -> 执行task生成en output -> 翻译成local output。在多个任务上都验证了CoT能够显著提升模型性能。

思维链是一种简单的思考策略,在面临更复杂推理时表现也欠佳。因此衍生出各种变形,比较典型的是思维树(ToT)和思维图(GoT)。

img

1.3. 大模型能力缺陷

img

人与环境交互包含感知、思考和实现三步。大模型在这三个方面都有不少缺陷,距离通用人工智能还有不小差距。

img

1.3.1. 感知缺陷

当前主流大模型输入还是文本,部分大模型可以处理图片信息。虽然多模态快速发展,目前距离人的信息输入还是有很大差距-触觉、嗅觉、味觉等。

img

1.3.2. 思考缺陷

虽然大模型具备一定推理和知识,但也存在几个明显问题:

  1. 幻觉(Hallucination). 幻觉指生成的信息要么与现有来源相冲突(内在幻觉),要么无法通过可用来源进行验证(外在幻觉)。
img
  1. 知识实效性(Knowledge recency). 大模型内在的知识仅限于训练时见到的语料,对于新信息是无法感知的。
img
  1. 推理不一致(Reasoning inconsistency). 推理不一致性指大模型可能在无效的推理路径下生成正确答案,或者在正确的推理过程后产生错误答案,导致得出的答案与推理过程之间的不一致。
img
  1. 计算问题(Numerical computation).
img

1.3.3. 实现缺陷

主流大模型的输出还只是文本,而人可以有丰富的行动。

img

1.3.4. 改进缺陷

主要有两种途径:一是从基础模型层面,可以通过数据和模型设计增强基础大模型能力;二是从应用层面,通过prompt工程、fine-tune和机制设计扩展大模型能力,而Agent就是其中的一种途径。

2. LLM Agent理论

2.1. Agent基础概念

自主代理(Autonomous agents)长久以来被认为是实现人工通用智能(AGI)的有前景的途径,预期能够通过自我指导的计划和行动来完成任务。一般来讲,Agent需要具备感知环境、自主决策、设定目标与任务、行动能力、适应环境及学习能力等关键特点。Agent并不是一个新概念,从技术上有RL Agent、LLM Agent等。根据Agent数量可分为Single Agent和Multi Agent两种类型。

img

从Agent完成任务层级难度可以简单分为三种:面向任务的Agent、面向创新的Agent和面向生命周期的Agent。代表的三种能力分别是:使用工具、制造工具和在人类社会生存。

img

2.2. Agent架构设计

LLM Agent一般包含Profile、Memory、Planning和Action等模块。

img

2.2.1. Profile

Agent通常通过扮演特定的角色来执行任务,如程序员、教师和领域专家。配置模块用来指示智能体角色的档案,这些档案通常写入提示中以影响大模型的行为。智能体档案通常包括基本信息,如年龄、性别和职业,以及心理信息,反映智能体的个性,和社会信息,详细描述智能体之间的关系。例如"你是一个外向的人…"。

角色设定通常有人工生成和大模型生成两种方式。人工生成较为常见,一般需要一定的领域知识。大模型生成一般通过大模型的内在知识生成一些必要的补充。

2.2.2. Memory

记忆模块在智能体架构设计中起着非常重要的作用。它存储从环境中感知到的信息,并利用记录下的记忆来促进未来的行动。记忆模块可以帮助智能体积累经验,自我演化,并以更加一致、合理和有效的方式行动。

从时间的维度来看,记忆分为短期记忆和长期记忆。短期记忆包含关于Agent当前情况的上下文信息,而长期记忆存储了Agent过去的行为和思想。长期记忆可以是原始信息(行为),也可以利用大模型归纳得到的信息(经验和思想)。长期记忆可以根据当前事件检索获取,即检索增强(Retrieval Augmented Generation,RAG)。

img

RAG可以选取语义相似的记忆,记忆挑选需要综合实效性(recency)相关性(relevance)重要性(importance)

img

2.2.3. Planning

规划分为单路径推理(Single-path Reasoning)、多路径推理(Multi-path Reasoning)和外部规划器(External Planner)三种类型,前两种一般基于思维链及其改进技术。做规划时可以利用之前的反馈,比如将上次失败的反馈传给大模型知道其修正规划。

img

2.2.4. Action

动作空间一般包含两部分:(1)外部工具,包括API、知识库和模型等工具。(2)内部知识。

2.3. Agent能力获取

从应用的范畴来看,机器学习领域解锁能力主要靠模型自身参数,传统大模型应用领域解锁能力主要靠Prompt设计,Agent应用领域解锁能力主要靠机制设计(试错、经验累积和自我进化等)。Agent提升能力有三种手段:1. fine-tune模型 2. prompt工程 3.机制设计。

img

3. 单Agent应用:AutoGPT

3.1. 实践示例

该例子演示了AutoGPT使用工具、制定规划和反馈机制等特性。

img
img
img
img
img
img
img
img
img
img
img

3.2. 关键设计

3.2.1. 工作流

img

3.2.2. 核心Prompt

我们以任务分析下2024美国大选为例,看下每一步执行的prompt是怎么构成的。

3.2.2.1. 通用指令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 英文原版
constraints: [
  'Exclusively use the commands listed below.',
  'You can only act proactively, and are unable to start background jobs or set up webhooks for yourself. Take this into account when planning your actions.',
  'You are unable to interact with physical objects. If this is absolutely necessary to fulfill a task or objective or to complete a step, you must ask the user to do it for you. If the user refuses this, and there is no other way to achieve your goals, you must terminate to avoid wasting time and energy.'
]
resources: [
  'Internet access for searches and information gathering.',
  'The ability to read and write files.',
  'You are a Large Language Model, trained on millions of pages of text, including a lot of factual knowledge. Make use of this factual knowledge to avoid unnecessary gathering of information.'
]
best_practices: [
  'Continuously review and analyze your actions to ensure you are performing to the best of your abilities.',
  'Constructively self-criticize your big-/img behavior constantly.',
  'Reflect on past decisions and strategies to refine your approach.',
  'Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.',
  'Only make use of your information gathering abilities to find information that you don''t yet have knowledge of.'
]

# 中文翻译
约束条件:[
	'只能使用下面列出的命令。',
	'你只能采取主动行动,不能启动后台任务或为自己设置网络钩子。在规划你的行动时要考虑到这一点。',
	'你无法与物理对象进行交互。如果绝对必要以完成任务或目标,或者完成步骤,你必须请求用户代你执行。如果用户拒绝,且无其他方式实现目标,你必须中止行动,以避免浪费时间和能源。'
]
资源:[
	'用于搜索和信息收集的互联网访问权限。',
	'读写文件的能力。',
	'你是一个大型语言模型,经过数百万页文本的训练,包括许多事实知识。利用这些事实知识,避免不必要的信息搜集。'
]

最佳实践:[
	'持续回顾和分析你的行动,以确保你以最佳能力执行。',
	'持续地建设性地自我批评你的整体行为。',
	'反思过去的决策和策略以改善你的做法。',
	'每条命令都有成本,因此要聪明和高效。目标是以最少的步骤完成任务。',
	'只有在你缺乏信息时才使用你的信息收集能力。'
]

3.2.2.2. 任务指令

根据用户输入的任务,设计Prompt生成个性化的任务指令。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Your job is to respond to a user-defined task, given in triple quotes, by invoking the `create_agent` function to generate an autonomous agent to complete the task. You should supply a role-based name for the agent (_GPT), an informative description for what the agent does, and 1 to 5 directives in each of the categories Best Practices and Constraints, that are optimally aligned with the successful completion of its assigned task.

Example Input:
"""Help me with marketing my business"""

Example Call:
```
[
    {
        "type": "function",
        "function": {
            "name": "create_agent",
            "arguments": {
                "name": "CMOGPT",
                "description": "a professional digital marketer AI that assists Solopreneurs in growing their businesses by providing world-class expertise in solving marketing problems for SaaS, content products, agencies, and more.",
                "directives": {
                    "best_practices": [
                        "Engage in effective problem-solving, prioritization, planning, and supporting execution to address your marketing needs as your virtual Chief Marketing Officer.",
                        "Provide specific, actionable, and concise advice to help you make informed decisions without the use of platitudes or overly wordy explanations.",
                        "Identify and prioritize quick wins and cost-effective campaigns that maximize results with minimal time and budget investment.",
                        "Proactively take the lead in guiding you and offering suggestions when faced with unclear information or uncertainty to ensure your marketing strategy remains on track."
                    ],
                    "constraints": [
                        "Do not suggest illegal or unethical plans or strategies.",
                        "Take reasonable budgetary limits into account."
                    ]
                }
            }
        }
    }
]
```
分析下2024美国大选

返回的结果如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
  "type": "function",
  "function": {
    "name": "create_agent",
    "arguments": {
      "name": "ElectionAnalyzerGPT",
      "description": "一个针对2024年美国总统大选的政治分析AI,能够通过研究公开信息和历史数据,预测选举走势,分析候选人竞选战略。",
      "directives": {
        "best_practices": [
          "积极探索公开数据和信息来为你提供最可能的选举结果。",
          "提供明确且有见地的候选人战略分析。",
          "不断更新分析报告以反映最新的政治风向和选民情绪。",
          "在提供情况分析的同时,提出可能影响肯定结果的关键因素。"
        ],
        "constraints": [
          "在分析中保持中立,避免露出任何偏向。",
          "不进行未经证实的猜测或情况预测。",
          "在没有充足的证据支持的情况下,不做具体的选举结果预测。",
          "尊重所有信息来源的隐私,不用任何非公开的信息作为分析基础。",
          "不参与任何可能影响选举结果的行为,仅提供分析信息。"
        ]
      }
    }
  }
}

3.2.2.3. 工具指令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
These are the ONLY commands you can use. Any action you perform must be possible through one of these commands:
1. execute_python_code: Executes the given Python code inside a single-use Docker container with access to your workspace folder. Params: (code: string)
2. execute_python_file: Execute an existing Python file inside a single-use Docker container with access to your workspace folder. Params: (filename: string, args: array)
3. list_folder: List the items in a folder. Params: (folder: string)
4. open_file: Opens a file for editing or continued viewing; creates it if it does not exist yet. Note: If you only need to read or write a file once, use `write_to_file` instead.. Params: (file_path: string)
5. open_folder: Open a folder to keep track of its content. Params: (path: string)
6. read_file: Read an existing file. Params: (filename: string)
7. write_file: Write a file, creating it if necessary. If the file exists, it is overwritten.. Params: (filename: string, contents: string)
8. ask_user: If you need more details or information regarding the given goals, you can ask the user for input. Params: (question: string)
9. google: Google Search. Params: (query: string)
10. web_search: Searches the web. Params: (query: string)
11. read_webpage: Read a webpage, and extract specific information from it if a question is specified. If you are looking to extract specific information from the webpage, you should specify a question.. Params: (url: string, question: string)
12. finish: Use this to shut down once you have completed your task, or when there are insurmountable problems that make it impossible for you to finish your task.. Params: (reason: string)

3.2.2.4. 用户任务

1
分析下2024美国大选

3.2.2.5. 系统时间

1
The current time and date is Fri Jan 19 08:51:11 2024

3.2.2.6. 长期记忆

1
2
3
4
5
6
7
8
9
### Step 1: Executed `google(query='2024 US presidential election candidates')`
- **Reasoning:** "根据任务要求,我们应首先获取相关候选人的公开信息,这将为之后的分析奠定基础。"
- **Status:** `success`
- **Output:** ["https://www.nytimes.com/interactive/2023/us/politics/presidential-candidates-2024.html", "https://ballotpedia.org/Presidential_candidates,_2024", "https://www.reuters.com/world/us/who-are-candidates-running-2024-us-presidential-election-2023-09-19/", "https://en.wikipedia.org/wiki/2024_United_States_presidential_election", "https://www.nbcnews.com/politics/2024-elections/presidential-candidates-tracker", "https://www.cnn.com/election/2024/presidential-candidates", "https://www.axios.com/2023/05/24/us-president-elections-2024-candidates", "https://www.cnn.com/election/2024/calendar"]

### Step 2: Executed `read_webpage(url='https://en.wikipedia.org/wiki/2024_United_States_presidential_election', question='谁是2024年美国总统大选的候选人?')`
- **Reasoning:** "给定的信息源包含了包括纽约时报、wikipedia、路透社等已知可靠的新闻及信息来源。为了获取最全面且精准的候选人列表,定向到维基页面会是一个好的选择。维基百科的信息通常广泛包含各方面的信息,并且内容比较全面。"
- **Status:** `interrupted_by_human`
- **Feedback:** 我无法访问wiki

3.2.2.7. 其他信息

主要包括返回格式说明,字段说明等固定信息。

3.2.2.8. 最终Prompt

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
============== ChatPrompt ==============
Length: 6 messages
----------------- SYSTEM -----------------
You are ElectionAnalyzerGPT, 一个针对2024年美国总统大选的政治分析AI,能够通过研究公开信息和历史数据,预测选举走势,分析候选人竞选战略。.

Your decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications.

## Constraints
You operate within the following constraints:
1. Exclusively use the commands listed below.
2. You can only act proactively, and are unable to start background jobs or set up webhooks for yourself. Take this into account when planning your actions.
3. You are unable to interact with physical objects. If this is absolutely necessary to fulfill a task or objective or to complete a step, you must ask the user to do it for you. If the user refuses this, and there is no other way to achieve your goals, you must terminate to avoid wasting time and energy.
4. 在分析中保持中立,避免露出任何偏向。
5. 不进行未经证实的猜测或情况预测。
6. 在没有充足的证据支持的情况下,不做具体的选举结果预测。
7. 尊重所有信息来源的隐私,不用任何非公开的信息作为分析基础。
8. 不参与任何可能影响选举结果的行为,仅提供分析信息。

## Resources
You can leverage access to the following resources:
1. Internet access for searches and information gathering.
2. The ability to read and write files.
3. You are a Large Language Model, trained on millions of pages of text, including a lot of factual knowledge. Make use of this factual knowledge to avoid unnecessary gathering of information.

## Commands
These are the ONLY commands you can use. Any action you perform must be possible through one of these commands:
1. execute_python_code: Executes the given Python code inside a single-use Docker container with access to your workspace folder. Params: (code: string)
2. execute_python_file: Execute an existing Python file inside a single-use Docker container with access to your workspace folder. Params: (filename: string, args: array)
3. list_folder: List the items in a folder. Params: (folder: string)
4. open_file: Opens a file for editing or continued viewing; creates it if it does not exist yet. Note: If you only need to read or write a file once, use `write_to_file` instead.. Params: (file_path: string)
5. open_folder: Open a folder to keep track of its content. Params: (path: string)
6. read_file: Read an existing file. Params: (filename: string)
7. write_file: Write a file, creating it if necessary. If the file exists, it is overwritten.. Params: (filename: string, contents: string)
8. ask_user: If you need more details or information regarding the given goals, you can ask the user for input. Params: (question: string)
9. google: Google Search. Params: (query: string)
10. web_search: Searches the web. Params: (query: string)
11. read_webpage: Read a webpage, and extract specific information from it if a question is specified. If you are looking to extract specific information from the webpage, you should specify a question.. Params: (url: string, question: string)
12. finish: Use this to shut down once you have completed your task, or when there are insurmountable problems that make it impossible for you to finish your task.. Params: (reason: string)

## Best practices
1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.
2. Constructively self-criticize your big-/img behavior constantly.
3. Reflect on past decisions and strategies to refine your approach.
4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.
5. Only make use of your information gathering abilities to find information that you don't yet have knowledge of.
6. 积极探索公开数据和信息来为你提供最可能的选举结果。
7. 提供明确且有见地的候选人战略分析。
8. 不断更新分析报告以反映最新的政治风向和选民情绪。
9. 在提供情况分析的同时,提出可能影响肯定结果的关键因素。

## Your Task
The user will specify a task for you to execute, in triple quotes, in the next message. Your job is to complete the task while following your directives as given above, and terminate when your task is done.
------------------ USER ------------------
"""分析下2024美国大选"""
----------------- SYSTEM -----------------
## Progress

### Step 1: Executed `google(query='2024 US presidential election candidates')`
- **Reasoning:** "根据任务要求,我们应首先获取相关候选人的公开信息,这将为之后的分析奠定基础。"
- **Status:** `success`
- **Output:** ["https://www.nytimes.com/interactive/2023/us/politics/presidential-candidates-2024.html", "https://ballotpedia.org/Presidential_candidates,_2024", "https://www.reuters.com/world/us/who-are-candidates-running-2024-us-presidential-election-2023-09-19/", "https://en.wikipedia.org/wiki/2024_United_States_presidential_election", "https://www.nbcnews.com/politics/2024-elections/presidential-candidates-tracker", "https://www.cnn.com/election/2024/presidential-candidates", "https://www.axios.com/2023/05/24/us-president-elections-2024-candidates", "https://www.cnn.com/election/2024/calendar"]

### Step 2: Executed `read_webpage(url='https://en.wikipedia.org/wiki/2024_United_States_presidential_election', question='谁是2024年美国总统大选的候选人?')`
- **Reasoning:** "给定的信息源包含了包括纽约时报、wikipedia、路透社等已知可靠的新闻及信息来源。为了获取最全面且精准的候选人列表,定向到维基页面会是一个好的选择。维基百科的信息通常广泛包含各方面的信息,并且内容比较全面。"
- **Status:** `interrupted_by_human`
- **Feedback:** 我无法访问wiki

----------------- SYSTEM -----------------
The current time and date is Fri Jan 19 08:51:11 2024
----------------- SYSTEM -----------------
Respond with pure JSON. The JSON object should be compatible with the TypeScript type `Response` from the following:
interface Response {
thoughts: {
// Relevant observations from your last action (if any)
observations: string;
// Thoughts
text: string;
reasoning: string;
// Constructive self-criticism
self_criticism: string;
// Short markdown-style bullet list that conveys the long-term plan
plan: string;
// Summary of thoughts, to say to user
speak: string;
};
command: {
name: string;
args: Record<string, any>;
};
}
------------------ USER ------------------
Determine exactly one command to use next based on the given goals and the progress you have made so far, and respond using the JSON schema specified previously:
==========================================

调用结果如下:

img

3.3. 源码解读

源码版本:V0.5.0

3.3.1. 解读工具

3.3.1.1. pydeps

img

3.3.1.2. GPT4

我们可以利用GPT4解读源码,下面的代码解释都是由GPT生成,prompt如下:

1
2
3
你是一个代码解释专家,请帮助我解读一个叫AutoGPT的开源项目代码。对于我给你的每一段代码,请执行以下任务:
1. 解释重要函数的功能和函数依赖关系
2. 介绍下代码整体的功能和函数依赖关系

3.3.2. 入口:main.py

1
2
3
4
5
"""AutoGPT: A GPT powered AI Assistant"""
import autogpt.app.cli

if __name__ == "__main__":
    autogpt.app.cli.cli()
img

3.3.3. app

3.3.3.1. cli.py

img

3.3.3.2. main.py

img

3.4. 环境配置

3.4.1. 相关依赖

3.4.2. 安装指引

3.4.2.1. 通用设置

  1. 将.env.template复制为.env文件,修改.env文件下面配置项:
  • OPENAI_API_KEY=xxxx
  • GOOGLE_API_KEY=xxxx
  • GOOGLE_CUSTOM_SEARCH_ENGINE_ID=xxxx
  • USE_AZURE=XXXX(使用Azure账号需要设置为True)
  1. 按照下面命令安装:
1
2
3
4
5
6
7
cd autogpts/autogpt
# 查看Python3版本
python3 -V
# 假设Python3版本为3.10.13,设置poetry环境
poetry env use 3.10.13
# 利用poetry安装环境
poetry install --without dev

Poetry安装时可能会有超时问题,有两个解决方案:

  1. 使用pip安装超时的包:poetry run pip install numpy==1.26.2
  2. 改为国内安装源,参考#link

3.4.2.2. Azure用户设置

  1. 将azure.yaml.template复制为azure.yaml,修改配置。
  2. 将OpenAI类的has_function_call_api=False,原因参考#issue6519

3.4.2.3. 其他设置

  1. 在AutoGPT目录下运行 ,原因参考#issue4752

3.4.2.4. 运行AutoGPT

AutoGPT参数较多,可以运行./autogpt.sh run –help查看参数。下面示例含义是只用GPT-4,跳过项目新闻:

1
./autogpt.sh run --gpt4only --skip-news

如果任务中涉及运行代码,需要在开启docker情况下运行:

1
sudo docker compose run --rm auto-gpt run --gpt4only --skip-news

3.5. 使用体验

AutoGPT并不是银弹,具有一定实验性,应用到具体场景会有不少问题。

从整体上:

  1. 灵活性差:与OpenAI接口深度耦合,难以换基座模型。
  2. 鲁棒性差:架构设计偏demo,任务完成的可控性较差,容易陷入死循环。
  3. 效率低&成本高:解决一个简单问题都需要多次调用,成本较高。

从细节上:

  1. Plan:规划能力不够强,例如在重复执行一个action得不到结果时,不能及时找到合理的方式退出死循环。
  2. Memory:因维护成本较高,AutoGPT放弃了RAG模式,这就导致无法解决领域问题,例如对话模型需要根据用户意图调用不同的知识库。
  3. Tools:工具设计不够鲁棒,例如执行python代码,主函数监控函数执行的STDOUT,但大模型写的python代码常常不会print最终结果,导致陷入死循环。
  4. Feedback:只能接受及时的反馈,和实际领域工作流不匹配,无法端到端执行完整任务。例如问大家要批量生产1w样本给BPO评审,但是程序不能一直等待。

总结使用通用Agent解决具体业务问题并不可靠,可以在具体业务问题中应用Agent的部分思想即可,例如意图识别、工具使用、做规划等

4. 多Agent应用:MetaGPT

4.1. 基本思想

现有基于LLM的多代理系统已经能够解决简单的对话任务。然而,解决更复杂的任务却因为通过简单连接LLM导致的逻辑不一致和连锁幻觉而变得复杂。MetaGPT是一种创新的元编程框架,将高效的人类工作流程融入基于LLM的多代理协作中。MetaGPT将**标准操作程序(SOPs)**编码到提示序列中,以实现更顺畅的工作流程,从而让具有类似人类领域专业知识的代理能够验证中间结果并减少错误。MetaGPT采用装配线范例为不同的代理分配多样化的角色,高效地将复杂任务拆分为涉及许多协同工作的代理的子任务。

img

4.2. 机制设计

上面我们说过Agent的核心就是机制设计,MetaGPT核心的设计有以下两个:

  1. 结构化通信接口. 大多数当前基于大模型的多代理框架使用不受约束的自然语言作为通信接口。然而,尽管自然语言的多功能性,但仍有一个问题:纯自然语言通信是否足以解决复杂任务?例如,在传话游戏中,经过几轮通信后,原始信息可能会变得非常扭曲。受到人类社会结构的启发,我们提议使用结构化通信来构建代理人的通信。MetaGPT为每个角色建立了一个模式和格式,并要求个体根据其特定的角色和上下文提供必要的输出。

  2. 发布-订阅机制.

  3. 消息发布. 在合作中,共享信息至关重要。例如,架构师和工程师通常需要参考产品需求文档(PRD)。每次都以一对一的方式通信,可能会使通信拓扑复杂化,导致效率低下。为了解决这一挑战,一个可行的方法是将信息存储在全局消息池中。MetaGPT引入了一个共享消息池,允许所有代理人直接交换消息。这些代理不仅在池中发布他们的结构化消息,还可以透明地访问来自其他实体的消息。任何代理都可以直接从共享池中检索所需信息,无需询问其他代理并等待他们的回应。这提高了通信效率。

  4. 消息订阅. 与每个代理共享所有信息可能会导致信息过载。在任务执行期间,代理通常倾向于仅接收与任务相关的信息,避免通过无关细节的干扰。代理不依赖于对话,而是利用与角色相关的兴趣来提取相关信息,他们可以基于他们的角色档案选择要关注的信息。例如架构师主要关注由产品经理提供的PRD,而来自测试等角色的文档可能关注较少。

img

4.3. 示例

img

5. 参考文献