Skip to main content

LostInTheMiddleRanker

A LostInTheMiddle Ranker.

Basic Information

  • Type: haystack_integrations.rankers.lost_in_the_middle.LostInTheMiddleRanker

Inputs

ParameterTypeDefaultDescription
documentsList[Document]List of Documents to reorder.
top_kOptional[int]NoneThe maximum number of documents to return.
word_count_thresholdOptional[int]NoneThe maximum total number of words across all documents selected by the ranker.

Outputs

ParameterTypeDefaultDescription
documentsList[Document]A dictionary with the following keys: - documents: Reranked list of Documents

Overview

Work in Progress

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

A LostInTheMiddle Ranker.

Ranks documents based on the 'lost in the middle' order so that the most relevant documents are either at the beginning or end, while the least relevant are in the middle.

LostInTheMiddleRanker assumes that some prior component in the pipeline has already ranked documents by relevance and requires no query as input but only documents. It is typically used as the last component before building a prompt for an LLM to prepare the input context for the LLM.

Lost in the Middle ranking lays out document contents into LLM context so that the most relevant contents are at the beginning or end of the input context, while the least relevant is in the middle of the context. See the paper "Lost in the Middle: How Language Models Use Long Contexts" for more details.

Usage example:

from haystack.components.rankers import LostInTheMiddleRanker
from haystack import Document

ranker = LostInTheMiddleRanker()
docs = [Document(content="Paris"), Document(content="Berlin"), Document(content="Madrid")]
result = ranker.run(documents=docs)
for doc in result["documents"]:
print(doc.content)

Usage Example

components:
LostInTheMiddleRanker:
type: components.rankers.lost_in_the_middle.LostInTheMiddleRanker
init_parameters:

Parameters

Init Parameters

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
word_count_thresholdOptional[int]NoneThe maximum total number of words across all documents selected by the ranker.
top_kOptional[int]NoneThe maximum number of documents to return.

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
documentsList[Document]List of Documents to reorder.
top_kOptional[int]NoneThe maximum number of documents to return.
word_count_thresholdOptional[int]NoneThe maximum total number of words across all documents selected by the ranker.