Skip to main content
Version: 3.17

ai-request-rewrite

描述#

ai-request-rewrite 插件在将客户端请求转发到上游服务之前,先将请求发送到 LLM 服务进行转换处理。这使得 LLM 能够对请求进行数据脱敏、内容增强或格式转换等修改。该插件支持集成 OpenAI、DeepSeek、Gemini、Vertex AI、Anthropic、OpenRouter 以及其他 OpenAI 兼容的 API。

插件属性#

名称类型必选项默认值有效值描述
promptstring发送到 LLM 服务用于重写客户端请求的提示词。
providerstring[openai, deepseek, azure-openai, aimlapi, gemini, vertex-ai, anthropic, openrouter, openai-compatible]LLM 服务提供商。设置为 aimlapi 时,插件使用 OpenAI 兼容驱动并将请求代理到 https://api.aimlapi.com/v1/chat/completions。设置为 openai-compatible 时,插件将请求代理到 override 中配置的自定义端点。设置为 azure-openai 时,插件同样将请求代理到 override 中配置的自定义端点,并会额外移除用户请求中的 model 参数。
authobject身份验证配置。
auth.headerobject身份验证请求头。键必须匹配模式 ^[a-zA-Z0-9._-]+$headerquery 至少需要配置其中一个。
auth.queryobject身份验证查询参数。键必须匹配模式 ^[a-zA-Z0-9._-]+$headerquery 至少需要配置其中一个。
optionsobject模型配置。除了 model 之外,还可以配置其他参数,这些参数会在请求体中转发给上游 LLM 服务。例如,使用 OpenAI 时,可以配置 temperaturetop_pstream 等参数。更多可用选项请参阅 LLM 提供商的 API 文档。
options.modelstringLLM 模型名称,例如 gpt-4gpt-3.5。更多可用模型请参阅 LLM 提供商的 API 文档。
overrideobject覆盖设置。
override.endpointstringLLM 提供商端点。当 provideropenai-compatible 时必填。
timeoutinteger300001 - 60000请求 LLM 服务的超时时间(毫秒)。
keepalivebooleantrue是否在请求 LLM 服务时保持连接。
keepalive_timeoutinteger60000≥ 1000请求 LLM 服务的 keepalive 超时时间(毫秒)。
keepalive_poolinteger30≥ 1连接 LLM 服务的 keepalive 连接池大小。
ssl_verifybooleantrue是否验证 LLM 服务的 SSL 证书。

工作原理#

示例#

以下示例演示如何为不同场景配置 ai-request-rewrite

示例使用 OpenAI 作为 LLM 服务。请先获取 OpenAI API 密钥并将其保存到环境变量:

export OPENAI_API_KEY=<your-api-key>
note

你可以使用以下命令从 config.yaml 中获取 admin_key 并保存到环境变量:

admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g')

脱敏敏感信息#

以下示例演示如何使用 ai-request-rewrite 插件在请求到达上游服务之前脱敏敏感信息。

发送一个包含个人敏感信息的 POST 请求到路由:

curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"content": "John said his debit card number is 4111 1111 1111 1111 and SIN is 123-45-6789."
}'

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

{
"args": {},
"data": "{\"content\": \"John said his debit card number is **** **** **** 1111 and SIN is ***-**-***.\"}",
...,
"json": {
"content": "John said his debit card number is **** **** **** 1111 and SIN is ***-**-***."
},
"method": "POST",
"origin": "192.168.97.1, 103.97.2.170",
"url": "http://127.0.0.1/anything"
}

格式转换#

以下示例演示如何使用 ai-request-rewrite 插件在请求到达上游服务之前对数据进行格式转换。

发送一个 POST 请求到路由:

curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"content": "Book a flight from NYC to LA on April 10, 2022."
}'

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

{
"args": {},
"data": "{\n \"intent\": \"BookFlight\",\n \"parameters\": {\n \"origin\": \"NYC\",\n \"destination\": \"LA\",\n \"date\": \"2022-04-10\"\n }\n}",
...,
"json": {
"intent": "BookFlight",
"parameters": {
"date": "2022-04-10",
"destination": "LA",
"origin": "NYC"
}
},
"method": "POST",
"origin": "192.168.97.1, 103.97.2.167",
"url": "http://127.0.0.1/anything"
}

信息摘要#

以下示例演示如何使用 ai-request-rewrite 插件在请求到达上游服务之前对信息进行摘要。

发送一个包含冗长内容的 POST 请求到路由:

curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{
"content": "Hey! So, I'\''m planning a trip to Japan next spring for about three weeks, and I want to visit Tokyo, Kyoto, and Osaka, but I'\''m not sure how to split my time between them. I really love history and cultural sites, so temples and shrines are a must. I'\''m also a big foodie, especially into ramen and sushi, so I'\''d love recommendations on the best spots. I prefer quieter areas for accommodation, but I don'\''t mind traveling into busy areas for sightseeing. Oh, and I'\''d also like to do a day trip somewhere outside these cities—maybe Hakone or Nara? I heard the cherry blossoms might still be in bloom in early April, so I'\''d love to catch them if possible. Also, what'\''s the best way to get around—should I get a JR Pass, or would individual tickets be better? Thanks!"
}'

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

{
"args": {},
"data": "The individual is planning a three-week trip to Japan in the spring, looking to visit Tokyo, Kyoto, and Osaka. They are interested in history, culture, temples, and shrines. They love ramen and sushi, so are seeking food recommendations. Accommodation should be in quieter areas, but they are open to busy sites for sightseeing. Along with these cities, they plan to make a day trip to either Hakone or Nara, hoping to see the cherry blossoms in early April. The best transport method between buying the JR Pass or individual tickets is also a query.",
...,
"method": "POST",
"origin": "192.168.97.1, 103.97.2.171",
"url": "http://127.0.0.1/anything"
}

向 OpenAI 兼容的 LLM 发送请求#

以下示例演示如何通过将 provider 设置为 openai-compatible 并在 override.endpoint 中配置自定义端点,来使用 ai-request-rewrite 插件与 OpenAI 兼容的 LLM 提供商。