Vercel AI SDK
用 OpenAI 相容 provider 在 Vercel AI SDK 中呼叫 HopBase,含串流輸出與工具呼叫。
Vercel 的 AI SDK 是建構 AI 應用的 TypeScript 工具包。HopBase 透過官方的 @ai-sdk/openai-compatible provider 接入;Claude 模型用 @ai-sdk/anthropic。
| 設定項 | 值 |
|---|---|
| 套件 | @ai-sdk/openai-compatible |
baseURL | https://api.hop-base.com/v1 |
apiKey | 控制台建立的金鑰,以 Authorization: Bearer 發送 |
| 模型 ID | 用這把金鑰呼叫 GET /v1/models 回傳的原樣 ID,例如 deepseek-v4.1-flash-1017 |
已在生產環境用 ai 7.0 與 @ai-sdk/openai-compatible 3.0 實測:generateText、streamText 和 tool() 工具呼叫都正常,用量正常回傳。
HopBase 的金鑰只能呼叫它所含分組裡的模型。在控制台建立金鑰時,選中提供目標模型的分組——例如 deepseek-v4.1-flash-1017 選DeepSeek,deepseek-v4.1-flash-1017-sg 選DeepSeek 新加坡機房,gemini-3.8-flash 選Gemini 全系(含生圖)。金鑰不含該模型所在分組時會回傳 404 model_not_found。
安裝
npm i ai @ai-sdk/openai-compatible zod把金鑰放在環境變數裡:
export HOPBASE_API_KEY=sk-your-key建立 provider
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
export const hopbase = createOpenAICompatible({
name: 'hopbase',
baseURL: 'https://api.hop-base.com/v1',
apiKey: process.env.HOPBASE_API_KEY,
includeUsage: true, // report token usage on streamed responses
});hopbase('<模型 ID>') 回傳一個對話模型,ID 必須與 GET /v1/models 完全一致。
生成文字
import { generateText } from 'ai';
import { hopbase } from './hopbase';
const { text } = await generateText({
model: hopbase('deepseek-v4.1-flash-1017'),
prompt: 'Reply with one word: hello.',
});
console.log(text);串流輸出
import { streamText } from 'ai';
import { hopbase } from './hopbase';
const result = streamText({
model: hopbase('deepseek-v4.1-flash-1017'),
prompt: 'Count from 1 to 5.',
});
for await (const delta of result.textStream) process.stdout.write(delta);工具呼叫
import { generateText, tool, stepCountIs } from 'ai';
import { z } from 'zod';
import { hopbase } from './hopbase';
const { text } = await generateText({
model: hopbase('deepseek-v4.1-flash-1017'),
tools: {
getWeather: tool({
description: 'Get the weather for a city',
inputSchema: z.object({ city: z.string() }),
execute: async ({ city }) => ({ city, tempC: 21 }),
}),
},
stopWhen: stepCountIs(3),
prompt: 'What is the weather in Tokyo? Use the tool.',
});
console.log(text);換模型只需改 ID,例如用包含「Gemini 全系(含生圖)」分組的金鑰呼叫 hopbase('gemini-3.8-flash')。
Claude 模型
Claude 模型走 Anthropic Messages 協議。使用 @ai-sdk/anthropic,並且 baseURL 要保留 /v1:這個套件會在後面接上 /messages,請求最終發往 https://api.hop-base.com/v1/messages。
import { createAnthropic } from '@ai-sdk/anthropic';
import { generateText } from 'ai';
const hopbaseClaude = createAnthropic({
baseURL: 'https://api.hop-base.com/v1',
apiKey: process.env.HOPBASE_API_KEY,
});
const { text } = await generateText({
model: hopbaseClaude('claude-sonnet-5-5'),
prompt: 'Reply with one word: hello.',
});金鑰必須包含 Claude 分組。這一點與 Anthropic 官方 SDK 和 Claude Code 不同:後兩者填不帶路徑的 https://api.hop-base.com,自己接上 /v1/messages。
各模型價格見價格頁。
常見問題
| 現象 | 原因 |
|---|---|
401 | 程序裡沒有設定 HOPBASE_API_KEY,或金鑰已停用 |
404 model_not_found | 金鑰不含該模型所在分組,或 ID 寫簡了。重新查看 GET /v1/models |
只有 @ai-sdk/anthropic 的請求失敗 | baseURL 少了 /v1。請求位址必須以 /v1/messages 結尾 |
route_not_found | 金鑰屬於圖片或影片分組,沒有對話入口 |
下一步
- OpenCode:在程式設計助手裡設定同一個 provider 套件
- OpenAI SDK 與 Anthropic SDK:用官方 SDK 呼叫相同的入口
- 可用模型:查看一把金鑰能用的模型 ID