LostInTheMiddleRanker
Reorder documents so the most relevant ones appear at the beginning and end of the list, where LLMs tend to pay more attention.
Key Features
- Addresses the "lost in the middle" phenomenon where LLMs pay less attention to documents in the middle of a long context.
- Reorders documents so the most relevant ones appear at the beginning and end of the list.
- Works with any list of documents; no model inference required.
- Configurable number of documents to return via
top_k.
Configuration
- Drag the
LostInTheMiddleRankercomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- On the General tab:
- Set
top_kto control how many documents to return.
- Set
Connections
LostInTheMiddleRanker accepts a list of documents as input. Connect it after a retriever or DocumentJoiner in a query pipeline.
It outputs a reordered list of documents. Connect its documents output to ChatPromptBuilder or AnswerBuilder.
Source Code
To check this component's source code, open lost_in_the_middle.py in the Haystack repository.
Usage Examples
Basic Configuration
LostInTheMiddleRanker:
type: haystack.components.rankers.lost_in_the_middle.LostInTheMiddleRanker
init_parameters: {}
Using the Component in a Pipeline
This query pipeline reorders retrieved documents before sending them to an LLM:
# haystack-pipeline
components:
retriever:
type: haystack_integrations.components.retrievers.opensearch.bm25_retriever.OpenSearchBM25Retriever
init_parameters:
document_store:
type: haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore
init_parameters:
index: default
top_k: 20
ranker:
type: haystack.components.rankers.lost_in_the_middle.LostInTheMiddleRanker
init_parameters:
top_k: 10
llm:
type: haystack.components.generators.chat.llm.LLM
init_parameters:
chat_generator:
type: haystack.components.generators.chat.openai.OpenAIChatGenerator
init_parameters:
model: gpt-4o-mini
api_key:
type: env_var
env_vars:
- OPENAI_API_KEY
strict: false
user_prompt: |-
{% message role="user" %}
Answer based on the documents.
{% for doc in documents %}
{{ doc.content }}
{% endfor %}
Question: {{ query }}
{% endmessage %}
required_variables:
- query
- documents
connections:
- sender: retriever.documents
receiver: ranker.documents
- sender: ranker.documents
receiver: llm.documents
inputs:
query:
- retriever.query
- llm.query
outputs:
messages: llm.last_message
max_runs_per_component: 100
metadata: {}
Parameters
Inputs
| Parameter | Type | Default | Description |
|---|---|---|---|
documents | List[Document] | A list of documents to reorder. | |
top_k | Optional[int] | None | The maximum number of documents to return. |
Outputs
| Parameter | Type | Default | Description |
|---|---|---|---|
documents | List[Document] | Reordered list of documents with the most relevant ones at the beginning and end. |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
top_k | Optional[int] | None | The 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
documents | List[Document] | A list of documents to reorder. | |
top_k | Optional[int] | None | The maximum number of documents to return. |
Was this page helpful?