-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlangchain-history.js
48 lines (40 loc) · 1.35 KB
/
langchain-history.js
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
import { ChatOllama } from "@langchain/community/chat_models/ollama";
import { ChatMessageHistory } from "langchain/stores/message/in_memory";
import { RunnableWithMessageHistory } from "@langchain/core/runnables";
import {
ChatPromptTemplate,
MessagesPlaceholder,
} from "@langchain/core/prompts";
const model = new ChatOllama({
model: "mistral-openorca",
temperature: 0,
top_k: 20,
});
const prompt = ChatPromptTemplate.fromMessages([
["system", "You're an assistant who's good at {ability}"],
new MessagesPlaceholder("history"),
["human", "{question}"],
]);
const chain = prompt.pipe(model);
const history = new ChatMessageHistory();
const chainWithHistory = new RunnableWithMessageHistory({
runnable: chain,
getMessageHistory: (_sessionId) => history,
inputMessagesKey: "question",
historyMessagesKey: "history",
});
let question = "What does cosine mean?";
const result = await chainWithHistory.invoke(
{ ability: "math", question },
{ configurable: { sessionId: "test" } },
);
console.log(`----------- Question: ${question} -------------`);
console.log(result.content);
question = "What's its inverse?";
const result2 = await chainWithHistory.invoke(
{ ability: "math", question },
{ configurable: { sessionId: "test" } },
);
console.log("\n");
console.log("----------- Follow Up Question: ${question} -------------");
console.log(result2.content);