forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombined.ts
77 lines (65 loc) · 1.99 KB
/
combined.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { ChatOpenAI } from "@langchain/openai";
import {
BufferMemory,
CombinedMemory,
ConversationSummaryMemory,
} from "langchain/memory";
import { ConversationChain } from "langchain/chains";
import { PromptTemplate } from "@langchain/core/prompts";
// buffer memory
const bufferMemory = new BufferMemory({
memoryKey: "chat_history_lines",
inputKey: "input",
});
// summary memory
const summaryMemory = new ConversationSummaryMemory({
llm: new ChatOpenAI({ model: "gpt-3.5-turbo", temperature: 0 }),
inputKey: "input",
memoryKey: "conversation_summary",
});
//
const memory = new CombinedMemory({
memories: [bufferMemory, summaryMemory],
});
const _DEFAULT_TEMPLATE = `The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
Summary of conversation:
{conversation_summary}
Current conversation:
{chat_history_lines}
Human: {input}
AI:`;
const PROMPT = new PromptTemplate({
inputVariables: ["input", "conversation_summary", "chat_history_lines"],
template: _DEFAULT_TEMPLATE,
});
const model = new ChatOpenAI({ temperature: 0.9, verbose: true });
const chain = new ConversationChain({ llm: model, memory, prompt: PROMPT });
const res1 = await chain.call({ input: "Hi! I'm Jim." });
console.log({ res1 });
/*
{
res1: {
response: "Hello Jim! It's nice to meet you. How can I assist you today?"
}
}
*/
const res2 = await chain.call({ input: "Can you tell me a joke?" });
console.log({ res2 });
/*
{
res2: {
response: 'Why did the scarecrow win an award? Because he was outstanding in his field!'
}
}
*/
const res3 = await chain.call({
input: "What's my name and what joke did you just tell?",
});
console.log({ res3 });
/*
{
res3: {
response: 'Your name is Jim. The joke I just told was about a scarecrow winning an award because he was outstanding in his field.'
}
}
*/