langchain.prompts.few_shot.FewShotChatMessagePromptTemplate¶
class langchain.prompts.few_shot.FewShotChatMessagePromptTemplate[source]¶
Bases: 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:
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):
from langchain.prompts import (
    FewShotChatMessagePromptTemplate,
    ChatPromptTemplate
)
examples = [
    {"input": "2+2", "output": "4"},
    {"input": "2+3", "output": "5"},
]
example_prompt = ChatPromptTemplate.from_messages(
    [('human', '{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: