Chat Message History
info
Head to Integrations for documentation on built-in chat message history integrations with 3rd-party databases and tools.
One of the core utility classes underpinning most (if not all) memory modules is the ChatMessageHistory
class. This is a wrapper that provides convenience methods for saving HumanMessage
s, AIMessage
s, and other chat messages and then fetching them.
You may want to use this class directly if you are managing memory outside of a chain.
Below is a basic example with an in-memory, ephemeral message store:
import { HumanMessage, AIMessage } from "@langchain/core/messages";
import { ChatMessageHistory } from "langchain/stores/message/in_memory";
const history = new ChatMessageHistory();
await history.addMessage(new HumanMessage("hi"));
await history.addMessage(new AIMessage("what is up?"));
console.log(await history.getMessages());
/*
[
HumanMessage {
content: 'hi',
additional_kwargs: {}
},
AIMessage {
content: 'what is up?',
additional_kwargs: {}
}
]
*/
API Reference:
- HumanMessage from
@langchain/core/messages
- AIMessage from
@langchain/core/messages
- ChatMessageHistory from
langchain/stores/message/in_memory
The added messages are saved in memory rather than saved externally as a session. For ways of persisting conversations, check out this Integrations section.