Home / Function/ prompt_length() — langchain Function Reference

prompt_length() — langchain Function Reference

Architecture documentation for the prompt_length() function in stuff.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  59bc2403_9bf0_8dbd_e707_e0536608da83["prompt_length()"]
  2d0480d1_fbf6_c0df_d443_92c8437d1fff["StuffDocumentsChain"]
  59bc2403_9bf0_8dbd_e707_e0536608da83 -->|defined in| 2d0480d1_fbf6_c0df_d443_92c8437d1fff
  e7742348_cc60_c503_d46b_e83cb5c9de9a["_get_inputs()"]
  59bc2403_9bf0_8dbd_e707_e0536608da83 -->|calls| e7742348_cc60_c503_d46b_e83cb5c9de9a
  style 59bc2403_9bf0_8dbd_e707_e0536608da83 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/chains/combine_documents/stuff.py lines 227–245

    def prompt_length(self, docs: list[Document], **kwargs: Any) -> int | None:
        """Return the prompt length given the documents passed in.

        This can be used by a caller to determine whether passing in a list
        of documents would exceed a certain prompt length. This useful when
        trying to ensure that the size of a prompt remains below a certain
        context limit.

        Args:
            docs: a list of documents to use to calculate the total prompt length.
            **kwargs: additional parameters to use to get inputs to LLMChain.

        Returns:
            Returns None if the method does not depend on the prompt length,
            otherwise the length of the prompt in tokens.
        """
        inputs = self._get_inputs(docs, **kwargs)
        prompt = self.llm_chain.prompt.format(**inputs)
        return self.llm_chain._get_num_tokens(prompt)  # noqa: SLF001

Subdomains

Frequently Asked Questions

What does prompt_length() do?
prompt_length() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/chains/combine_documents/stuff.py.
Where is prompt_length() defined?
prompt_length() is defined in libs/langchain/langchain_classic/chains/combine_documents/stuff.py at line 227.
What does prompt_length() call?
prompt_length() calls 1 function(s): _get_inputs.

Analyze Your Own Codebase

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

Try Supermodel Free