# Vercel AI SDK (/docs/zh-CN/sdk/vercel-ai-sdk)

[AI SDK](https://ai-sdk.dev) 通过它标准的 OpenAI provider 接入 OhMyGPT。把 OhMyGPT 的 base URL 和 Key 传给 `createOpenAI`，`generateText`、`streamText` 和工具调用就能跑在 Claude、GPT、GLM 以及模型列表里的所有对话模型上。示例使用 `ai` 7 和 `@ai-sdk/openai` 4。

## 安装 [#安装]

```bash
npm install ai @ai-sdk/openai zod
```

## 创建 provider [#创建-provider]

```ts title="ohmygpt.ts"
import { createOpenAI } from '@ai-sdk/openai';

export const ohmygpt = createOpenAI({
  baseURL: 'https://api.ohmygpt.com/v1',
  apiKey: process.env.OHMYGPT_API_KEY,
});
```

`GET /v1/models` 中的任意模型 ID 都可以传给 `ohmygpt(...)`。请求发往 OhMyGPT 的 Responses 端点，所有对话模型都支持这个端点。

## 生成文本 [#生成文本]

先用免费测试模型 `ohmygpt/mock`，再换成别的 ID：

```ts
import { generateText } from 'ai';
import { ohmygpt } from './ohmygpt';

const { text, usage } = await generateText({
  model: ohmygpt('ohmygpt/mock'),
  prompt: 'Write a haiku about the sea.',
});
console.log(text);
console.log(usage.inputTokens, usage.outputTokens);
```

```text
This is a mock reply from OhMyGPT. Your integration works; switch to a real model when ready.
21 17
```

换成 `zhipu/glm-5.3-flash`：

```text
Waves crash on the shore—
salt spray kisses the warm sand,
the tide pulls me home.
20 383
```

输出 token 数包含了模型的推理 token。

## 流式输出 [#流式输出]

```ts
import { streamText } from 'ai';
import { ohmygpt } from './ohmygpt';

const result = streamText({
  model: ohmygpt('zhipu/glm-5.3-flash'),
  prompt: 'Write a haiku about the sea.',
});
for await (const chunk of result.textStream) {
  process.stdout.write(chunk);
}
```

## 设置推理强度 [#设置推理强度]

`@ai-sdk/openai` 按模型 ID 判断是不是推理模型，只认 `gpt-5.6-luna` 这样不带前缀的 OpenAI ID。用 `openai/gpt-5.6-luna`、`zhipu/glm-5.3-flash` 这类 OhMyGPT 的 ID 时，它会直接丢掉 `reasoningEffort`，也不报错。加上 `forceReasoning: true` 才会发出去：

```ts
import { generateText } from 'ai';
import { ohmygpt } from './ohmygpt';

const { text } = await generateText({
  model: ohmygpt('openai/gpt-5.6-luna'),
  prompt: 'Prove that there are infinitely many primes.',
  providerOptions: {
    openai: { reasoningEffort: 'high', forceReasoning: true },
  },
});
```

`@ai-sdk/openai-compatible`（见[选择供应商](#选择供应商)）会原样发送 `reasoningEffort`，不需要这样设置。

## 调用工具 [#调用工具]

`execute` 负责执行工具，`stopWhen` 让 SDK 把结果发回模型，拿到最终回答：

```ts
import { generateText, isStepCount, tool } from 'ai';
import { z } from 'zod';
import { ohmygpt } from './ohmygpt';

const { text } = await generateText({
  model: ohmygpt('zhipu/glm-5.3-flash'),
  tools: {
    getWeather: tool({
      description: 'Get the current weather for a city',
      inputSchema: z.object({ city: z.string() }),
      execute: async ({ city }) => ({ city, temperatureC: 18, condition: 'cloudy' }),
    }),
  },
  stopWhen: isStepCount(3),
  prompt: 'What is the weather in Paris?',
});
console.log(text);
```

```text
The current weather in Paris is **cloudy** with a temperature of **18°C** (about 64°F). It's a mild day, so you might want a light jacket if you're heading out!
```

在 `ohmygpt/mock` 上，同一段程序会以 `city: ""` 调用 `getWeather`，然后打印 mock 的回复，整个循环不花一分钱。请求与响应的格式见[工具调用](/docs/zh-CN/tool-calling)。

## 选择供应商 [#选择供应商]

要发送 OhMyGPT 的 `provider` 对象（例如优先选最低价），请使用 `@ai-sdk/openai-compatible`。它会把 `providerOptions.ohmygpt` 复制到请求体中：

```bash
npm install @ai-sdk/openai-compatible
```

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

const ohmygpt = createOpenAICompatible({
  name: 'ohmygpt',
  baseURL: 'https://api.ohmygpt.com/v1',
  apiKey: process.env.OHMYGPT_API_KEY,
});

const { text } = await generateText({
  model: ohmygpt('zhipu/glm-5.3-flash'),
  prompt: 'Say hi.',
  providerOptions: {
    ohmygpt: { provider: { sort: 'price' } },
  },
});
```

字段列表见[供应商路由](/docs/zh-CN/provider-routing)。

## Anthropic provider [#anthropic-provider]

如果你的代码用的是 `@ai-sdk/anthropic`，用同样的方式指向 OhMyGPT 即可。它的 `baseURL` 要带 `/v1`：

```ts
import { createAnthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';

const anthropic = createAnthropic({
  baseURL: 'https://api.ohmygpt.com/v1',
  apiKey: process.env.OHMYGPT_API_KEY,
});

const { text } = await generateText({
  model: anthropic('anthropic/claude-haiku-4-5'),
  prompt: 'Say hi.',
});
```
