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

# POST /v1/rerank — 文档重排序

> 对文档列表按相关性重新排序，支持 Cohere 和 Jina 模型。

`/v1/rerank` 端点接收一个查询字符串和一组候选文档，按与查询的相关性排序后返回。重排序通常用于快速初始检索后的二次过滤——例如在 RAG 流程中进行向量相似度搜索后——以提升传递给语言模型的结果质量。

Anyone 的重排序端点同时兼容 Cohere Rerank API 格式和 Jina Rerank API 格式。

***

## POST /v1/rerank

### 请求体

<ParamField body="model" type="string" required>
  重排序模型，例如 `rerank-english-v3.0`（Cohere）或 `jina-reranker-v2-base-multilingual`（Jina）。可用模型取决于你配置的渠道。
</ParamField>

<ParamField body="query" type="string" required>
  用于对文档排序的搜索查询。
</ParamField>

<ParamField body="documents" type="string[] | object[]" required>
  要排序的文档列表。每个元素可以是字符串，或包含 `text` 字段的对象。
</ParamField>

<ParamField body="top_n" type="integer">
  返回的最高排名结果数。省略时返回所有文档，按相关性分数排序。
</ParamField>

<ParamField body="return_documents" type="boolean">
  设为 `true` 时，每个结果除索引和相关性分数外还包含原始文档文本。默认 `false`。
</ParamField>

<ParamField body="max_chunk_per_doc" type="integer">
  服务商内部拆分长文档时每个文档的最大分块数。
</ParamField>

<ParamField body="overlap_tokens" type="integer">
  服务商拆分长文档时分块之间的重叠 token 数。
</ParamField>

### 响应

<ResponseField name="results" type="object[]">
  排序后的文档结果列表，从最相关到最不相关。

  <Expandable title="result 对象属性">
    <ResponseField name="index" type="integer">
      该文档在原始 `documents` 数组中的零基索引。
    </ResponseField>

    <ResponseField name="relevance_score" type="number">
      `0.0` 到 `1.0` 之间的分数，表示文档与查询的相关程度。越高越相关。
    </ResponseField>

    <ResponseField name="document" type="object">
      原始文档，仅在 `return_documents` 为 `true` 时出现。

      <Expandable title="document 属性">
        <ResponseField name="text" type="string">
          文档文本。
        </ResponseField>
      </Expandable>
    </ResponseField>
  </Expandable>
</ResponseField>

<ResponseField name="usage" type="object">
  请求的 token 用量。

  <Expandable title="usage 属性">
    <ResponseField name="prompt_tokens" type="integer">
      查询和文档消耗的 token 数。
    </ResponseField>

    <ResponseField name="total_tokens" type="integer">
      处理的总 token 数。
    </ResponseField>
  </Expandable>
</ResponseField>

***

### 示例

<CodeGroup>
  ```bash curl theme={null}
  curl https://api.anyone.ai/v1/rerank \
    -H "Authorization: Bearer YOUR_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "rerank-english-v3.0",
      "query": "What is the capital of France?",
      "documents": [
        "Paris is the capital and largest city of France.",
        "The Eiffel Tower is located in Paris.",
        "Berlin is the capital of Germany.",
        "France has a population of over 67 million."
      ],
      "top_n": 2,
      "return_documents": true
    }'
  ```

  ```python python theme={null}
  import httpx

  response = httpx.post(
      "https://api.anyone.ai/v1/rerank",
      headers={"Authorization": "Bearer YOUR_TOKEN"},
      json={
          "model": "rerank-english-v3.0",
          "query": "What is the capital of France?",
          "documents": [
              "Paris is the capital and largest city of France.",
              "The Eiffel Tower is located in Paris.",
              "Berlin is the capital of Germany.",
              "France has a population of over 67 million.",
          ],
          "top_n": 2,
          "return_documents": True,
      },
  )

  for result in response.json()["results"]:
      print(result["index"], result["relevance_score"])
  ```
</CodeGroup>

### 响应示例

```json theme={null}
{
  "results": [
    {
      "index": 0,
      "relevance_score": 0.9921,
      "document": { "text": "Paris is the capital and largest city of France." }
    },
    {
      "index": 1,
      "relevance_score": 0.4103,
      "document": { "text": "The Eiffel Tower is located in Paris." }
    }
  ],
  "usage": {
    "prompt_tokens": 62,
    "total_tokens": 62
  }
}
```

***

## 支持的服务商

| 服务商        | 示例模型                                                            |
| ---------- | --------------------------------------------------------------- |
| **Cohere** | `rerank-english-v3.0`、`rerank-multilingual-v3.0`                |
| **Jina**   | `jina-reranker-v2-base-multilingual`、`jina-reranker-v1-base-en` |
