Apertis Docs

SDKs & Libraries

LlamaIndex

LlamaIndex has native support for Apertis through the llama-index-llms-apertis package.

Updated
Reading time
1 min

On this page

Installation

pip install llama-index-llms-apertis llama-index

Basic Setup

from llama_index.llms.apertis import Apertis
from llama_index.core.llms import ChatMessage

# Configure with your API key from https://apertis.ai/setting?tab=keys
llm = Apertis(
    api_key="sk-your-api-key",
    model="gpt-5.2",
    max_tokens=256,
    context_window=4096,
)

Chat API

message = ChatMessage(role="user", content="Tell me a joke")
resp = llm.chat([message])
print(resp)

Streaming Chat

message = ChatMessage(role="user", content="Tell me a story in 250 words")
resp = llm.stream_chat([message])
for r in resp:
    print(r.delta, end="")

Completion API

resp = llm.complete("Tell me a joke")
print(resp)

Streaming Completion

resp = llm.stream_complete("Tell me a story in 250 words")
for r in resp:
    print(r.delta, end="")

Using Different Models

Use any compatible model returned by the Apertis model catalog:

# OpenAI GPT
llm = Apertis(model="gpt-5.2")

# Anthropic Claude
llm = Apertis(model="claude-sonnet-4.5")

# Google Gemini
llm = Apertis(model="gemini-3-flash-preview")

Context Compression

Enable context compression via extra headers to reduce token usage for long conversations:

from llama_index.llms.openai_like import OpenAILike

llm = OpenAILike(
    model="gpt-4.1",
    api_key="APERTIS_API_KEY",
    api_base="https://api.apertis.ai/v1",
    additional_kwargs={
        "extra_headers": {
            "X-Context-Compression": "on",
            "X-Compression-Model": "gpt-4.1-mini",
        }
    },
)

response = llm.complete("Summarize the key points from our discussion")

Supported Models

Provider Model ID
OpenAI gpt-5.2, gpt-4.1-mini
Anthropic claude-sonnet-4.5, claude-haiku-4.5
Google gemini-3-pro-preview, gemini-3-flash-preview

For the full list of models, visit Apertis Models.