Open mauriciocirelli opened 1 month ago
Yes, the template should be formatted to inject the history
variable. The RunnableWithMessageHistory
class is designed to manage and inject message history into the chain input. The example provided in the documentation shows that the history
variable is intended to be used within the prompt template:
const prompt = ChatPromptTemplate.fromMessages([
["system", "You're an assistant who's good at {ability}"],
new MessagesPlaceholder("history"),
["human", "{question}"],
]);
Additionally, the test case in history_aware_retriever.test.ts
further confirms that the history
(or chat_history
) variable is expected to be injected into the prompt. The test demonstrates how the chat_history
is passed and utilized within the prompt template:
const questionGenPrompt = ChatPromptTemplate.fromTemplate(
`hi! {input} {chat_history}`
);
If the history
variable is not being injected, it could be due to a misconfiguration or a bug in the implementation. The documentation also provides a detailed example of how to set up and use the RunnableWithMessageHistory
class to ensure that message history is correctly managed and injected:
import { RunnableWithMessageHistory } from "@langchain/core/runnables";
const chainWithHistory = new RunnableWithMessageHistory({
runnable: chain,
getMessageHistory: (sessionId) =>
new UpstashRedisChatMessageHistory({
sessionId,
config: {
url: process.env.UPSTASH_REDIS_REST_URL!,
token: process.env.UPSTASH_REDIS_REST_TOKEN!,
},
}),
inputMessagesKey: "question",
historyMessagesKey: "history",
});
This setup ensures that the history
variable is properly injected into the prompt template, allowing the chain to utilize historical messages effectively [1][2][3].
To continue talking to Dosu, mention @dosu.
Dear,
I am using this exact example.
The
history
variable is not injected into the prompt, so I get the error.Is it a bug in docs? Should we format the template to inject the
history
variable?Thank you.
System Info
npm info langchain
langchain@0.2.18 | MIT | deps: 14 | versions: 291 Typescript bindings for langchain https://github.com/langchain-ai/langchainjs/tree/main/langchain/