Model comparsion

Use the API to list all available models and compare their responses.

The API used in this example is listed here.

Example #1: Compare two random text models

For example, you can send the same request to two random text models and compare the results:

import requests
import random
from openai import OpenAI

# API credentials
BASE_URL = "https://api.aimlapi.com/v1"
# put your AIML API Key instead of <YOUR_API_KEY>:
API_KEY = "<YOUR_API_KEY>"

def get_models():
    headers = {"Authorization": f"Bearer {API_KEY}"}
    response = requests.get(f"{BASE_URL}/models", headers=headers)
    return list(response.json().keys())

def generate_joke(model):
    client = OpenAI(base_url=BASE_URL, api_key=API_KEY)
    
    system_prompt = "You are an AI assistant that only responds with jokes."
    user_prompt = "Why is the sky blue?"
    
    response = client.chat.completions.create(
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt},
        ],
        model=model,
    )
    
    return response.choices[0].message.content

def main():
    models = get_models()
    random.shuffle(models)
    selected_models = models[:2]

    for model in selected_models:
        joke = generate_joke(model)
        print(f"--- {model} ---")
        print(f"USER: Why is the sky blue?")
        print(f"AI  : {joke}")

if __name__ == "__main__":
    main()

Will return something like this:

--- zero-one-ai/Yi-34B-Chat ---
USER: Why is the sky blue?
AI  : Why is the sky blue? Because it's full of blueberries!
--- allenai/OLMo-7B-Instruct ---
USER: Why is the sky blue?
AI  : Because the white sun beams enter the blue Earth's atmosphere and get dispersed, resulting in the beautiful color we call "sky blue." It's like looking at paint being blown on a canvas by the wind! Just a joke, but the real answer is physics. 😎

Example #2: Compare two predefined text models

You can also send the same request to two specific text models of your choice and compare the results:

import requests
from openai import OpenAI

# API credentials
BASE_URL = "https://api.aimlapi.com/v1"
# put your AIML API Key instead of <YOUR_API_KEY>:
API_KEY = "<YOUR_API_KEY>"

# Specify the models to compare
model1 = "gpt-4o"
model2 = "MiniMax-Text-01"

def generate_joke(model):
    client = OpenAI(base_url=BASE_URL, api_key=API_KEY)
    
    system_prompt = "You are an AI assistant that only responds with jokes."
    user_prompt = "Why is the sky blue?"
    
    response = client.chat.completions.create(
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt},
        ],
        model=model,
    )
    
    return response.choices[0].message.content

def main():
    for model in [model1, model2]:
        joke = generate_joke(model)
        print(f"--- {model} ---")
        print(f"USER: Why is the sky blue?")
        print(f"AI  : {joke}")

if __name__ == "__main__":
    main()

Will return something like this:

--- gpt-4o ---
USER: Why did the programmer break up with their keyboard?
AI  : Because it just wasn't their type!
--- MiniMax-Text-01 ---
USER: Why did the programmer break up with their keyboard?
AI  : Because it kept hitting the wrong keys!

Last updated

Was this helpful?