Rerank results
Rerank results according to their relevance to a query.
For guidance and examples, see Rerank results.
POST /rerank
curl --request POST \
--url https://api.pinecone.io/rerank \
--header 'Api-Key: <api-key>' \
--header 'X-Pinecone-Api-Version: <x-pinecone-api-version>' \
--header 'Content-Type: application/json' \
--data '{
"model": "bge-reranker-v2-m3",
"query": "What is the capital of France?",
"top_n": 5,
"return_documents": true,
"rank_fields": [
"<string>"
],
"documents": [
{
"id": "1",
"text": "Paris is the capital of France.",
"title": "France",
"url": "https://example.com"
}
],
"parameters": {
"truncate": "END"
}
}'import requests
url = "https://api.pinecone.io/rerank"
payload = {
"model": "bge-reranker-v2-m3",
"query": "What is the capital of France?",
"top_n": 5,
"return_documents": True,
"rank_fields": [
"<string>"
],
"documents": [
{
"id": "1",
"text": "Paris is the capital of France.",
"title": "France",
"url": "https://example.com"
}
],
"parameters": {
"truncate": "END"
}
}
headers = {
"Api-Key": "<api-key>",
"X-Pinecone-Api-Version": "<x-pinecone-api-version>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {method: "POST", headers: {"Api-Key": "<api-key>", "X-Pinecone-Api-Version": "<x-pinecone-api-version>", "Content-Type": "application/json"}, body: JSON.stringify({
"model": "bge-reranker-v2-m3",
"query": "What is the capital of France?",
"top_n": 5,
"return_documents": true,
"rank_fields": [
"<string>"
],
"documents": [
{
"id": "1",
"text": "Paris is the capital of France.",
"title": "France",
"url": "https://example.com"
}
],
"parameters": {
"truncate": "END"
}
})};
fetch("https://api.pinecone.io/rerank", options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.pinecone.io/rerank",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"model\":\"bge-reranker-v2-m3\",\"query\":\"What is the capital of France?\",\"top_n\":5,\"return_documents\":true,\"rank_fields\":[\"<string>\"],\"documents\":[{\"id\":\"1\",\"text\":\"Paris is the capital of France.\",\"title\":\"France\",\"url\":\"https://example.com\"}],\"parameters\":{\"truncate\":\"END\"}}",
CURLOPT_HTTPHEADER => [
"Api-Key: <api-key>",
"X-Pinecone-Api-Version: <x-pinecone-api-version>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.pinecone.io/rerank"
payload := strings.NewReader("{\"model\":\"bge-reranker-v2-m3\",\"query\":\"What is the capital of France?\",\"top_n\":5,\"return_documents\":true,\"rank_fields\":[\"<string>\"],\"documents\":[{\"id\":\"1\",\"text\":\"Paris is the capital of France.\",\"title\":\"France\",\"url\":\"https://example.com\"}],\"parameters\":{\"truncate\":\"END\"}}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Api-Key", "<api-key>")
req.Header.Add("X-Pinecone-Api-Version", "<x-pinecone-api-version>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.pinecone.io/rerank")
.header("Api-Key", "<api-key>")
.header("X-Pinecone-Api-Version", "<x-pinecone-api-version>")
.header("Content-Type", "application/json")
.body("{\"model\":\"bge-reranker-v2-m3\",\"query\":\"What is the capital of France?\",\"top_n\":5,\"return_documents\":true,\"rank_fields\":[\"<string>\"],\"documents\":[{\"id\":\"1\",\"text\":\"Paris is the capital of France.\",\"title\":\"France\",\"url\":\"https://example.com\"}],\"parameters\":{\"truncate\":\"END\"}}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.pinecone.io/rerank")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Api-Key"] = '<api-key>'
request["X-Pinecone-Api-Version"] = '<x-pinecone-api-version>'
request["Content-Type"] = 'application/json'
request.body = "{\"model\":\"bge-reranker-v2-m3\",\"query\":\"What is the capital of France?\",\"top_n\":5,\"return_documents\":true,\"rank_fields\":[\"<string>\"],\"documents\":[{\"id\":\"1\",\"text\":\"Paris is the capital of France.\",\"title\":\"France\",\"url\":\"https://example.com\"}],\"parameters\":{\"truncate\":\"END\"}}"
response = http.request(request)
puts response.read_body{
"model": "bge-reranker-v2-m3",
"data": [
{
"index": 123,
"score": 0.5,
"document": {
"id": "1",
"text": "Paris is the capital of France.",
"title": "France",
"url": "https://example.com"
}
}
],
"usage": {
"rerank_units": 1
}
}{
"error": {
"code": "INVALID_ARGUMENT",
"message": "Bad request. The request body included invalid request parameters."
},
"status": 400
}{
"error": {
"code": "UNAUTHENTICATED",
"message": "Invalid API key."
},
"status": 401
}{
"error": {
"code": "UNKNOWN",
"message": "Internal server error"
},
"status": 500
}Authorizations
Section titled “Authorizations”Api-KeystringrequiredAn API Key is required to call Pinecone APIs. Get yours from the console.
Headers
Section titled “Headers”X-Pinecone-Api-VersionstringrequiredRequired date-based version header
Rerank documents for the given query
modelstringrequiredExample: bge-reranker-v2-m3
The model to use for reranking.
querystringrequiredThe query to rerank documents against.
Example: What is the capital of France?
top_n?integerThe number of results to return sorted by relevance. Defaults to the number of inputs.
Example: 5
return_documents?booleanWhether to return the documents in the response.
Example: true
rank_fields?string[]The field(s) to consider for reranking. If not provided, the default is ["text"]. The number of fields supported is model-specific.
documentsobject[]requiredThe documents to rerank.
parameters?objectAdditional model-specific parameters. Refer to the model guide for available model parameters.
Response
Section titled “Response”200 — OK
The result of a reranking request.
modelstringrequiredThe model used to rerank documents.
Example: bge-reranker-v2-m3
dataobject[]requiredThe reranked documents.
Show child attributes
indexintegerrequiredThe index position of the document from the original request.
scorenumberrequiredThe relevance of the document to the query, normalized between 0 and 1, with scores closer to 1 indicating higher relevance.
Example: 0.5
document?objectDocument for reranking
usageobjectrequiredUsage statistics for the model inference.
Show child attributes
rerank_units?integerThe number of rerank units consumed by this operation.
Required range: 0 <= x. Example: 1