langchain.prompts.few_shot.FewShotChatMessagePromptTemplate¶
- class langchain.prompts.few_shot.FewShotChatMessagePromptTemplate(*, examples: Optional[List[dict]] = None, example_selector: Optional[BaseExampleSelector] = None, input_variables: List[str] = None, output_parser: Optional[BaseOutputParser] = None, partial_variables: Mapping[str, Union[str, Callable[[], str]]] = None, example_prompt: Union[BaseMessagePromptTemplate, BaseChatPromptTemplate])[source]¶
Bases:
BaseChatPromptTemplate,_FewShotPromptTemplateMixinChat 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:
from langchain.prompts import SemanticSimilarityExampleSelector from langchain.embeddings import OpenAIEmbeddings from langchain.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() vectorstore = Chroma.from_texts( to_vectorize, embeddings, metadatas=examples ) example_selector = SemanticSimilarityExampleSelector( vectorstore=vectorstore ) from langchain.schema import SystemMessage from langchain.prompts import HumanMessagePromptTemplate from langchain.prompts.few_shot import FewShotChatMessagePromptTemplate few_shot_prompt = FewShotChatMessagePromptTemplate( # Which variable(s) will be passed to the example selector. input_variables=["input"], example_selector=example_selector, # Define how each example will be formatted. # In this case, each example will become 2 messages: # 1 human, and 1 AI example_prompt=( HumanMessagePromptTemplate.from_template("{input}") + AIMessagePromptTemplate.from_template("{output}") ), ) # Define the overall prompt. final_prompt = ( SystemMessagePromptTemplate.from_template( "You are a helpful AI Assistant" ) + few_shot_prompt + HumanMessagePromptTemplate.from_template("{input}") ) # Show the prompt print(final_prompt.format_messages(input="What's 3+3?")) # Use within an LLM from langchain.chat_models import ChatAnthropic chain = final_prompt | ChatAnthropic() chain.invoke({"input": "What's 3+3?"})
Create a new model by parsing and validating input data from keyword arguments.
Raises ValidationError if the input data cannot be parsed to form a valid model.
- param example_prompt: Union[BaseMessagePromptTemplate, BaseChatPromptTemplate] [Required]¶
The class to format each example.
- param example_selector: Optional[BaseExampleSelector] = None¶
ExampleSelector to choose the examples to format into the prompt. Either this or examples should be provided.
- param examples: Optional[List[dict]] = None¶
Examples to format into the prompt. Either this or example_selector should be provided.
- param input_variables: List[str] [Optional]¶
A list of the names of the variables the prompt template will use to pass to the example_selector, if provided.
- param output_parser: Optional[BaseOutputParser] = None¶
How to parse the output of calling an LLM on this formatted prompt.
- param partial_variables: Mapping[str, Union[str, Callable[[], str]]] [Optional]¶
- validator check_examples_and_selector » all fields¶
Check that one and only one of examples/example_selector are provided.
- dict(**kwargs: Any) Dict¶
Return dictionary representation of prompt.
- format(**kwargs: Any) str[source]¶
Format the prompt with inputs generating a string.
Use this method to generate a string representation of a prompt consisting of chat messages.
Useful for feeding into a string based completion language model or debugging.
- Parameters
**kwargs – keyword arguments to use for formatting.
- Returns
A string representation of the prompt
- format_messages(**kwargs: Any) List[BaseMessage][source]¶
Format kwargs into a list of messages.
- Parameters
**kwargs – keyword arguments to use for filling in templates in messages.
- Returns
A list of formatted messages with all template variables filled in.
- format_prompt(**kwargs: Any) PromptValue¶
Format prompt. Should return a PromptValue. :param **kwargs: Keyword arguments to use for formatting.
- Returns
PromptValue.
- invoke(input: Dict, config: langchain.schema.runnable.RunnableConfig | None = None) PromptValue¶
- partial(**kwargs: Union[str, Callable[[], str]]) BasePromptTemplate¶
Return a partial of the prompt template.
- save(file_path: Union[Path, str]) None¶
Save the prompt.
- Parameters
file_path – Path to directory to save prompt to.
Example: .. code-block:: python
prompt.save(file_path=”path/prompt.yaml”)
- to_json() Union[SerializedConstructor, SerializedNotImplemented]¶
- to_json_not_implemented() SerializedNotImplemented¶
- validator validate_variable_names » all fields¶
Validate variable names do not include restricted names.
- property lc_attributes: Dict¶
Return a list of attribute names that should be included in the serialized kwargs. These attributes must be accepted by the constructor.
- property lc_namespace: List[str]¶
Return the namespace of the langchain object. eg. [“langchain”, “llms”, “openai”]
- property lc_secrets: Dict[str, str]¶
Return a map of constructor argument names to secret ids. eg. {“openai_api_key”: “OPENAI_API_KEY”}
- property lc_serializable: bool¶
Return whether the prompt template is lc_serializable.
- Returns
Boolean indicating whether the prompt template is lc_serializable.