文件導航

TypeSafe Python SDK

安裝 TypeSafe Python SDK,開始使用非同步或同步 API 呼叫。

瀏覽 GitHub 上的 Python SDK 原始碼。

面向 TypeSafe API 的非同步與同步 Python 客戶端。如何用 TypeSafe 見這裡。

快速開始

  1. 安裝 SDK:

    uv add typesafe-sdk
    pip install typesafe-sdk

    加上 http2 extra(typesafe-sdk[http2])即可啟用 HTTP/2 支援。

  2. 在環境裡設定 TYPESAFE_API_KEY(在這裡建立)

  3. 呼叫 System One API:

    配合 AsyncTypeSafeClient:

    from typesafe_sdk import AsyncTypeSafeClient, Choice, Noul, Score
    
    async def main() -> None:
        async with AsyncTypeSafeClient() as client:
            response = await client.system_one(
                state={"document": "I was charged twice. Please fix this ASAP."},
                questions={
                    "billing": Noul(instructions="Is this ticket about billing?"),
                    "tone": Choice(
                        instructions="What is the customer's tone?",
                        criteria={"calm": None, "frustrated": None, "angry": None},
                    ),
                    "urgency": Score(
                        instructions="How urgent is this ticket?",
                        criteria=["can wait", "this week", "today"],
                    ),
                },
            )
    
        print(response.nouls["billing"].noul)
        print(response.choices["tone"].choice)
        print(response.scores["urgency"].score)

    配合 TypeSafeClient:

    from typesafe_sdk import Choice, Noul, Score, TypeSafeClient
    
    with TypeSafeClient() as client:
        response = client.system_one(
            state={"document": "I was charged twice. Please fix this ASAP."},
            questions={
                "billing": Noul(instructions="Is this ticket about billing?"),
                "tone": Choice(
                    instructions="What is the customer's tone?",
                    criteria={"calm": None, "frustrated": None, "angry": None},
                ),
                "urgency": Score(
                    instructions="How urgent is this ticket?",
                    criteria=["can wait", "this week", "today"],
                ),
            },
        )
    
    print(response.nouls["billing"].noul)
    print(response.choices["tone"].choice)
    print(response.scores["urgency"].score)

後續步驟

閱讀使用指南,瞭解更多模式,例如型別化響應、模型選擇、重試、HTTP/2和錯誤處理。