Skip to main content

Rerank API and parameters

under construction

The English version of this document is under construction and will be available soon.

Rerank API 的核心功能是利用機器學習和自然語言處理技術,根據給定的模型對輸入的文本進行重新排序(rerank),模型會對每個候選答案進行評分,分數越高表示該答案與查詢的相關性越高。常用於資訊檢索、推薦系統和自然語言處理任務中,基於某種評分或評估標準,對初步排序的結果進行進一步優化,以提供更符合使用者期望的資訊。


使用情境

Rerank API 可以應用在各種檢索相關的使用情境中,例如:

  • 資訊檢索系統:對初步檢索出的結果進行重新排序,提升最相關結果的排名。
  • 問答系統:從多個潛在答案中選出最相關和正確的答案。
  • 推薦系統:根據用戶偏好對推薦結果進行重新排序,提供更個性化的推薦。
  • 文本匹配:在文本相似度計算中,對多個候選匹配結果進行排序,選擇最相似的文本。

使用範例

以下是一個使用 curl 指令調用 Rerank API 的範例,並對兩組查詢和答案進行重新排序。

info

提示:目前 Rerank API 的限制會受到以下數值影響

  • Rerank Model:sequence length = 8192

設定環境

export API_KEY={API_KEY}
export API_URL={API_URL}
export MODEL_NAME={MODEL_NAME}

使用 curl 指令取得 rerank 結果時,預設會回傳前三名的分數與索引 (index,索引編號從 0 開始)。如果需要調整回傳數量,可以在 parameters 中透過 top_n (optional, 默認值為 3)來設定。

輸入格式支援以下兩種方式,兩者的輸出結果沒有差異:

  1. 使用inputs參數: 提供查詢與候選答案兩兩成組的列表。
注意事項

使用 inputs 時,每個 pair 的第一項(查詢)需保持一致。

使用範例

   ​curl "${API_URL}/models/rerank" \
​ -H "X-API-KEY:${API_KEY}" \
​ -H "content-type: application/json" \
​ -d '{
​​​​ "model": "'${MODEL_NAME}'",
​​​​ "inputs": [
​​​​ [
​​​​ "Where is the capital of Canada?",
​​​​ "Europe is a continent."
​​​​ ],
​​​​ [
​​​​ "Where is the capital of Canada?",
​​​​ "The capital of Canada is Ottawa."
​​​​ ],
[
"Where is the capital of Canada?",
"Canada is a big country."
]
​​​​ ]
​​​​ }'

回傳範例

   {
"scores": [
0.000016571451851632446,
0.9998936653137207,
0.040769271552562714
],
"total_time_taken": "0.07 sec",
"usage": {
"prompt_tokens": 41,
"total_tokens": 41
}
}
  1. 使用 querydocuments 參數: 使用 query 指定查詢,並用 documents 提供所有候選答案,請參考下面範例。

使用範例

​   ​curl "${API_URL}/models/rerank" \
​ -H "X-API-KEY:${API_KEY}" \
​ -H "content-type: application/json" \
​ -d '{
​​​​ "model": "'${MODEL_NAME}'",
​​​​ "query": "Where is the capital of Canada?",
​​​​ "documents": [
​​​​ "Europe is a continent.",
​​​​ "The capital of Canada is Ottawa.",
"Canada is a big country."
​​​​ ],
​​​​ "parameters": {
​​​​ "top_n": 2
​​​​ }
​​​​ }'

回傳範例

​​​​​​​​{
​​​​ "results": [
​​​​ {
​​​​ "score": 0.9998936653137207,
​​​​ "index": 1
​​​​ },
​​​​ {
​​​​ "score": 0.040769271552562714,
​​​​ "index": 2
​​​​ }
​​​​ ],
​​​​ "total_time_taken": "0.07 sec",
​​​​ "usage": {
​​​​ "prompt_tokens": 53,
​​​​ "total_tokens": 53
​​​​ }
​​​​}

以上範例向 Rerank API 傳遞了一組查詢("Where is the capital of Canada?")和三組候選答案:

  1. index 0 : "Europe is a continent."
  2. index 1 : "The capital of Canada is Ottawa."
  3. index 2 : "Canada is a big country."

Rerank API 回傳了前兩名候選答案的相關性分數。其中,第二個答案的分數(0.9998936653137207)明顯高於第三個答案的分數(0.040769271552562714),這表明 "The capital of Canada is Ottawa." 更為相關且正確。

開發者可以根據這些分數對候選答案進行排序,以選擇最相關的結果。這種排序方法能有效提升資訊檢索和問答系統的準確性與有效性。