Reranking is used as part of a two-stage vector retrieval process to improve the quality of results. You first query an index for a given number of relevant results, and then you send the query and results to a reranking model. The reranking model scores the results based on their semantic relevance to the query and returns a new, more accurate ranking. This approach is one the simplest methods for improving quality in retrieval augmented generation (RAG) pipelines.

This page shows you how to rerank search results using one of the reranking models hosted by Pinecone.

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

Choose a reranking model

Choose a reranking model hosted by Pinecone or an externally hosted reranking model.

Rerank results on the default field

To rerank search results, specify a supported reranking model, and provide documents and a query as well as other model-specific parameters. By default, Pinecone expects the documents to be in the documents.text field.

For example, the following request uses the bge-reranker-v2-m3 reranking 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.

from pinecone.grpc import PineconeGRPC as Pinecone

pc = Pinecone(api_key="YOUR_API_KEY")

result = pc.inference.rerank(
    model="bge-reranker-v2-m3",
    query="The tech company Apple is known for its innovative products like the iPhone.",
    documents=[
        {"id": "vec1", "text": "Apple is a popular fruit known for its sweetness and crisp texture."},
        {"id": "vec2", "text": "Many people enjoy eating apples as a healthy snack."},
        {"id": "vec3", "text": "Apple Inc. has revolutionized the tech industry with its sleek designs and user-friendly interfaces."},
        {"id": "vec4", "text": "An apple a day keeps the doctor away, as the saying goes."},
    ],
    top_n=4,
    return_documents=True,
    parameters={
        "truncate": "END"
    }
)

print(result)

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.

RerankResult(
  model='bge-reranker-v2-m3',
  data=[
    { index=2, score=0.48357219,
      document={id="vec3", text="Apple Inc. has re..."} },
    { index=0, score=0.048405956,
      document={id="vec1", text="Apple is a popula..."} },
    { index=3, score=0.007846239,
      document={id="vec4", text="An apple a day ke..."} },
    { index=1, score=0.0006563728,
      document={id="vec2", text="Many people enjoy..."} }
  ],
  usage={'rerank_units': 1}
)

Rerank results on a custom field

To rerank results on a field other than documents.text, provide the rank_fields parameter to specify the fields on which to rerank.

The bge-reranker-v2-m3 and pinecone-rerank-v0 models support only a single rerank field. cohere-rerank-3.5 supports multiple rerank fields, ranked based on the order of the fields specified.

For example, the following request reranks documents based on the values of the documents.my_field field:

from pinecone.grpc import PineconeGRPC as Pinecone

pc = Pinecone(api_key="YOUR_API_KEY")

result = pc.inference.rerank(
    model="bge-reranker-v2-m3",
    query="The tech company Apple is known for its innovative products like the iPhone.",
    documents=[
        {"id": "vec1", "my_field": "Apple is a popular fruit known for its sweetness and crisp texture."},
        {"id": "vec2", "my_field": "Many people enjoy eating apples as a healthy snack."},
        {"id": "vec3", "my_field": "Apple Inc. has revolutionized the tech industry with its sleek designs and user-friendly interfaces."},
        {"id": "vec4", "my_field": "An apple a day keeps the doctor away, as the saying goes."},
    ],
    rank_fields=["my_field"],
    top_n=4,
    return_documents=True,
    parameters={
        "truncate": "END"
    }
)