Chat Completion

Create chat completion

Please note that there is not just one, but several variations of this API described below, each with its own set of supported models and available parameters. You can explore these variations by selecting different options from the object dropdown menu and choose the one that suits you best.

Generate a conversational response using a language model.

Creates a chat completion using a language model, allowing interactive conversation by predicting the next response based on the given chat history. This is useful for AI-driven dialogue systems and virtual assistants.

post

/v1/chat/completions

Authorizations
Body
one of
Responses
curl -L \
  --request POST \
  --url 'https://api.aimlapi.com/v1/chat/completions' \
  --header 'Authorization: Bearer JWT' \
  --header 'Content-Type: application/json' \
  --data '{"max_tokens":512,"stream":false,"model":"microsoft/WizardLM-2-8x22B","logit_bias":{},"stream_options":{"include_usage":true},"tools":[{"type":"function","function":{"description":"text","name":"text"}}],"tool_choice":"none","response_format":{"type":"text"},"messages":[{"role":"system"}]}'

No body

Example of chat completion

We will call the gpt-4o model using the Python programming language and the OpenAI SDK.

%pip install openai
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.aimlapi.com/v1",

    # Insert your AIML API Key in the quotation marks instead of <YOUR_API_KEY>:
    api_key="<YOUR_API_KEY>",  
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {
            "role": "system",
            "content": "You are an AI assistant who knows everything.",
        },
        {
            "role": "user",
            "content": "Why do the seasons change?"
        },
    ],
)

message = response.choices[0].message.content

print(f"Assistant: {message}")

Last updated

Was this helpful?