feat: support Vercel AI Gateway provider (#8883)

*  feat: support Vercel AI Gateway provider

* fix: add `vercelaigateway` to package.json.exports

* chore: 把 runtime 挪到 providers 目录下
This commit is contained in:
WangYK
2025-09-16 16:49:54 +08:00
committed by GitHub
parent 47874ac6c7
commit 5a4b0fd344
18 changed files with 2093 additions and 5 deletions
+5
View File
@@ -178,6 +178,11 @@ OPENAI_API_KEY=sk-xxxxxxxxx
# NEWAPI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# NEWAPI_PROXY_URL=https://your-newapi-server.com
### Vercel AI Gateway ###
# VERCELAIGATEWAY_API_KEY=your_vercel_ai_gateway_api_key
########################################
############ Market Service ############
########################################
+3 -1
View File
@@ -257,7 +257,9 @@ ENV \
# FAL
FAL_API_KEY="" FAL_MODEL_LIST="" \
# BFL
BFL_API_KEY="" BFL_MODEL_LIST=""
BFL_API_KEY="" BFL_MODEL_LIST="" \
# Vercel AI Gateway
VERCELAIGATEWAY_API_KEY="" VERCELAIGATEWAY_MODEL_LIST=""
USER nextjs
+3 -1
View File
@@ -299,7 +299,9 @@ ENV \
# FAL
FAL_API_KEY="" FAL_MODEL_LIST="" \
# BFL
BFL_API_KEY="" BFL_MODEL_LIST=""
BFL_API_KEY="" BFL_MODEL_LIST="" \
# Vercel AI Gateway
VERCELAIGATEWAY_API_KEY="" VERCELAIGATEWAY_MODEL_LIST=""
USER nextjs
+3 -1
View File
@@ -255,7 +255,9 @@ ENV \
# FAL
FAL_API_KEY="" FAL_MODEL_LIST="" \
# BFL
BFL_API_KEY="" BFL_MODEL_LIST=""
BFL_API_KEY="" BFL_MODEL_LIST="" \
# Vercel AI Gateway
VERCELAIGATEWAY_API_KEY="" VERCELAIGATEWAY_MODEL_LIST=""
USER nextjs
@@ -3,6 +3,7 @@ title: LobeChat Model Service Providers - Environment Variables and Configuratio
description: >-
Learn about the environment variables and configuration settings for various model service providers like OpenAI, Google AI, AWS Bedrock, Ollama, Perplexity AI, Anthropic AI, Mistral AI, Groq AI, OpenRouter AI, and 01.AI.
tags:
- Model Service Providers
- Environment Variables
@@ -693,4 +694,27 @@ The above example disables all models first, then enables `flux-pro-1.1` and `fl
NewAPI is a multi-provider model aggregation service that supports automatic model routing based on provider detection. It offers cost management features and provides a single endpoint for accessing models from multiple providers including OpenAI, Anthropic, Google, and more. Learn more about NewAPI at [https://github.com/Calcium-Ion/new-api](https://github.com/Calcium-Ion/new-api).
## Vercel AI Gateway
### `ENABLED_VERCELAIGATEWAY`
- Type: Optional
- Description: Enables Vercel AI Gateway as a model provider by default. Set to `0` to disable the Vercel AI Gateway service.
- Default: `1`
- Example: `0`
### `VERCELAIGATEWAY_API_KEY`
- Type: Required
- Description: This is the API key you applied for in the Vercel AI Gateway service.
- Default: -
- Example: `vck_xxxxxx...xxxxxx`
### `VERCELAIGATEWAY_MODEL_LIST`
- Type: Optional
- Description: Used to control the Vercel AI Gateway model list. Use `+` to add a model, `-` to hide a model, and `model_name=display_name` to customize the display name of a model. Separate multiple entries with commas. The definition syntax follows the same rules as other providers' model lists.
- Default: `-`
- Example: `-all,+vercel-model-1,+vercel-model-2=vercel-special`
[model-list]: /docs/self-hosting/advanced/model-list
@@ -691,7 +691,33 @@ LobeChat 在部署时提供了丰富的模型服务商相关的环境变量,
- 示例:`https://your-newapi-server.com`
<Callout type={'info'}>
NewAPI 是一个多供应商模型聚合服务,支持基于供应商检测的自动模型路由。它提供成本管理功能,并为访问包括 OpenAI、Anthropic、Google 等多个供应商的模型提供单一端点。了解更多关于 NewAPI 的信息请访问 [https://github.com/Calcium-Ion/new-api](https://github.com/Calcium-Ion/new-api)。
NewAPI
是一个多供应商模型聚合服务,支持基于供应商检测的自动模型路由。它提供成本管理功能,并为访问包括
OpenAI、Anthropic、Google 等多个供应商的模型提供单一端点。了解更多关于 NewAPI 的信息请访问
[https://github.com/Calcium-Ion/new-api](https://github.com/Calcium-Ion/new-api)。
</Callout>
## Vercel AI Gateway
### `ENABLED_VERCELAIGATEWAY`
- 类型:可选
- 描述:默认启用 Vercel AI Gateway 作为模型供应商,当设为 0 时关闭 Vercel AI Gateway 服务
- 默认值:`1`
- 示例:`0`
### `VERCELAIGATEWAY_API_KEY`
- 类型:必选
- 描述:这是你在 Vercel AI Gateway 服务中申请的 API 密钥
- 默认值:-
- 示例:`vck_xxxxxx...xxxxxx`
### `VERCELAIGATEWAY_MODEL_LIST`
- 类型:可选
- 描述:用来控制 Vercel AI Gateway 模型列表,使用 `+` 增加一个模型,使用 `-` 来隐藏一个模型,使用 `模型名=展示名` 来自定义模型的展示名,用英文逗号隔开。模型定义语法规则与其他 provider 保持一致。
- 默认值:`-`
- 示例:`-all,+vercel-model-1,+vercel-model-2=vercel-special`
[model-list]: /zh/docs/self-hosting/advanced/model-list
@@ -0,0 +1,62 @@
---
title: Using Vercel AI Gateway in LobeChat
description: >-
Learn how to integrate and utilize Vercel AI Gateway's unified API in LobeChat.
tags:
- LobeChat
- Vercel AI Gateway
- API Key
- Web UI
---
# Using Vercel AI Gateway in LobeChat
[Vercel AI Gateway](https://vercel.com/ai-gateway) is a unified API that provides access to 100+ AI models through a single endpoint. It offers features like budget management, usage monitoring, load balancing, and fallback handling.
This article will guide you on how to use Vercel AI Gateway in LobeChat.
<Steps>
### Step 1: Create an API Key in Vercel AI Gateway
- Go to [Vercel Dashboard](https://vercel.com/dashboard)
- Click on the **AI Gateway** tab on the left side
- Click on **API keys** in the left sidebar
- Click **Create key** and then **Create key** in the dialog to complete
### Step 2: Configure Vercel AI Gateway in LobeChat
- Go to the `Settings` page in LobeChat
- Under `AI Service Provider`, find the setting for `Vercel AI Gateway`
- Enter the API Key you obtained
- Choose a model from Vercel AI Gateway for your AI assistant to start the conversation
<Callout type={'warning'}>
During usage, you may need to pay the API service provider, so please refer to Vercel AI Gateway's
[pricing policy](https://vercel.com/docs/ai-gateway/models).
</Callout>
</Steps>
At this point, you can start chatting using the models provided by Vercel AI Gateway in LobeChat.
## Model Selection
Vercel AI Gateway supports various model providers including:
- **OpenAI**: `openai/gpt-4o`, `openai/gpt-4o-mini`, `openai/o1`, etc.
- **Anthropic**: `anthropic/claude-3-5-sonnet`, `anthropic/claude-3-opus`, etc.
- **Google**: `google/gemini-2.5-pro`, `google/gemini-2.0-flash`, etc.
- **DeepSeek**: `deepseek/deepseek-chat`, `deepseek/deepseek-reasoner`, etc.
- And many more...
For a complete list of supported models, visit [Vercel AI Gateway Models](https://vercel.com/ai-gateway/models).
## API Configuration
Vercel AI Gateway uses OpenAI-compatible API format. The base URL is:
```
https://ai-gateway.vercel.sh/v1
```
You can use any OpenAI-compatible client with this endpoint and your API key.
@@ -0,0 +1,61 @@
---
title: 在 LobeChat 中使用 Vercel AI Gateway
description: 了解如何在 LobeChat 中集成和使用 Vercel AI Gateway 的统一 API
tags:
- LobeChat
- Vercel AI Gateway
- API 密钥
- Web 界面
---
# 在 LobeChat 中使用 Vercel AI Gateway
[Vercel AI Gateway](https://vercel.com/ai-gateway) 是一个统一的 API,通过单一端点提供对 100+ AI 模型的访问。它提供预算管理、使用监控、负载均衡和回退处理等功能。
本文将指导您如何在 LobeChat 中使用 Vercel AI Gateway。
<Steps>
### 第一步:在 Vercel AI Gateway 中创建 API 密钥
- 访问 [Vercel 控制台](https://vercel.com/dashboard)
- 点击左侧的 **AI Gateway** 标签
- 点击左侧边栏的 **API 密钥**
- 点击 **创建密钥**,然后在对话框中点击 **创建密钥** 完成创建
### 第二步:在 LobeChat 中配置 Vercel AI Gateway
- 进入 LobeChat 的 `设置` 页面
- 在 `AI 服务提供商` 下,找到 `Vercel AI Gateway` 设置
- 输入您获得的 API 密钥
- 选择 Vercel AI Gateway 的模型,开始与 AI 助手对话
<Callout type={'warning'}>
使用过程中可能需要向 API 服务提供商付费,请参考 Vercel AI Gateway 的
[定价政策](https://vercel.com/docs/ai-gateway/models)。
</Callout>
</Steps>
至此,您可以在 LobeChat 中使用 Vercel AI Gateway 提供的模型开始聊天了。
## 模型选择
Vercel AI Gateway 支持多种模型提供商,包括:
- **OpenAI**: `openai/gpt-4o`、`openai/gpt-4o-mini`、`openai/o1` 等
- **Anthropic**: `anthropic/claude-3-5-sonnet`、`anthropic/claude-3-opus` 等
- **Google**: `google/gemini-2.5-pro`、`google/gemini-2.0-flash` 等
- **DeepSeek**: `deepseek/deepseek-chat`、`deepseek/deepseek-reasoner` 等
- 以及更多...
如需查看完整的支持模型列表,请访问 [Vercel AI Gateway 模型](https://vercel.com/ai-gateway/models)。
## API 配置
Vercel AI Gateway 使用 OpenAI 兼容的 API 格式。基础 URL 为:
```
https://ai-gateway.vercel.sh/v1
```
您可以使用任何 OpenAI 兼容的客户端与此端点和您的 API 密钥一起使用。
+2 -1
View File
@@ -59,6 +59,7 @@
"./togetherai": "./src/aiModels/togetherai.ts",
"./upstage": "./src/aiModels/upstage.ts",
"./v0": "./src/aiModels/v0.ts",
"./vercelaigateway": "./src/aiModels/vercelaigateway.ts",
"./vertexai": "./src/aiModels/vertexai.ts",
"./vllm": "./src/aiModels/vllm.ts",
"./volcengine": "./src/aiModels/volcengine.ts",
@@ -75,4 +76,4 @@
"dependencies": {
"zod": "^3.25.76"
}
}
}
@@ -53,6 +53,7 @@ import { default as tencentcloud } from './tencentcloud';
import { default as togetherai } from './togetherai';
import { default as upstage } from './upstage';
import { default as v0 } from './v0';
import { default as vercelaigateway } from './vercelaigateway';
import { default as vertexai } from './vertexai';
import { default as vllm } from './vllm';
import { default as volcengine } from './volcengine';
@@ -136,6 +137,7 @@ export const LOBE_DEFAULT_MODEL_LIST = buildDefaultModelList({
togetherai,
upstage,
v0,
vercelaigateway,
vertexai,
vllm,
volcengine,
@@ -201,6 +203,7 @@ export { default as tencentcloud } from './tencentcloud';
export { default as togetherai } from './togetherai';
export { default as upstage } from './upstage';
export { default as v0 } from './v0';
export { default as vercelaigateway } from './vercelaigateway';
export { default as vertexai } from './vertexai';
export { default as vllm } from './vllm';
export { default as volcengine } from './volcengine';
File diff suppressed because it is too large Load Diff
@@ -55,6 +55,7 @@ export enum ModelProvider {
Upstage = 'upstage',
V0 = 'v0',
VLLM = 'vllm',
VercelAIGateway = 'vercelaigateway',
VertexAI = 'vertexai',
Volcengine = 'volcengine',
Wenxin = 'wenxin',
@@ -0,0 +1,62 @@
import { createOpenAICompatibleRuntime } from '../../core/openaiCompatibleFactory';
import { AgentRuntimeErrorType, ChatCompletionErrorPayload, ModelProvider } from '../../types';
import { processMultiProviderModelList } from '../../utils/modelParse';
export interface VercelAIGatewayModelCard {
id: string;
}
export const LobeVercelAIGatewayAI = createOpenAICompatibleRuntime({
baseURL: 'https://ai-gateway.vercel.sh/v1',
chatCompletion: {
handleError: (error: any): Omit<ChatCompletionErrorPayload, 'provider'> | undefined => {
let errorResponse: Response | undefined;
if (error instanceof Response) {
errorResponse = error;
} else if ('status' in (error as any)) {
errorResponse = error as Response;
}
if (errorResponse) {
if (errorResponse.status === 401) {
return {
error: errorResponse.status,
errorType: AgentRuntimeErrorType.InvalidProviderAPIKey,
};
}
if (errorResponse.status === 403) {
return {
error: errorResponse.status,
errorType: AgentRuntimeErrorType.ProviderBizError,
message:
'Please check if your API key has sufficient balance or if you have access to the requested model.',
};
}
}
return {
error,
};
},
handlePayload: (payload) => {
const { model, ...rest } = payload;
return {
...rest,
model,
} as any;
},
},
debug: {
chatCompletion: () => process.env.DEBUG_VERCELAIGATEWAY_CHAT_COMPLETION === '1',
},
errorType: {
bizError: AgentRuntimeErrorType.ProviderBizError,
invalidAPIKey: AgentRuntimeErrorType.InvalidProviderAPIKey,
},
models: async ({ client }) => {
const modelsPage = (await client.models.list()) as any;
const modelList: VercelAIGatewayModelCard[] = modelsPage.data;
return processMultiProviderModelList(modelList, 'vercelaigateway');
},
provider: ModelProvider.VercelAIGateway,
});
+2
View File
@@ -52,6 +52,7 @@ import { LobeTencentCloudAI } from './providers/tencentcloud';
import { LobeTogetherAI } from './providers/togetherai';
import { LobeUpstageAI } from './providers/upstage';
import { LobeV0AI } from './providers/v0';
import { LobeVercelAIGatewayAI } from './providers/vercelaigateway';
import { LobeVLLMAI } from './providers/vllm';
import { LobeVolcengineAI } from './providers/volcengine';
import { LobeWenxinAI } from './providers/wenxin';
@@ -115,6 +116,7 @@ export const providerRuntimeMap = {
togetherai: LobeTogetherAI,
upstage: LobeUpstageAI,
v0: LobeV0AI,
vercelaigateway: LobeVercelAIGatewayAI,
vllm: LobeVLLMAI,
volcengine: LobeVolcengineAI,
wenxin: LobeWenxinAI,
@@ -93,6 +93,7 @@ export interface UserKeyVaults extends SearchEngineKeyVaults {
togetherai?: OpenAICompatibleKeyVault;
upstage?: OpenAICompatibleKeyVault;
v0?: OpenAICompatibleKeyVault;
vercelaigateway?: OpenAICompatibleKeyVault;
vertexai?: OpenAICompatibleKeyVault;
vllm?: OpenAICompatibleKeyVault;
volcengine?: OpenAICompatibleKeyVault;
+4
View File
@@ -54,6 +54,7 @@ import TencentcloudProvider from './tencentcloud';
import TogetherAIProvider from './togetherai';
import UpstageProvider from './upstage';
import V0Provider from './v0';
import VercelAIGatewayProvider from './vercelaigateway';
import VertexAIProvider from './vertexai';
import VLLMProvider from './vllm';
import VolcengineProvider from './volcengine';
@@ -116,6 +117,7 @@ export const LOBE_DEFAULT_MODEL_LIST: ChatModelCard[] = [
Search1APIProvider.chatModels,
InfiniAIProvider.chatModels,
QiniuProvider.chatModels,
VercelAIGatewayProvider.chatModels,
].flat();
export const DEFAULT_MODEL_PROVIDER_LIST = [
@@ -181,6 +183,7 @@ export const DEFAULT_MODEL_PROVIDER_LIST = [
QiniuProvider,
NebiusProvider,
CometAPIProvider,
VercelAIGatewayProvider,
];
export const filterEnabledModels = (provider: ModelProviderCard) => {
@@ -247,6 +250,7 @@ export { default as TencentCloudProviderCard } from './tencentcloud';
export { default as TogetherAIProviderCard } from './togetherai';
export { default as UpstageProviderCard } from './upstage';
export { default as V0ProviderCard } from './v0';
export { default as VercelAIGatewayProviderCard } from './vercelaigateway';
export { default as VertexAIProviderCard } from './vertexai';
export { default as VLLMProviderCard } from './vllm';
export { default as VolcengineProviderCard } from './volcengine';
@@ -0,0 +1,21 @@
import { ModelProviderCard } from '@/types/llm';
const VercelAIGateway: ModelProviderCard = {
apiKeyUrl: 'https://vercel.com/dashboard/ai-gateway',
chatModels: [],
checkModel: 'openai/gpt-4o-mini',
description:
'Vercel AI Gateway 提供统一的 API 来访问 100+ 模型,通过单一端点即可使用 OpenAI、Anthropic、Google 等多个提供商的模型。支持预算设置、使用监控、请求负载均衡和故障转移。',
enabled: true,
id: 'vercelaigateway',
modelList: { showModelFetcher: true },
modelsUrl: 'https://vercel.com/ai-gateway/models',
name: 'Vercel AI Gateway',
settings: {
responseAnimation: 'smooth',
showModelFetcher: true,
},
url: 'https://vercel.com/ai-gateway',
};
export default VercelAIGateway;
+6
View File
@@ -178,6 +178,9 @@ export const getLLMConfig = () => {
ENABLED_V0: z.boolean(),
V0_API_KEY: z.string().optional(),
ENABLED_VERCELAIGATEWAY: z.boolean(),
VERCELAIGATEWAY_API_KEY: z.string().optional(),
ENABLED_AI302: z.boolean(),
AI302_API_KEY: z.string().optional(),
@@ -366,6 +369,9 @@ export const getLLMConfig = () => {
ENABLED_V0: !!process.env.V0_API_KEY,
V0_API_KEY: process.env.V0_API_KEY,
ENABLED_VERCELAIGATEWAY: !!process.env.VERCELAIGATEWAY_API_KEY,
VERCELAIGATEWAY_API_KEY: process.env.VERCELAIGATEWAY_API_KEY,
ENABLED_AI302: !!process.env.AI302_API_KEY,
AI302_API_KEY: process.env.AI302_API_KEY,