集成 Azure OpenAI Service
Azure OpenAI Service 通过 Azure 企业基础设施提供 OpenAI 模型。本指南介绍如何使用 ai-proxy 插件,通过 API7 网关将流量路由到 Azure OpenAI。
前置条件
-
安装 Docker。
-
安装 cURL,用于发送请求并验证服务。
-
拥有一个正在运行的 API7 网关实例。
-
拥有 Azure 账户,并已部署 Azure OpenAI Service 资源。
-
从控制台获取令牌,并保存到环境变量:
export API_KEY=your-dashboard-token # 请替换为你的控制台令牌 -
将
{gateway_group_id}替换为网关组 ID。如果正在按照快速入门操作,请使用default。 -
如果使用 Admin API 示例,请创建或复用一个服务。如果尚无服务,请按照创建或复用服务操作,然后将其 ID 保存到环境变量:
export SERVICE_ID=your-service-id # 请替换为你的服务 ID
配置 Azure 身份认证
从 Azure OpenAI 资源获取 API Key(API 密钥)和端点,并将其保存到环境变量:
export AZ_OPENAI_API_KEY=your-azure-api-key # 请替换为你的 Azure OpenAI API Key
Azure OpenAI 使用 api-key 请求头进行身份认证,而不是 OpenAI 使用的 Authorization 请求头。
为 Azure 配置 AI 代理
创建一个启用 ai-proxy 插件的路由:
- Admin API
- ADC
curl -k "https://localhost:7443/apisix/admin/routes?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-d '{
"id": "azure-openai-route",
"service_id": "'"$SERVICE_ID"'",
"paths": ["/azure-openai"],
"plugins": {
"ai-proxy": {
"provider": "azure-openai",
"auth": {
"header": {
"api-key": "'"$AZ_OPENAI_API_KEY"'"
}
},
"options": {
"model": "gpt-4"
},
"override": {
"endpoint": "https://YOUR-RESOURCE.openai.azure.com/openai/deployments/YOUR-DEPLOYMENT/chat/completions?api-version=2024-10-21"
}
}
}
}'
❶ 将服务提供方设置为 azure-openai。
❷ 使用 api-key 请求头附加 Azure API Key。
❸ 指定完整的 Azure OpenAI 端点,其中包括资源名称、Deployment 名称和 API 版本。
services:
- name: Azure OpenAI Service
routes:
- uris:
- /azure-openai
name: azure-openai-route
plugins:
ai-proxy:
provider: azure-openai
auth:
header:
api-key: ${AZ_OPENAI_API_KEY}
options:
model: gpt-4
override:
endpoint: https://YOUR-RESOURCE.openai.azure.com/openai/deployments/YOUR-DEPLOYMENT/chat/completions?api-version=2024-10-21
❶ 将服务提供方设置为 azure-openai。
❷ 使用 api-key 请求头附加 Azure API Key。
❸ 指定完整的 Azure OpenAI 端点,其中包括资源名称、Deployment 名称和 API 版本。
将配置同步到 API7 网关:
adc sync -f adc.yaml
跨 Deployment 的多模型路由
使用 ai-proxy-multi 在多个 Azure 区域或 Deployment 版本之间路由流量,实现高可用。
- Admin API
- ADC
curl -k "https://localhost:7443/apisix/admin/routes?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-d '{
"id": "azure-multi-route",
"service_id": "'"$SERVICE_ID"'",
"paths": ["/azure-openai"],
"plugins": {
"ai-proxy-multi": {
"instances": [
{
"name": "east-us",
"provider": "azure-openai",
"auth": { "header": { "api-key": "'"$AZ_OPENAI_API_KEY"'" } },
"options": { "model": "gpt-4" },
"override": { "endpoint": "https://my-resource-eastus.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-10-21" },
"weight": 1,
"priority": 2
},
{
"name": "west-europe",
"provider": "azure-openai",
"auth": { "header": { "api-key": "'"$AZ_OPENAI_API_KEY_EU"'" } },
"options": { "model": "gpt-4" },
"override": { "endpoint": "https://my-resource-westeu.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-10-21" },
"weight": 1,
"priority": 1
}
]
}
}
}'
❶ 在美国东部区域定义主实例。
❷ 将 priority 设置为最高值 2,优先使用此实例。
❸ 将西欧实例的 priority 设置为 1,在美国东部不可用时作为备用实例。
services:
- name: Azure OpenAI Multi-Region
routes:
- uris:
- /azure-openai
name: azure-multi-route
plugins:
ai-proxy-multi:
instances:
- name: east-us
provider: azure-openai
auth:
header:
api-key: ${AZ_OPENAI_API_KEY}
options:
model: gpt-4
override:
endpoint: https://my-resource-eastus.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-10-21
weight: 1
priority: 2
- name: west-europe
provider: azure-openai
auth:
header:
api-key: ${AZ_OPENAI_API_KEY_EU}
options:
model: gpt-4
override:
endpoint: https://my-resource-westeu.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-10-21
weight: 1
priority: 1
❶ 在美国东部区域定义主实例。
❷ 将 priority 设置为最高值 2,优先使用此实例。
❸ 将西欧实例的 priority 设置为 1,在美国东部不可用时作为备用实例。
将配置同步到 API7 网关:
adc sync -f adc.yaml
更多路由策略请参阅多模型路由和故障转移。
验证配置
发送聊天补全请求:
curl "http://127.0.0.1:9080/azure-openai" -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 a 50-word introduction for API gateways." }
]
}'
你应收到类似以下内容的响应:
{
"choices": [
{
"message": {
"role": "assistant",
"content": "API gateways serve as the central entry point for managing, securing, and routing API traffic in modern architectures. They handle authentication, rate limiting, load balancing, and protocol translation, enabling organizations to expose services reliably while maintaining security and observability across distributed systems."
},
"index": 0,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 28,
"completion_tokens": 45,
"total_tokens": 73
}
}
要启用流式响应,请在请求体中设置 "stream": true。使用 proxy-buffering 插件禁用 NGINX proxy_buffering,避免服务器发送事件(SSE)被缓冲。
后续步骤
你已经了解如何通过 API7 网关将流量路由到 Azure OpenAI。更多信息请参阅 Azure OpenAI Service REST API 参考。
- 多模型路由和故障转移 — 跨 Azure 区域和服务提供方进行路由。
- RAG 集成 — 将 Azure OpenAI 与 Azure AI Search 结合,实现检索增强生成。