SDKs

Python

Thunderbit Open API のための Python イディオムなパターン

公式 SDK は開発中です。それまでは、Thunderbit API は素の HTTP/JSON REST インターフェースなので、httpx(または requests)だけで十分です。

Install

pip install httpx

Configure

import httpx, os

API = "https://openapi.thunderbit.com/openapi/v1"
H = {"Authorization": f"Bearer {os.environ['THUNDERBIT_API_KEY']}"}
client = httpx.Client(base_url=API, headers=H, timeout=60.0)

ページを Distill する

resp = client.post("/distill", json={"url": "https://thunderbit.com/playground"})
resp.raise_for_status()
print(resp.json()["data"]["markdown"])

構造化データを Extract する

resp = client.post("/extract", json={
    "url": "https://example.com/product/iphone-15-pro",
    "schema": {
        "type": "object",
        "properties": {
            "name":  {"type": "string"},
            "price": {"type": "number"},
        },
        "required": ["name", "price"],
    },
})
print(resp.json()["data"])

Async

高スループットなパイプラインでは httpx.AsyncClient に切り替えてください。

import asyncio, httpx

async def distill_many(urls: list[str]):
    async with httpx.AsyncClient(headers=H, timeout=60.0) as client:
        tasks = [client.post(f"{API}/distill", json={"url": u}) for u in urls]
        resps = await asyncio.gather(*tasks)
        return [r.json()["data"]["markdown"] for r in resps]

10 件以上の URL を扱う場合は、単発呼び出しを並列展開するより /batch/distill のほうが望ましいです —— Batch Job Lifecycle を参照してください。

公式 Python SDK は開発中です —— もう少しお待ちください。