MessagesPlaceholder#
- class langchain_core.prompts.chat.MessagesPlaceholder[source]#
Bases:
BaseMessagePromptTemplate
Prompt template that assumes variable is already list of messages.
A placeholder which can be used to pass in a list of messages.
Direct usage:
from langchain_core.prompts import MessagesPlaceholder prompt = MessagesPlaceholder("history") prompt.format_messages() # raises KeyError prompt = MessagesPlaceholder("history", optional=True) prompt.format_messages() # returns empty list [] prompt.format_messages( history=[ ("system", "You are an AI assistant."), ("human", "Hello!"), ] ) # -> [ # SystemMessage(content="You are an AI assistant."), # HumanMessage(content="Hello!"), # ]
Building a prompt with chat history:
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder prompt = ChatPromptTemplate.from_messages( [ ("system", "You are a helpful assistant."), MessagesPlaceholder("history"), ("human", "{question}") ] ) prompt.invoke( { "history": [("human", "what's 5 + 2"), ("ai", "5 + 2 is 7")], "question": "now multiply that by 4" } ) # -> ChatPromptValue(messages=[ # SystemMessage(content="You are a helpful assistant."), # HumanMessage(content="what's 5 + 2"), # AIMessage(content="5 + 2 is 7"), # HumanMessage(content="now multiply that by 4"), # ])
Limiting the number of messages:
from langchain_core.prompts import MessagesPlaceholder prompt = MessagesPlaceholder("history", n_messages=1) prompt.format_messages( history=[ ("system", "You are an AI assistant."), ("human", "Hello!"), ] ) # -> [ # HumanMessage(content="Hello!"), # ]
- param n_messages: PositiveInt | None = None#
Maximum number of messages to include. If None, then will include all. Defaults to None.
- param optional: bool = False#
If True format_messages can be called with no arguments and will return an empty list. If False then a named argument with name variable_name must be passed in, even if the value is an empty list.
- param variable_name: str [Required]#
Name of variable to use as messages.
- async aformat_messages(**kwargs: Any) list[BaseMessage] #
Async format messages from kwargs. Should return a list of BaseMessages.
- Parameters:
**kwargs (Any) – Keyword arguments to use for formatting.
- Returns:
List of BaseMessages.
- Return type:
list[BaseMessage]
- format_messages(**kwargs: Any) list[BaseMessage] [source]#
Format messages from kwargs.
- Parameters:
**kwargs (Any) – Keyword arguments to use for formatting.
- Returns:
List of BaseMessage.
- Raises:
ValueError – If variable is not a list of messages.
- Return type:
list[BaseMessage]
- pretty_print() None #
Print a human-readable representation.
- Return type:
None
- pretty_repr(html: bool = False) str [source]#
Human-readable representation.
- Parameters:
html (bool) – Whether to format as HTML. Defaults to False.
- Returns:
Human-readable representation.
- Return type:
str
- property input_variables: list[str]#
Input variables for this prompt template.
- Returns:
List of input variable names.
Examples using MessagesPlaceholder