API 參考
TypeSafe 評估端點的完整 HTTP API 參考。
用一個型別化 questions 對映來評估 state,拿回結構化的 answers,每個問題一個。想跟著入門,請從原語開始。
評估端點
POST https://api.typesafe.ai/v1/systemone
Authorization: Bearer <API_KEY>
Content-Type: application/json
請求體
每次請求的頂層結構。questions 對映中的每一項都是你命名的型別化問題。
state
要評估的內容。文本用普通字串,聊天記錄、資料記錄或應用的當前狀態之類則用結構化資料(物件/陣列)。格式和最佳實踐見狀態。
model
處理該請求的模型。用 "jev-latest",也就是 TypeSafe 的旗艦模型。可用模型和別名見模型。
questions
{
"state": "Help! My payouts have been failing for 3 days.",
"model": "jev-latest",
"questions": {
"is_urgent": {
"type": "noul",
"instructions": "Does this convey urgency?"
}
}
}
問題型別
一個 Question 是三種類型之一,由其 type 欄位決定。三者都共有 type 和 instructions;各自再加上自己的 criteria。
instructions 屬性可以是字串、物件或陣列。如果一個問題很長,還帶有額外上下文或需要引用的資料,你可以把它拆成一個結構化物件。把問題放在一個欄位裡,把資料放在其它欄位裡,並用反引號按名字引用那些資料欄位,就像把一個問題的指向對準 state 裡巢狀的值一樣:
"instructions": {
"potential_duplicate": {
"name": "John Smith",
"location": "Oakland, California",
"last_employer": "Google"
},
"question": "Is the resume for the same person as `potential_duplicate`?"
}
詳見在問題中使用結構化資料。
Noul
一個是/否問題。返回答案為「是」的機率。
type
instructions
要評估的是/否問題。物件可以把問題放在一個欄位裡,把它引用的資料放在其它欄位裡;見在問題中使用結構化資料。
criteria
可選,描述「是」和「否」分別意味著什麼。
屬性
true
「是」(取值接近 1)意味著什麼。
false
「否」(取值接近 0)意味著什麼。
{
"state": "Help! My payouts have been failing for 3 days.",
"model": "jev-latest",
"questions": {
"is_urgent": {
"type": "noul",
"instructions": "Does this convey urgency?",
"criteria": {
"true": "Explicitly time-sensitive",
"false": "No urgency expressed"
}
}
}
}
Choice
從你定義的一組選項中挑出一個。返回被選中的選項以及完整的機率分佈。
type
instructions
要讓模型判斷什麼。物件可以把問題放在一個欄位裡,把它引用的資料放在其它欄位裡;見結構化的 instructions 與 criteria。
criteria
選項到量規描述的對映;某個選項不需要額外說明時用 null。一個 Choice 最多可以有 255 個選項。
對映項
‹option›
一個由你選擇的鍵。對該選項的描述。
{
"state": "Help! My payouts have been failing for 3 days.",
"model": "jev-latest",
"questions": {
"department": {
"type": "choice",
"instructions": "Which team should handle this?",
"criteria": {
"billing": "Payments, invoicing, refunds",
"technical": "Bugs, outages, integrations",
"sales": "Pricing, upgrades, new accounts"
}
}
}
}
Score
按你定義的量規給狀態評分。返回一個在你各檔位之間按機率加權的值。
type
instructions
要讓模型評分什麼。物件可以把問題放在一個欄位裡,把它引用的資料放在其它欄位裡;見在問題中使用結構化資料。
criteria
一個有序的檔位描述陣列。一個 Score 至少要有兩個檔位;API 最多接受 10 個。
{
"state": "Help! My payouts have been failing for 3 days.",
"model": "jev-latest",
"questions": {
"frustration": {
"type": "score",
"instructions": "How frustrated is the customer?",
"criteria": ["Calm", "Frustrated", "Very angry"]
}
}
}
響應體
每個問題一個答案,以你提供的相同 id 返回。
model
執行這次評估的模型。
answers
每個問題一個 Answer,以你在 questions 中用過的相同 id 為鍵。
對映項
‹question id›
你在 questions 中選擇的同一個 id。
usage
該請求的 token 用量。
屬性
input_tokens
output_tokens
{
"model": "jev-1.13.0",
"answers": {
"is_urgent": {
"type": "noul",
"noul": 0.95
}
},
"usage": { "input_tokens": 296, "output_tokens": 20 }
}
答案型別
每個答案都帶有一個與其問題匹配的 type。Choice 和 Score 答案還帶有一個 0 到 1 之間的 confidence,由該答案的機率分佈推導得出。見置信度。
Noul 答案
type
noul
是/否答案,取值從 0(否)到 1(是)。
{
"model": "jev-1.13.0",
"answers": {
"is_urgent": {
"type": "noul",
"noul": 0.95
}
},
"usage": { "input_tokens": 307, "output_tokens": 20 }
}
Choice 答案
type
choice
機率最高的選項。
probabilities
每個選項對映到它的機率(求和為 1 的浮點數)。
對映項
‹option›
你在 criteria 中定義的一個選項。
confidence
模型的確定程度,由機率分佈推導得出。
{
"model": "jev-1.13.0",
"answers": {
"department": {
"type": "choice",
"choice": "billing",
"probabilities": { "billing": 0.88, "technical": 0.12, "sales": 0.0 },
"confidence": 0.81
}
},
"usage": { "input_tokens": 318, "output_tokens": 34 }
}
Score 答案
type
score
在各檔位之間按機率加權的答案;可能落在檔位之間。
legend
每個檔位編號映射回它的描述。
probabilities
每個檔位(字串鍵)對映到它的機率(求和為 1 的浮點數)。
對映項
‹level›
檔位索引,作為與 legend 匹配的字串鍵。
confidence
模型的確定程度,由機率分佈推導得出。
{
"model": "jev-1.13.0",
"answers": {
"frustration": {
"type": "score",
"score": 1.05,
"legend": { "0": "Calm", "1": "Frustrated", "2": "Very angry" },
"probabilities": { "0": 0.0, "1": 0.95, "2": 0.05 },
"confidence": 0.92
}
},
"usage": { "input_tokens": 304, "output_tokens": 18 }
}
錯誤
錯誤使用標準 HTTP 狀態碼,並附帶描述哪裡出錯的 JSON 響應體。
| 狀態碼 | 含義 |
|---|---|
401 Unauthorized |
API key 缺失或無效。檢查 Authorization 請求頭。 |
422 Unprocessable Entity |
請求體未通過校驗 —— 比如缺少必填欄位或問題格式有誤。響應體會詳細說明出錯的欄位。 |
429 Too Many Requests |
你超出了速率限制。退避一小段時間後再重試。 |
529 Overloaded |
TypeSafe 暫時過載。稍等片刻再重試。 |
處理限流
當你收到 429 Too Many Requests 或 529 Overloaded 響應時,不要立即重試,而應使用指數退避重試該請求。我們的客戶端 SDK 會自動處理這一點,所以只要你使用我們的 SDK 且沿用其預設重試策略,就無需額外處理。