curl -X POST "$KONNECT_PROXY_URL/v1/chat/completions" \
--no-progress-meter --fail-with-body \
-H "Accept: application/json"\
-H "Content-Type: application/json"\
-H "Authorization: Bearer $OPENAI_API_KEY" \
--json '{
"messages": [
{
"role": "user",
"content": "Say this is a test!"
}
],
"model": "my-gpt-4o"
}'Get started with AI Gateway
AI Gateway provides first-class entities for managing LLM providers and models in Kong Konnect. Create an AI Model Provider entity to connect and authenticate to an LLM service like OpenAI, then create an AI Model entity to specify which model is available for requests.
This tutorial shows you how to set up an AI Provider and AI Model for OpenAI in Kong Konnect using kongctl and how to proxy your first request to OpenAI.
Prerequisites
AI Gateway running
This is a Konnect tutorial and requires a Konnect personal access token.
-
Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.
-
Export your token to an environment variable:
export KONNECT_TOKEN='YOUR_KONNECT_PAT' -
Run the AI Gateway quickstart script to automatically provision a control plane and data plane in Kong Konnect, and configure your environment:
curl -Ls https://get.konghq.com/ai | bash -s -- -k $KONNECT_TOKEN
This sets up a AI Gateway control plane named ai-quickstart, provisions a local data plane, and prints out the following environment variables export:
export AI_GATEWAY_ID=your-gateway-id
export KONNECT_TOKEN=$KONNECT_TOKEN
export KONNECT_CONTROL_PLANE_NAME=ai-quickstart
export KONNECT_CONTROL_PLANE_URL=https://us.api.konghq.com
export KONNECT_PROXY_URL='http://localhost:8000'Copy and paste these into your terminal to configure your session.
kongctl v1.15.0+
This tutorial uses kongctl to manage Konnect resources programmatically. We recommend keeping kongctl up to date with the latest version (1.15.0).
- Install kongctl from developer.konghq.com/kongctl.
-
Verify the installation:
kongctl version
OpenAI
This tutorial uses OpenAI:
- Create an OpenAI account.
- Get an API key.
-
Export your API key as an environment variable:
export OPENAI_API_KEY='YOUR_OPENAI_API_KEY'
Create an AI Model Provider entity
Create an AI Model Provider entity to define your connection to OpenAI and store your authentication credentials.
First, set the OPENAI_AUTH_HEADER environment variable to your OpenAI API key:
export OPENAI_AUTH_HEADER="Bearer $OPENAI_API_KEY"Then, apply the configuration using kongctl:
kongctl apply -f - --auto-approve --pat "$KONNECT_TOKEN" << 'EOF'
ai_gateway_model_providers:
- ref: generic-openai
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: generic-openai
display_name: "generic-openai"
type: openai
config:
auth:
type: basic
headers:
- name: Authorization
value: !secret {source: !env OPENAI_AUTH_HEADER}
EOF
!env AI_GATEWAY_IDreferences the AI Gateway created by the quickstart script in the prerequisites, instead of creating a new one.
In this example, we’re setting up the AI Model Provider with:
type: openai: Specifies that this provider connects to the OpenAI service using OpenAI’s standard API format.name: generic-openai: A unique identifier that AI Models will reference to route requests through this provider.config.auth: Stores your OpenAI API key. AI Gateway securely manages this credential and injects it into upstream requests automatically, eliminating the need for clients to pass API keys.
Create an AI Model entity
Create an AI Model entity to declare which upstream models are available, configure how client requests are routed, and specify which AI Model Provider to use:
kongctl apply -f - --auto-approve --pat "$KONNECT_TOKEN" << 'EOF'
ai_gateway_models:
- ref: my-gpt-4o
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: my-gpt-4o
display_name: "my-gpt-4o"
type: model
formats:
- type: openai
config:
route:
paths:
- /v1
model:
body_param: model
values:
- my-gpt-4o
targets:
- name: gpt-4o
provider: generic-openai
config:
type: openai
policies: []
capabilities:
- generate
EOF
!env AI_GATEWAY_IDreferences the AI Gateway created by the quickstart script, same as in the previous step.
In this example, we’re setting up the AI Model with:
type: model: Specifies this is a synchronous model for request/response workloads.name: my-gpt-4o: A unique identifier for this model.formats: [type: openai]: Declares that this model accepts requests in OpenAI-compatible format.config.route.paths: [/v1]: Configures the custom base path for this model’s endpoints. Clients send requests to paths that combine this base path with capability-specific paths.capabilities: [generate]: Enables the text generation capability. Thegeneratecapability creates a/chat/completionsendpoint, so combined with your base path, clients send chat requests to/v1/chat/completions.config.route.model: { body_param: model, values: [my-gpt-4o] }: Lets clients sendmy-gpt-4oin the requestmodelfield instead of the upstream model name.targets: Specifies which upstream AI Model Provider model to route requests to. Here,provider: generic-openaireferences the AI Model Provider we created earlier, andname: gpt-4ospecifies which OpenAI model to call upstream.
Validate
Send a chat request to verify your setup:
Cleanup
Clean up AI Gateway resources
To clean up all AI Gateway resources created in this guide, run:
curl -Ls https://get.konghq.com/ai | bash -s -- -d