Cohere 重排序格式
POST v1/rerank重要提示:cohere 的 Rerank 模型接口和 Jina的Rerank模型接口格式 是一样的。
官方文档:https://docs.cohere.com/reference/rerank
简介:给定查询和文本列表,重排序API将根据与查询的相关性对文本进行排序。每个文本都会被分配一个相关性分数,从而产生一个有序的数组结果。此功能特别适用于搜索和检索应用,可以优化文档的排序,帮助用户更快找到相关信息
请求参数
Authorization
在 Header 添加参数 Authorization,其值为在 Bearer 之后拼接 Token。
Authorization: Bearer ******************Body 参数
Content-Type: application/json
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| model | string | 必需 | 要使用的模型 ID。有关哪些模型适用于 Chat API 的详细信息,请参阅模型端点兼容性表。 |
| query | string | 必需 | 查询文本内容。 |
| top_n | integer | 必需 | 返回最相关结果的数量。 |
| documents | array[string] | 必需 | 用于检索或分析的文档字符串数组。 |
| messages | array [object] | 必需 | 模型聊天记录。包含 role(可选:user/assistant/system)和 content(可选:文本内容)。 |
| stream | boolean | 必需 | 如果设置为 true,模型响应数据将在生成时通过服务器发送事件流式传输到客户端。 |
| store | boolean | 可选 | 是否存储此聊天补全请求的输出以用于模型蒸馏或评估产品。 |
| reasoning_effort | string | 可选 | 仅适用于 o系列 的模型。约束推理模型的推理工作。当前支持的值为 low、medium 和 high。减少推理工作可以加快响应速度并减少响应中用于推理的标记数。 |
| metadata | object | 可选 | 可以附加到对象的16个键值对集合。这对于以结构化格式存储对象的其他信息很有用。键是最大长度为64个字符的字符串。值是最大长度为512个字符的字符串。 |
| modalities | array[string] | 可选 | 您希望模型为此请求生成的输出类型。大多数模型都能生成文本,这是默认设置: ["text"]。该模型还可以用于生成音频。要请求此模型同时生成文本和音频响应,您可以使用: ["text", "audio"]。 |
| prediction | object | 可选 | 预测输出的配置,当提前知道模型响应的大部分内容时,可以大大提高响应时间。这在您只对文件进行微小更改时最常见。 |
| audio | object | 可选 | 音频输出的参数。当使用 modalities: ["audio"] 请求音频输出时需要。 |
| temperature | number | 可选 | 要使用的采样温度,介于 0 和 2 之间。较高的值(如0.8)会使输出更加随机,而较低的值(如0.2)会使其更加集中和确定性。我们通常建议更改此值或 top_p,但不要同时更改。 |
| top_p | number | 可选 | 一种替代采样温度的方法,称为核采样,其中模型考虑具有 top_p 概率质量的标记结果。因此,0.1 意味着只考虑包含前 10% 概率质量的标记。我们通常建议更改此值或 temperature,但不要同时更改。 |
| n | integer | 可选 | 为每个输入消息生成多少个聊天补全选择。请注意,您将根据所有选择生成的标记数量收费。保持 n 为 1 可最大限度地降低成本。 |
| stop | string | 可选 | 不支持最新的推理模型和 .o3、o4-mini。API 将停止生成更多标记的最多 4 个序列。返回的文本不会包含停止序列。 |
| max_completion_tokens | integer | 可选 | 补全中可以生成的标记数的上限,包括可见输出标记和推理标记。 |
| presence_penalty | number | 可选 | 介于 -2.0 和 2.0 之间的数字。正值根据新标记到目前为止在文本中出现的情况来惩罚它们,从而增加模型讨论新主题的可能性。 |
| frequency_penalty | number | 可选 | 介于 -2.0 和 2.0 之间的数字。正值根据新标记到目前为止在文本中的现有频率来惩罚它们,从而降低模型逐字重复同一行的可能性。 |
| logit_bias | string | 可选 | 修改指定标记出现在补全中的可能性。接受一个 JSON 对象,该对象将标记(由分词器中的标记 ID 指定)映射到从 -100 到 100 的关联偏差值。在数学上,偏差被添加到模型在采样之前生成的对数中。确切的效果会因模型而异,但介于 -1 和 1 之间的值应该会减少或增加选择的可能性;像 -100 或 100 这样的值应该导致相关标记被禁止或独占选择。 |
| logprobs | boolean | 可选 | 是否返回输出标记的对数概率。 |
| user | string | 可选 | 表示最终用户的唯一标识符,可以帮助 OpenAI 监控和检测滥用行为。 |
请求示例 (JSON)
{
"model": "jina-reranker-v2-base-multilingual",
"query": "What is the capital of the United States?",
"top_n": 3,
"documents": [
"Carson City is the capital city of the American state of Nevada.",
"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.",
"Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.",
"Capitalization or capitalisation in English grammar is the use of a capital letter at the start of a word. English usage varies from capitalization in other languages.",
"Capital punishment (the death penalty) has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states."
]
}返回响应
200 成功
Content-Type: application/json
响应参数
| 字段名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| id | string | 必需 | 请求唯一标识符。用于定位和追踪本次 API 调用的 ID。 |
| object | string | 必需 | 对象类型。对于聊天补全接口,该值固定为 "chat.completion"。 |
| created | integer | 必需 | 创建时间戳。生成本次响应的 Unix 时间戳(秒级)。 |
| model | string | 必需 | 实际使用的模型。生成此响应的具体模型名称及版本(例如 gpt-4o-2024-05-13)。 |
| choices | array [object] | 必需 | 回复选项数组。包含模型生成的回复内容列表(默认长度为1)。 |
| ├─ index | integer | 可选 | 当前选项在数组中的索引位置。 |
| ├─ message | object | 可选 | 生成的消息对象,包含以下核心字段: |
| ├─ role | string | 必需 | 角色身份,通常固定为 "assistant"。 |
| ├─ content | null/string | 必需 | 模型生成的文本内容。若模型进行了工具调用或纯音频输出,此处可能为 null。 |
| ├─ refusal | null/string | 必需 | 如果模型拒绝回答某些敏感或不合规的内容,拒绝的理由会在此字段显示。 |
| ├─ audio | object | 必需 | 音频输出对象。当请求了音频模态时,这里会包含生成的音频数据及相关参数。 |
| └─ annotations | array[string] | 必需 | 针对回复内容的附加注释或引用信息。 |
| ├─ logprobs | null | 可选 | 输出 token 的对数概率信息(需在请求中开启 logprobs: true)。 |
| └─ finish_reason | string | 可选 | 停止原因。模型停止生成的原因,常见值有: • stop:自然结束或遇到停止词 • length:达到最大 token 限制 • tool_calls:模型发起了工具/函数调用。 |
| usage | object | 必需 | Token 用量统计,包含以下明细: |
| ├─ prompt_tokens | integer | 必需 | 输入提示词(Prompt)消耗的 Token 数量。 |
| ├─ completion_tokens | integer | 必需 | 模型生成的回复(Completion)消耗的 Token 数量。 |
| ├─ total_tokens | integer | 必需 | 本次请求消耗的总 Token 数(输入 + 输出)。 |
响应示例
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}示例代码
python
import http.client
import json
conn = http.client.HTTPSConnection("nxaiapp.com")
payload = json.dumps({
"model": "jina-reranker-v2-base-multilingual",
"query": "What is the capital of the United States?",
"top_n": 3,
"documents": [
"Carson City is the capital city of the American state of Nevada.",
"The Commonwealth of the Northern Mariana Islands is a group of islands in the Pacific Ocean. Its capital is Saipan.",
"Washington, D.C. (also known as simply Washington or D.C., and officially as the District of Columbia) is the capital of the United States. It is a federal district.",
"Capitalization or capitalisation in English grammar is the use of a capital letter at the start of a word. English usage varies from capitalization in other languages.",
"Capital punishment (the death penalty) has existed in the United States since beforethe United States was a country. As of 2017, capital punishment is legal in 30 of the 50 states."
]
})
headers = {
'Authorization': 'Bearer <token>',
'Content-Type': 'application/json'
}
conn.request("POST", "/v1/rerank", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))