Route OpenAI traffic to Amazon Bedrock Mantle with AI Gateway

Incompatible with
on-prem
Tags
Minimum Version
AI Gateway - 2.3
TL;DR

Create an AI Model Provider of type bedrock with your AWS credentials, then create an AI Model with the openai format and a Bedrock target that sets endpoint_type to mantle. AI Gateway sends OpenAI requests to bedrock-mantle.{region}.api.aws in OpenAI format, without translating them to Bedrock Converse, and signs them with SigV4.

Prerequisites

This is a Konnect tutorial and requires a Konnect personal access token.

  1. Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.

  2. Export your token to an environment variable:

    export KONNECT_TOKEN='YOUR_KONNECT_PAT'
  3. 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.

Want a fully-hosted AI Gateway? See AI Gateway 2.0 on Dedicated Cloud Gateways.

This tutorial uses kongctl to manage Konnect resources programmatically. We recommend keeping kongctl up to date with the latest version (1.24.0).

  1. Install kongctl from developer.konghq.com/kongctl.
  2. Verify the installation:

    kongctl version
  1. In the AWS Management Console, confirm that Amazon Bedrock Mantle is available in your region and that your account has access to the model you want to use.
  2. Create an IAM user or role with permission to call Bedrock Mantle, then create access keys for it. The following is an example security policy you can apply to your IAM user or role:
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "VisualEditor0",
          "Effect": "Allow",
          "Action": [
            "bedrock-mantle:GetModel",
            "bedrock-mantle:ListFiles",
            "bedrock:InvokeModel",
            "bedrock-mantle:CreateFile",
            "bedrock-mantle:ListModels",
            "bedrock-mantle:CancelInference",
            "bedrock-mantle:GetFile",
            "bedrock:InvokeModelWithResponseStream",
            "bedrock-mantle:CreateInference",
            "bedrock-mantle:CountTokens"
          ],
          "Resource": [
            "arn:aws:bedrock-mantle:{region}:{account_id}:project/{project_name_or_default}",
            "arn:aws:bedrock-mantle:{region}:{account_id}:project/{project_name_or_default}/{model_name_1}",
            "arn:aws:bedrock-mantle:{region}:{account_id}:project/{project_name_or_default}/{model_name_2}"
          ]
        }
      ]
    }
  3. Export your AWS credentials, region, and the Mantle model ID:
    export AWS_ACCESS_KEY_ID='YOUR_AWS_ACCESS_KEY_ID'
    export AWS_SECRET_ACCESS_KEY='YOUR_AWS_SECRET_ACCESS_KEY'
    export AWS_REGION='YOUR_AWS_REGION'
    export MANTLE_MODEL_ID='YOUR_MANTLE_MODEL_ID'

Create an AI Model Provider

Create a bedrock type AI Model Provider that stores your AWS credentials. The same provider works for Bedrock Runtime and Bedrock Mantle targets:

kongctl apply -f - --auto-approve --pat "$KONNECT_TOKEN" << 'EOF'
ai_gateway_model_providers:
  - ref: my-aws-account
    ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
    name: my-aws-account
    display_name: "AWS Production"
    type: bedrock
    config:
      auth:
        type: aws
        access_key_id: !env AWS_ACCESS_KEY_ID
        secret_access_key: !secret {source: !env AWS_SECRET_ACCESS_KEY}
EOF

To authenticate with an Amazon Bedrock API key instead of SigV4, set config.auth.type to basic and add an Authorization header with the value Bearer <BEDROCK_API_KEY> in config.auth.headers.

Create an AI Model with a Mantle target

Create an AI Model that accepts OpenAI requests and routes them to Bedrock Mantle:

kongctl apply -f - --auto-approve --pat "$KONNECT_TOKEN" << 'EOF'
ai_gateway_models:
  - ref: my-mantle-model
    ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
    name: my-mantle-model
    display_name: "my-mantle-model"
    type: model
    formats:
      - type: openai
    config:
      route:
        paths:
          - /
        model:
          body_param: model
          values:
            - my-mantle-model
    targets:
      - name: !env MANTLE_MODEL_ID
        provider: my-aws-account
        config:
          type: bedrock
          region: !env AWS_REGION
          endpoint_type: mantle
    capabilities:
      - generate
EOF

The AI Model uses the following settings:

  • formats: [type: openai]: Accepts OpenAI Chat Completions requests. A Mantle target can’t be paired with the bedrock format, because Mantle has no Converse or InvokeModel API, and Konnect rejects that configuration.
  • targets[].config.endpoint_type: mantle: Sends requests to https://bedrock-mantle.{region}.api.aws instead of the default Bedrock Runtime endpoint. AI Gateway keeps the OpenAI request format instead of translating it to Bedrock Converse, and changes only the host, path, and authentication headers.
  • config.route.model: Lets clients send the alias my-mantle-model in the model field instead of the upstream model ID.

Validate

Send a chat request to the AI Model:

curl -X POST "$KONNECT_PROXY_URL/v1/chat/completions" \
     --no-progress-meter --fail-with-body  \
     -H "Accept: application/json"\
     -H "Content-Type: application/json" \
     --json '{
       "messages": [
         {
           "role": "user",
           "content": "Say this is a test!"
         }
       ],
       "model": "my-mantle-model"
     }'

A 200 response with an OpenAI Chat Completions body confirms that Bedrock Mantle served the request.

Cleanup

To clean up all AI Gateway resources created in this guide, run:

curl -Ls https://get.konghq.com/ai | bash -s -- -d

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!