代理 Azure OpenAI 请求
Azure OpenAI Service 是一项完全托管的服务,提供对 OpenAI 语言模型(如 GPT-4 和 GPT-3.5-Turbo)的统一 REST API 访问。它们可以轻松集成到应用程序中,以添加内容生成、文本补全、语义搜索等功能。
本指南将引导你完成将 APISIX 与 Azure OpenAI Service 集成的过程。
前置条件
- 拥有一个 Azure 帐户并登录到 Azure 门户。
- 安装 Docker。
- 安装 cURL 以向服务发送请求进行验证。
- 按照 快速入门教程 在 Docker 或 Kubernetes 中启动一个新的 APISIX 实例。
请求访问 Azure OpenAI 服务
在继续之前,作为微软对负责任 AI 承诺的一部分,你应该首先通过填写注册表 请求访问 Azure OpenAI 服务。
请请求访问 GPT-3.5, GPT-3.5 Turbo, GPT-4, GPT-4 Turbo, and/or Embeddings Models 以继续操作。

部署 Azure OpenAI 服务
获得访问权限后,搜索 Azure AI services,导航到左侧面板中的 Azure OpenAI,然后单击 Create Azure OpenAI:

填写项目和实例详细信息:

在 Network 选项卡中,选择 All networks 选项,或根据你的基础设施进行相应调整:

继续设置直到部署完成:

获取 API 信息
转到 Azure AI Foundry 并选择 Chat。单击 View Code:

将命令切换到 curl 并选择 key authentication 选项卡:

复制生成的 curl command、endpoint 和 API key。
你可以选择将 API 密钥保存到环境变量:
export AZ_OPENAI_API_KEY=57cha9ee8e8a89a12c0aha174f180f4 # 替换为你的 API 密钥
创建到 Azure OpenAI 的路由
创建一个路由并配置 ai-proxy 插件,如下所示:
- Admin API
- ADC
- Ingress Controller
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": "azure-openai",
"auth": {
"header": {
// Annotate 2
"api-key": "'"$AZ_OPENAI_API_KEY"'"
}
},
"options": {
"model": "gpt-4"
},
"override": {
// Annotate 3
"endpoint": "https://api7-auzre-openai.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-15-preview"
}
}
}
}'
❶ 将提供商设置为 azure-openai。
❷ 在标头中附加 Azure OpenAI API 密钥。
❸ 指定 Azure OpenAI 端点。
services:
- name: Azure OpenAI Service
routes:
- uris:
- /anything
name: azure-openai-route
plugins:
ai-proxy:
// Annotate 1
provider: azure-openai
auth:
header:
// Annotate 2
api-key: 57cha9ee8e8a89a12c0aha174f180f4
options:
model: gpt-4
override:
// Annotate 3
endpoint: https://api7-auzre-openai.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-15-preview
❶ 将提供商设置为 azure-openai。
❷ 在标头中附加 Azure OpenAI API 密钥。
❸ 指定 Azure OpenAI 端点。
将配置同步到 APISIX:
adc sync -f adc.yaml
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: ingress-apisix
name: ai-proxy-plugin-config
spec:
plugins:
- name: ai-proxy
config:
// Annotate 1
provider: azure-openai
auth:
header:
// Annotate 2
api-key: 57cha9ee8e8a89a12c0aha174f180f4
options:
model: gpt-4
override:
// Annotate 3
endpoint: https://api7-auzre-openai.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-15-preview
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: ingress-apisix
name: azure-openai-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: ai-proxy-plugin-config
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: ingress-apisix
name: azure-openai-route
spec:
ingressClassName: apisix
http:
- name: azure-openai-route
match:
paths:
- /anything
plugins:
- name: ai-proxy
enable: true
config:
// Annotate 1
provider: azure-openai
auth:
header:
// Annotate 2
api-key: 57cha9ee8e8a89a12c0aha174f180f4
options:
model: gpt-4
override:
// Annotate 3
endpoint: https://api7-auzre-openai.openai.azure.com/openai/deployments/gpt-4/chat/completions?api-version=2024-02-15-preview
❶ 将提供商设置为 azure-openai。
❷ 在标头中附加 Azure OpenAI API 密钥。
❸ 指定 Azure OpenAI 端点。
将配置应用到你的集群:
kubectl apply -f azure-openai-route.yaml
验证
向路由发送带有以下提示的请求:
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": [
{
...,
"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"
}
}
],
...
}
下一步
你现在已经学会了如何将 APISIX 与 Azure OpenAI Service 集成。请参阅 Azure OpenAI 服务 REST API 参考 以了解更多信息。
如果你想流式传输 Azure API 响应,你可以将 Azure OpenAI 服务的 stream 参数 设置为 true,并在 APISIX 中使用 proxy-buffering 插件来禁用 NGINX 的 proxy_buffering 指令,这避免了缓冲服务器发送事件 (SSE)。