You can use the Inference API rerank endpoint to rerank documents, such as text passages, according to their relevance to a query.

This feature is in public preview.

To run through this guide in your browser, see the Rerank example notebook.

Before you begin

Ensure you have the following:

The rerank endpoint is only available to organizations on the Standard or Enterprise plans.

The Inference API is a stand-alone service. You can store your embeddings in a Pinecone vector database, but you are not required to do so.

1. Install the Python package

To use the rerank endpoint via the Python SDK, upgrade the SDK and install the pinecone-plugin-inference package as follows:

pip install --upgrade pinecone 
pip install --upgrade --pre pinecone-plugin-inference==1.1.0.*

2. Choose a model

The following reranking models are available:

ModelMax query tokensMax query + doc tokensMax documents
bge-reranker-v2-m32561024100

3. Rerank documents

To rerank documents, use the inference.rerank operation. Specify a supported reranking model and provide the query and the documents to rerank. The rerank endpoint returns your documents with associated relevance scores that represent the relevance of each document to the provided query.

The following example reranks text documents by the relevance to the query “The tech company Apple is known for its innovative products like the iPhone.” using the bge-reranker-v2-m3 model. When no rank_fields parameter is present, the endpoint reranks the documents based on the values of the text field.

The example above returns the following object containing documents with relevance scores.

{
  "data":[
    {
      "index":2,
      "document":{
        "id":"vec3",
        "text":"Apple Inc. has revolutionized the tech industry with its sleek designs and user-friendly interfaces."
      },
      "score":0.47654688
    },
    {
      "index":0,
      "document":{
        "id":"vec1",
        "text":"Apple is a popular fruit known for its sweetness and crisp texture."
      },
      "score":0.047963805
    },
    {
      "index":3,
      "document":{
        "id":"vec4",
        "text":"An apple a day keeps the doctor away, as the saying goes."
      },
      "score":0.007587992
    },
    {
      "index":1,
      "document":{
        "id":"vec2",
        "text":"Many people enjoy eating apples as a healthy snack."
      },
      "score":0.0006491712
    }
  ],
  "usage":{
    "rerank_units":1
  }
}

Rerank documents on a custom field

To rerank documents on a field other than text, use the inference.rerank operation, providing the rank_fields parameter to specify the fields on which to rerank.

The following example reranks text documents by the relevance to the query. Because rank_fields parameter is present, the endpoint reranks the documents based on the values of the my_field field.

The example above returns the following object containing documents with relevance scores.

{
  "model": "bge-reranker-v2-m3",
  "data":[
    {
      "index":2,
      "document":{
        "id":"vec3",
        "my_field":"Apple Inc. has revolutionized the tech industry with its sleek designs and user-friendly interfaces."
      },
      "score":0.47654688
    },
    {
      "index":0,
      "document":{
        "id":"vec1",
        "my_field":"Apple is a popular fruit known for its sweetness and crisp texture."
      },
      "score":0.047963805
    },
    {
      "index":3,
      "document":{
        "id":"vec4",
        "my_field":"An apple a day keeps the doctor away, as the saying goes."
      },
      "score":0.007587992
    },
    {
      "index":1,
      "document":{
        "id":"vec2",
        "my_field":"Many people enjoy eating apples as a healthy snack."
      },
      "score":0.0006491712
    }
  ],
  "usage":{
    "rerank_units":1
  }
}