OpenAI SDK: Chat route with dynamic model selectionv3.8+

Configure a chat route that reads the target model from the request path instead of hardcoding it in the configuration.

For this plugin to work properly, you need a Gateway Route with the following configuration:

routes:
 - name: openai-chat
   paths:
     - "~/(?<model>[^#?/]+)/chat/completions$"
   methods:
     - POST

Applying this configuration will let you target different models dynamically using an OpenAI-compatible SDK. For example:

client = OpenAI(
  base_url="http://localhost:8000/gpt-4"
)

In this case, the Python SDK automatically expands the URL to http://localhost:8000/gpt-4/chat/completions, and Kong Gateway extracts the model name (gpt-4) from the path to route the request appropriately.

Prerequisites

  • OpenAI account

Environment variables

  • OPENAI_API_KEY: The API key to authenticate requests to OpenAI.

Set up the plugin

Something wrong?

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!