Documentation

AI 集成

基于 Vercel AI SDK 的多厂商 AI 集成,支持 12 家厂商、100+ 模型

AI 集成层为多厂商大语言模型(LLM)提供统一的文本生成接口,基于 Vercel AI SDK,支持通过后台管理面板动态配置 API Key。

架构

模块结构

文件说明
src/integrations/ai/index.ts入口,getAIProvider(),配置提取
src/integrations/ai/providers.tsProvider 实例与模型映射
src/integrations/ai/models.ts模型元数据(id、label、能力、creditCost)
src/integrations/ai/types.ts类型定义
src/integrations/ai/utils.ts工具函数

快速开始

配置 API Key

进入 后台 → AI 配置,填写所需厂商的 API Key。也可通过环境变量配置。

在 API 路由中获取 Provider

import { getAIProvider } from "@/integrations/ai"
import { streamText } from "ai"

export async function POST(req: Request) {
  const provider = await getAIProvider()
  const model = provider.languageModel("openai/gpt-4o")

  const result = await streamText({
    model,
    messages: [{ role: "user", content: "你好" }],
  })

  return result.toDataStreamResponse()
}

使用模型元数据

import {
  getModelConfig,
  canUseModel,
  hasVisionSupport,
  getCreditCost,
  getModelsByProvider,
} from "@/integrations/ai"

const model = getModelConfig("openai/gpt-4o")
const { canUse } = canUseModel("openai/gpt-4o", isProUser)
const hasVision = hasVisionSupport("openai/gpt-4o")
const cost = getCreditCost("openai/gpt-4o")
const openaiModels = getModelsByProvider("openai")

模型能力

每个模型包含以下能力元数据:

能力说明
vision支持图片输入(多模态)
reasoning支持扩展思考 / 链式推理
pdf支持 PDF 文档处理

工具函数参考

函数说明
getModelConfig(modelId)根据 ID 获取模型元数据
canUseModel(modelId, isProUser)检查用户是否可使用该模型(层级校验)
hasVisionSupport(modelId)是否支持 vision
hasReasoningSupport(modelId)是否支持 reasoning
hasPdfSupport(modelId)是否支持 PDF
getMaxOutputTokens(modelId)最大输出 token 数
getModelParameters(modelId)默认参数(temperature、topP 等)
getCreditCost(modelId)单次调用积分消耗
getModelsByProvider(provider)按厂商列出模型
getModelsByTier(tier)按层级列出模型(free/pro)
getAvailableProviders()列出所有有模型的厂商
getAllModels()列出所有模型

模型层级

  • free: 所有用户可用
  • pro: 需要 Pro 订阅(canUseModel 会校验)

支持的厂商

厂商模型数配置项
OpenAI22ai_openai_api_key, ai_openai_base_url
Anthropic5ai_anthropic_api_key
Google10ai_google_api_key
xAI9ai_xai_api_key
Groq6ai_groq_api_key
Mistral11ai_mistral_api_key
Cohere3ai_cohere_api_key
DeepSeek2ai_deepseek_api_key, ai_deepseek_base_url
HuggingFace11ai_huggingface_api_key
Novita21ai_novita_api_key
SiliconFlow2ai_siliconflow_api_key
Baseten6ai_baseten_api_key

模型列表(概览)

厂商数量示例
OpenAI22gpt-4o, gpt-5, o3, o4-mini, gpt-5.1-codex, gpt-oss-20b
Anthropic5claude-haiku-4-5, claude-sonnet-4-5, claude-opus-4-5
Google10gemini-2.0-flash, gemini-2.5-pro, gemini-3-flash
xAI9grok-3-mini, grok-4, grok-4-fast-thinking, grok-code
Groq6llama-3.3-70b, qwen3-32b, kimi-k2
Mistral11ministral-3b, mistral-large, codestral, devstral
Cohere3command-a, command-a-thinking, command-r-plus
DeepSeek2deepseek-chat, deepseek-reasoner
HuggingFace11qwen3-4b, qwen3-30b, qwen3-235b
Novita21deepseek-v3.2, qwen3-coder-30b, glm-4.5, minimax-m2
SiliconFlow2deepseek-v3, qwen-2.5-72b
Baseten6deepseek-v3, qwen3-coder-480b, glm-4.7, kimi-k2

Reasoning 模型

capabilities.reasoning: true 的模型会使用 extractReasoningMiddleware,推理内容会包裹在 <think> 标签中并在最终输出中移除。如有推荐参数,可通过 getModelParameters 获取。

模型 ID 格式

模型 ID 格式为 provider/model-name,例如 openai/gpt-4oanthropic/claude-sonnet-4-5deepseek/deepseek-v3.2。调用 provider.languageModel(id) 时使用该 ID。

扩展

接入新厂商

1. 类型 (types.ts)

AIProviderKey 中新增:

export type AIProviderKey =
  | "openai"
  | "newprovider"  // 新增
  | ...

2. Provider 实例 (providers.ts)

createProviderInstances 中初始化并在 languageModels 中注册模型。

...(p.newprovider && {
  "newprovider/model-id": p.newprovider.chatModel("厂商实际模型ID"),
})

3. 配置 (dynamic-config.ts)

新增配置项,并在 aiConfigGroups 增加子分组。

4. 国际化 + 配置提取

config.content.ts 中增加字段文案,在 index.tsextractProviderConfigs 中映射配置。

接入新模型(已有厂商)

1. 在 providers.ts 注册

"openai/new-model": p.openai("openai实际模型名")

2. 在 models.ts 增加元数据

{
  id: "openai/new-model",
  label: "New Model Name",
  provider: "openai",
  providerModelId: "openai实际模型名",
  capabilities: { vision: false, reasoning: false, pdf: false },
  tier: "free",
  maxOutputTokens: 16384,
  creditCost: 1,
}

检查清单

步骤新厂商新模型
types.ts补充 AIProviderKey
providers.ts实例 + languageModels补充 languageModels
models.ts补充元数据
dynamic-config.ts配置项 + 子分组
config.content.ts国际化
index.tsextractProviderConfigs

目录