SDKs nativos

OpenAI SDK

Utilice TokenLab con los SDK oficiales de OpenAI y clientes compatibles con OpenAI

Descripción general

Apunte un cliente de SDK de OpenAI a https://api.tokenlab.sh/v1 y utilice una clave de API de TokenLab. Mantenga el método de API que su aplicación ya utiliza y confirme que el modelo seleccionado lo admite a través de accepted_request_formats.

Python, JavaScript y Go cuentan con SDK oficiales de OpenAI. PHP funciona bien con clientes de la comunidad compatibles con OpenAI, pero no es un SDK oficial de OpenAI.

Instalación

pip install openai

Utilice POST /v1/responses para elementos de entrada, herramientas o continuación de Responses. Los campos opcionales dependen del modelo seleccionado.

Configuración del cliente

from openai import OpenAI

client = OpenAI(
    api_key="sk-your-tokenlab-key",
    base_url="https://api.tokenlab.sh/v1",
)

Recomendado: Chat Completions

response = client.chat.completions.create(
    model="gpt-5.6-terra",
    messages=[{"role": "user", "content": "Reply with: Connected to TokenLab."}]
)

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

Avanzado: Responses API

Utilice esta ruta solo cuando su herramienta o flujo de trabajo dependa explícitamente de la semántica de Responses de OpenAI.

Streaming con Responses

stream = client.responses.create(
    model="gpt-5.6-terra",
    input="Write a short poem about coding.",
    stream=True,
)

for event in stream:
    if event.type == "response.output_text.delta":
        print(event.delta, end="")

Herramientas / Llamada a funciones (Function Calling)

response = client.responses.create(
    model="gpt-5.6-terra",
    input="What's the weather in Tokyo?",
    tools=[{
        "type": "function",
        "name": "get_weather",
        "description": "Get weather for a location",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {"type": "string"}
            },
            "required": ["location"]
        }
    }]
)

for item in response.output:
    if item.type == "function_call":
        print(item.name)
        print(item.arguments)

Visión con Responses

response = client.responses.create(
    model="gpt-5.6-terra",
    input=[{
        "role": "user",
        "content": [
            {"type": "input_text", "text": "What's in this image?"},
            {"type": "input_image", "image_url": "https://example.com/image.jpg"}
        ]
    }]
)

print(response.output_text)

Embeddings

response = client.embeddings.create(
    model="text-embedding-3-small",
    input="Hello world"
)

print(response.data[0].embedding[:5])

Solución de problemas

En esta página