Skip to main content

Agent2 — 模块化 Agent 系统框架

一个从零构建的 Python Agent 系统框架,用于深入理解 AI Agent 的核心架构和设计模式。

核心特性

特性 说明
🧠 LLM 抽象层 统一接口支持 OpenAI / Anthropic / Google / Ollama
🔧 工具系统 @tool 装饰器自动生成 JSON Schema,支持同步/异步
🔄 ReAct 模式 Thought → Action → Observation 推理循环
📋 Plan-and-Execute 先规划后执行,支持动态重规划
🪞 自我反思 ReflectionMixin 添加输出自评和迭代改进
💾 记忆系统 短期 (WorkingMemory) + 长期 (LongTermMemory/TF-IDF)
👥 多 Agent 编排 顺序/监督者/辩论 三种协作模式

快速开始

# 安装
uv pip install -e "."

# 安装 LLM 提供商 SDK(按需)
uv pip install -e ".[openai]"      # OpenAI
uv pip install -e ".[all-llm]"     # 所有 LLM

# 设置 API Key
export AGENT2_OPENAI_API_KEY=sk-...

最简示例

import asyncio
from agent2.llm import create_llm
from agent2.agent import ReActAgent
from agent2.tools.builtin import python_exec

async def main():
    llm = create_llm("openai", model="gpt-4o-mini")
    agent = ReActAgent("assistant", llm=llm, tools=[python_exec])
    result = await agent.run("What is 2^100?")
    print(result)

asyncio.run(main())

示例

uv run examples/01_single_agent.py   # 单 Agent ReAct
uv run examples/02_tool_use.py       # 自定义工具
uv run examples/03_planning.py       # Plan-and-Execute
uv run examples/04_memory.py         # 记忆系统(无需 API Key)
uv run examples/05_multi_agent.py    # 多 Agent 协作

架构

agent2/
├── llm/        # LLM 抽象层 — 统一多提供商接口
├── tools/      # 工具系统 — @tool 装饰器 + Registry
├── agent/      # Agent 核心 — ReAct / Planner / Reflection
├── memory/     # 记忆系统 — Working / LongTerm
├── crew/       # 多 Agent — Sequential / Supervisor / Debate
└── utils/      # 配置 + 日志

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agent2-0.1.0.tar.gz (72.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agent2-0.1.0-py3-none-any.whl (47.3 kB view details)

Uploaded Python 3

File details

Details for the file agent2-0.1.0.tar.gz.

File metadata

  • Download URL: agent2-0.1.0.tar.gz
  • Upload date:
  • Size: 72.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for agent2-0.1.0.tar.gz
Algorithm Hash digest
SHA256 eeeba440a7fb16f47bb9339acbaca6acfdc43dbdf2bdc4c25518fd5b9e3efaf2
MD5 ffa906438d86aba9577dd84161fe74aa
BLAKE2b-256 0c240d05c4a89952d3f4351368a716e54d4c9293982e920856b67480a848addc

See more details on using hashes here.

File details

Details for the file agent2-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: agent2-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 47.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.14 {"installer":{"name":"uv","version":"0.11.14","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for agent2-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f088ec3a41cfa1328afcb5abe1d64a5d19a4333decb46b18de779e78509a445b
MD5 32d55b71a3f177ce06b70003e357cbd7
BLAKE2b-256 ddd20ce4bee1e6dc1f70e652490fe9709b4bf4900f48543fb280fae5746e4ba3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page