Home / File/ chain_extract.py — langchain Source File

chain_extract.py — langchain Source File

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

File python CoreAbstractions RunnableInterface 12 imports 2 functions 2 classes

Entity Profile

Dependency Diagram

graph LR
  0eb98ce3_194e_b222_d9bd_d9a220621a2c["chain_extract.py"]
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  0eb98ce3_194e_b222_d9bd_d9a220621a2c --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  0eb98ce3_194e_b222_d9bd_d9a220621a2c --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"]
  0eb98ce3_194e_b222_d9bd_d9a220621a2c --> f3bc7443_c889_119d_0744_aacc3620d8d2
  c554676d_b731_47b2_a98f_c1c2d537c0aa["langchain_core.documents"]
  0eb98ce3_194e_b222_d9bd_d9a220621a2c --> c554676d_b731_47b2_a98f_c1c2d537c0aa
  ba43b74d_3099_7e1c_aac3_cf594720469e["langchain_core.language_models"]
  0eb98ce3_194e_b222_d9bd_d9a220621a2c --> ba43b74d_3099_7e1c_aac3_cf594720469e
  83d7c7fd_1989_762c_9cf3_cecb50ada22b["langchain_core.output_parsers"]
  0eb98ce3_194e_b222_d9bd_d9a220621a2c --> 83d7c7fd_1989_762c_9cf3_cecb50ada22b
  e6b4f61e_7b98_6666_3641_26b069517d4a["langchain_core.prompts"]
  0eb98ce3_194e_b222_d9bd_d9a220621a2c --> e6b4f61e_7b98_6666_3641_26b069517d4a
  2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c["langchain_core.runnables"]
  0eb98ce3_194e_b222_d9bd_d9a220621a2c --> 2ceb1686_0f8c_8ae0_36d1_7c0b702fda1c
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  0eb98ce3_194e_b222_d9bd_d9a220621a2c --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"]
  0eb98ce3_194e_b222_d9bd_d9a220621a2c --> 91721f45_4909_e489_8c1f_084f8bd87145
  31974615_0d58_bd26_13f1_776e0a9d1413["langchain_classic.chains.llm"]
  0eb98ce3_194e_b222_d9bd_d9a220621a2c --> 31974615_0d58_bd26_13f1_776e0a9d1413
  87a55ce3_12da_afae_1442_586cf5a139e0["langchain_classic.retrievers.document_compressors.chain_extract_prompt"]
  0eb98ce3_194e_b222_d9bd_d9a220621a2c --> 87a55ce3_12da_afae_1442_586cf5a139e0
  style 0eb98ce3_194e_b222_d9bd_d9a220621a2c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""DocumentFilter that uses an LLM chain to extract the relevant parts of documents."""

from __future__ import annotations

from collections.abc import Callable, Sequence
from typing import Any, cast

from langchain_core.callbacks import Callbacks
from langchain_core.documents import BaseDocumentCompressor, Document
from langchain_core.language_models import BaseLanguageModel
from langchain_core.output_parsers import BaseOutputParser, StrOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import Runnable
from pydantic import ConfigDict
from typing_extensions import override

from langchain_classic.chains.llm import LLMChain
from langchain_classic.retrievers.document_compressors.chain_extract_prompt import (
    prompt_template,
)


def default_get_input(query: str, doc: Document) -> dict[str, Any]:
    """Return the compression chain input."""
    return {"question": query, "context": doc.page_content}


class NoOutputParser(BaseOutputParser[str]):
    """Parse outputs that could return a null string of some sort."""

    no_output_str: str = "NO_OUTPUT"

    @override
    def parse(self, text: str) -> str:
        cleaned_text = text.strip()
        if cleaned_text == self.no_output_str:
            return ""
        return cleaned_text


def _get_default_chain_prompt() -> PromptTemplate:
    output_parser = NoOutputParser()
    template = prompt_template.format(no_output_str=output_parser.no_output_str)
    return PromptTemplate(
        template=template,
        input_variables=["question", "context"],
        output_parser=output_parser,
    )


class LLMChainExtractor(BaseDocumentCompressor):
    """LLM Chain Extractor.

    Document compressor that uses an LLM chain to extract
    the relevant parts of documents.
    """

    llm_chain: Runnable
    """LLM wrapper to use for compressing documents."""

// ... (67 more lines)

Subdomains

Dependencies

  • collections.abc
  • langchain_classic.chains.llm
  • langchain_classic.retrievers.document_compressors.chain_extract_prompt
  • langchain_core.callbacks
  • langchain_core.documents
  • langchain_core.language_models
  • langchain_core.output_parsers
  • langchain_core.prompts
  • langchain_core.runnables
  • pydantic
  • typing
  • typing_extensions

Frequently Asked Questions

What does chain_extract.py do?
chain_extract.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 chain_extract.py?
chain_extract.py defines 2 function(s): _get_default_chain_prompt, default_get_input.
What does chain_extract.py depend on?
chain_extract.py imports 12 module(s): collections.abc, langchain_classic.chains.llm, langchain_classic.retrievers.document_compressors.chain_extract_prompt, langchain_core.callbacks, langchain_core.documents, langchain_core.language_models, langchain_core.output_parsers, langchain_core.prompts, and 4 more.
Where is chain_extract.py in the architecture?
chain_extract.py is located at libs/langchain/langchain_classic/retrievers/document_compressors/chain_extract.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