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

# 创建重排序

> 对多个示例文档根据与查询的相关性进行重新排序

## 功能说明

重排序（Rerank）接口可以将一组文档按照与查询（Query）的相关性进行打分和排序。这通常作为 RAG（检索增强生成）流程的最后一步，用于进一步提高检索结果的准确性。

## 请求参数

| 名称          | 类型        | 必选 | 说明                          |
| :---------- | :-------- | :- | :-------------------------- |
| `model`     | `string`  | 是  | 模型名称，如 `bge-reranker-v2-m3` |
| `query`     | `string`  | 是  | 查询文本                        |
| `documents` | `array`   | 是  | 待排序的文档列表（字符串数组）             |
| `top_n`     | `integer` | 否  | 返回前几个结果                     |

## 调用示例

<CodeGroup>
  ```python Python theme={null}
  import requests

  url = "https://api.qhaigc.net/v1/rerank"
  headers = {
      "Content-Type": "application/json",
      "Authorization": "Bearer sk-your-api-key-here"
  }

  data = {
      "model": "bge-reranker-v2-m3",
      "query": "Organic skincare products for sensitive skin",
      "top_n": 3,
      "documents": [
          "Organic skincare for sensitive skin with aloe vera and chamomile...",
          "New makeup trends focus on bold colors and innovative techniques...",
          "Bio-Hautpflege für empfindliche Haut mit Aloe Vera und Kamille..."
      ]
  }

  response = requests.post(url, headers=headers, json=data)
  print(response.json())
  ```

  ```bash cURL theme={null}
  curl https://api.qhaigc.net/v1/rerank \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer sk-your-api-key-here" \
    -d '{
      "model": "bge-reranker-v2-m3",
      "query": "Organic skincare products for sensitive skin",
      "top_n": 3,
      "documents": [
          "Organic skincare for sensitive skin with aloe vera and chamomile...",
          "New makeup trends focus on bold colors and innovative techniques...",
          "Bio-Hautpflege für empfindliche Haut mit Aloe Vera und Kamille..."
      ]
    }'
  ```
</CodeGroup>

## 返回结果

返回每个文档的原始索引和相关性得分（分数越高越相关）。

<CodeGroup>
  ```json 响应示例 theme={null}
  {
    "results": [
      {
        "index": 0,
        "relevance_score": 0.985363245010376
      },
      {
        "index": 2,
        "relevance_score": 0.6773008704185486
      },
      {
        "index": 1,
        "relevance_score": 0.00001600799623702187
      }
    ],
    "usage": {
      "prompt_tokens": 77,
      "total_tokens": 77
    }
  }
  ```
</CodeGroup>

## 相关参考

* [模型列表](/docs/models#重排序模型)


## OpenAPI

````yaml POST /v1/rerank
openapi: 3.1.0
info:
  title: 启航 AI API
  version: 1.0.0
  description: 启航 AI API 文档，支持绘图、语音生成、视频生成、音乐生成等多种 AIGC 接口。
servers:
  - url: https://api.qhaigc.net
    description: 生产服务器
security:
  - bearerAuth: []
paths:
  /v1/rerank:
    post:
      tags:
        - 重排序模型
      summary: 创建重排序
      description: 对多个示例文档根据与查询的相关性进行重新排序
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                model:
                  type: string
                query:
                  type: string
                top_n:
                  type: integer
                documents:
                  type: array
                  items:
                    type: string
              required:
                - model
                - query
                - documents
      responses:
        '200':
          description: 成功响应
          content:
            application/json:
              schema:
                type: object
                properties:
                  results:
                    type: array
                    items:
                      type: object
                      properties:
                        index:
                          type: integer
                        relevance_score:
                          type: number
                  usage:
                    type: object
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer

````