Get started with AI Gateway
With Kong’s AI Gateway, you can deploy AI infrastructure for traffic that is sent to one or more LLMs. This lets you semantically route, secure, observe, accelerate, and govern traffic using a special set of AI plugins that are bundled with Kong Gateway distributions.
This tutorial will help you get started with AI Gateway by setting up the AI Proxy plugin with OpenAI.
Note: This quickstart runs a Docker container to explore Kong Gateway’s capabilities. If you want to run Kong Gateway as a part of a production-ready API platform, start with the Install page.
Prerequisites
Kong Konnect
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'
Copied to clipboard! -
Run the quickstart script to automatically provision a Control Plane and Data Plane, and configure your environment:
curl -Ls https://get.konghq.com/quickstart | bash -s -- -k $KONNECT_TOKEN --deck-output
Copied to clipboard!This sets up a Konnect Control Plane named
quickstart
, provisions a local Data Plane, and prints out the following environment variable exports:export DECK_KONNECT_TOKEN=$KONNECT_TOKEN export DECK_KONNECT_CONTROL_PLANE_NAME=quickstart export KONNECT_CONTROL_PLANE_URL=https://us.api.konghq.com export KONNECT_PROXY_URL='http://localhost:8000'
Copied to clipboard!Copy and paste these into your terminal to configure your session.
Check that Kong Gateway is running
We’ll be using decK for this tutorial, so let’s check that Kong Gateway is running and that decK can access it:
deck gateway ping
If everything is running, then you should get the following response:
Successfully Konnected to the Kong organization!
Create a Gateway Service
Create a Service to contain the Route for the LLM provider:
echo '
_format_version: "3.0"
services:
- name: llm-service
url: http://localhost:32000
' | deck gateway apply -
The URL can point to any empty host, as it won’t be used by the plugin.
Create a Route
Create a Route for the LLM provider. In this example we’re creating a chat route, so we’ll use /chat
as the path:
echo '
_format_version: "3.0"
routes:
- name: openai-chat
service:
name: llm-service
paths:
- "/chat"
' | deck gateway apply -
Enable the AI Proxy plugin
Enable the AI Proxy plugin to create a chat route:
echo '
_format_version: "3.0"
plugins:
- name: ai-proxy
config:
route_type: llm/v1/chat
model:
provider: openai
' | deck gateway apply -
In this example, we’re setting up the plugin with minimal configuration, which means:
- The client is allowed to use any model in the
openai
provider and must provide the model name in the request body. - The client must provide an
Authorization
header with an OpenAI API key.
If needed, you can restrict the models that can be consumed by specifying the model name explicitly using the config.model.name
parameter.
You can also provide the OpenAI API key directly in the configuration with the config.auth.header_name
and config.auth.header_value
parameters so that the client doesn’t have to send them.
Validate
To validate, you can send a POST
request to the /chat
endpoint, using the correct input format.
Since we didn’t add the model name and API key in the plugin configuration, make sure to include them in the request:
curl -X POST "$KONNECT_PROXY_URL/chat" \
-H "Accept: application/json"\
-H "Content-Type: application/json"\
-H "Authorization: Bearer $OPENAI_KEY" \
--json '{
"model": "gpt-4",
"messages": [
{
"role": "user",
"content": "Say this is a test!"
}
]
}'
curl -X POST "http://localhost:8000/chat" \
-H "Accept: application/json"\
-H "Content-Type: application/json"\
-H "Authorization: Bearer $OPENAI_KEY" \
--json '{
"model": "gpt-4",
"messages": [
{
"role": "user",
"content": "Say this is a test!"
}
]
}'
You should get a 200 OK
response, and the response body should contain This is a test
.
Cleanup
Clean up Konnect environment
If you created a new control plane and want to conserve your free trial credits or avoid unnecessary charges, delete the new control plane used in this tutorial.