OhMyGPT
SDK 集成

Vercel AI SDK

用你熟悉的 OpenAI provider,在 Vercel AI SDK 中调用 OhMyGPT 的所有模型。

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

安装

npm install ai @ai-sdk/openai zod

创建 provider

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:

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);
This is a mock reply from OhMyGPT. Your integration works; switch to a real model when ready.
21 17

换成 zhipu/glm-5.3-flash:

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

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

流式输出

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);
}

调用工具

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

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);
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 的回复,整个循环不花一分钱。请求与响应的格式见工具调用。

选择供应商

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

npm install @ai-sdk/openai-compatible
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' } },
  },
});

字段列表见供应商路由。

Anthropic provider

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

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.',
});

本页目录