multiple retrievers and then combines them.   
[ Long-Context Reorder ](/docs/modules/data_connection/retrievers/long_context_reorder) |  Any  |  No  |  If you are working with a long-context model and noticing that it's not paying attention to information in the middle of retrieved documents.  |  This fetches documents from an underlying retriever, and then reorders them so that the most similar are near the beginning and end. This is useful because it's been shown that for longer context models they sometimes don't pay attention to information in the middle of the context window.   
  
##  [ Third Party Integrations ](/docs/integrations/retrievers/) ​

LangChain also integrates with many third-party retrieval services. For a full
list of these, check out [ this list ](/docs/integrations/retrievers/) of all
integrations.

##  Using Retrievers in LCEL  ​

Since retrievers are ` Runnable ` 's, we can easily compose them with other `
Runnable ` objects:

    
    
    from langchain_openai import ChatOpenAI  
    from langchain_core.prompts import ChatPromptTemplate  
    from langchain_core.output_parsers import StrOutputParser  
    from langchain_core.runnables import RunnablePassthrough  
      
    template = """Answer the question based only on the following context:  
      
    {context}  
      
    Question: {question}  
    """  
    prompt = ChatPromptTemplate.from_template(template)  
    model = ChatOpenAI()  
      
      
    def format_docs(docs):  
        return "\n\n".join([d.page_content for d in docs])  
      
      
    chain = (  
        {"context": retriever | format_docs, "question": RunnablePassthrough()}  
        | prompt  
        | model  
        | StrOutputParser()  
    )  
      
    chain.invoke("What did the president say about technology?")  
      
    

##  Custom Retriever  ​

Since the retriever interface is so simple, it's pretty easy to write a custom
one.

    
    
    from langchain_core.retrievers import BaseRetriever  
    from langchain_core.callbacks import CallbackManagerForRetrieverRun  
    from langchain_core.documents import Document  
    from typing import List  
      
      
    class CustomRetriever(BaseRetriever):  
          
        def _get_relevant_documents(  
            self, query: str, *, run_manager: CallbackManagerForRetrieverRun  
        ) -> List[Document]:  
            return [Document(page_content=query)]  
      
    retriever = CustomRetriever()  
      
    retriever.get_relevant_documents("bar")  
    

* * *

####  Help us out by providing feedback on this documentation page:

[ Previous  Vector stores  ](/docs/modules/data_connection/vectorstores/) [
Next  Vector store-backed retriever
](/docs/modules/data_connection/retrievers/vectorstore)

  * Advanced Retrieval Types 
  * Third Party Integrations 
  * Using Retrievers in LCEL 
  * Custom Retriever 

Community

  * [ Discord  ](https://discord.gg/cU2adEyC7w)
  * [ Twitter  ](https://twitter.com/LangChainAI)

GitHub

  * [ Python  ](https://github.com/langchain-ai/langchain)
  * [ JS/TS  ](https://github.com/langchain-ai/langchainjs)

More

  * [ Homepage  ](https://langchain.com)
  * [ Blog  ](https://blog.langchain.dev)
  * [ YouTube  ](https://www.youtube.com/@LangChain)

Copyright © 2024 LangChain, Inc.