重排序
一、接口请求体(Body)参数详解
1. model(必需)
- 类型:枚举字符串
- 描述:指定使用的重排序模型名称。模型会定期更新、上下线或调整服务功能,变更会通过公告等方式通知。
- 可选值:
BAAI/bge-reranker-v2-m3
Pro/BAAI/bge-reranker-v2-m3
netease-youdao/bce-reranker-base_v1
- 示例值:
"BAAI/bge-reranker-v2-m3"
2. query(必需)
- 类型:字符串
- 描述:搜索查询文本,用于对文档进行重排序的依据。
- 示例值:
"Apple"
3. documents(必需)
- 类型:字符串列表
- 描述:需要重排序的文档列表,目前仅支持字符串列表,未来可能会支持文档对象。
- 示例值:
[
"apple",
"banana",
"fruit",
"vegetable"
]
4. top_k
- 类型:整数
- 描述:返回最相关文档或索引的数量。
- 示例值:
5
5. with_doc
- 类型:布尔值
- 描述:如果设置为
true,响应中将包含输入文档的文本;如果为 false,则不包含文档文本。
- 默认值:
false
- 示例值:
true
6. max_chunks_per_doc
- 类型:整数
- 描述:从单个文档中生成的最大片段数量。长文档会被分割成多个片段进行计算,取片段中的最高分数作为文档的分数。
- 示例值:
3
7. chunk_overlap
- 类型:整数
- 描述:当文档被分割成片段时,相邻片段之间的 token 重叠数量。要求
chunk_overlap <= 80。
- 示例值:
20
8. doc_content
- 类型:字符串
- 描述:原始文档内容。
- 示例值:
"This is the content of the document."
9. doc_index
- 类型:整数
- 描述:输入候选文档数组中的位置索引。
- 示例值:
0
二、使用示例
1. 基础重排序请求示例
import requests
import json
# 设置接口URL
url = "https://api.siliconflow.cn/v1/rerank"
# 设置请求头,包含认证信息和内容类型
headers = {
"Authorization": "Bearer YOUR_API_KEY", # 替换为你的API密钥
"Content-Type": "application/json" # 指定发送的数据格式为JSON
}
# 设置请求体
data = {
"model": "BAAI/bge-reranker-v2-m3", # 使用的模型
"query": "Apple", # 搜索文档壤据搜查
"documents": ["apple", "banana", "fruit", "vegetable"] # 待重排序的文档列表
}
# 发送POST请求
response = requests.post(url, headers=headers, json=data)
# 打印返回结果
print(response.json())
2. 完整重排序请求示例(包含所有参数)
import requests
import json
url = "https://api.siliconflow.cn/v1/rerank"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
# 完整请求体,包含所有参数
data = {
"model": "BAAI/bge-reranker-v2-m3",
"query": "Apple",
"documents": ["apple", "banana", "fruit", "vegetable"],
"top_k": 3,
"with_doc": True,
"max_chunks_per_doc": 2,
"chunk_overlap": 10,
"doc_content": "This is the content of the document.",
"doc_index": 0
}
response = requests.post(url, headers=headers, json=data)
print(response.json())