> ## Documentation Index
> Fetch the complete documentation index at: https://docs.siflow.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# Vercel AI SDK

> 了解如何使用 Vercel AI SDK 的 @ai-sdk/openai-compatible provider，接入 siflow endpoint，并构建 TypeScript AI 应用。

[Vercel AI SDK](https://github.com/vercel/ai) 是用于构建 AI 应用的 TypeScript 工具包。它的 `@ai-sdk/openai-compatible` provider 可连接任意 OpenAI-compatible endpoint，因此可以接入 siflow endpoint。

本文默认模型为 `glm-5.2`；完整模型列表和价格请查看 [模型广场](https://console.siflow.cn/model-inference/models)。

## 配置

在 [API 密钥](https://console.siflow.cn/model-inference/api_keys) 页面创建 API Key，并将其导出为环境变量：

```bash theme={null}
export SIFLOW_API_KEY="<Your API Key>"
```

安装 `ai` 和 `@ai-sdk/openai-compatible`，然后创建一个 provider：

```ts theme={null}
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { generateText } from 'ai';

const siflow = createOpenAICompatible({
  name: 'siflow',
  baseURL: 'https://api.siflow.cn/model-api/v1',
  apiKey: process.env.SIFLOW_API_KEY,
});
```

| 字段               | 说明                                                                                                      |
| ---------------- | ------------------------------------------------------------------------------------------------------- |
| `baseURL`        | siflow OpenAI-compatible endpoint：`https://api.siflow.cn/model-api/v1`。provider 会追加 `/chat/completions` |
| `apiKey`         | 从环境变量读取的 API Key                                                                                        |
| `siflow('<id>')` | 模型 ID，例如 `siflow('glm-5.2')`                                                                            |

请使用 `@ai-sdk/openai-compatible`，不要使用 `@ai-sdk/openai`。后者面向 OpenAI 自有 API，并默认使用 Responses API；compatible provider 用于这类自定义 OpenAI-style endpoints。

如果要使用 SDK 的 tool-calling / `streamText` tool 功能，请选择原生支持 tool calling 的模型。

## 验证

生成文本。如果返回正常回复，即表示连接成功：

```ts theme={null}
const { text } = await generateText({
  model: siflow('glm-5.2'),
  prompt: 'In one short sentence, which model are you?',
});
console.log(text);
// → I'm GLM, a large language model developed by Z.ai.
```
