## andrewyng / aisuite
### OpenWorker
OpenWorker 是一款桌面端 AI 协作者,基于 aisuite 构建——现已迁移至独立仓库:andrewyng/openworker。OpenWorker 能够进行对话、执行深度研究,并在你的电脑上完成真实任务——包括在授权下读取文件、连接 Slack/邮件、生成 PDF、文档和电子表格,以及运行定时自动化任务。你可以使用自己的 API 密钥(OpenAI、Anthropic、Google),或通过 Ollama 完全本地运行;你的数据将保留在本地机器上。
⬇ 下载 macOS 版本 (macOS 13+,Apple Silicon) · ⬇ 下载 Windows 版本 (Windows 10/11,x64) · 快速开始
OpenWorker 的开发已迁移至新仓库。其源代码快照暂时保留在此处的 `platform/` 目录下,并将在未来版本中移除。
### aisuite
aisuite 是一个轻量级 Python 库,用于构建基于 LLM 的应用,分为两层:一个跨提供商的统一 Chat Completions API,以及在其之上构建的、包含工具和工具包的 Agents API。aisuite 也为 OpenWorker 提供支持,OpenWorker 是一款桌面端 AI 协作者,在其独立仓库中开发:
┌───────────────────────────────────────────────┐ │ OpenWorker (独立仓库) │ │ 用于执行日常任务的智能体框架 │ ├───────────────────────────────────────────────┤ │ Agents API · 工具包 · MCP │ │ 跨多个 LLM 构建智能体 │ ├───────────────────────────────────────────────┤ │ Chat Completions API │ │ 跨多个 LLM 提供商的统一 API │ ├────────┬───────────┬────────┬────────┬────────┤ │ OpenAI │ Anthropic │ Google │ Ollama │ 其他 │ └────────┴───────────┴────────┴────────┴────────┘
**Chat Completions API** — 一个统一的、OpenAI 风格的接口,支持 OpenAI、Anthropic、Google、Mistral、Hugging Face、AWS、Cohere、Ollama、OpenRouter、Requesty 等。只需更改一个字符串即可切换提供商。
**Agents API · 工具包 · MCP** — 将真实的 Python 函数作为工具赋予模型,运行多轮循环,附加现成的工具包(文件、git、shell)或任何 MCP 服务器,并通过工具策略进行管控。
**OpenWorker** — 一款使用 aisuite 构建的桌面端 AI 协作者,以应用程序形式发布,用于执行日常任务。在其独立仓库中开发。
### 安装
安装基础包,或包含你计划使用的提供商的 SDK:
pip install aisuite # 基础包,不含提供商 SDK pip install 'aisuite[anthropic]' # 包含特定提供商的 SDK pip install 'aisuite[all]' # 包含所有提供商的 SDK
你还需要为你调用的提供商准备 API 密钥——Chat Completions 快速入门指南涵盖了密钥设置和首次调用。
正在寻找 OpenWorker 桌面应用?请前往其发布页面下载。
### Chat Completions — 跨提供商的统一 API
聊天 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 — 安装、密钥设置、本地模型及更多示例。
#### 流式传输
设置 `stream=True` 可从任何支持的提供商(OpenAI、Anthropic、Ollama 以及兼容 OpenAI 的端点)获取 OpenAI 格式的块迭代器——相同的循环适用于所有提供商:
for chunk in client.chat.completions.create( model=model, messages=messages, stream=True ): print(chunk.choices[0].delta.content or "", end="", flush=True)
异步变体为 `await client.chat.completions.acreate(..., stream=True)`,并使用 `async for` 进行迭代。
工具调用同样支持流式传输:schema 字典和可调用对象照常传递给模型,块中包含增量 `delta.tool_calls` 片段供你组装和执行(流式传输是手动工具调用——不能与 `max_turns` 结合使用)。
### 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 # 工具调用的最大往返次数 )
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:声明一个智能体,使用 Runner 运行它,并附加工具包——为文件、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 co" )