AutoGPT解析

AutoGPT是一个相对全面精巧可以构建AI Agent的框架,包含了AI代理的创建、部署和持续管理。

AI agent的结构被设计成很像人类,具有个性、记忆、思维过程和能力等不同组件。


AI agent的组件

Forge

对于AI代理的创建,AutoGPT提供了一个叫Forge的工具模版,自动生成Agent的代码,配置和运行脚本,甚至还包括了基准测试系统和前端。

创建Agent:

./run agent create YOUR_AGENT_NAME

运行Agent:

./run agent start YOUR_AGENT_NAME
Agent运行起来

Forge的核心代码结构包含:

  • agent: Forge的核心,包含Agent的主要逻辑
  • agent_protocol: Agent协议,Forge的一个特点,为了兼容各种Agent以及Agent市场
  • config: agent的配置,包含BaseConfigAIProfileAIDirectives
  • component: 构建agent的能力组件,继承自AgentComponent或者实现Protocols接口,代表agent的某种能力
  • llm/prompting: 提示词模版

component

from forge.agent import BaseAgent
from forge.agent.components import AgentComponent

class HelloComponent(AgentComponent):
    pass

class SomeComponent(AgentComponent):
    def __init__(self, hello_component: HelloComponent):
        self.hello_component = hello_component

class MyAgent(BaseAgent):
    def __init__(self):
        # These components will be automatically discovered and used
        self.hello_component = HelloComponent()
        # We pass HelloComponent to SomeComponent
        self.some_component = SomeComponent(self.hello_component)

按顺序指定component:

class MyAgent(Agent):
    def __init__(self):
        self.hello_component = HelloComponent()
        self.calculator_component = CalculatorComponent()
        # Explicitly set components list
        self.components = [self.hello_component, self.calculator_component]

可以从配置文件导入components:

{
    "CodeExecutorConfiguration": {
        "execute_local_commands": false,
        "shell_command_control": "allowlist",
        "shell_allowlist": ["cat", "echo"],
        "shell_denylist": [],
        "docker_container_name": "agent_sandbox"
    },
    "FileManagerConfiguration": {
        "storage_path": "agents/AutoGPT/",
        "workspace_path": "agents/AutoGPT/workspace"
    },
    "GitOperationsConfiguration": {
        "github_username": null
    },
    "ActionHistoryConfiguration": {
        "llm_name": "gpt-3.5-turbo",
        "max_tokens": 1024,
        "spacy_language_model": "en_core_web_sm"
    },
    "ImageGeneratorConfiguration": {
        "image_provider": "dalle",
        "huggingface_image_model": "CompVis/stable-diffusion-v1-4",
        "sd_webui_url": "http://localhost:7860"
    },
    "WebSearchConfiguration": {
        "duckduckgo_max_attempts": 3
    },
    "WebSeleniumConfiguration": {
        "llm_name": "gpt-3.5-turbo",
        "web_browser": "chrome",
        "headless": true,
        "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36",
        "browse_spacy_language_model": "en_core_web_sm"
    }
}

Agent Protocol

定义了Agent接口,方便统一构建各种Agent。
Agent Protocol主要定义了Task 任务、Step 步骤、Artifact 生成文件,以及对应的API定义:

  • /ap/v1/agent/tasks [POST] - This endpoint is used to create a new task for the agent.
  • /ap/v1/agent/tasks/{task_id}/steps [POST] - This endpoint is used to trigger next step of the task.

Protocol还包括记忆模块db.py,核心类是AgentDB,在Agent初始化时传入db的配置信息。

Agent

BaseAgent 提供了两个Agent工作需要的抽象方法:

  • propose_action: 这个方法负责思考,提供思考结果对应的action, 返回ThoughtProcessOutput.
  • execute: 这个方法负责执行action, 返回 ActionResult.

Agent是AutoGPT实现的继承自BaseAgent 并实现了上面两个抽象方法,包含所有内建的components。这样最简单的创建自己的Agent的方法就是继承这个Agent,然后添加自己额外的component。当然也可以直接继承BaseAgent 可以做更多的定制化。

class MyComponent(AgentComponent):
    pass

class MyAgent(Agent):
    def __init__(
        self,
        settings: AgentSettings,
        llm_provider: MultiProvider
        file_storage: FileStorage,
        app_config: AppConfig,
    ):
        # Call the parent constructor to bring in the default components
        super().__init__(settings, llm_provider, file_storage, app_config)
        # Add your custom component
        self.my_component = MyComponent()

重构

最近AutoGPT做了一次全新的重构,把Forge等组件都移动到classic文件夹,新的核心目录变成autogpt_platform,包含如下等模块:

  • backend
  • frontend
  • infra
  • market
    AutoGPT新架构使用了一些企业级的框架工具:
  • Supabase : 一个开源框架,一键集成了一些企业级基础设施,例如认证授权,Postgres数据库访问,文件存储等,号称让你用一个周末即可开发一个百万并发应用。
Supabase架构
  • Prisma:基于Node.js的ORM工具,很好的平衡SQL生产力和控制力。
    Prisma
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容