Search Documentation

Search for pages and headings in the documentation

Python SDK

Liutong is fully compatible with the OpenAI Python SDK. No special client library is needed. Point the base URL at Liutong and you are done.

Installation

pip install openai

Setup

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.liutong.llby.org/v1",
    api_key=os.environ.get("LIUTONG_API_KEY", "lt_your_api_key"),
)

Chat completion

response = client.chat.completions.create(
    model="crimson-falcon-4",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What are the benefits of Rust?"},
    ],
    temperature=0.7,
    max_tokens=512,
)

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

Streaming

stream = client.chat.completions.create(
    model="crimson-falcon-4",
    messages=[{"role": "user", "content": "Write a short poem about the moon."}],
    stream=True,
)

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

Reasoning

response = client.chat.completions.create(
    model="indigo-owl-4",
    messages=[
        {"role": "user", "content": "What is 2^100? Show your work."},
    ],
)

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

Embeddings

response = client.embeddings.create(
    model="jade-mole-4",
    input="The quick brown fox jumps over the lazy dog.",
)

embedding = response.data[0].embedding
print(f"Vector dimensions: {len(embedding)}")

Error handling

from openai import APIError, AuthenticationError

try:
    response = client.chat.completions.create(
        model="crimson-falcon-4",
        messages=[{"role": "user", "content": "Hello"}],
    )
except AuthenticationError:
    print("Invalid API key")
except APIError as e:
    print(f"API error: {e}")

Async usage

import asyncio
from openai import AsyncOpenAI

client = AsyncOpenAI(
    base_url="https://api.liutong.llby.org/v1",
    api_key="lt_your_api_key",
)

async def main():
    response = await client.chat.completions.create(
        model="crimson-falcon-4",
        messages=[{"role": "user", "content": "Hello!"}],
    )
    print(response.choices[0].message.content)

asyncio.run(main())