Skip to main content

SentenceTransformersDiversityRanker

A Diversity Ranker based on Sentence Transformers.

Basic Information

  • Type: haystack_integrations.rankers.sentence_transformers_diversity.SentenceTransformersDiversityRanker

Inputs

ParameterTypeDefaultDescription
querystrThe search query.
documentsList[Document]List of Document objects to be ranker.
top_kOptional[int]NoneOptional. An integer to override the top_k set during initialization.
lambda_thresholdOptional[float]NoneOverride the trade-off parameter between relevance and diversity. Only used when strategy is "maximum_margin_relevance".

Outputs

ParameterTypeDefaultDescription
documentsList[Document]A dictionary with the following key: - documents: List of Document objects that have been selected based on the diversity ranking.

Overview

Work in Progress

Bear with us while we're working on adding pipeline examples and most common components connections.

A Diversity Ranker based on Sentence Transformers.

Applies a document ranking algorithm based on one of the two strategies:

  1. Greedy Diversity Order:

    Implements a document ranking algorithm that orders documents in a way that maximizes the overall diversity of the documents based on their similarity to the query.

    It uses a pre-trained Sentence Transformers model to embed the query and the documents.

  2. Maximum Margin Relevance:

    Implements a document ranking algorithm that orders documents based on their Maximum Margin Relevance (MMR) scores.

    MMR scores are calculated for each document based on their relevance to the query and diversity from already selected documents. The algorithm iteratively selects documents based on their MMR scores, balancing between relevance to the query and diversity from already selected documents. The 'lambda_threshold' controls the trade-off between relevance and diversity.

Usage Example

components:
SentenceTransformersDiversityRanker:
type: haystack.components.rankers.sentence_transformers_diversity.SentenceTransformersDiversityRanker
init_parameters:

Parameters

Init Parameters

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
modelstrsentence-transformers/all-MiniLM-L6-v2Local path or name of the model in Hugging Face's model hub, such as 'sentence-transformers/all-MiniLM-L6-v2'.
top_kint10The maximum number of Documents to return per query.
deviceOptional[ComponentDevice]NoneThe device on which the model is loaded. If None, the default device is automatically selected.
tokenOptional[Secret]Secret.from_env_var(['HF_API_TOKEN', 'HF_TOKEN'], strict=False)The API token used to download private models from Hugging Face.
similarityUnion[str, DiversityRankingSimilarity]cosineSimilarity metric for comparing embeddings. Can be set to "dot_product" (default) or "cosine".
query_prefixstrA string to add to the beginning of the query text before ranking. Can be used to prepend the text with an instruction, as required by some embedding models, such as E5 and BGE.
query_suffixstrA string to add to the end of the query text before ranking.
document_prefixstrA string to add to the beginning of each Document text before ranking. Can be used to prepend the text with an instruction, as required by some embedding models, such as E5 and BGE.
document_suffixstrA string to add to the end of each Document text before ranking.
meta_fields_to_embedOptional[List[str]]NoneList of meta fields that should be embedded along with the Document content.
embedding_separatorstr\nSeparator used to concatenate the meta fields to the Document content.
strategyUnion[str, DiversityRankingStrategy]greedy_diversity_orderThe strategy to use for diversity ranking. Can be either "greedy_diversity_order" or "maximum_margin_relevance".
lambda_thresholdfloat0.5The trade-off parameter between relevance and diversity. Only used when strategy is "maximum_margin_relevance".
model_kwargsOptional[Dict[str, Any]]NoneAdditional keyword arguments for AutoModelForSequenceClassification.from_pretrained when loading the model. Refer to specific model documentation for available kwargs.
tokenizer_kwargsOptional[Dict[str, Any]]NoneAdditional keyword arguments for AutoTokenizer.from_pretrained when loading the tokenizer. Refer to specific model documentation for available kwargs.
config_kwargsOptional[Dict[str, Any]]NoneAdditional keyword arguments for AutoConfig.from_pretrained when loading the model configuration.
backendLiteral['torch', 'onnx', 'openvino']torchThe backend to use for the Sentence Transformers model. Choose from "torch", "onnx", or "openvino". Refer to the Sentence Transformers documentation for more information on acceleration and quantization options.

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.

ParameterTypeDefaultDescription
querystrThe search query.
documentsList[Document]List of Document objects to be ranker.
top_kOptional[int]NoneOptional. An integer to override the top_k set during initialization.
lambda_thresholdOptional[float]NoneOverride the trade-off parameter between relevance and diversity. Only used when strategy is "maximum_margin_relevance".