Route Traffic to OpenAI
OpenAI provides access to models such as GPT-4o and o1 for chat completions, embeddings, and more. This guide shows how to route traffic to OpenAI through API7 Gateway using the ai-proxy plugin.
Prerequisites
-
Install Docker.
-
Install cURL to send requests to the services for validation.
-
Have a running API7 Gateway instance.
-
Create a token from the Dashboard and save it to an environment variable:
export API_KEY=your-dashboard-token # replace with your Dashboard token -
Replace
{gateway_group_id}with your gateway group ID. Usedefaultif you are following the quickstart. -
If you are following the Admin API examples, create or reuse a service in API7 Gateway. If you do not have one yet, follow Create or Reuse a Service, then save its ID to an environment variable:
export SERVICE_ID=your-service-id # replace with your service ID
Obtain an OpenAI API Key
Create an OpenAI account and generate an API key. Save the key to an environment variable:
export OPENAI_API_KEY=sk-proj-xxxxxxxxxxxxxxxxxxxxxxxx # replace with your OpenAI API key
Configure OpenAI as a Provider
Create a route with the ai-proxy plugin:
- 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}" \
--data-binary @- <<EOF
{
"id": "openai-route",
"service_id": "$SERVICE_ID",
"paths": ["/openai"],
"plugins": {
"ai-proxy": {
"provider": "openai",
"auth": {
"header": {
"Authorization": "Bearer $OPENAI_API_KEY"
}
},
"options": {
"model": "gpt-4o"
}
}
}
}
EOF
❶ Set the provider to openai.
❷ Attach the OpenAI API key in the Authorization header.
❸ Set the default model to gpt-4o.
services:
- name: OpenAI Service
routes:
- uris:
- /openai
name: openai-route
plugins:
ai-proxy:
provider: openai
auth:
header:
Authorization: "Bearer ${OPENAI_API_KEY}"
options:
model: gpt-4o
❶ Set the provider to openai.
❷ Attach the OpenAI API key in the Authorization header.
❸ Set the default model to gpt-4o.
Synchronize the configuration to API7 Gateway:
adc sync -f adc.yaml