当前位置:首页>python>一个Python库打通所有主流LLM,还附赠桌面AI助手,开发者狂喜

一个Python库打通所有主流LLM,还附赠桌面AI助手,开发者狂喜

  • 2026-10-11 07:20:17
一个Python库打通所有主流LLM,还附赠桌面AI助手,开发者狂喜
兄弟们,作为一个天天跟各种LLM API打交道的开发者,最烦的是什么?是每换一个模型就得重学一套接口、重新配一次环境。OpenAI、Anthropic、Google、Mistral、Cohere……每个厂商都有自己的SDK、自己的调用方式。你想做个对比评测或者搭建多模型切换的应用,写代码的时间比调模型还长。

最近我看到一个项目,来自 Andrew Ng 团队的 aisuite,瞬间觉得这痛点有救。它不仅用一个统一接口覆盖了十多个主流 LLM 提供商,还内置了一套 Agent 工具框架,甚至顺便打包了一个桌面AI助手 OpenCoworker。不用再啃各家文档写几百行胶水代码——改一行模型名,就能在 GPT-4o、Claude、Gemini 之间自由切换。

它到底是什么,解决什么问题

aisuite 是一个轻量级 Python 库,核心提供两层能力:

  • Chat Completions API
    :用 OpenAI 风格的接口统一调用 OpenAI、Anthropic、Google、Mistral、Hugging Face、AWS、Cohere、Ollama、OpenRouter 等几乎所有主流 LLM。你只需把模型名写成 provider:model 格式,库自动帮你路由到对应厂商,参数(temperature、max_tokens、tools等)全都保持一致。
  • Agents API + Toolkits + MCP
    :在统一调用之上,它还提供了工具调用、多轮 agent 循环、工具箱(文件、Git、Shell)和对 MCP 协议的原生支持。这意味着你不仅可以写一次代码切换模型,还能把具体功能(查天气、读文件、搜代码库)像插件一样挂给模型用。

额外值得一提的,是这个仓库里附带的桌面应用 OpenCoworker ——基于 aisuite 构建,安装后可以常驻桌面,帮你做深度研究、读文件、发 Slack、生成 PDF 报告,甚至定时发日报。你自己的 API key 全在本地,数据不出机器。

三个核心亮点,直击开发日常

亮点一:一行代码换模型,告别多 SDK 割裂

这是 aisuite 最直接的价值。你只需要 import aisuite,创建一个 Client,然后传 "openai:gpt-4o" 或 "anthropic:claude-3-5-sonnet-20240620" 就能发出请求。所有公共参数(temperature、max_tokens、tools、stream 等)一致。最爽的是做模型对比时,直接写个 models 列表,循环调用,代码量减少80%。

import aisuite as aiclient = ai.Client()models = ["openai:gpt-4o", "anthropic:claude-3-5-sonnet-20240620"]messages = [    {"role": "system", "content": "Respond in Pirate English."},    {"role": "user", "content": "Tell me a joke."},]for model in models:    response = client.chat.completions.create(        model=model,        messages=messages,        temperature=0.75    )    print(response.choices[0].message.content)

亮点二:给模型“动手能力”——工具调用和 Agent 框架

光对话不够,很多需求需要模型调用外部函数。aisuite 把工具调用简化成一个装饰器风格:你写一个纯 Python 函数,加上 type hints 和 docstring,直接作为 tools 参数传入。通过设置 max_turns,库会自动完成“模型生成工具调用→执行函数→返回结果给模型”的多轮循环。

def will_it_rain(location: str, time_of_day: str):    """Check if it will rain in a location at a given time today.    Args:        location (str): Name of the city        time_of_day (str): Time of the day in HH:MM format.    """    return "YES"client = ai.Client()response = client.chat.completions.create(    model="openai:gpt-4o",    messages=[{        "role": "user",        "content": "I live in San Francisco. Can you check for weather "                   "and plan an outdoor picnic for me at 2pm?"    }],    tools=[will_it_rain],    max_turns=2  # Maximum number of back-and-forth tool calls)print(response.choices[0].message.content)

更高级的是 Agents API,它允许你声明一个 Agent,绑定多个工具箱(文件、Git、Shell),用 Runner 跑起来。还支持工具审批策略、持久化状态存储、产物追踪。这对构建自动化代码助手、文档机器人特别实用。

import aisuite as aifrom aisuite import Agent, Runneragent = Agent(    name="repo-helper",    model="anthropic:claude-sonnet-4-6",    instructions="You are a careful repo assistant. Use your tools to answer from the code.",    tools=[*ai.toolkits.files(root="."), *ai.toolkits.git(root=".")],)result = Runner.run(agent, "What changed in the last commit? Summarize in 3 bullets.")print(result.final_output)

亮点三:原生支持 MCP,接入工具生态零门槛

如果你用过 Model Context Protocol(MCP),就知道它的威力——你可以任意挂载一个 MCP server 的工具。aisuite 直接内置了 MCP 支持(需 pip install aisuite[mcp]),用法简洁到只需要在 tools 列表里声明一个 "type": "mcp" 的字典。

client = ai.Client()response = client.chat.completions.create(    model="openai:gpt-4o",    messages=[{"role": "user", "content": "List the files in the current directory"}],    tools=[{        "type": "mcp",        "name": "filesystem",        "command": "npx",        "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/directory"]    }],    max_turns=3)print(response.choices[0].message.content)

十分钟上手,从安装到跑通第一个任务

1. 安装 aisuite 库

pip install aisuite               # 基础包,不含任何 provider SDKpip install 'aisuite[anthropic]'  # 带特定 provider SDKpip install 'aisuite[all]'        # 一次性装所有支持的 SDK

2. 配置 API 密钥按照 Chat Completions 快速入门 设置环境变量即可。

3. 跑一个 Chat Completions 示例直接复制上面的“一行代码换模型”代码段,换成你自己的 API key,运行就能看到两种模型的回复对比。

4. 尝试桌面助手 OpenCoworker直接下载安装包(macOS 或 Windows),带上你自己的 API key(或用 Ollama 跑本地模型),它会常驻在桌面上。你可以让它阅读文件、发邮件、生成日报——所有操作数据只留在你机器上。macOS 下载  · Windows 下载

总结——为什么值得收藏

aisuite 不是又一个“包装过的 API 调用库”,它把模型调用、工具集成、Agent 化、桌面应用整合在一个框架里,而且保持了极简的设计。无论你是想快速做模型对比、构建带工具调用的智能助手,还是需要一个桌面 AI 副驾驶员,它都能给你省下大量折腾时间。代码开源(MIT 协议),可以直接商用。

一句话:把原本需要几周集成的事,压缩到几分钟。收藏起来,下次遇到多模型需求,第一个想起它。

┌───────────────────────────────────────────────┐│                 OpenCoworker                  │   agent harness for doing everyday tasks├───────────────────────────────────────────────┤│        Agents API  ·  Toolkits  ·  MCP        │   build agents across multiple LLMs├───────────────────────────────────────────────┤│             Chat Completions API              │   one API across multiple LLM providers├────────┬───────────┬────────┬────────┬────────┤│ OpenAI │ Anthropic │ Google │ Ollama │ Others │└────────┴───────────┴────────┴────────┴────────┘

最新文章

随机文章