Agent2 — 模块化 Agent 系统框架
一个从零构建的 Python Agent 系统框架,用于深入理解 AI Agent 的核心架构和设计模式。
核心特性
| 特性 | 说明 |
|---|---|
| 🧠 LLM 抽象层 | 统一 OpenAI 兼容接口,支持 OpenAI / DeepSeek / Ollama / vLLM / Qwen 等 |
| 🔧 工具系统 | @tool 装饰器自动生成 JSON Schema,支持同步/异步 |
| 🔄 ReAct 模式 | Thought → Action → Observation 推理循环 |
| 📋 Plan-and-Execute | 先规划后执行,支持动态重规划 |
| 🪞 自我反思 | ReflectionMixin 添加输出自评和迭代改进 |
| 💾 记忆系统 | 短期 (WorkingMemory) + 长期 (LongTermMemory/TF-IDF) |
| 👥 多 Agent 编排 | 顺序/监督者/辩论 三种协作模式 |
快速开始
# 安装
uv pip install -e "."
# 设置 API Key
export AGENT2_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
Metadata
Release files for agent2 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| agent2-0.1.1.tar.gz | 80.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| agent2-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 147.6 kB
Release files / agent2-0.1.1.tar.gz
| Download URL | agent2-0.1.1.tar.gz |
|---|---|
| Size | 80.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6e49f8b2213d6310ce8c2fc1d72606465ab4a052e5abf39ec9469742dfdd82bb
|
|
BLAKE2b-256 checksum How to use checksums |
54252b1d9d7c37af8b7d4b0ccd6998297b57571875eefcdcf7dd0575fb62f77d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Release files / agent2-0.1.1-py3-none-any.whl
| Download URL | agent2-0.1.1-py3-none-any.whl |
|---|---|
| Size | 67.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
775abf8d3fd74eba5aca04430b857c0792fc06679f7c3b60129f5a000ba33563
|
|
BLAKE2b-256 checksum How to use checksums |
51c36d71c7ad1b744dc62cf9c153f7c3d4edc8e9afaa1a5be60a6f7206f17a30
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|