

























今天在 GitHub Trending 上看到一个有意思的项目:aisuite,由 Andrew Ng 主导开源。它解决了一个在 LLM 应用开发中非常真实的痛点:各家 API 接口不统一、切换模型要改大量代码。
aisuite 是一个轻量级 Python 库,为 LLM 应用开发提供两层抽象:
项目同时包含一个实战级桌面应用 OpenCoworker(源代码在 platform/ 目录下),可作为基于 aisuite 构建 Agent harness 的参考实现。
核心特性一览:
| 特性 | 说明 |
|---|---|
| 统一模型名格式 | <provider>:<model-name>,如 openai:gpt-4o |
| 零改动切换提供商 | 改一个字符串即可切换底层模型 |
| 原生 Tool Calling | 直接传入 Python 函数,aisuite 自动生成 schema 并执行 |
| Agents API | 声明式 Agent + Runner,内置文件/git/Shell 工具包 |
| MCP 原生支持 | 兼容 Model Context Protocol,可接入任意 MCP Server |
| 多后端支持 | 云服务 + 本地(Ollama)均可 |
┌───────────────────────────────────────────────┐
│ OpenCoworker │ Agent 应用层
├───────────────────────────────────────────────┤
│ Agents API · Toolkits · MCP │ Agent 构建层
├───────────────────────────────────────────────┤
│ Chat Completions API │ 统一 API 层
├────────┬───────────┬────────┬────────┬────────┤
│ OpenAI │ Anthropic │ Google │ Ollama │ Others │ 提供商适配层
└────────┴───────────┴────────┴────────┴────────┘
aisuite 通过一套约定优于配置的机制实现提供商的即插即用。新增提供商只需遵循命名规范:
# providers/openai_provider.py
class OpenaiProvider(BaseProvider):
def chat_completions_create(self, model, messages, **kwargs):
# 将统一参数转换为 OpenAI SDK 的调用格式
...
模块文件命名为 <provider>_provider.py,类名命名为 <Provider>Provider,系统会自动发现并加载,无需注册代码。
当设置 max_turns 参数时,aisuite 内部维护一个多轮对话循环:
max_turns 上限完整的交互历史保存在 response.choices[0].intermediate_messages 中,方便序列化后继续对话。
Agents API 提供了更接近生产级 Agent 框架的抽象:
agent = Agent(
name="repo-helper",
model="anthropic:claude-sonnet-4-6",
instructions="You are a careful repo assistant.",
tools=[*ai.toolkits.files(root="."), *ai.toolkits.git(root=".")],
)
内置工具包:
ai.toolkits.files(root):沙箱化文件系统读写ai.toolkits.git(root):在指定目录执行 git 命令ai.toolkits.shell(root):受控 Shell 执行aisuite 原生支持 Model Context Protocol(需安装 pip install 'aisuite[mcp]'),可将任意 MCP Server 的工具直接注入模型上下文:
tools=[{
"type": "mcp",
"name": "filesystem",
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/dir"]
}]
对于需要复用、安全过滤或工具名前缀隔离的场景,可使用 MCPClient 进行显式管理。
# 仅安装基础包(不含任何提供商 SDK)
pip install aisuite
# 安装时一并安装指定提供商的 SDK
pip install 'aisuite[anthropic]'
pip install 'aisuite[openai]'
# 安装所有提供商 SDK
pip install 'aisuite[all]'
# 含 MCP 支持
pip install 'aisuite[mcp]'
import aisuite as ai
client = ai.Client()
models = [
"openai:gpt-4o",
"anthropic:claude-3-5-sonnet-20240620"
]
messages = [
{"role": "system", "content": "请用海盗英语回复。"},
{"role": "user", "content": "给我讲个笑话。"},
]
for model in models:
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=0.75
)
print(f"[{model}]\n{response.choices[0].message.content}\n")
切换模型只需将 "openai:gpt-4o" 改为 "google:gemini-pro" 即可,无需改动任何调用代码。
def get_weather(city: str, date: str):
"""获取指定城市某天的天气。
Args:
city (str): 城市名称
date (str): 日期,格式 YYYY-MM-DD
"""
# 实际场景中这里会调用天气 API
return f"{city} 在 {date} 的天气:晴,26°C"
client = ai.Client()
response = client.chat.completions.create(
model="openai:gpt-4o",
messages=[{"role": "user", "content": "北京明天适合爬山吗?"}],
tools=[get_weather],
max_turns=3 # 最多 3 轮工具调用循环
)
print(response.choices[0].message.content)
import aisuite as ai
from aisuite import Agent, Runner
agent = Agent(
name="repo-analyzer",
model="anthropic:claude-sonnet-4-6",
instructions="你是一个代码分析助手,只能通过工具获取信息,不要猜测。",
tools=[*ai.toolkits.files(root="."), *ai.toolkits.git(root=".")],
)
result = Runner.run(
agent,
"最近一次 commit 改了哪些文件?用中文总结改动意图(3 条要点)"
)
print(result.final_output)
生产环境中通常需要对工具调用进行治理,aisuite 内置了多种策略:
from aisuite.agents.policies import RequireApprovalPolicy
agent = Agent(
name="safe-agent",
model="openai:gpt-4o",
tools=[*ai.toolkits.shell(root=".")],
# 所有 Shell 工具调用前需人工确认
tool_policy=RequireApprovalPolicy(tools=["shell.*"])
)
也支持 allow/deny list 和自定义 callable 策略函数。
from aisuite.agents.state import FileStateStore
store = FileStateStore(path="./agent_runs.json")
agent = Agent(name="persistent-agent", model="openai:gpt-4o", state_store=store)
# 第一次运行
result1 = Runner.run(agent, "帮我分析这个项目")
# 序列化状态后,可在另一个进程/请求中恢复并继续
Runner.run(agent, "接着刚才的分析,补充性能优化建议")
ModuleNotFoundError: No module named 'anthropic'原因:pip install aisuite 只安装基础包,不包含任何提供商的 SDK。
解决:按需安装对应 extra,如 pip install 'aisuite[anthropic]',或一次性安装全部:pip install 'aisuite[all]'。
max_turns 用完了但模型还没给出最终答案怎么办?原因:复杂任务可能需要更多轮工具调用。
解决:增大 max_turns 参数,或检查工具函数实现是否有 bug 导致模型无法获得有效返回。可打印 response.choices[0].intermediate_messages 调试每轮交互。
解决:aisuite 通过 OpenAI 兼容接口支持 Ollama,安装 pip install 'aisuite[ollama]' 后使用 ollama:model-name 格式:
client.chat.completions.create(
model="ollama:llama3",
messages=[{"role": "user", "content": "Hello"}]
)
确保 Ollama 服务已在本地运行(ollama serve)。
npx: command not found原因:使用 MCP 工具时需要系统已安装 Node.js 和 npx。 解决:安装 Node.js(含 npx),或使用已安装 MCP Server 的 Python 包替代。
解决:aisuite 内置 Artifacts & Tracing 机制,可在 Runner.run 时传入 tracing=True,或在 Agent 配置中指定 tracing backend,捕获每次工具调用、模型响应和最终产出。
aisuite 的核心价值在于降低 LLM 应用的多提供商切换成本,同时提供从简单补全到生产级 Agent 的渐进式抽象。对于需要在多个 LLM 之间做 A/B 测试、或希望规避单一提供商锁定风险的团队,这是一个非常实用的基础库。
亮点总结:
项目地址:https://github.com/andrewyng/aisuite
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。