跳转至

AutoGPT 自主 Agent

一句话概述:AutoGPT 是自主 AI Agent 的先驱项目,它允许 LLM 自己设定目标、制定计划、执行任务并评估结果,实现真正的"自动驾驶"式 AI 操作。

核心知识点速查表

知识点说明
为什么要学 AutoGPT见对应章节
核心概念见对应章节
安装配置见对应章节
LLM 配置见对应章节
本地 LLM 替代见对应章节
工具配置见对应章节
执行限制见对应章节
快速上手见对应章节

为什么要学 AutoGPT

AutoGPT 是自主 AI Agent 的先驱项目,它允许 LLM 自己设定目标、制定计划、执行任务并评估结果,实现真正的"自动驾驶"式 AI 操作。最新版本已经从单纯的实验项目演变为一个成熟的 Agent 平台,提供了可视化构建器、持久化记忆和可靠的任务执行能力。对于想要理解和构建自主 AI 系统的开发者来说,AutoGPT 是必学的参考架构。


核心概念

概念白话解释用途
Agent自主智能体能独立思考和行动的 AI
Task Decomposition任务分解将大目标拆成可执行的小步骤
Tool/Action工具/动作Agent 可以执行的具体操作
Memory记忆长期记忆和短期工作记忆
Planning Loop规划循环思考→行动→观察→反思
Benchmark基准测试评估 Agent 能力的标准测试

安装配置

前置要求

  • Python 3.10+
  • Docker & Docker Compose
  • OpenAI API Key 或本地 LLM

安装步骤

# 克隆仓库
git clone https://github.com/Significant-Gravitas/AutoGPT.git
cd AutoGPT

# 复制环境配置
cp .env.template .env

# 编辑 .env
# OPENAI_API_KEY=sk-xxx
# 或使用本地模型
# SMART_LLM=ollama/llama3
# FAST_LLM=ollama/llama3

# 使用 Docker Compose 启动
docker compose up -d

环境变量配置

# .env
## LLM 配置
OPENAI_API_KEY=sk-your-key
SMART_LLM=gpt-4               # 用于复杂推理
FAST_LLM=gpt-3.5-turbo        # 用于简单任务

## 本地 LLM 替代
# SMART_LLM=ollama/llama3:70b
# FAST_LLM=ollama/llama3:8b
# OLLAMA_BASE_URL=http://localhost:11434

## 工具配置
GOOGLE_API_KEY=               # Google 搜索
GOOGLE_CUSTOM_SEARCH_ID=      # 自定义搜索引擎 ID

## 执行限制
EXECUTE_LOCAL_COMMANDS=True
RESTRICT_TO_WORKSPACE=True    # 限制文件操作范围

前端 UI 启动

# 启动 AutoGPT Platform(新版可视化界面)
cd autogpt_platform
docker compose up -d

# 访问 http://localhost:8080

快速上手

CLI 模式

# 进入 AutoGPT 目录
cd autogpt

# 运行 Agent
python -m autogpt run

# 带目标运行
python -m autogpt run --goal "研究 Python 3.12 新特性并写一份总结报告"

Platform 模式(可视化)

  1. 打开 http://localhost:8080
  2. 创建新的 Agent
  3. 定义目标和约束
  4. 配置可用工具
  5. 启动 Agent 并监控执行

基本工作流程

用户设定目标
Agent 分解任务
循环执行:
    思考 → 选择动作 → 执行 → 观察结果 → 反思
完成目标 / 请求人类反馈

预设 Agent 模板

# agent_config.yaml
name: "ResearchAgent"
role: "AI Research Assistant"
goals:
  - "Research the given topic thoroughly"
  - "Write a comprehensive summary"
  - "Save findings to a file"
constraints:
  - "Only use reliable sources"
  - "Cite all sources"
  - "Stay focused on the topic"
tools:
  - web_search
  - web_scrape
  - write_file
  - read_file

进阶用法

自定义工具

from autogpt.tools import tool

@tool(
    name="database_query",
    description="Query the internal database for information",
    parameters={
        "query": {
            "type": "string",
            "description": "SQL query to execute"
        }
    }
)
def database_query(query: str) -> str:
    """Execute a read-only database query."""
    import sqlite3
    conn = sqlite3.connect("data.db")
    cursor = conn.execute(query)
    results = cursor.fetchall()
    conn.close()
    return str(results)

Agent 间协作

# Multi-Agent 配置
agents:
  researcher:
    role: "Research Specialist"
    tools: [web_search, web_scrape]
    goal: "Find and verify information"

  writer:
    role: "Content Writer"
    tools: [write_file, read_file]
    goal: "Write well-structured content"

  reviewer:
    role: "Quality Reviewer"
    tools: [read_file, write_file]
    goal: "Review and improve content quality"

workflow:
  - agent: researcher
    output: research_notes
  - agent: writer
    input: research_notes
    output: draft
  - agent: reviewer
    input: draft
    output: final_document

记忆系统配置

memory:
  backend: "chromadb"  # 或 redis, pinecone

  # 长期记忆配置
  long_term:
    enabled: true
    max_entries: 10000
    relevance_threshold: 0.7

  # 短期记忆(工作记忆)
  short_term:
    max_tokens: 4000
    summary_threshold: 3000  # 超过此值时自动摘要

Benchmark 评估

# 运行官方基准测试
cd benchmark
python -m agbenchmark run

# 运行特定类别
python -m agbenchmark run --category code
python -m agbenchmark run --category data

# 查看结果
python -m agbenchmark report

安全沙箱

# Docker 沙箱配置
sandbox:
  enabled: true
  image: "autogpt-sandbox:latest"
  network: "none"          # 禁止网络访问
  memory_limit: "512m"
  cpu_limit: 1
  timeout: 300             # 5 分钟超时
  allowed_commands:
    - python
    - pip
    - node
  blocked_paths:
    - /etc
    - /var

常见问题

Q: Agent 陷入循环怎么办?

  1. 设置最大执行步数限制
  2. 添加"不要重复相同操作"的约束
  3. 启用人类反馈模式(每 N 步需要人工确认)
  4. 使用更强的 LLM(如 GPT-4)减少推理错误

Q: 如何控制 API 消耗?

# .env 中设置预算
BUDGET_LIMIT=10.0          # 美元上限
MAX_TOKENS_PER_STEP=1000   # 每步 token 限制
FAST_LLM_FOR_PLANNING=True # 规划用便宜模型

Q: 使用本地模型效果如何?

  • 70B+ 模型(如 Llama3 70B)可以完成大部分任务
  • 7-13B 模型只适合简单任务,复杂规划会出错
  • 建议:规划用大模型,执行用小模型

Q: 与 CrewAI、MetaGPT 的区别?

  • AutoGPT:单 Agent 自主执行,平台化方向
  • CrewAI:多 Agent 协作框架,角色定义更清晰
  • MetaGPT:模拟软件公司流程,适合代码生成

参考资源

  • GitHub:https://github.com/Significant-Gravitas/AutoGPT
  • 文档:https://docs.agpt.co/
  • Benchmark:https://github.com/Significant-Gravitas/AutoGPT/tree/master/benchmark
  • Forum:https://discourse.agpt.co/
  • Discord:https://discord.gg/autogpt