Home / File/ listwise_rerank.py — langchain Source File

listwise_rerank.py — langchain Source File

Architecture documentation for listwise_rerank.py, a python file in the langchain codebase. 8 imports, 0 dependents.

File python CoreAbstractions RunnableInterface 8 imports 2 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  5187ecee_36c8_09f3_8b10_4e08cc8c0bf1["listwise_rerank.py"]
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  5187ecee_36c8_09f3_8b10_4e08cc8c0bf1 --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  5187ecee_36c8_09f3_8b10_4e08cc8c0bf1 --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"]
  5187ecee_36c8_09f3_8b10_4e08cc8c0bf1 --> f3bc7443_c889_119d_0744_aacc3620d8d2
  c554676d_b731_47b2_a98f_c1c2d537c0aa["langchain_core.documents"]
  5187ecee_36c8_09f3_8b10_4e08cc8c0bf1 --> c554676d_b731_47b2_a98f_c1c2d537c0aa
  ba43b74d_3099_7e1c_aac3_cf594720469e["langchain_core.language_models"]
  5187ecee_36c8_09f3_8b10_4e08cc8c0bf1 --> ba43b74d_3099_7e1c_aac3_cf594720469e
  e6b4f61e_7b98_6666_3641_26b069517d4a["langchain_core.prompts"]
  5187ecee_36c8_09f3_8b10_4e08cc8c0bf1 --> e6b4f61e_7b98_6666_3641_26b069517d4a
  2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c["langchain_core.runnables"]
  5187ecee_36c8_09f3_8b10_4e08cc8c0bf1 --> 2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  5187ecee_36c8_09f3_8b10_4e08cc8c0bf1 --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  style 5187ecee_36c8_09f3_8b10_4e08cc8c0bf1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Filter that uses an LLM to rerank documents listwise and select top-k."""

from collections.abc import Sequence
from typing import Any

from langchain_core.callbacks import Callbacks
from langchain_core.documents import BaseDocumentCompressor, Document
from langchain_core.language_models import BaseLanguageModel
from langchain_core.prompts import BasePromptTemplate, ChatPromptTemplate
from langchain_core.runnables import Runnable, RunnableLambda, RunnablePassthrough
from pydantic import BaseModel, ConfigDict, Field

_default_system_tmpl = """{context}

Sort the Documents by their relevance to the Query."""
_DEFAULT_PROMPT = ChatPromptTemplate.from_messages(
    [("system", _default_system_tmpl), ("human", "{query}")],
)


def _get_prompt_input(input_: dict) -> dict[str, Any]:
    """Return the compression chain input."""
    documents = input_["documents"]
    context = ""
    for index, doc in enumerate(documents):
        context += f"Document ID: {index}\n```{doc.page_content}```\n\n"
    document_range = "empty list"
    if len(documents) > 0:
        document_range = f"Document ID: 0, ..., Document ID: {len(documents) - 1}"
    context += f"Documents = [{document_range}]"
    return {"query": input_["query"], "context": context}


def _parse_ranking(results: dict) -> list[Document]:
    ranking = results["ranking"]
    docs = results["documents"]
    return [docs[i] for i in ranking.ranked_document_ids]


class LLMListwiseRerank(BaseDocumentCompressor):
    """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:
        ```python
        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"),
// ... (87 more lines)

Subdomains

Dependencies

  • collections.abc
  • langchain_core.callbacks
  • langchain_core.documents
  • langchain_core.language_models
  • langchain_core.prompts
  • langchain_core.runnables
  • pydantic
  • typing

Frequently Asked Questions

What does listwise_rerank.py do?
listwise_rerank.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in listwise_rerank.py?
listwise_rerank.py defines 2 function(s): _get_prompt_input, _parse_ranking.
What does listwise_rerank.py depend on?
listwise_rerank.py imports 8 module(s): collections.abc, langchain_core.callbacks, langchain_core.documents, langchain_core.language_models, langchain_core.prompts, langchain_core.runnables, pydantic, typing.
Where is listwise_rerank.py in the architecture?
listwise_rerank.py is located at libs/langchain/langchain_classic/retrievers/document_compressors/listwise_rerank.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/langchain/langchain_classic/retrievers/document_compressors).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free