class FileSystemChatMessageHistoryconst model = new ChatOpenAI({
model: "gpt-3.5-turbo",
temperature: 0,
});
const prompt = ChatPromptTemplate.fromMessages([
[
"system",
"You are a helpful assistant. Answer all questions to the best of your ability.",
],
["placeholder", "chat_history"],
["human", "{input}"],
]);
const chain = prompt.pipe(model).pipe(new StringOutputParser());
const chainWithHistory = new RunnableWithMessageHistory({
runnable: chain,
inputMessagesKey: "input",
historyMessagesKey: "chat_history",
getMessageHistory: async (sessionId) => {
const chatHistory = new FileSystemChatMessageHistory({
sessionId: sessionId,
userId: "userId", // Optional
})
return chatHistory;
},
});
await chainWithHistory.invoke(
{ input: "What did I just say my name was?" },
{ configurable: { sessionId: "session-id" } }
);Store chat message history using a local JSON file. For demo and development purposes only.