> For the complete documentation index, see [llms.txt](https://docs.cloud.deepset.ai/llms.txt)

# TransformersSimilarityRanker

Rank documents based on their semantic similarity to the query.

> **Removed in Haystack 3**
> 
> `TransformersSimilarityRanker` was removed in Haystack 3. Use [`SentenceTransformersSimilarityRanker`](https://docs.cloud.deepset.ai/docs/sentencetransformerssimilarityranker.md) instead. It provides the same ranking behavior and adds async support.

## Key Features

-   Uses a pre-trained cross-encoder model from Hugging Face to rank documents by semantic similarity to the query.
-   Configurable number of results returned via `top_k`.
-   Supports score scaling via Sigmoid activation for normalized similarity scores.
-   Supports calibration of probabilities via `calibration_factor`.
-   Supports filtering by score threshold.

## Configuration

1.  Drag the `TransformersSimilarityRanker` component onto the canvas from the Component Library.
2.  Click on the component to open the configuration panel.
3.  On the **General** tab:
    -   Set the ranking model. Pass a local path or the Hugging Face model name of a cross-encoder model.
    -   Set `top_k` to control how many documents to return.
4.  Go to the **Advanced** tab to configure `scale_score`, `calibration_factor`, `score_threshold`, and `batch_size`.

## Connections

`TransformersSimilarityRanker` accepts a query string and a list of documents as inputs. Connect it after a retriever or `DocumentJoiner` in a query pipeline.

It outputs a ranked list of documents sorted from most to least relevant to the query. Connect its `documents` output to `ChatPromptBuilder`, `AnswerBuilder`, or another downstream component.

## Source Code

To check this component's source code, open [`transformers_similarity.py`](https://github.com/deepset-ai/haystack/blob/main/haystack/components/rankers/transformers_similarity.py) in the [Haystack repository](https://github.com/deepset-ai/haystack).

## Usage Examples

### Basic Configuration

```yaml
  TransformersSimilarityRanker:
    type: haystack.components.rankers.transformers_similarity.TransformersSimilarityRanker
    init_parameters: {}
```

```yaml
components:
  TransformersSimilarityRanker:
    type: haystack.components.rankers.transformers_similarity.TransformersSimilarityRanker
    init_parameters:
```

## Parameters

### Inputs

| Parameter | Type | Default | Description |
| --- | --- | --- | --- |
| `query` | str |  | The input query to compare the documents to. |
| `documents` | List[Document] |  | A list of documents to be ranked. |
| `top_k` | Optional[int] | None | The maximum number of documents to return. |
| `scale_score` | Optional[bool] | None | If `True`, scales the raw logit predictions using a Sigmoid activation function. If `False`, disables scaling. |
| `calibration_factor` | Optional[float] | None | Use this factor to calibrate probabilities with `sigmoid(logits * calibration_factor)`. Used only if `scale_score` is `True`. |
| `score_threshold` | Optional[float] | None | Return documents only with a score above this threshold. |

### Outputs

| Parameter | Type | Default | Description |
| --- | --- | --- | --- |
| `documents` | List[Document] |  | A list of documents closest to the query, sorted from most similar to least similar. |

### Init Parameters

These are the parameters you can configure in Pipeline Builder:

| Parameter | Type | Default | Description |
| --- | --- | --- | --- |
| `model` | Union[str, Path] | cross-encoder/ms-marco-MiniLM-L-6-v2 | The ranking model. Pass a local path or the Hugging Face model name of a cross-encoder model. |
| `device` | Optional[ComponentDevice] | None | The device on which the model is loaded. If `None`, overrides the default device. |
| `token` | Optional[Secret] | Secret.from_env_var(['HF_API_TOKEN', 'HF_TOKEN'], strict=False) | The API token to download private models from Hugging Face. |
| `top_k` | int | 10 | The maximum number of documents to return per query. |
| `query_prefix` | str |  | A string to add at the beginning of the query text before ranking. Use it to prepend the text with an instruction, as required by reranking models like `bge`. |
| `document_prefix` | str |  | A string to add at the beginning of each document before ranking. |
| `meta_fields_to_embed` | Optional[List[str]] | None | List of metadata fields to embed with the document. |
| `embedding_separator` | str | \n | Separator to concatenate metadata fields to the document. |
| `scale_score` | bool | True | If `True`, scales the raw logit predictions using a Sigmoid activation function. |
| `calibration_factor` | Optional[float] | 1.0 | Use this factor to calibrate probabilities with `sigmoid(logits * calibration_factor)`. Used only if `scale_score` is `True`. |
| `score_threshold` | Optional[float] | None | Return documents with a score above this threshold only. |
| `model_kwargs` | Optional[Dict[str, Any]] | None | Additional keyword arguments for `AutoModelForSequenceClassification.from_pretrained` when loading the model. |
| `tokenizer_kwargs` | Optional[Dict[str, Any]] | None | Additional keyword arguments for `AutoTokenizer.from_pretrained` when loading the tokenizer. |
| `batch_size` | int | 16 | The batch size to use for inference. The higher the batch size, the more memory is required. |

### Run Method Parameters

These are the parameters you can configure for the component's `run()` method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see [Modify Pipeline Parameters at Query Time](https://docs.cloud.deepset.ai/docs/set-runtime-parameters-to-override-pipeline-configuration.md).

| Parameter | Type | Default | Description |
| --- | --- | --- | --- |
| `query` | str |  | The input query to compare the documents to. |
| `documents` | List[Document] |  | A list of documents to be ranked. |
| `top_k` | Optional[int] | None | The maximum number of documents to return. |
| `scale_score` | Optional[bool] | None | If `True`, scales the raw logit predictions using a Sigmoid activation function. If `False`, disables scaling. |
| `calibration_factor` | Optional[float] | None | Use this factor to calibrate probabilities with `sigmoid(logits * calibration_factor)`. Used only if `scale_score` is `True`. |
| `score_threshold` | Optional[float] | None | Return documents only with a score above this threshold. |
