Python · typesafe-sdk
用官方 typesafe-sdk Python 套件接入 EasyJev:pip 安裝、TypeSafeClient 設定 sk- 金鑰與本站 base_url、noul/choice/score 三種題型、異常映射與 request_id 追蹤。
安裝
pip install typesafe-sdk建立客戶端
from typesafe_sdk import TypeSafeClient client = TypeSafeClient( api_key="sk-your-key", # EasyJev console -> API Keys base_url="https://api.easyjev.online", # the only other edit )
兩個值也可以用環境變數提供:TYPESAFE_API_KEY 與 TYPESAFE_BASE_URL。
三種題型
noul(是非題)——返回「是」的校準概率,區間 [0, 1]。適合做門控判斷:這是退款請求嗎?是垃圾訊息嗎?
from typesafe_sdk import Noul result = client.system_one( state="User asks: can I get a refund for order #1024?", questions={"refund": Noul(instructions="Is this a refund request?")}, ) print(result.nouls["refund"].noul) # probability of "yes", e.g. 0.97
choice(選擇題)——在最多 255 個命名候選裡選一個,附每個候選的概率。適合路由與分類。
from typesafe_sdk import Choice result = client.system_one( state="Ticket: my order #1024 arrived damaged", questions={ "route": Choice( instructions="Pick the team that should handle this ticket", criteria={ "billing": "Refunds, charges, payment issues", "support": "Order status, delivery, damaged items", "sales": "Pre-sale questions, quotes", }, ) }, ) print(result.choices["route"].choice) # e.g. "support"
score(評分題)——按 2–10 個有序層級打分,返回可在層級間插值的分數。適合嚴重度與品質評級。
from typesafe_sdk import Score result = client.system_one( state="Comment: this product changed my life, best purchase ever!!", questions={ "severity": Score( instructions="Rate how urgent moderation is", criteria=["benign", "minor", "review", "block"], ) }, ) print(result.scores["severity"].score) # e.g. 0.25 (between benign and minor)
錯誤處理
SDK 拋出的仍是官方異常類;EasyJev 把閘道錯誤映射到與官方 API 相同的異常體系。每個回應都帶 request_id(回應標頭 X-Request-Id)——聯絡支援時請附上它。
from typesafe_sdk import errors try: result = client.system_one( state="...", questions={"q": {"type": "noul", "instructions": "..."}}, model="typesafe/jev-latest", ) except errors.AuthenticationError: ... # 401/403: wrong or disabled sk- key except errors.RateLimitError: ... # 429: RPM/TPM limit hit, back off and retry except errors.APIError as e: ... # other gateway errors; e carries the unified error body