Home / File/ reduce.py — langchain Source File

reduce.py — langchain Source File

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

File python CoreAbstractions RunnableInterface 7 imports 3 functions 3 classes

Entity Profile

Dependency Diagram

graph LR
  8315cc95_af6c_fb9a_2a29_0cd70480019e["reduce.py"]
  cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"]
  8315cc95_af6c_fb9a_2a29_0cd70480019e --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  8315cc95_af6c_fb9a_2a29_0cd70480019e --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  b19a8b7e_fbee_95b1_65b8_509a1ed3cad7["langchain_core._api"]
  8315cc95_af6c_fb9a_2a29_0cd70480019e --> b19a8b7e_fbee_95b1_65b8_509a1ed3cad7
  f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"]
  8315cc95_af6c_fb9a_2a29_0cd70480019e --> f3bc7443_c889_119d_0744_aacc3620d8d2
  c554676d_b731_47b2_a98f_c1c2d537c0aa["langchain_core.documents"]
  8315cc95_af6c_fb9a_2a29_0cd70480019e --> c554676d_b731_47b2_a98f_c1c2d537c0aa
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  8315cc95_af6c_fb9a_2a29_0cd70480019e --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  a0d4f6c3_2d96_364c_720a_e778ffd171a0["langchain_classic.chains.combine_documents.base"]
  8315cc95_af6c_fb9a_2a29_0cd70480019e --> a0d4f6c3_2d96_364c_720a_e778ffd171a0
  style 8315cc95_af6c_fb9a_2a29_0cd70480019e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Combine many documents together by recursively reducing them."""

from __future__ import annotations

from collections.abc import Callable
from typing import Any, Protocol

from langchain_core._api import deprecated
from langchain_core.callbacks import Callbacks
from langchain_core.documents import Document
from pydantic import ConfigDict

from langchain_classic.chains.combine_documents.base import BaseCombineDocumentsChain


class CombineDocsProtocol(Protocol):
    """Interface for the combine_docs method."""

    def __call__(self, docs: list[Document], **kwargs: Any) -> str:
        """Interface for the combine_docs method."""


class AsyncCombineDocsProtocol(Protocol):
    """Interface for the combine_docs method."""

    async def __call__(self, docs: list[Document], **kwargs: Any) -> str:
        """Async interface for the combine_docs method."""


def split_list_of_docs(
    docs: list[Document],
    length_func: Callable,
    token_max: int,
    **kwargs: Any,
) -> list[list[Document]]:
    """Split `Document` objects to subsets that each meet a cumulative len. constraint.

    Args:
        docs: The full list of `Document` objects.
        length_func: Function for computing the cumulative length of a set of `Document`
            objects.
        token_max: The maximum cumulative length of any subset of `Document` objects.
        **kwargs: Arbitrary additional keyword params to pass to each call of the
            `length_func`.

    Returns:
        A `list[list[Document]]`.
    """
    new_result_doc_list = []
    _sub_result_docs = []
    for doc in docs:
        _sub_result_docs.append(doc)
        _num_tokens = length_func(_sub_result_docs, **kwargs)
        if _num_tokens > token_max:
            if len(_sub_result_docs) == 1:
                msg = (
                    "A single document was longer than the context length,"
                    " we cannot handle this."
                )
                raise ValueError(msg)
// ... (330 more lines)

Subdomains

Dependencies

  • collections.abc
  • langchain_classic.chains.combine_documents.base
  • langchain_core._api
  • langchain_core.callbacks
  • langchain_core.documents
  • pydantic
  • typing

Frequently Asked Questions

What does reduce.py do?
reduce.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 reduce.py?
reduce.py defines 3 function(s): acollapse_docs, collapse_docs, split_list_of_docs.
What does reduce.py depend on?
reduce.py imports 7 module(s): collections.abc, langchain_classic.chains.combine_documents.base, langchain_core._api, langchain_core.callbacks, langchain_core.documents, pydantic, typing.
Where is reduce.py in the architecture?
reduce.py is located at libs/langchain/langchain_classic/chains/combine_documents/reduce.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/langchain/langchain_classic/chains/combine_documents).

Analyze Your Own Codebase

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

Try Supermodel Free