531 字
3 分鐘

Perplexity Decider locate 怎麼用?JavaScript 驗證原文片段與位移

本文目錄

Perplexity Decisions API 的 locate 可從原文找出錯誤碼、產品名稱等片段。回應的 start、end 以 Unicode 字元計數。JavaScript 字串的 slice() 使用 UTF-16 code unit,若片段前有 emoji,就先把原文轉成 code point 陣列再切。Decisions API 文件

請求保留一份完整原文#

以下是送往 POST https://api.perplexity.ai/v1/decisions 的 JSON body 示意。應用程式應保留送出的 state 來驗證結果。若畫面會刪除空白或正規化文字,先在原文校驗,再處理畫面上的位置對應。

{
"model": "pplx-decider-v1.3",
"state": "🧪錯誤 E42,請稍後再試",
"questions": {
"error_codes": {
"type": "locate",
"instructions": "Find each literal error code in the text."
},
"asks_to_retry": {
"type": "noul",
"instructions": "Does the message ask the reader to try again?"
}
}
}

用含 emoji 的假回應驗證索引#

官方定義 end 為不包含的結束位移。以下用一筆固定的回應樣本,測試應用程式如何處理 answers.error_codes.spans,也能在本機執行。

const state = "🧪錯誤 E42,請稍後再試"
const span = { start: 4, end: 7, text: "E42", score: 0.99 }
const points = Array.from(state)
function matchesSource({ start, end, text }) {
return Number.isInteger(start) && Number.isInteger(end)
&& start >= 0 && end > start && end <= points.length
&& points.slice(start, end).join("") === text
}
if (!matchesSource(span)) throw new Error("有效片段未通過")
if (matchesSource({ ...span, start: 5 })) throw new Error("錯誤位移通過")
console.log(state.slice(span.start, span.end)) // " E4"
console.log(points.slice(span.start, span.end).join("")) // "E42"

切出的文字和 text 一致,只能確認位移對得上。若要判斷抽取是否漏掉某個錯誤碼,另外準備「沒有錯誤碼」、「同一錯誤碼出現兩次」和「含 emoji」的人工標註樣本。把空陣列和請求失敗分開處理。若下游有分類門檻,可沿用 Gemini Decisions API 的路由驗收方法 設計固定樣本。

收到 400 時查模型、state 與 mode#

目前文件列出 pplx-decider-v1.3 和 pplx-decider-flash-v1.3 支援 locate。同一次請求可混用 noul、choice、score,但 state 必須是字串且總題數最多 128。含 locate 的請求若同時設定 mode: "web_grounding",會回傳 HTTP 400,param 為 questions。需要網路查證時另送查證請求,保留原文抽取結果與外部證據的來源。Decisions API 文件

參考資料:

Perplexity:Decisions API

Perplexity Decider locate 怎麼用?JavaScript 驗證原文片段與位移
https://laplusda.com/posts/perplexity-decider-locate-unicode-offsets/
作者
Zero
發佈於
2026-11-14
許可協議
CC BY-NC-SA 4.0