-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
message_history.ts
62 lines (55 loc) · 1.53 KB
/
message_history.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import {
ChatPromptTemplate,
MessagesPlaceholder,
} from "@langchain/core/prompts";
import { ChatAnthropic } from "@langchain/anthropic";
import { RunnableWithMessageHistory } from "@langchain/core/runnables";
import { UpstashRedisChatMessageHistory } from "@langchain/community/stores/message/upstash_redis";
// For demos, you can also use an in-memory store:
// import { ChatMessageHistory } from "langchain/stores/message/in_memory";
const prompt = ChatPromptTemplate.fromMessages([
["system", "You're an assistant who's good at {ability}"],
new MessagesPlaceholder("history"),
["human", "{question}"],
]);
const chain = prompt.pipe(
new ChatAnthropic({ model: "claude-3-sonnet-20240229" })
);
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",
});
const result = await chainWithHistory.invoke(
{
ability: "math",
question: "What does cosine mean?",
},
{
configurable: {
sessionId: "foobarbaz",
},
}
);
console.log(result);
const result2 = await chainWithHistory.invoke(
{
ability: "math",
question: "What's its inverse?",
},
{
configurable: {
sessionId: "foobarbaz",
},
}
);
console.log(result2);