Skip to content

Commit

Permalink
Merge pull request #17 from GoldWaterFall/main
Browse files Browse the repository at this point in the history
chapter3
  • Loading branch information
limafang authored Mar 2, 2024
2 parents 9a49b5c + 98fe903 commit 4c472a3
Show file tree
Hide file tree
Showing 26 changed files with 2,339 additions and 0 deletions.
28 changes: 28 additions & 0 deletions docs/chapter3/Agent概念概述.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## 3.1 Agent概念概述

在MetaGPT看来,我们把Agent想象成环境中的数字人,其中 Agent = 大语言模型(LLM) + 观察 + 思考 + 行动 + 记忆
这个公式概括了智能体的功能本质。为了理解每个组成部分,让我们将其与人类进行
类比:
1. 大语言模型(LLM):LLM作为智能体的“大脑”部分,使其能够处理信息,从交
互中学习,做出决策并执行行动。
2. 观察:这是智能体的感知机制,使其能够感知其环境。智能体可能会接收来自另
一个智能体的文本消息、来自监视摄像头的视觉数据或来自客户服务录音的音频等一
系列信号。这些观察构成了所有后续行动的基础。
3. 思考:思考过程涉及分析观察结果和记忆内容并考虑可能的行动。这是智能体内
部的决策过程,其可能由LLM进行驱动。
4. 行动:这些是智能体对其思考和观察的显式响应。行动可以是利用 LLM 生成代
码,或是手动预定义的操作,如阅读本地文件。此外,智能体还可以执行使用工具的
操作,包括在互联网上搜索天气,使用计算器进行数学计算等。
5. 记忆:智能体的记忆存储过去的经验。这对学习至关重要,因为它允许智能体参
考先前的结果并据此调整未来的行动。

MetaGPT 中定义的一个agent 运行示例如下:

![metagptagent](/docs/chapter3/img/metagptagent.png)

• 一个agent在启动后他会观察自己能获取到的信息,加入自己的记忆中 • 下一步进行思考,决定下一步的行动,也就是从 Action1,Action2,Action3 中选择执行的 Action
• 决定行动后,紧接着就执行对应行动,得到这个环节的结果
而在**MetaGPT内 Role 类是智能体的逻辑抽象。**一个 Role 能执行特定的 Action,拥有记忆、思考并采用各种策略行动。基本上,它充当一个将所有这些组件 联系在一起的凝聚实体。目前,让我们只关注一个执行动作的智能体,并看看如何实现一个最简单的 Agent



Empty file.
49 changes: 49 additions & 0 deletions docs/chapter3/RoleContext对象分析.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## 3.2 RoleContext

而 Role 在与环境上下文进行交互时,是通过内部的 RoleContext 对象来实现的。本 篇我们就来看看 RoleContext 中都有哪些内容。

> 代码版本使用 v0.6.6
```python
class RoleContext(BaseModel):
"""Role Runtime Context"""

model_config = ConfigDict(arbitrary_types_allowed=True)

# # env exclude=True to avoid `RecursionError: maximum recursion depth exceeded in comparison`
env: "Environment" = Field(default=None, exclude=True) # # avoid circular import
# TODO judge if ser&deser
msg_buffer: MessageQueue = Field(
default_factory=MessageQueue, exclude=True
) # Message Buffer with Asynchronous Updates
memory: Memory = Field(default_factory=Memory)
# long_term_memory: LongTermMemory = Field(default_factory=LongTermMemory)
state: int = Field(default=-1) # -1 indicates initial or termination state where todo is None
todo: Action = Field(default=None, exclude=True)
watch: set[str] = Field(default_factory=set)
news: list[Type[Message]] = Field(default=[], exclude=True) # TODO not used
react_mode: RoleReactMode = (
RoleReactMode.REACT
) # see `Role._set_react_mode` for definitions of the following two attributes
max_react_loop: int = 1
```

**env**:Environment 对象,当在 Environment 添加 Role 时会同时设置 Role 对 Environment 的引用。

**msg_buffer**:一个 MessageQueue 对象,该对象是对 asyncio 的 Queue 进行简单封装,主要是提供了非阻塞的 pop / push 方法。Role 通过该对象与环境中的其他 Role 进行信息交互。

**memory**:记忆对象。当 Role 执行 _act 时,会将执行得到的响应转换为 Message 对象放入 memory 中。另外当 Role 执行 _observe 时,会把 msg_buffer 的所有消息转移到 memory 中。

**state**:记录 Role 的执行状态。初始状态值为 -1,当全部 Action 执行完成之后也会被重置为 -1。

**todo**:下一个待执行的 Action。当 state >= 0 时会指向最后一个 Action。

**watch**:用 str 表示的当前 Role 观察的 Action 列表,目前用在 _observe 获取 news 时进行消息过滤。

**news**:存储那些在本次执行 _observe 时读取到的与当前 Role 上下游相关的消息。

**react_mode**:ReAct 循环的模式,目前支持 REACT、BY_ORDER、PLAN_AND_ACT 3种模式,默认使用 REACT 模式。在 _set_react_mode 方法中有相关说明。简单来说,BY_ORDER 模式按照指定的 Action 顺序执行。PLAN_AND_ACT 则为一次思考后执行多个动作,即 _think -> _act -> act -> ...,而 REACT 模式按照 ReAct 论文中的思考——行动循环来执行,即 _think -> _act -> _think -> _act -> ...。

**max_react_loop**:在 react_mode 为 REACT 模式时生效,用于设置最大的思考-循环次数,超过后会停止 _react 执行。

**部分参数的讲解将在多智能体篇详解**
Binary file added docs/chapter3/img/3.5.1require.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/chapter3/img/Message.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/chapter3/img/analyse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/chapter3/img/discord1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/chapter3/img/discord2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/chapter3/img/discordres.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/chapter3/img/flow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/chapter3/img/github.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/chapter3/img/html.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/chapter3/img/html2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/chapter3/img/metagptagent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/chapter3/img/oss.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/chapter3/img/require.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/chapter3/img/wxpublisherres.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/chapter3/img/wxpusher1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/chapter3/img/wxpusher2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions docs/chapter3/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
comments: true
---

# 3.第三章:单智能体开发

# 前期准备

本章节内我们将了解将详细了解metagpt并学习如何使用metagpt进行单智能体的开发,通过实现技术文档助手和OSS订阅智能体,进一步带大家感受metagpt的功能

```
Loading

0 comments on commit 4c472a3

Please sign in to comment.