MessagesPlaceholder inside a chat prompt template. This approach ensures previous messages are included in the prompt so the model can respond with appropriate context.
Minimal example (no memory)
This example creates a ChatPromptTemplate without any history placeholder and then invokes the model twice. Because the previous exchange is not included in the prompt, the second invocation has no context about the first.Adding a MessagesPlaceholder to carry short-term memory
To give the model access to prior turns, add aMessagesPlaceholder to the chat prompt template and pass a history list on invocation. At runtime the placeholder will be replaced with the provided messages.
history list that represents the prior human/AI exchange, then invoke the chain while passing that history:
The
variable_name you assign to MessagesPlaceholder (for example, "history") is the key you must use when passing the list to invoke. The name can be anything, but the invocation dictionary key must match the placeholder’s variable_name.Quick reference
Summary and next steps
- Without explicit history included in the prompt, the model has no memory of prior invocations.
- Adding a
MessagesPlaceholderto your chat prompt template and providing ahistorylist at invocation time gives your application short-term conversational memory. - Use this technique for multi-step workflows, follow-up questions, or any conversational app that needs access to preceding messages.
Further reading
Experiment: try changing the contents ofhistory, the ability parameter, or the user input to observe how the model’s responses change when conversation history is included.