Examples

Endpoints

Chat Completions

  • Endpoint: POST /chat/completions

  • Description: Generate responses based on given prompts.

Example Request in Python and JS

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.aimlapi.com",
)

response = client.chat.completions.create(
    model="mistralai/Mistral-7B-Instruct-v0.2",
    messages=[
        {
            "role": "system",
            "content": "You are an AI assistant who knows everything.",
        },
        {
            "role": "user",
            "content": "Tell me, why is the sky blue?"
        },
    ],
)

message = response.choices[0].message.content
print(f"Assistant: {message}")

Image Generation

Endpoint: POST /images/generations Description: Generate images based on given prompts.

Example Request in Python and JS

import requests
import base64


def main():
    headers = {
        "Authorization": "Bearer YOUR_API_KEY",
    }

    payload = {
        "prompt": "Hyperrealistic art featuring a cat in costume.",
        "model": "stabilityai/stable-diffusion-2-1",
    }

    response = requests.post(
        "https://api.aimlapi.com/images/generations", headers=headers, json=payload
    )

    image_base64 = response.json()["output"]["choices"][0]["image_base64"]
    image_data = base64.b64decode(image_base64)

    with open("./image.png", "wb") as file:
        file.write(image_data)


main()

Speech to Text

  • Endpoint: POST api.aimlapi.com/stt

  • Description: Convert audio files to text.

import requests

url = "https://api.aimlapi.com/stt"
headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}
payload = {
    "model": "#g1_nova-2-general",
    "url": "https://audio-samples.github.io/samples/mp3/blizzard_unconditional/sample-0.mp3"
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())

Text to Speech

  • Endpoint: POST api.aimlapi.com/tts

  • Description: Convert text to audio.

  • Example Request (JS):

import requests

url = "https://api.aimlapi.com/tts"
headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json"
}
payload = {
    "model": "#g1_aura-asteria-en",
    "text": "Hi! I'm your friendly assistant."
}
response = requests.post(url, json=payload, headers=headers)
with open("output.wav", "wb") as file:
    file.write(response.content)

Response Structure

Common Response Fields

  • id: Unique identifier for the request.

  • object: Type of object returned.

  • created: Timestamp of the request.

  • model: Model used for the request.

  • choices: List of completion choices.

  • usage: Token usage information.

Example Response (Chat Completion):

{
  "id": "cmpl-2zKST3SO0NMoQ",
  "object": "text_completion",
  "created": 1615241840,
  "model": "mistralai/Mistral-7B-Instruct-v0.2",
  "choices": [
    {
          "text": "\n\nSure! Here's a joke for you: Why don't scientists trust atoms? Because they make up everything!",
      "index": 0,
      "logprobs": null,
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 23,
    "total_tokens": 28
  }
}

Last updated