1️⃣ Direct API Call to AiHubMix

Replace <AIHUBMIX_API_KEY> with your Aihubmix Key. Please note the key’s validity period and usage limits.

For available model options, please check the Model Gallery. Simply copy and paste the model name to use it.

import requests
import json

response = requests.post(
  url="https://aihubmix.com/v1/chat/completions",
  headers={
    "Authorization": "Bearer <AIHUBMIX_API_KEY>",
    "Content-Type": "application/json",
  },
  data=json.dumps({
    "model": "gpt-4o-mini", # replace model id
    "messages": [
      {
        "role": "user",
        "content": "What is the meaning of life?"
      }
    ]
  })
)

For streaming responses, simply add the parameter stream: true

2️⃣ Using OpenAI SDK

Replace <AIHUBMIX_API_KEY> with your Aihubmix Key. Please note the key’s validity period and usage limits. For available model options, please check the Model Gallery. Simply copy and paste the model name to use it.

from openai import OpenAI

client = OpenAI(
  base_url="https://aihubmix.com/v1",
  api_key="<AIHUBMIX_API_KEY>",
)

completion = client.chat.completions.create(
  model="gpt-4o-mini", # replace model id
  messages=[
    {
      "role": "developer",
      "content": "Always reply in Chinese"
    },    
    {
      "role": "user",
      "content": "What is the meaning of life?"
    }
  ],
  temperature=0.8,
  max_tokens=1024,
  top_p=1,
  frequency_penalty=0,
  presence_penalty=0,
  seed=random.randint(1, 1000000000),
)

print(completion.choices[0].message.content)

For models that support web search, you can add the following parameter:

  web_search_options={}, # search parameters

Available models: gpt-4o-search-preview, gpt-4o-mini-search-preview.

Please note that search models currently do not support parameters like temperature.

3️⃣ Making Requests via Third-party Clients

Refer to Use Cases