ネイティブ SDK

OpenAI SDK

TokenLabを公式のOpenAI SDKおよびOpenAI互換クライアントで使用する

概要

OpenAI SDKクライアントの接続先を https://api.tokenlab.sh/v1 に設定し、TokenLab APIキーを使用します。アプリケーションで既に使用しているAPIメソッドをそのまま利用し、選択したモデルが accepted_request_formats を通じてそのメソッドをサポートしていることを確認してください。

Python、JavaScript、Goには公式のOpenAI SDKがあります。PHPはOpenAI互換のコミュニティクライアントで動作しますが、公式のOpenAI SDKではありません。

インストール

pip install openai

Responsesの入力アイテム、ツール、または継続には POST /v1/responses を使用してください。オプションのフィールドは選択したモデルによって異なります。

クライアントの設定

from openai import OpenAI

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

推奨: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)

上級者向け:Responses API

このパスは、ツールやワークフローがOpenAI Responsesのセマンティクスに明示的に依存している場合にのみ使用してください。

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 / 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)

ResponsesによるVision

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])

トラブルシューティング

このページの内容