TS TypeSafe 文档中文版 原文 ↗

TypeSafe Python SDK

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

在 GitHub 上浏览 Python SDK 源码。

用于 TypeSafe API 的异步与同步 Python 客户端。TypeSafe 的使用方法见 这里。

快速开始

  1. 安装 SDK:
bash
uv add typesafe-sdk
 
bash
pip install typesafe-sdk
 
  1. 在环境中设置 TYPESAFE_API_KEY(在 这里 创建)
  2. 调用 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)
 

用法

更多内容见 用法指南。