Home / Function/ _reduce_tokens_below_limit() — langchain Function Reference

_reduce_tokens_below_limit() — langchain Function Reference

Architecture documentation for the _reduce_tokens_below_limit() function in vector_db.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  a733a44b_a858_f174_7563_58bb90e6fa35["_reduce_tokens_below_limit()"]
  5eb6dd31_a76d_429d_2244_999951beb530["VectorDBQAWithSourcesChain"]
  a733a44b_a858_f174_7563_58bb90e6fa35 -->|defined in| 5eb6dd31_a76d_429d_2244_999951beb530
  351a2d31_1d62_5bc7_cdde_9096743e5070["_get_docs()"]
  351a2d31_1d62_5bc7_cdde_9096743e5070 -->|calls| a733a44b_a858_f174_7563_58bb90e6fa35
  style a733a44b_a858_f174_7563_58bb90e6fa35 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/chains/qa_with_sources/vector_db.py lines 34–50

    def _reduce_tokens_below_limit(self, docs: list[Document]) -> list[Document]:
        num_docs = len(docs)

        if self.reduce_k_below_max_tokens and isinstance(
            self.combine_documents_chain,
            StuffDocumentsChain,
        ):
            tokens = [
                self.combine_documents_chain.llm_chain._get_num_tokens(doc.page_content)  # noqa: SLF001
                for doc in docs
            ]
            token_count = sum(tokens[:num_docs])
            while token_count > self.max_tokens_limit:
                num_docs -= 1
                token_count -= tokens[num_docs]

        return docs[:num_docs]

Subdomains

Called By

Frequently Asked Questions

What does _reduce_tokens_below_limit() do?
_reduce_tokens_below_limit() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/chains/qa_with_sources/vector_db.py.
Where is _reduce_tokens_below_limit() defined?
_reduce_tokens_below_limit() is defined in libs/langchain/langchain_classic/chains/qa_with_sources/vector_db.py at line 34.
What calls _reduce_tokens_below_limit()?
_reduce_tokens_below_limit() is called by 1 function(s): _get_docs.

Analyze Your Own Codebase

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

Try Supermodel Free