跳到主要内容

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 模型名称。

about model endpoint

上述配置将请求代理到 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 格式的账号凭据。

你的凭据文件应该类似于以下内容:

credentials.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 格式的账号凭据。

你的凭据文件应该类似于以下内容:

credentials.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 表达式

在访问日志中包含 LLM 信息

以下示例演示了如何在网关的访问日志中记录 LLM 请求相关信息,以改进分析和审计。除了 NGINX 变量外,还可以使用以下变量:

  • apisix_upstream_response_time:APISIX 发送请求到上游服务并接收完整响应所花费的时间。从 API7 企业版 3.8.8 开始可用。
  • request_type:请求类型,值可以是 traditional_httpai_chatai_stream
  • llm_time_to_first_token:从发送请求到从 LLM 服务收到第一个 token 的持续时间(以毫秒为单位)。
  • llm_model:转发到上游 LLM 服务的 LLM 模型名称。
  • request_llm_model:请求中指定的 LLM 模型名称。
  • llm_prompt_tokens:提示词中的 token 数量。
  • llm_completion_tokens:聊天完成中的 token 数量。
提示

这些变量在访问日志格式中进行了演示,也可用于日志插件。

更新 配置文件 中的访问日志格式,以包含额外的 LLM 相关变量:

conf/config.yaml
nginx_config:
http:
access_log_format: "$remote_addr - $remote_user [$time_local] $http_host \"$request_line\" $status $body_bytes_sent $request_time \"$http_referer\" \"$http_user_agent\" $upstream_addr $upstream_status $apisix_upstream_response_time \"$upstream_scheme://$upstream_host$upstream_uri\" \"$apisix_request_id\" \"$request_type\" \"$llm_time_to_first_token\" \"$llm_model\" \"$request_llm_model\" \"$llm_prompt_tokens\" \"$llm_completion_tokens\""

重新加载网关 以使配置更改生效。

现在,如果你按照 [代理到 OpenAI 示例](#代理到 OpenAI) 创建一个路由。发送如下请求:

curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5",
"messages": [
{ "role": "system", "content": "You are a mathematician" },
{ "role": "user", "content": "What is 1+1?" }
]
}'

由于 ai-proxy 中的模型是 gpt-4,因此请求将被转发到 GPT-4 模型,你将收到类似以下的响应:

{
...,
"model": "gpt-4-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "1+1 equals 2.",
"refusal": null,
"annotations": []
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 23,
"completion_tokens": 8,
"total_tokens": 31,
"prompt_tokens_details": {
"cached_tokens": 0,
"audio_tokens": 0
},
...
},
"service_tier": "default",
"system_fingerprint": null
}

在网关的访问日志中,你应该看到类似以下的日志条目:

192.168.215.1 - - [29/Aug/2025:09:54:16 +0000] 127.0.0.1:9080 "POST /anything HTTP/1.1" 200 808 2.670 "-" "curl/8.6.0" - - 2670 "http://127.0.0.1:9080" "6526bf5c961b6e6bb8cfcb66486f02dc" "ai_chat" "2670" "gpt-4" "gpt-3.5" "23" "7"

访问日志条目显示 APISIX 上游响应时间为 2.670 秒,请求类型为 ai_chat,首字节响应时间为 2670 毫秒,请求转发到的 LLM 模型为 gpt-4,请求 LLM 模型为 gpt-3.5,提示词 token 使用量为 23,完成 token 使用量为 7

发送请求日志到 Logger

以下示例演示了如何记录请求和请求信息,包括 LLM 模型、Token 和 Payload,并将其推送到 Logger。在开始之前,你应该首先设置一个 Logger,例如 Kafka。有关更多信息,请参见 kafka-logger

创建一个通往 LLM 服务的路由并按如下方式配置日志记录详情:

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",
"uri": "/anything",
"methods": ["POST"],
"plugins": {
"ai-proxy": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer '"$OPENAI_API_KEY"'"
}
},
"options": {
"model": "gpt-4"
},
"logging": {
// Annotate 1
"summaries": true,
// Annotate 2
"payloads": true
}
},
"kafka-logger": {
// Annotate 3
"brokers": [
{
"host": "127.0.0.1",
"port": 9092
}
],
// Annotate 4
"kafka_topic": "test2",
// Annotate 5
"key": "key1",
// Annotate 6
"batch_max_size": 1
}
}
}
}'

❶ 记录请求 LLM 模型、持续时间、请求和响应 Token。

❷ 记录请求和响应 Payload。

❸ 更新为你的 Kafka 地址。

❹ 更新为你的 Kafka Topic。

❺ 更新为你的 Kafka Key。

❻ 设置为 1 以立即发送日志条目。

向该路由发送一个 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?" }
]
}'

你应该收到类似以下的响应:

{
...,
"model": "gpt-4-0613",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "1+1 equals 2.",
"refusal": null
},
"logprobs": null,
"finish_reason": "stop"
}
],
...
}

在 Kafka Topic 中,你也应该看到对应于该请求的日志条目,其中包含 LLM 摘要和请求/响应 Payload。