Wuwei - 无为而治的 AI 智能体框架
Project description
Wuwei 2.1
无为而治的 AI 智能体框架
Wuwei 2.1 是一个轻量、可扩展的 Python Agent 框架,借鉴了 LangChain、LangGraph、Deep Agents、Hermes Agent、AgentScope、Claude Code、agent-framework、rig 八大框架的优点。
特性
- 🚀 轻量核心 — 核心包仅 ~50KB,按需安装扩展
- 🔌 多提供商 — 支持 OpenAI、Anthropic、智谱、DashScope、Ollama
- 📊 状态图编排 — 借鉴 LangGraph 的 StateGraph + Channels
- 🧩 可组合中间件 — 替代固定 Hook,支持动态插拔
- 🔗 MCP 支持 — Model Context Protocol 客户端
- 🌐 多平台网关 — 微信、钉钉、飞书、Telegram、Webhook
- 🛡️ 沙箱执行 — Docker/E2B 隔离执行
- 📈 可观测性 — OpenTelemetry 追踪
- 🤖 多 Agent 协作 — Swarm 团队协作模式
- 📦 插件系统 — 自动发现和加载插件
- 📝 Output Parsers — JSON/Pydantic/列表解析器
- 🔄 上下文压缩 — 自动压缩长对话
- 📡 流式模式 — 支持多种流式输出模式
安装
# 最小安装(核心)
pip install wuwei
# 按需安装扩展
pip install wuwei[graph] # 状态图
pip install wuwei[middleware] # 中间件
pip install wuwei[mcp] # MCP 支持
pip install wuwei[skill] # 技能系统
pip install wuwei[gateway] # 多平台网关
pip install wuwei[memory] # 记忆系统
pip install wuwei[observability] # 可观测性
pip install wuwei[sandbox] # 沙箱执行
pip install wuwei[all] # 全部扩展
快速开始
最小示例
import asyncio
from wuwei import Agent, LLMGateway, tool
# 定义工具
@tool
def get_weather(city: str) -> str:
"""获取城市天气"""
return f"{city} 今天晴天,25°C"
# 创建 Agent
llm = LLMGateway.from_env()
agent = Agent(llm=llm, tools=[get_weather])
# 运行
async def main():
result = await agent.run("北京今天天气怎么样?")
print(result)
asyncio.run(main())
多提供商支持
from wuwei import LLMGateway
# OpenAI(默认)
llm = LLMGateway.from_env(provider="openai")
# 智谱 AI
llm = LLMGateway.from_env(provider="zhipu")
# Anthropic
llm = LLMGateway.from_env(provider="anthropic")
# 阿里云 DashScope
llm = LLMGateway.from_env(provider="dashscope")
# Ollama(本地模型)
llm = LLMGateway.from_env(provider="ollama")
状态图编排
from wuwei.graph import StateGraph, State, END
from wuwei.graph.channels import LastValue, Topic
async def llm_node(state: State, config: dict) -> State:
# 调用 LLM
return state
async def tool_node(state: State, config: dict) -> State:
# 执行工具
return state
graph = StateGraph(State)
graph.add_node("llm", llm_node)
graph.add_node("tool", tool_node)
graph.add_edge("llm", "tool")
graph.add_edge("tool", END)
graph.set_entry_point("llm")
app = graph.compile()
state = await app.invoke(State())
中间件系统
from wuwei.middleware import Middleware, MiddlewareStack, ContextCompressionMiddleware
class LoggingMiddleware(Middleware):
async def before_llm(self, ctx):
print(f"LLM 调用 - 消息数: {len(ctx.state.messages)}")
return ctx
stack = MiddlewareStack()
stack.add(LoggingMiddleware())
stack.add(ContextCompressionMiddleware(llm=llm, trigger_tokens=4000))
agent = Agent(llm=llm, tools=tools, middleware=stack)
Output Parsers
from wuwei.parsers import JsonOutputParser, PydanticOutputParser
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int
# JSON 解析
parser = JsonOutputParser()
result = parser.parse('{"name": "test", "age": 25}')
# Pydantic 验证
parser = PydanticOutputParser(schema=User)
result = parser.parse('{"name": "test", "age": 25}')
# result = User(name="test", age=25)
插件系统
from wuwei.plugin import PluginLoader, PluginRegistry
# 加载插件
loader = PluginLoader("plugins/")
plugins = loader.load_all()
# 注册插件
registry = PluginRegistry()
for plugin in plugins:
registry.register(plugin)
# 获取插件钩子和工具
hooks = registry.get_hook("pre_tool_call")
tools = registry.list_tools()
MCP 支持
from wuwei.mcp import MCPConfig, MCPSessionManager
config = MCPConfig.load()
session = MCPSessionManager(config)
await session.connect_all()
tools = session.get_all_tools()
agent = Agent(llm=llm, tools=tools)
多 Agent 协作
from wuwei.agent import Swarm, TeamMember
leader = Agent(llm=llm, tools=[...])
members = [
TeamMember(name="researcher", agent=researcher, role="研究员"),
TeamMember(name="writer", agent=writer, role="写手"),
]
swarm = Swarm(leader=leader, members=members)
result = await swarm.run("写一篇关于 AI 的文章")
项目结构
wuwei/
├── core/ # 核心抽象层(Runnable/消息/错误)
├── llm/ # LLM 网关(6 个适配器)
├── tools/ # 工具系统(11 组内置工具)
├── agent/ # Agent(单 Agent + 多 Agent Swarm)
├── graph/ # 状态图编排(StateGraph/检查点/Channels)
├── middleware/ # 中间件系统(日志/HITL/上下文压缩)
├── mcp/ # MCP 协议支持
├── skill/ # 技能系统
├── gateway/ # 多平台网关(微信/钉钉/飞书/Telegram/Webhook)
├── sandbox/ # 沙箱执行
├── observability/ # 可观测性(OpenTelemetry)
├── config/ # YAML 配置
├── plugin/ # 插件系统
├── parsers/ # Output Parsers
├── streaming/ # 流式模式
└── tests/ # 测试(72 个)
内置工具
| 工具组 | 工具 |
|---|---|
time |
get_now — 获取当前时间 |
file |
read_file / write_file / append_file / replace_in_file / delete_file / list_files |
git |
git_status / git_diff / git_log / git_show / git_add / git_commit |
python |
run_python — 执行 Python 脚本 |
npm |
npm_list_scripts / npm_run / npm_install |
calc |
calculate — 安全数学表达式计算 |
rag |
ingest_document / search_knowledge — RAG 文档导入与检索 |
json |
json_parse / json_extract — JSON 处理 |
http |
http_get / http_post — HTTP 请求 |
text |
text_replace / text_split / text_join / text_upper / text_lower / text_trim |
skill |
list_skills / load_skill / run_skill_script — 技能系统 |
文档
测试
# 运行所有测试
pytest
# 运行特定测试
pytest tests/test_core.py -v
# 运行框架完整性测试
pytest tests/test_framework.py -v
贡献
欢迎贡献!请参阅 CONTRIBUTING.md。
许可证
Apache-2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
wuwei-2.2.0.tar.gz
(133.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
wuwei-2.2.0-py3-none-any.whl
(141.6 kB
view details)
File details
Details for the file wuwei-2.2.0.tar.gz.
File metadata
- Download URL: wuwei-2.2.0.tar.gz
- Upload date:
- Size: 133.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91e0408bd3e3cebded14fd929adf380beb3975401e98f6336cf40cd4313102fc
|
|
| MD5 |
925cab3f381a254fc4a62b49517d213c
|
|
| BLAKE2b-256 |
56d6263f89cb1c3a20f85dba38927614784311565377333b9c1697c13846e3cb
|
File details
Details for the file wuwei-2.2.0-py3-none-any.whl.
File metadata
- Download URL: wuwei-2.2.0-py3-none-any.whl
- Upload date:
- Size: 141.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bed1924f4d87acb00d22d4c2edf927b858b74fde64665cec939e1180c9726519
|
|
| MD5 |
11ec798aa1e9c4fb486e59b5041e78ff
|
|
| BLAKE2b-256 |
e50cd9ab2bde3ff738a9a133dd9d62b416fecfd5b34f4b872961ee1977ebb96f
|