文档目录

Python SDK

安装 TypeSafe Python SDK,开始用异步或同步方式调用 API。

非官方中文翻译 · 内容以 英文原文 ↗ 为准 · 同步于 2026-10-15

在 GitHub 上浏览 Python SDK 源码。

面向 TypeSafe API 的 Python 异步和同步客户端。在 此处 学习如何使用 TypeSafe。

快速开始

  1. 安装 SDK:

    uv

    sh
    uv add typesafe-sdk

    pip

    sh
    pip install typesafe-sdk

    支持的 extras(例如 typesafe-sdk[http2]):

  2. 在环境中设置 TYPESAFE_API_KEY(在 此处 创建)

  3. 调用 System One API:

    异步

    使用 AsyncTypeSafeClient:

    python
    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:

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

下一步

访问使用指南,了解如何:

本文档由 Mintlify(开发者文档平台)构建和托管。