[langchainjs][Docs][Tutorial] Build Error Managing Conversation History Code #6246
-
Checked other resources
Commit to Help
Example Codeimport { InMemoryChatMessageHistory } from "@langchain/core/chat_history";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { AzureChatOpenAI } from "@langchain/openai";
import { RunnableWithMessageHistory } from "@langchain/core/runnables";
import { StringOutputParser } from "@langchain/core/output_parsers";
import type { BaseMessage } from "@langchain/core/messages";
import {
RunnablePassthrough,
RunnableSequence,
} from "@langchain/core/runnables";
async function main() {
const model = new AzureChatOpenAI({
azureOpenAIApiKey: "<YOUR KEY>",
azureOpenAIApiInstanceName: "<YOUR INSTANCE NAME>",
azureOpenAIApiDeploymentName: "<YOUR DEPLOYMENT>",
azureOpenAIApiVersion: "<YOUR API VERSION>",
});
const messageHistories: Record<string, InMemoryChatMessageHistory> = {};
const prompt = ChatPromptTemplate.fromMessages([
[
"system",
`You are a helpful assistant who remembers all details the user shares with you.`,
],
["placeholder", "{chat_history}"],
["human", "{input}"],
]);
const parser = new StringOutputParser();
const filterMessages = ({ chat_history }: { chat_history: BaseMessage[] }) => {
return chat_history.slice(-10);
};
const chain2 = RunnableSequence.from([
RunnablePassthrough.assign({
chat_history: filterMessages, // <---- ERRORS OUT HERE
}),
prompt,
model,
])
const chain = prompt.pipe(model).pipe(parser);
const withMessageHistory = new RunnableWithMessageHistory({
runnable: chain,
getMessageHistory: async (sessionId) => {
if (messageHistories[sessionId] === undefined) {
const messageHistory = new InMemoryChatMessageHistory();
messageHistories[sessionId] = messageHistory;
}
return messageHistories[sessionId];
},
inputMessagesKey: "input",
historyMessagesKey: "chat_history",
});
const config = {
configurable: {
sessionId: "abc2",
},
};
const response = await withMessageHistory.invoke(
{
input: "Hello!!! I am Bob.",
},
config
);
console.log(response);
const followUpResponse = await withMessageHistory.invoke(
{
input: "What is my name?",
},
config
);
console.log(followUpResponse);
}
main(); Description
Note: In the above code, I have used System Info
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The error you're encountering is due to a type mismatch in the Here's the corrected code: import { InMemoryChatMessageHistory } from "@langchain/core/chat_history";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { AzureChatOpenAI } from "@langchain/openai";
import { RunnableWithMessageHistory } from "@langchain/core/runnables";
import { StringOutputParser } from "@langchain/core/output_parsers";
import type { BaseMessage } from "@langchain/core/messages";
import {
RunnablePassthrough,
RunnableSequence,
} from "@langchain/core/runnables";
async function main() {
const model = new AzureChatOpenAI({
azureOpenAIApiKey: "<YOUR KEY>",
azureOpenAIApiInstanceName: "<YOUR INSTANCE NAME>",
azureOpenAIApiDeploymentName: "<YOUR DEPLOYMENT>",
azureOpenAIApiVersion: "<YOUR API VERSION>",
});
const messageHistories: Record<string, InMemoryChatMessageHistory> = {};
const prompt = ChatPromptTemplate.fromMessages([
[
"system",
`You are a helpful assistant who remembers all details the user shares with you.`,
],
["placeholder", "{chat_history}"],
["human", "{input}"],
]);
const parser = new StringOutputParser();
const filterMessages = (input: Record<string, unknown>) => {
const { chat_history } = input as { chat_history: BaseMessage[] };
return chat_history.slice(-10);
};
const chain2 = RunnableSequence.from([
RunnablePassthrough.assign({
chat_history: filterMessages,
}),
prompt,
model,
]);
const chain = prompt.pipe(model).pipe(parser);
const withMessageHistory = new RunnableWithMessageHistory({
runnable: chain,
getMessageHistory: async (sessionId) => {
if (messageHistories[sessionId] === undefined) {
const messageHistory = new InMemoryChatMessageHistory();
messageHistories[sessionId] = messageHistory;
}
return messageHistories[sessionId];
},
inputMessagesKey: "input",
historyMessagesKey: "chat_history",
});
const config = {
configurable: {
sessionId: "abc2",
},
};
const response = await withMessageHistory.invoke(
{
input: "Hello!!! I am Bob.",
},
config
);
console.log(response);
const followUpResponse = await withMessageHistory.invoke(
{
input: "What is my name?",
},
config
);
console.log(followUpResponse);
}
main(); In this corrected code, the |
Beta Was this translation helpful? Give feedback.
The error you're encountering is due to a type mismatch in the
RunnablePassthrough.assign
method. ThefilterMessages
function's input type does not match the expected type forRunnablePassthrough.assign
. To resolve this, you need to ensure that the input type forfilterMessages
matches the expected structure.Here's the corrected code: