本页面记录了 MetaGPT 的配置系统和上下文管理,它们是定制框架和在执行过程中维护状态的关键组件。配置系统允许用户指定 LLM 设置、工具配置和各种其他参数,而上下文系统提供运行时状态管理和资源访问。
MetaGPT 使用一个分层配置系统,从多个来源加载设置,并使用一个上下文管理系统来维护运行时状态。这些系统使组件能够跨框架共享配置和状态。
来源: metagpt/config2.py1-183 metagpt/context.py1-128 metagpt/context_mixin.py1-102
Config 类位于 metagpt/config2.py,是 MetaGPT 配置系统的基础。它继承了 CLIParams 用于命令行参数,以及 YamlModel 用于 YAML 文件解析。
来源: metagpt/config2.py30-47 metagpt/config2.py49-170
MetaGPT 从多个来源加载配置,并遵循明确的优先级顺序
配置来源使用 merge_dict 函数进行合并,后面的来源覆盖前面的。
来源: metagpt/config2.py102-123 metagpt/config2.py173-178
配置文件按不同组件分段。以下是基于 config2.example.yaml 的示例结构
来源: config/config2.example.yaml1-130
配置包含不同组件的各种部分
| 章节 | 目的 | 关键属性 |
|---|---|---|
llm | LLM 提供商配置 | api_type、model、api_key、base_url |
embedding | 向量嵌入设置 | api_type、model、dimensions |
roles | 角色特定配置 | 每个角色的 LLM 设置 |
exp_pool | 经验池设置 | enabled、enable_read、enable_write、retrieval_type |
role_zero | RoleZero 内存设置 | enable_longterm_memory、memory_k、similarity_top_k |
proxy | 全局代理设置 | 字符串 URL |
search、browser、mermaid | 工具配置 | 工具特定参数 |
s3、redis | 存储配置 | 连接参数 |
来源: metagpt/config2.py52-98 config/config2.example.yaml1-130
Context 类作为运行时状态、配置和资源的容器
Context 类提供
AttrDict 动态属性字典,用于存储任意键值对来源: metagpt/context.py58-128 metagpt/context.py26-46
AttrDict 是一种特殊的类字典对象,允许使用点符号访问属性
来源: metagpt/context.py26-46 tests/metagpt/test_context.py12-28
ContextMixin 为角色和动作等组件提供了一种标准化的方式来访问上下文资源
Mixin 提供
来源: metagpt/context_mixin.py17-102
有多种方式可以初始化配置
使用默认配置
创建一个带有自定义 LLM 的配置
从特定的 YAML 文件加载
从 CLI 初始化(在 software_company.py 中使用)
来源: metagpt/config2.py101-137 metagpt/software_company.py13-74
通常创建和使用上下文的方式如下
来源: metagpt/context.py58-128 metagpt/software_company.py40-41
MetaGPT 允许为不同角色定义不同的配置
来源: config/config2.example.yaml23-52
ExperienceManager 使用配置来确定其行为和存储选项
经验池
exp_pool 部分的配置设置来源: metagpt/exp_pool/manager.py18-242 metagpt/exp_pool/decorator.py29-227 metagpt/configs/exp_pool_config.py1-26
RoleZero 使用配置来实现其长期记忆功能
RoleZero 记忆系统
来源: metagpt/memory/role_zero_memory.py24-201 metagpt/configs/role_zero_config.py1-12
当使用 CLI 启动新项目时
配置流程如下
来源: metagpt/software_company.py77-124
Experience Pool 使用读取配置的装饰器来缓存函数结果
装饰器
来源: metagpt/exp_pool/decorator.py29-94 examples/exp_pool/decorator.py1-32
MetaGPT 中的配置和上下文系统为自定义和运行多代理工作流提供了灵活的基础。配置系统支持来自多个来源的分层设置,而上下文系统提供运行时状态管理和资源访问。这些系统使组件能够跨框架共享配置和状态,从而更容易构建复杂的基于代理的应用程序。