DocumentationAPI Reference📓 Tutorials🧑‍🍳 Cookbook🤝 Integrations💜 Discord🎨 Studio (Waitlist)
Documentation

TransformersSimilarityRanker

Use this component to rank Documents based on their similarity to the query. The TransformersSimilarityRanker is a powerful, model-based Ranker that uses a cross-encoder model to produce Document and query embeddings.

NameTransformersSimilarityRanker
Folder Path/rankers/
Position in a PipelineIn a query Pipeline, after a component that returns a list of Documents such as a Retriever.
Inputs“documents”: List of Document objects

”query”: A query string
Outputs“documents”: List of Document objects

Overview

TransformersSimilarityRanker ranks Documents based on how similar they are to the query. It uses a pre-trained cross-encoder model from the Hugging Face Hub to embed both the query and the Documents. It then compares the embeddings to determine how similar they are. The result is a list of Document objects in ranked order, with the Documents most similar to the query appearing first.

TransformersSimilarityRanker is most useful in query pipelines, such as a retrieval-augmented generation (RAG) pipeline or a document search pipeline, to ensure the retrieved Documents are ordered by relevance. You can use it after a Retriever (such as the InMemoryEmbeddingRetriever) to improve the search results. When using TransformersSimilarityRanker with a Retriever, consider setting the Retriever's top_k to a small number. This way the Ranker will have fewer Documents to process which can help make your pipeline faster.

By default, this component uses the cross-encoder/ms-marco-MiniLM-L-6-v2 model, but it's flexible. You can switch to a different model by adjusting the model parameter when initializing the Ranker. For details on different initialization settings, check out the API reference for this component.

Authorization

The component uses a HF_API_TOKEN environment variable by default. Otherwise, you can pass a Hugging Face API token at initialization with token – see code examples below.

ranker = TransformersSimilarityRanker(token=Secret.from_token("<your-api-key>"))

Usage

On its own

You can use TransformersSimilarityRanker outside of a pipeline to order Documents based on your query.

This example uses the TransformersSimilarityRanker to rank two simple Documents. To run the Ranker, pass a query, provide the Documents, and set the number of Documents to return in the top_k parameter.

from haystack import Document
from haystack.components.rankers import TransformersSimilarityRanker

docs = [Document(content="Paris"), Document(content="Berlin")]

ranker = TransformersSimilarityRanker()
ranker.warm_up()

ranker.run(query="City in France", documents=docs, top_k=1)

In a pipeline

TransformersSimilarityRanker is most efficient in query pipelines when used after a Retriever.

Below is an example of a pipeline that retrieves Documents from an InMemoryDocumentStore based on keyword search (using InMemoryBM25Retriever). It then uses the TransformersSimilarityRanker to rank the retrieved Documents according to their similarity to the query. The pipeline uses the default settings of the Ranker.

from haystack import Document, Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
from haystack.components.rankers import TransformersSimilarityRanker

docs = [Document(content="Paris is in France"), 
        Document(content="Berlin is in Germany"),
        Document(content="Lyon is in France")]
document_store = InMemoryDocumentStore()
document_store.write_documents(docs)

retriever = InMemoryBM25Retriever(document_store = document_store)
ranker = TransformersSimilarityRanker()
ranker.warm_up()

document_ranker_pipeline = Pipeline()
document_ranker_pipeline.add_component(instance=retriever, name="retriever")
document_ranker_pipeline.add_component(instance=ranker, name="ranker")

document_ranker_pipeline.connect("retriever.documents", "ranker.documents")

query = "Cities in France"
document_ranker_pipeline.run(data={"retriever": {"query": query, "top_k": 3}, 
                                   "ranker": {"query": query, "top_k": 2}})

📘

Ranker top_k

In the example above, the top_k values for the Retriever and the Ranker are different. The Retriever's top_k specifies how many Documents it returns. The Ranker then orders these Documents.

You can set the same or a smaller top_k value for the Ranker. The Ranker's top_k is the number of Documents it returns (if it's the last component in the pipeline) or forwards to the next component. In the pipeline example above, the Ranker is the last component, so the output you get when you run the pipeline are the top two Documents, as per the Ranker's top_k.

Adjusting the top_k values can help you optimize performance. In this case, a smaller top_k value of the Retriever means fewer Documents to process for the Ranker, which can speed up the pipeline.


Related Links

See the parameters details in our API reference: