TokenLab

SDK gốc

OpenAI SDK

Sử dụng TokenLab với các SDK OpenAI chính thức và các client tương thích với OpenAI

Tổng quan

Trỏ client SDK OpenAI tới https://api.tokenlab.sh/v1 và sử dụng API key của TokenLab. Giữ nguyên phương thức API mà ứng dụng của bạn đang sử dụng và xác nhận rằng model đã chọn hỗ trợ phương thức đó thông qua accepted_request_formats.

Python, JavaScript và Go có các SDK OpenAI chính thức. PHP hoạt động tốt với các client cộng đồng tương thích với OpenAI, nhưng đây không phải là SDK OpenAI chính thức.

Cài đặt

pip install openai

Sử dụng POST /v1/responses cho các mục nhập Responses, tools hoặc continuation. Các trường tùy chọn phụ thuộc vào model đã chọn.

Cấu hình Client

from openai import OpenAI

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

Khuyên dùng: 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)

Nâng cao: Responses API

Chỉ sử dụng đường dẫn này khi công cụ hoặc quy trình làm việc của bạn phụ thuộc rõ ràng vào ngữ nghĩa của OpenAI Responses.

Streaming với 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="")

Tools / Gọi hàm (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)

Vision với 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])

Khắc phục sự cố

Trên trang này