English

Connect OpenAI SDK

Connect OpenAI SDK to ModelPort using Chat Completions.

Setup

  1. Install the Python openai package and set MODELPORT_API_KEY.
  2. Set MODELPORT_TEXT_MODEL to a text model available to your account. The example calls Chat Completions.

OpenAI SDK configuration

import os
from uuid import uuid4
from openai import OpenAI

client = OpenAI(api_key=os.environ["MODELPORT_API_KEY"], base_url="https://api.apimodel.store/v1")
completion = client.chat.completions.create(
    model=os.environ["MODELPORT_TEXT_MODEL"],
    messages=[{"role": "user", "content": "Reply with one short sentence."}],
    max_completion_tokens=128,
    extra_headers={"Idempotency-Key": str(uuid4())},
)
print(completion.choices[0].message.content)

Verify the connection

Check the model catalog for a text model with available: true. A text route must be open to your account before you can send chat requests.

Text API and billing

OpenAI SDK official documentation ↗