ai-proxy
ai-proxy 插件通过将插件配置转换为指定的请求格式,简化了对 LLM 和 Embedding 模型的访问。它支持集成 OpenAI、DeepSeek、Gemini、Vertex AI 以及其他兼容 OpenAI 的 API。
此外,该插件还支持在访问日志中记录 LLM 请求信息,例如 Token 使用情况、模型、首字节响应时间等。
示例
以下示例演示了如何针对不同场景配置 ai-proxy。
代理到 OpenAI
以下示例演示了如何在 ai-proxy 插件中配置 API Key、模型和其他参数,并在路由上配置该插件以将用户提示词代理到 OpenAI。
获取 OpenAI API Key 并将其保存到环境变量中:
export OPENAI_API_KEY=<YOUR_OPENAI_API_KEY> # 替换为你的 API 密钥
创建一个路由并按如下方式配置 ai-proxy 插件:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "ai-proxy-route",
"uri": "/anything",
"methods": ["POST"],
"plugins": {
"ai-proxy": {
// Annotate 1
"provider": "openai",
"auth": {
"header": {
// Annotate 2
"Authorization": "Bearer '"$OPENAI_API_KEY"'"
}
},
"options":{
// Annotate 3
"model": "gpt-4"
}
}
}
}'
❶ 指定提供商为 openai。
❷ 在 Authorization 头中附带 OpenAI API Key。
❸ 指定模型名称。
向该路由发送一个 POST 请求,请求体中包含系统提示词和示例用户问题:
curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-H "Host: api.openai.com" \
-d '{
"messages": [
{ "role": "system", "content": "You are a mathematician" },
{ "role": "user", "content": "What is 1+1?" }
]
}'
你应该收到类似以下的响应:
{
...,
"model": "gpt-4-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "1+1 equals 2.",
"refusal": null
},
"logprobs": null,
"finish_reason": "stop"
}
],
...
}
代理到 DeepSeek
以下示例演示了如何配置 ai-proxy 插件将请求代理到 DeepSeek。
获取 DeepSeek API Key 并将其保存到环境变量中:
export DEEPSEEK_API_KEY=<YOUR_DEEPSEEK_API_KEY> # 替换为你的 API 密钥
创建一个路由并按如下方式配置 ai-proxy 插件:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "ai-proxy-route",
"uri": "/anything",
"methods": ["POST"],
"plugins": {
// Annotate 1
"ai-proxy": {
"provider": "deepseek",
"auth": {
"header": {
// Annotate 2
"Authorization": "Bearer '"$DEEPSEEK_API_KEY"'"
}
},
"options": {
// Annotate 3
"model": "deepseek-chat"
}
}
}
}'
❶ 指定提供商为 deepseek,以便插件将请求代理到 https://api.deepseek.com/chat/completions。
❷ 在 Authorization 头中附带 OpenAI API Key。
❸ 指定模型名称。
向该路由发送一个 POST 请求,请求体中包含一个示例问题:
curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"role": "system",
"content": "You are an AI assistant that helps people find information."
},
{
"role": "user",
"content": "Write me a 50-word introduction for Apache APISIX."
}
]
}'
你应该收到类似以下的响应:
{
...
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Apache APISIX is a dynamic, real-time, high-performance API gateway and cloud-native platform. It provides rich traffic management features like load balancing, dynamic upstream, canary release, circuit breaking, authentication, observability, and more. Designed for microservices and serverless architectures, APISIX ensures scalability, security, and seamless integration with modern DevOps workflows."
},
"logprobs": null,
"finish_reason": "stop"
}
],
...
}
代理到 Azure OpenAI
以下示例演示了如何配置 ai-proxy 插件将请求代理到其他 LLM 服务,例如 Azure OpenAI。
获取 Azure OpenAI API Key 并将其 保存到环境变量中:
export AZ_OPENAI_API_KEY=<YOUR_AZ_OPENAI_API_KEY> # 替换为你的 API 密钥
创建一个路由并按如下方式配置 ai-proxy 插件:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "ai-proxy-route",
"uri": "/anything",
"methods": ["POST"],
"plugins": {
// Annotate 1
"ai-proxy": {
"provider": "azure-openai",
"auth": {
"header": {
// Annotate 2
"api-key": "'"$AZ_OPENAI_API_KEY"'"
}
},
"options":{
// Annotate 3
"model": "gpt-4"
},
"override": {
// Annotate 4
"endpoint": "https://api7-auzre-openai.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-15-preview"
}
}
}
}'
❶ 将提供商设置为 azure-openai。
❷ 在 Authorization 头中附带 OpenAI API Key。
❸ 指定模型名称。
❹ 指定 Azure OpenAI 端点。
向该路由发送一个 POST 请求,请求体中包含一个示例问题:
curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"role": "system",
"content": "You are an AI assistant that helps people find information."
},
{
"role": "user",
"content": "Write me a 50-word introduction for Apache APISIX."
}
],
"max_tokens": 800,
"temperature": 0.7,
"frequency_penalty": 0,
"presence_penalty": 0,
"top_p": 0.95,
"stop": null
}'
你应该收到类似以下的响应:
{
"choices": [
{
...,
"message": {
"content": "Apache APISIX is a modern, cloud-native API gateway built to handle high-performance and low-latency use cases. It offers a wide range of features, including load balancing, rate limiting, authentication, and dynamic routing, making it an ideal choice for microservices and cloud-native architectures.",
"role": "assistant"
}
}
],
...
}
代理到 Gemini
以下示例演示了如何配置 ai-proxy 插件将请求代理到 Google 的 Gemini API 进行聊天完成。此示例仅适用于 API7 企业版 3.9.2 及以上版本,不适用于 APISIX。
获取 Gemini API Key 并可选择将其保存到环境变量中:
export GEMINI_API_KEY=AIza... # 替换为你的 API 密钥
创建一个路由并按如下方式配置 ai-proxy 插件:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "ai-proxy-gemini-route",
"uri": "/anything",
"methods": ["POST"],
"plugins": {
"ai-proxy": {
// Annotate 1
"provider": "gemini",
"auth": {
"header": {
// Annotate 2
"Authorization": "Bearer '"$GEMINI_API_KEY"'"
}
},
"options": {
// Annotate 3
"model": "gemini-2.5-flash"
}
}
}
}'
❶ 指定提供商为 gemini。
❷ 在 Authorization 头中替换为你的 Gemini API Key。
❸ 指定 Gemini 模型名称。
上述配置将请求代理到 https://generativelanguage.googleapis.com/v1beta/openai/chat/completions 的聊天完成端点。要将请求代理到 Embedding 模型,请在 override 字段中显式配置 Embedding 模型端点。
向该路由发送一个 POST 请求,请求体中包含系统提示词和示例用户问题:
curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"messages": [
{ "role": "system", "content": "You are a helpful AI assistant" },
{ "role": "user", "content": "What is the capital of France?" }
]
}'
你应该收到类似以下的响应:
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "The capital of France is **Paris**.",
"role": "assistant"
}
}
],
"model": "gemini-2.5-flash",
"object": "chat.completion",
"usage": {
"completion_tokens": 8,
"prompt_tokens": 15,
"total_tokens": 41
},
...
}
代理到 Vertex AI 聊天完成
以下示例演示了如何配置 ai-proxy 插件,使用 GCP 服务账号认证将请求代理到 Google Cloud 的 Vertex AI 平台。 此示例仅适用于 API7 企业版 3.9.2 及以上版本,不适用于 APISIX。
在开始之前:
- 为你的 GCP 项目 启用 Vertex AI 和计费。
- 按照 服务账号凭据 部分在 GCP 中创建一个服务账号,为该账号分配 "Vertex AI User" 角色,并获取 JSON 格式的账号凭据。
你的凭据文件应该类似于以下内容:
{
"type": "service_account",
"project_id": "api7-vertex",
"private_key_id": "...",
"private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
"client_email": "api7-docs@api7-vertex.iam.gserviceaccount.com",
"client_id": "....",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/api7-docs%40api7-vertex.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}
可选择将 JSON 保存到环境变量中:
export GCP_SA_JSON="$(cat credentials.json)"
创建一个路由并按如下方式配置 ai-proxy 插件:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "ai-proxy-vertex-ai-route",
"uri": "/anything",
"methods": ["POST"],
"plugins": {
"ai-proxy": {
// Annotate 1
"provider": "vertex-ai",
"auth": {
"gcp": {
// Annotate 2
"service_account_json": "'"$GCP_SA_JSON"'"
}
},
// Annotate 3
"provider_conf": {
"project_id": "api7-vertex",
"region": "us-central1"
},
"options": {
// Annotate 4
"model": "google/gemini-2.5-flash"
}
}
}
}'
❶ 指定提供商为 vertex-ai。
❷ 替换为你的 JSON 凭据。确保它是 JSON 转义的字符串。
❸ 替换为你的 Vertex AI 项目 ID 和区域。
❹ 指定通过 Vertex AI 调用的 Gemini 模型名称,格式为 <publisher>/<model>。
向该路由发送一个 POST 请求,请求体中包含系统提示词和示例用户问题:
curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"messages": [
{ "role": "system", "content": "You are a mathematician" },
{ "role": "user", "content": "What is 1+1?" }
]
}'
你应该收到类似以下的响应:
{
"choices": [
{
"message": {
"role": "assistant",
"content": "1 + 1 = 2\n"
},
"index": 0,
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"completion_tokens": 8,
"extra_properties": {
"google": {
"traffic_type": "ON_DEMAND"
}
},
"total_tokens": 19,
"prompt_tokens": 11
},
"object": "chat.completion",
"model": "google/gemini-2.5-flash",
...
}
代理到 Vertex AI Embedding 模型
以下示例演示了如何配置 ai-proxy 插件,使用 GCP 服务账号认证将请求代理到 Vertex AI Embedding 模型。此示例仅适用于 API7 企业版 3.9.2 及以上版本,不适用于 APISIX。
在开始之前:
- 为你的 GCP 项目 启用 Vertex AI 和计费。
- 按照 服务账号凭据 部分在 GCP 中创建一个服务账号,为该账号分配 "Vertex AI User" 角色,并获取 JSON 格式的账号凭据。
你的凭据文件应该类似于以下内容:
{
"type": "service_account",
"project_id": "api7-vertex",
"private_key_id": "...",
"private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",
"client_email": "api7-docs@api7-vertex.iam.gserviceaccount.com",
"client_id": "....",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/api7-docs%40api7-vertex.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}
可选择将 JSON 保存到环境变量中:
export GCP_SA_JSON="$(cat credentials.json)"
创建一个路由并按如下方式配置 ai-proxy 插件:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "ai-proxy-vertex-ai-embeddings-route",
"uri": "/embeddings",
"methods": ["POST"],
"plugins": {
"ai-proxy": {
// Annotate 1
"provider": "vertex-ai",
"auth": {
// Annotate 2
"gcp": {
"service_account_json": "'"$GCP_SA_JSON"'"
}
},
// Annotate 3
"provider_conf": {
"project_id": "api7-vertex",
"region": "us-central1"
},
"options": {
// Annotate 4
"model": "gemini-embedding-001"
}
}
}
}'
❶ 指定提供商为 vertex-ai。
❷ 替换为你的 JSON 凭据。确保它是 JSON 转义的字符 串。
❸ 替换为你的 Vertex AI 项目 ID 和区域。
❹ 指定 Vertex AI Gemini Embedding 模型的名称。
向该路由发送一个 POST 请求,请求体中包含输入字符串:
curl "http://127.0.0.1:9080/embeddings" -X POST \
-H "Content-Type: application/json" \
-d '{
"input": "hello world"
}'
你应 该收到类似以下的响应:
{
"model": "gemini-embedding-001",
"usage": {
"total_tokens": 2,
"prompt_tokens": 2
},
"object": "list",
"data": [
{
"index": 0,
"object": "embedding",
"embedding": [
-0.0241838414222,
0.0098769934847951,
0.0074856607243419,
-0.067302219569683,
...
]
}
]
}
代理到 OpenAI Embedding 模型
以下示例演示了如何配置 ai-proxy 插件将请求代理到 Embedding 模型。此示例将使用 OpenAI Embedding 模型端点。
获取 OpenAI API Key 并将其保存到环境变量中:
export OPENAI_API_KEY=<YOUR_OPENAI_API_KEY> # 替换为你的 API 密钥
创建一个路由并按如下方式配置 ai-proxy 插件:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "ai-proxy-route",
"uri": "/embeddings",
"methods": ["POST"],
"plugins": {
"ai-proxy": {
// Annotate 1
"provider": "openai",
"auth": {
"header": {
// Annotate 2
"Authorization": "Bearer '"$OPENAI_API_KEY"'"
}
},
"options":{
// Annotate 3
"model": "text-embedding-3-small",
// Annotate 4
"encoding_format": "float"
},
"override": {
// Annotate 5
"endpoint": "https://api.openai.com/v1/embeddings"
}
}
}
}'
❶ 指定提供商为 openai,以便插件将请求代理到 https://api.openai.com/chat/completions。
❷ 在 Authorization 头中附带 OpenAI API Key。
❸ 指定 Embedding 模型名称。
❹ 添加额外参数 encoding_format,配置返回的 Embedding 向量为浮点数列表。
❺ 使用 Embedding API 端点 覆盖默认端点。
向该路由发送一个 POST 请求,请求体中包含输入字符串:
curl "http://127.0.0.1:9080/embeddings" -X POST \
-H "Content-Type: application/json" \
-d '{
"input": "hello world"
}'
你应该收到类似以下的响应:
{
"object": "list",
"data": [
{
"object": "embedding",
"index": 0,
"embedding": [
-0.0067144386,
-0.039197803,
0.034177095,
0.028763203,
-0.024785956,
-0.04201061,
...
],
}
],
"model": "text-embedding-3-small",
"usage": {
"prompt_tokens": 2,
"total_tokens": 2
}
}
根据请求体参数代理到指定模型
以下示例演示了如何根据用户请求中指定的模型,在同一 URI 上将请求代理到不同的模型。你将使用 post_arg.* 变量 来获取请求体参数的值。
该示例将使用 OpenAI 和 DeepSeek 作为示例 LLM 服务。获取 OpenAI 和 DeepSeek API Key 并将其保存到环境变量中:
# 替换为你的 API 密钥
export OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
export DEEPSEEK_API_KEY=<YOUR_DEEPSEEK_API_KEY>
创建一个使用 ai-proxy 插件通往 OpenAI API 的路由:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "ai-proxy-openai-route",
// Annotate 1
"uri": "/anything",
"methods": ["POST"],
// Annotate 2
"vars": [[ "post_arg.model", "==", "openai" ]],
"plugins": {
"ai-proxy": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer '"$OPENAI_API_KEY"'"
}
},
"options": {
"model": "gpt-4"
}
}
}
}'
❶ 将路由 URI 设置为 /anything。
❷ 将路由匹配到 model 请求体参数设置为 openai 的请求。
创建另一个通往 DeepSeek API 的路由 /anything 并配置 ai-proxy 插件:
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"id": "ai-proxy-deepseek-route",
// Annotate 1
"uri": "/anything",
"methods": ["POST"],
// Annotate 2
"vars": [[ "post_arg.model", "==", "deepseek" ]],
"plugins": {
"ai-proxy": {
"provider": "deepseek",
"auth": {
"header": {
"Authorization": "Bearer '"$DEEPSEEK_API_KEY"'"
}
},
"options": {
"model": "deepseek-chat"
}
}
}
}'
❶ 将路由 URI 设置为 /anything,与上一个路由相同。
❷ 将路由匹配到 model 请求体参数设置为 deepseek 的请求。
向该路由发送一个 POST 请求,将 model 设置为 openai:
curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"model": "openai",
"messages": [
{ "role": "system", "content": "You are a mathematician" },
{ "role": "user", "content": "What is 1+1?" }
]
}'
你应该收到类似以下的响应:
{
...,
"model": "gpt-4-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "1+1 equals 2.",
"refusal": null
},
"logprobs": null,
"finish_reason": "stop"
}
],
...
}
向该路由发送一个 POST 请求,将 model 设置为 deepseek:
curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek",
"messages": [
{ "role": "system", "content": "You are a mathematician" },
{ "role": "user", "content": "What is 1+1?" }
]
}'
你应该收到类似以下的响应:
{
...,
"model": "deepseek-chat",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The sum of 1 and 1 is 2. This is a basic arithmetic operation where you combine two units to get a total of two units."
},
"logprobs": null,
"finish_reason": "stop"
}
],
...
}
你还可以配置 post_arg.* 来获取嵌套的请求体参数。例如,如果请求格式为:
curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"model": {
"name": "openai"
},
"messages": [
{ "role": "system", "content": "You are a mathematician" },
{ "role": "user", "content": "What is 1+1?" }
]
}'
你可以将路由上的 vars 配置为 [[ "post_arg.model.name", "==", "openai" ]]。
有关表达式的更多信息,请参见 APISIX 表达式。