Home / Function/ format() — langchain Function Reference

format() — langchain Function Reference

Architecture documentation for the format() function in few_shot_with_templates.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  e11fdc59_f61b_01d0_db98_f83ce25c7a66["format()"]
  713e8d01_f24d_0ae6_d363_33f3f321e867["FewShotPromptWithTemplates"]
  e11fdc59_f61b_01d0_db98_f83ce25c7a66 -->|defined in| 713e8d01_f24d_0ae6_d363_33f3f321e867
  1be82c67_7cbe_640e_5a7c_6cbdd88cb446["aformat()"]
  1be82c67_7cbe_640e_5a7c_6cbdd88cb446 -->|calls| e11fdc59_f61b_01d0_db98_f83ce25c7a66
  cf752071_f46a_dd3b_3e62_5b429e8d1c30["_get_examples()"]
  e11fdc59_f61b_01d0_db98_f83ce25c7a66 -->|calls| cf752071_f46a_dd3b_3e62_5b429e8d1c30
  style e11fdc59_f61b_01d0_db98_f83ce25c7a66 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/prompts/few_shot_with_templates.py lines 122–167

    def format(self, **kwargs: Any) -> str:
        """Format the prompt with the inputs.

        Args:
            **kwargs: Any arguments to be passed to the prompt template.

        Returns:
            A formatted string.

        Example:
            ```python
            prompt.format(variable1="foo")
            ```
        """
        kwargs = self._merge_partial_and_user_variables(**kwargs)
        # Get the examples to use.
        examples = self._get_examples(**kwargs)
        # Format the examples.
        example_strings = [
            self.example_prompt.format(**example) for example in examples
        ]
        # Create the overall prefix.
        if self.prefix is None:
            prefix = ""
        else:
            prefix_kwargs = {
                k: v for k, v in kwargs.items() if k in self.prefix.input_variables
            }
            for k in prefix_kwargs:
                kwargs.pop(k)
            prefix = self.prefix.format(**prefix_kwargs)

        # Create the overall suffix
        suffix_kwargs = {
            k: v for k, v in kwargs.items() if k in self.suffix.input_variables
        }
        for k in suffix_kwargs:
            kwargs.pop(k)
        suffix = self.suffix.format(
            **suffix_kwargs,
        )

        pieces = [prefix, *example_strings, suffix]
        template = self.example_separator.join([piece for piece in pieces if piece])
        # Format the template with the input variables.
        return DEFAULT_FORMATTER_MAPPING[self.template_format](template, **kwargs)

Subdomains

Called By

Frequently Asked Questions

What does format() do?
format() is a function in the langchain codebase, defined in libs/core/langchain_core/prompts/few_shot_with_templates.py.
Where is format() defined?
format() is defined in libs/core/langchain_core/prompts/few_shot_with_templates.py at line 122.
What does format() call?
format() calls 1 function(s): _get_examples.
What calls format()?
format() is called by 1 function(s): aformat.

Analyze Your Own Codebase

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

Try Supermodel Free