Rerank documents
You can use the Inference API rerank
endpoint to rerank documents, such as text passages, according to their relevance to a query.
To run through this guide in your browser, see the Rerank example notebook.
Before you begin
Ensure you have the following:
1. Install an SDK
You can access the rerank
endpoint directly or use the latest Python, Node.js, Go, or Java SDK.
To install the latest SDK version, run the following command:
If you already have an SDK, upgrade to the latest version as follows:
2. Choose a model
Choose a reranking model hosted by Pinecone or an externally hosted reranking model.
3. Rerank documents
To rerank documents, use the inference.rerank
operation. Specify a supported reranking model, and provide documents and a query as well as other model-specific parameters.
For example, the following request uses the bge-reranker-v2-m3
model to rerank the values of the documents.text
field based on their relevance to the query, "The tech company Apple is known for its innovative products like the iPhone."
.
With truncate
set to "END"
, the input sequence (query
+ document
) is truncated at the token limit (1024
); to return an error instead, you’d set truncate
to "NONE"
or leave the parameter out.
The returned object contains documents with relevance scores:
Normalized between 0 and 1, the score
represents the relevance of a passage to the query, with scores closer to 1 indicating higher relevance.
Rerank documents on a custom field
To rerank documents on a field other than documents.text
, use the inference.rerank
endpoint, and provide the rank_fields
parameter to specify the fields on which to rerank.
For example, the following request reranks documents based on the values of the documents.my_field
field:
Was this page helpful?