andrewyng/aisuite

GitHub: andrewyng/aisuite

aisuite 是一个轻量级 Python 库,提供跨多家大语言模型提供商的统一 Chat 和 Agent 接口,解决 API 割裂和切换成本问题。

Stars: 14740 | Forks: 1544


# aisuite [![PyPI](https://img.shields.io/pypi/v/aisuite)](https://pypi.org/project/aisuite/) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) `aisuite` 是一个轻量级的 Python 库,用于构建 LLMs,分为两层:跨提供商的统一 **Chat Completions API**,以及在其之上带有工具和工具包的 **Agents API**。本仓库也是 **OpenCoworker** 的主页,这是一个使用 aisuite 构建的桌面 AI 同事: ``` ┌───────────────────────────────────────────────┐ │ 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 │ └────────┴───────────┴────────┴────────┴────────┘ ``` * **[Chat Completions API](#chat-completions)** — 适用于 *OpenAI, Anthropic, Google, Mistral, Hugging Face, AWS, Cohere, Ollama, OpenRouter* 等的统一、OpenAI 风格接口。只需更改一个字符串即可切换提供商。 * **[Agents API · Toolkits · MCP](#agents)** — 将真实的 Python 函数作为工具提供给模型,运行多轮循环,附加现成的工具包(文件、git、shell)或任何 MCP server,并通过工具策略来管理这一切。 * **[OpenCoworker](docs/opencoworker-quickstart.md)** — 一个使用 aisuite 构建的桌面 AI 同事,作为应用程序发布,用于处理日常任务。 ## 安装说明 ### aisuite 库 安装基础包,或者包含你计划使用的提供商的 SDK: ``` pip install aisuite # base package, no provider SDKs pip install 'aisuite[anthropic]' # with a specific provider's SDK pip install 'aisuite[all]' # with all provider SDKs ``` 你还需要为你调用的提供商准备 API 密钥 —— [Chat Completions 快速入门](docs/chat-completions-quickstart.md) 涵盖了密钥设置和你的首次调用。 ### OpenCoworker 应用程序 下载安装程序并自带 API 密钥(或使用 Ollama 运行本地模型): [**⬇ macOS (Apple Silicon)**](https://github.com/andrewyng/aisuite/releases/latest/download/OpenCoworker-macos-arm64.dmg)  ·  [**⬇ Windows 10/11 (x64)**](https://github.com/andrewyng/aisuite/releases/latest/download/OpenCoworker-windows-setup.exe)  ·  [OpenCoworker 快速入门](docs/opencoworker-quickstart.md) ## Chat Completions — 跨提供商的统一 API Chat API 为模型交互提供了高级抽象。它以与提供商无关的方式支持所有核心参数(`temperature`、`max_tokens`、`tools` 等),并标准化了请求和响应结构,以便你可以专注于业务逻辑,而不是 SDK 差异。 模型名称采用 `:` 格式;aisuite 会使用正确的参数将调用路由到相应的提供商: ``` import aisuite as ai client = 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) ``` **→ 快速入门:** [docs/chat-completions-quickstart.md](docs/chat-completions-quickstart.md) — 安装、密钥设置、本地模型以及更多示例。 ## Agents — 为模型提供真实工具 aisuite 将工具调用变成了一行代码:传入普通的 Python 函数,它就会生成 schema、执行调用,并将结果反馈给模型。 ### 使用 `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) ``` 设置了 `max_turns` 后,aisuite 会发送你的消息,执行模型请求的任何工具调用,将结果返回给模型,并重复此过程直到对话完成。如果你想继续对话,`response.choices[0].intermediate_messages` 会携带完整的工具交互历史记录。 想要完全的手动控制?省略 `max_turns` 并传入 OpenAI 格式的 JSON 工具规范 —— aisuite 会返回模型的工具调用请求,由你自己运行循环。有关这两种风格的示例,请参见 `examples/tool_calling_abstraction.ipynb`。 ### Agents API 对于运行时间较长、结构化的工作,有一个一流的 Agents API:只需声明一次 agent,使用 `Runner` 运行它,并附加 **toolkits** —— 用于文件、git 和 shell 的预构建沙盒工具系列: ``` import aisuite as ai from aisuite import Agent, Runner agent = 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) ``` Agents API 还为你提供了生产环境所需的各种组件: * **工具策略** — `RequireApprovalPolicy`、允许/拒绝列表,或者使用你自己的可调用对象来决定运行哪些工具调用。 * **状态存储** — 持久化并恢复运行(内存、文件或 Postgres),并跨进程继续对话。 * **Artifacts 与 tracing** — 捕获 agent 生成的内容及其沿途执行的每一个步骤。 ### MCP 工具 aisuite 原生支持 [Model Context Protocol](https://modelcontextprotocol.io/docs/getting-started/intro),因此无需样板代码即可将任何 MCP server 的工具交给模型(`pip install 'aisuite[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) ``` 对于可重用的连接、安全过滤器和工具前缀,请使用显式的 `MCPClient`。 **→ 快速入门:** [docs/agents-quickstart.md](docs/agents-quickstart.md) — 手动工具处理、完整的 Agents API、策略、状态存储以及深入的 MCP 介绍。 ## 扩展 aisuite:添加 Provider 可以通过实现一个轻量级 adapter 来添加新的 provider。系统使用命名约定来进行发现: | 元素 | 约定 | | --------------- | ---------------------------------- | | **Module file** | `_provider.py` | | **Class name** | `Provider` (首字母大写) | 示例: ``` # providers/openai_provider.py class OpenaiProvider(BaseProvider): ... ``` 此约定确保了一致性,并支持自动加载新的集成。 ## License 基于 **MIT License** 发布 — 可免费用于商业和非商业用途。
标签:AI智能体, AI风险缓解, DLL 劫持, Petitpotam, 人工智能, 多模型接口, 大语言模型, 生成式AI, 用户模式Hook绕过, 索引, 逆向工具