Sign up now for $10 free budget - Get an extra $40 when you get in touch

Using OpenAI

Call the AI Router using OpenAI python package

Setting up OpenAI

If you don't have it installed already, install the OpenAI package using pip:

pip install openai

Setting up AI Router

Set the openai base url to the AI Router base url https://api.airouter.io in your code and api key to the one you generated. Then proceed to call the OpenAI methods as you would normally do:

from openai import OpenAI

client = OpenAI.Client(
    base_url="https://api.airouter.io",
    api_key="<THE-API-KEY-YOU-GENERATED>"
)

client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
		{"role": "system", "content": "You are a helpful assistant."},
		{"role": "user", "content": "What is the meaning of life?"}
    ]
)

This example now uses the AI Router to select the most appropriate model for each request and uses gpt-4o-mini as a default model. You can also supply no model or specify a different model, as well as a list of models for the AI Router to consider to choose from.

Advanced usage

In case you want to pass some additional parameters as described in Model Selection or Weighting it can be done by adding the extra_body parameter to the call:

client.chat.completions.create(
        model='auto',
        messages=[<omitted>],
        extra_body={
            'models': ['mistral-medium-2312', 'gpt-4-0125-preview'],
            'weighting': {
                'quality': 1.0,
                'costs': 1.0,
                'latency': 1.0,
            }
        }
    )