*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to reorder retrieved results to mitigate the "lost in the middle" effect

How to reorder retrieved results to mitigate the "lost in the middle" effect
============================================================================

Substantial performance degradations in [RAG](/v0.2/docs/tutorials/rag/) applications have been [documented](https://arxiv.org/abs/2307.03172) as the number of retrieved documents grows (e.g., beyond ten). In brief: models are liable to miss relevant information in the middle of long contexts.

By contrast, queries against vector stores will typically return documents in descending order of relevance (e.g., as measured by cosine similarity of [embeddings](/v0.2/docs/concepts/#embedding-models)).

To mitigate the ["lost in the middle"](https://arxiv.org/abs/2307.03172) effect, you can re-order documents after retrieval such that the most relevant documents are positioned at extrema (e.g., the first and last pieces of context), and the least relevant documents are positioned in the middle. In some cases this can help surface the most relevant information to LLMs.

The [LongContextReorder](https://api.python.langchain.com/en/latest/document_transformers/langchain_community.document_transformers.long_context_reorder.LongContextReorder.html) document transformer implements this re-ordering procedure. Below we demonstrate an example.

    %pip install --upgrade --quiet  sentence-transformers langchain-chroma langchain langchain-openai langchain-huggingface > /dev/null

First we embed some artificial documents and index them in an (in-memory) [Chroma](/v0.2/docs/integrations/providers/chroma/) vector store. We will use [Hugging Face](/v0.2/docs/integrations/text_embedding/huggingfacehub/) embeddings, but any LangChain vector store or embeddings model will suffice.

    from langchain_chroma import Chromafrom langchain_huggingface import HuggingFaceEmbeddings# Get embeddings.embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")texts = [    "Basquetball is a great sport.",    "Fly me to the moon is one of my favourite songs.",    "The Celtics are my favourite team.",    "This is a document about the Boston Celtics",    "I simply love going to the movies",    "The Boston Celtics won the game by 20 points",    "This is just a random text.",    "Elden Ring is one of the best games in the last 15 years.",    "L. Kornet is one of the best Celtics players.",    "Larry Bird was an iconic NBA player.",]# Create a retrieverretriever = Chroma.from_texts(texts, embedding=embeddings).as_retriever(    search_kwargs={"k": 10})query = "What can you tell me about the Celtics?"# Get relevant documents ordered by relevance scoredocs = retriever.invoke(query)docs

**API Reference:**[HuggingFaceEmbeddings](https://api.python.langchain.com/en/latest/embeddings/langchain_huggingface.embeddings.huggingface.HuggingFaceEmbeddings.html)

    [Document(page_content='This is a document about the Boston Celtics'), Document(page_content='The Celtics are my favourite team.'), Document(page_content='L. Kornet is one of the best Celtics players.'), Document(page_content='The Boston Celtics won the game by 20 points'), Document(page_content='Larry Bird was an iconic NBA player.'), Document(page_content='Elden Ring is one of the best games in the last 15 years.'), Document(page_content='Basquetball is a great sport.'), Document(page_content='I simply love going to the movies'), Document(page_content='Fly me to the moon is one of my favourite songs.'), Document(page_content='This is just a random text.')]

Note that documents are returned in descending order of relevance to the query. The `LongContextReorder` document transformer will implement the re-ordering described above:

    from langchain_community.document_transformers import LongContextReorder# Reorder the documents:# Less relevant document will be at the middle of the list and more# relevant elements at beginning / end.reordering = LongContextReorder()reordered_docs = reordering.transform_documents(docs)# Confirm that the 4 relevant documents are at beginning and end.reordered_docs

**API Reference:**[LongContextReorder](https://api.python.langchain.com/en/latest/document_transformers/langchain_community.document_transformers.long_context_reorder.LongContextReorder.html)

    [Document(page_content='The Celtics are my favourite team.'), Document(page_content='The Boston Celtics won the game by 20 points'), Document(page_content='Elden Ring is one of the best games in the last 15 years.'), Document(page_content='I simply love going to the movies'), Document(page_content='This is just a random text.'), Document(page_content='Fly me to the moon is one of my favourite songs.'), Document(page_content='Basquetball is a great sport.'), Document(page_content='Larry Bird was an iconic NBA player.'), Document(page_content='L. Kornet is one of the best Celtics players.'), Document(page_content='This is a document about the Boston Celtics')]

Below, we show how to incorporate the re-ordered documents into a simple question-answering chain:

    from langchain.chains.combine_documents import create_stuff_documents_chainfrom langchain_core.prompts import PromptTemplatefrom langchain_openai import OpenAIllm = OpenAI()prompt_template = """Given these texts:-----{context}-----Please answer the following question:{query}"""prompt = PromptTemplate(    template=prompt_template,    input_variables=["context", "query"],)# Create and invoke the chain:chain = create_stuff_documents_chain(llm, prompt)response = chain.invoke({"context": reordered_docs, "query": query})print(response)

**API Reference:**[create\_stuff\_documents\_chain](https://api.python.langchain.com/en/latest/chains/langchain.chains.combine_documents.stuff.create_stuff_documents_chain.html) | [PromptTemplate](https://api.python.langchain.com/en/latest/prompts/langchain_core.prompts.prompt.PromptTemplate.html) | [OpenAI](https://api.python.langchain.com/en/latest/llms/langchain_openai.llms.base.OpenAI.html)

    The Celtics are a professional basketball team and one of the most iconic franchises in the NBA. They are highly regarded and have a large fan base. The team has had many successful seasons and is often considered one of the top teams in the league. They have a strong history and have produced many great players, such as Larry Bird and L. Kornet. The team is based in Boston and is often referred to as the Boston Celtics.

[Edit this page](https://github.com/langchain-ai/langchain/edit/master/docs/docs/how_to/long_context_reorder.ipynb)

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchain/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to get log probabilities

](/v0.2/docs/how_to/logprobs/)[

Next

How to split Markdown by Headers

](/v0.2/docs/how_to/markdown_header_metadata_splitter/)