Home / Class/ FewShotChatMessagePromptTemplate Class — langchain Architecture

FewShotChatMessagePromptTemplate Class — langchain Architecture

Architecture documentation for the FewShotChatMessagePromptTemplate class in few_shot.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  bc8a83aa_4b35_16c3_4580_cce73bba5daf["FewShotChatMessagePromptTemplate"]
  f4b52015_4596_1060_b9b9_802f4b5cda73["BaseChatPromptTemplate"]
  bc8a83aa_4b35_16c3_4580_cce73bba5daf -->|extends| f4b52015_4596_1060_b9b9_802f4b5cda73
  d2f3c5fa_0eaf_bafd_40aa_1d4884ff518c["_FewShotPromptTemplateMixin"]
  bc8a83aa_4b35_16c3_4580_cce73bba5daf -->|extends| d2f3c5fa_0eaf_bafd_40aa_1d4884ff518c
  aa1f155e_f1a2_3609_0e63_d06bf3004d3f["few_shot.py"]
  bc8a83aa_4b35_16c3_4580_cce73bba5daf -->|defined in| aa1f155e_f1a2_3609_0e63_d06bf3004d3f
  a58f6787_80f9_44aa_35da_4e15d7905e11["is_lc_serializable()"]
  bc8a83aa_4b35_16c3_4580_cce73bba5daf -->|method| a58f6787_80f9_44aa_35da_4e15d7905e11
  2b1856e6_dc07_d7b0_467f_b61481c0d07a["format_messages()"]
  bc8a83aa_4b35_16c3_4580_cce73bba5daf -->|method| 2b1856e6_dc07_d7b0_467f_b61481c0d07a
  37a6e3a8_1d5e_5529_9b47_401d58b50428["aformat_messages()"]
  bc8a83aa_4b35_16c3_4580_cce73bba5daf -->|method| 37a6e3a8_1d5e_5529_9b47_401d58b50428
  d81a55c1_61d8_17d8_17fd_bded4d38cb51["format()"]
  bc8a83aa_4b35_16c3_4580_cce73bba5daf -->|method| d81a55c1_61d8_17d8_17fd_bded4d38cb51
  2ef7ae59_e8a5_8333_12ce_900bb3658156["aformat()"]
  bc8a83aa_4b35_16c3_4580_cce73bba5daf -->|method| 2ef7ae59_e8a5_8333_12ce_900bb3658156
  dee0cf5e_5c27_ec01_a35a_5404ba52f60f["pretty_repr()"]
  bc8a83aa_4b35_16c3_4580_cce73bba5daf -->|method| dee0cf5e_5c27_ec01_a35a_5404ba52f60f

Relationship Graph

Source Code

libs/core/langchain_core/prompts/few_shot.py lines 255–476

class FewShotChatMessagePromptTemplate(
    BaseChatPromptTemplate, _FewShotPromptTemplateMixin
):
    """Chat prompt template that supports few-shot examples.

    The high level structure of produced by this prompt template is a list of messages
    consisting of prefix message(s), example message(s), and suffix message(s).

    This structure enables creating a conversation with intermediate examples like:

    ```txt
    System: You are a helpful AI Assistant

    Human: What is 2+2?

    AI: 4

    Human: What is 2+3?

    AI: 5

    Human: What is 4+4?
    ```

    This prompt template can be used to generate a fixed list of examples or else to
    dynamically select examples based on the input.

    Examples:
        Prompt template with a fixed list of examples (matching the sample
        conversation above):

        ```python
        from langchain_core.prompts import (
            FewShotChatMessagePromptTemplate,
            ChatPromptTemplate,
        )

        examples = [
            {"input": "2+2", "output": "4"},
            {"input": "2+3", "output": "5"},
        ]

        example_prompt = ChatPromptTemplate.from_messages(
            [
                ("human", "What is {input}?"),
                ("ai", "{output}"),
            ]
        )

        few_shot_prompt = FewShotChatMessagePromptTemplate(
            examples=examples,
            # This is a prompt template used to format each individual example.
            example_prompt=example_prompt,
        )

        final_prompt = ChatPromptTemplate.from_messages(
            [
                ("system", "You are a helpful AI Assistant"),
                few_shot_prompt,
                ("human", "{input}"),
            ]
        )
        final_prompt.format(input="What is 4+4?")
        ```

        Prompt template with dynamically selected examples:

        ```python
        from langchain_core.prompts import SemanticSimilarityExampleSelector
        from langchain_core.embeddings import OpenAIEmbeddings
        from langchain_core.vectorstores import Chroma

        examples = [
            {"input": "2+2", "output": "4"},
            {"input": "2+3", "output": "5"},
            {"input": "2+4", "output": "6"},
            # ...
        ]

        to_vectorize = [" ".join(example.values()) for example in examples]
        embeddings = OpenAIEmbeddings()

Frequently Asked Questions

What is the FewShotChatMessagePromptTemplate class?
FewShotChatMessagePromptTemplate is a class in the langchain codebase, defined in libs/core/langchain_core/prompts/few_shot.py.
Where is FewShotChatMessagePromptTemplate defined?
FewShotChatMessagePromptTemplate is defined in libs/core/langchain_core/prompts/few_shot.py at line 255.
What does FewShotChatMessagePromptTemplate extend?
FewShotChatMessagePromptTemplate extends BaseChatPromptTemplate, _FewShotPromptTemplateMixin.

Analyze Your Own Codebase

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

Try Supermodel Free