LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • MCP Adapters
    • Overview
    • Agents
    • Callbacks
    • Chains
    • Chat models
    • Embeddings
    • Evaluation
    • Globals
    • Hub
    • Memory
    • Output parsers
    • Retrievers
    • Runnables
    • LangSmith
    • Storage
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    MCP Adapters
    OverviewAgentsCallbacksChainsChat modelsEmbeddingsEvaluationGlobalsHubMemoryOutput parsersRetrieversRunnablesLangSmithStorage
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-classicretrieversdocument_compressorslistwise_rerankLLMListwiseRerank
    Classā—Since v1.0

    LLMListwiseRerank

    Copy
    LLMListwiseRerank()

    Bases

    BaseDocumentCompressor

    Attributes

    Methods

    Inherited fromBaseDocumentCompressor(langchain_core)

    Methods

    Macompress_documents
    View source on GitHub
    attribute
    reranker: Runnable[dict, list[Document]]

    LLM-based reranker to use for filtering documents. Expected to take in a dict with 'documents: Sequence[Document]' and 'query: str' keys and output a List[Document].

    attribute
    top_n: int

    Number of documents to return.

    attribute
    model_config
    method
    compress_documents

    Filter down documents based on their relevance to the query.

    method
    from_llm

    Create a LLMListwiseRerank document compressor from a language model.

    Document compressor that uses Zero-Shot Listwise Document Reranking.

    Adapted from: https://arxiv.org/pdf/2305.02156.pdf

    LLMListwiseRerank uses a language model to rerank a list of documents based on their relevance to a query.

    Note

    Requires that underlying model implement with_structured_output.

    Example usage:

    from langchain_classic.retrievers.document_compressors.listwise_rerank import (
        LLMListwiseRerank,
    )
    from langchain_core.documents import Document
    from langchain_openai import ChatOpenAI
    
    documents = [
        Document("Sally is my friend from school"),
        Document("Steve is my friend from home"),
        Document("I didn't always like yogurt"),
        Document("I wonder why it's called football"),
        Document("Where's waldo"),
    ]
    
    reranker = LLMListwiseRerank.from_llm(
        llm=ChatOpenAI(model="gpt-3.5-turbo"), top_n=3
    )
    compressed_docs = reranker.compress_documents(documents, "Who is steve")
    assert len(compressed_docs) == 3
    assert "Steve" in compressed_docs[0].page_content