Calling the API with Python

Calling the API with Python

After reading this guide, you can call our models using Python's official openai library. Our API is OpenAI-compatible, so you can use the official library directly.

Installation

pip install openai

Code

from openai import OpenAI

client = OpenAI(
    api_key="sk-xxxxxxxx",
    base_url="https://api.tvtapi.com/v1",
)

response = client.chat.completions.create(
    model="your-model-name",  # copy from model marketplace
    messages=[
        {"role": "user", "content": "Hello"}
    ],
)

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

Two substitutions:

  1. sk-xxxxxxxx → your API Key
  2. your-model-name → a model name copied from the model marketplace

Streaming Output

stream = client.chat.completions.create(
    model="your-model-name",
    messages=[{"role": "user", "content": "Tell me a story"}],
    stream=True,
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Troubleshooting