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