> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mixroute.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 文件重排序

> 使用 Cohere Rerank v4.0 Fast 和 Pro 進行搜尋與 RAG 重排序。

根據查詢對候選文件排序，返回相關性分數和原始文件索引，不生成回答。

## 支援模型

| 模型 ID              | 說明                         |
| ------------------ | -------------------------- |
| `rerank-v4.0-pro`  | Cohere 注重排序質量的多語言相關性重排序模型。 |
| `rerank-v4.0-fast` | Cohere 注重響應速度的多語言相關性重排序模型。 |

## 請求欄位

| 欄位          | 說明                                     |
| ----------- | -------------------------------------- |
| `model`     | 必填：rerank-v4.0-fast 或 rerank-v4.0-pro。 |
| `query`     | 必填，查詢字串。                               |
| `documents` | 必填，文件字串陣列。                             |
| `top_n`     | 可選，返回結果數，不超過文件數。                       |

## 請求示例

```bash theme={null}
curl --request POST "https://api.mixroute.ai/v1/rerank" \
  --header "Authorization: Bearer $MIXROUTE_API_KEY" \
  --header "Content-Type: application/json" \
  --data '{
  "model": "rerank-v4.0-pro",
  "query": "API documentation",
  "documents": [
    "An API reference for developers.",
    "A recipe for soup."
  ],
  "top_n": 1
}'
```

## 響應

讀取 `results[].index` 和 `results[].relevance_score`。index 指向原始輸入陣列；不要假設響應一定包含 document 物件。分數用於排序，不是校準後的機率。

```python theme={null}
import os
import requests

documents = ["An API reference for developers.", "A recipe for soup."]
response = requests.post(
    "https://api.mixroute.ai/v1/rerank",
    headers={"Authorization": "Bearer " + os.environ["MIXROUTE_API_KEY"]},
    json={"model": "rerank-v4.0-pro", "query": "API documentation",
          "documents": documents, "top_n": 1},
    timeout=60,
)
response.raise_for_status()
for item in response.json()["results"]:
    print(item["relevance_score"], documents[item["index"]])
```
