Python 指南
Python ≥ 3.10 项目推荐官方 SDK;不能装依赖的环境用标准库也能完整调用同一个端点。
方式一:官方 SDK
Section titled “方式一:官方 SDK”pip install typesafe-sdkfrom typesafe_sdk import Choice, Noul, TypeSafeClient # 异步版为 AsyncTypeSafeClient
with TypeSafeClient() as client: # 默认读取 TYPESAFE_API_KEY;模型默认 jev-latest response = client.system_one( state="Hi, my Stripe integration keeps failing. Please refund my subscription.", questions={ "department": Choice( instructions="Which team should handle this", criteria={ "billing": "Payment or subscription issues", "technical": "Bugs or integration problems", }, ), "asks_refund": Noul( instructions="The customer explicitly requests a refund", ), }, )
department = response.choices["department"]print(department.choice, department.confidence)print(response.nouls["asks_refund"].noul)SDK 提供 TypeSafeClient / AsyncTypeSafeClient 两个客户端和 Choice / Score / Noul 问题类型,也接受带 type 键的问题字典(与 API 参考里的 JSON 同构)。模型默认 jev-latest,要固定版本时给 system_one 传 model=。SDK 内置 RetryPolicy(重试次数、可重试状态码、退避曲线)。仓库:typesafe-ai/typesafe-sdk-python(MIT,178★,2026-09-22 核验)。
官方还有一个 system-one-adapter-python 适配器:用普通 LLM API 模拟 TypeSafeClient,适合 A/B 对照或降级运行(见开源生态)。
方式二:标准库 urllib
Section titled “方式二:标准库 urllib”完整可运行示例 triage.py 可以直接下载(仅用标准库):
curl -O https://typesafe-jev.com/examples/python/triage.pyTYPESAFE_USE_FIXTURE=1 python3 triage.pyTYPESAFE_API_KEY=你的Key python3 triage.py示例做的事:一条请求同时问部门 Choice、沮丧度 Score、退款 Noul,然后按 confidence 阈值决定自动派单还是转人工。核心结构:
request = urllib.request.Request( API_URL, data=json.dumps(PAYLOAD).encode("utf-8"), headers={ "Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json", }, method="POST",)with urllib.request.urlopen(request, timeout=30) as response: return json.loads(response.read().decode("utf-8"))在哪一步加业务逻辑
Section titled “在哪一步加业务逻辑”Python 侧同样遵循官方模式:模型只回答问题,阈值、路由、合并权重都留在你的代码里(Confidence-gated routing,经官方 llms.txt 索引核验)。示例中的 CONFIDENCE_THRESHOLD = 0.7 只是演示值,请在自己的标注样本上校准——完整做法见工单分流。
API 参考 · JavaScript 指南 · 能力总览