跳转到主要内容
使用语义相似度模型对文档进行重排序。适用于优化搜索结果和 RAG 应用。

请求体

同步请求超时: 这个非聊天端点会等待路由到的模型完成处理。大输入、长音频或大批量请求可能超过常见的 30s 客户端默认超时,因此请将 HTTP 客户端超时设置为至少 120s
model
string
必填
要使用的重排序模型 ID(例如 qwen3-vl-rerank)。
query
string
必填
用于对文档进行排序的查询语句。最大长度:32,000 个字符。
documents
array
必填
需要重排序的文档列表(字符串数组)。限制:最多 1,000 篇文档;每篇最多 100,000 个字符;所有文档合计最多 2,000,000 个字符。
top_n
integer
返回的前几个结果数量。默认为所有文档。必须至少为 1,且不能大于 documents.length。如果供应商后续公开更严格的限制,TokenLab 会在这里文档化并执行。
return_documents
boolean
默认值:"false"
是否在响应中包含原始文档文本。

响应

results
array
带有评分的已排序文档列表。每个结果包含:
  • index (integer): 原始文档索引
  • relevance_score (number): 相关性评分 (0-1)
  • document (string): 原始文本(如果 return_documents=true
model
string
用于重排序的模型。
usage
object
Token 使用统计。
curl -X POST "https://api.tokenlab.sh/v1/rerank" \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen3-vl-rerank",
    "query": "What is machine learning?",
    "documents": [
      "Machine learning is a subset of AI",
      "The weather is nice today",
      "Deep learning uses neural networks"
    ],
    "top_n": 2,
    "return_documents": true
  }'
import requests

response = requests.post(
    "https://api.tokenlab.sh/v1/rerank",
    headers={"Authorization": "Bearer sk-your-api-key"},
    json={
        "model": "qwen3-vl-rerank",
        "query": "What is machine learning?",
        "documents": [
            "Machine learning is a subset of AI",
            "The weather is nice today",
            "Deep learning uses neural networks"
        ],
        "top_n": 2
    }
)

print(response.json())
const response = await fetch('https://api.tokenlab.sh/v1/rerank', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer sk-your-api-key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    model: 'qwen3-vl-rerank',
    query: 'What is machine learning?',
    documents: [
      'Machine learning is a subset of AI',
      'The weather is nice today',
      'Deep learning uses neural networks'
    ],
    top_n: 2
  })
});

const data = await response.json();
console.log(data.results);
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "net/http"
)

func main() {
    payload := map[string]interface{}{
        "model": "qwen3-vl-rerank",
        "query": "What is machine learning?",
        "documents": []string{
            "Machine learning is a subset of AI",
            "The weather is nice today",
            "Deep learning uses neural networks",
        },
        "top_n": 2,
    }
    body, _ := json.Marshal(payload)

    req, _ := http.NewRequest("POST", "https://api.tokenlab.sh/v1/rerank", bytes.NewBuffer(body))
    req.Header.Set("Authorization", "Bearer sk-your-api-key")
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, _ := client.Do(req)
    defer resp.Body.Close()

    var result map[string]interface{}
    json.NewDecoder(resp.Body).Decode(&result)
    fmt.Println(result["results"])
}
<?php
$ch = curl_init('https://api.tokenlab.sh/v1/rerank');

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'Authorization: Bearer sk-your-api-key'
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'model' => 'qwen3-vl-rerank',
        'query' => 'What is machine learning?',
        'documents' => [
            'Machine learning is a subset of AI',
            'The weather is nice today',
            'Deep learning uses neural networks'
        ],
        'top_n' => 2
    ])
]);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
print_r($data['results']);
{
  "results": [
    {
      "index": 0,
      "relevance_score": 0.95,
      "document": "Machine learning is a subset of AI"
    },
    {
      "index": 2,
      "relevance_score": 0.82,
      "document": "Deep learning uses neural networks"
    }
  ],
  "model": "qwen3-vl-rerank",
  "usage": {
    "prompt_tokens": 45,
    "total_tokens": 45
  }
}