-
Notifications
You must be signed in to change notification settings - Fork 2k
/
combined_memory.ts
144 lines (130 loc) Β· 3.95 KB
/
combined_memory.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import {
BaseChatMemory,
BaseChatMemoryInput,
} from "@langchain/community/memory/chat_memory";
import {
InputValues,
MemoryVariables,
BaseMemory,
OutputValues,
} from "@langchain/core/memory";
/**
* Interface that defines the shape of the input object that the
* CombinedMemory constructor accepts. It extends the BaseChatMemoryInput
* interface and adds additional properties.
*/
export interface CombinedMemoryInput extends BaseChatMemoryInput {
memories: BaseMemory[];
humanPrefix?: string;
aiPrefix?: string;
memoryKey?: string;
}
/**
* Class that manages and manipulates previous chat messages. It extends
* from the BaseChatMemory class and implements the CombinedMemoryInput
* interface.
*/
export class CombinedMemory
extends BaseChatMemory
implements CombinedMemoryInput
{
humanPrefix = "Human";
aiPrefix = "AI";
memoryKey = "history";
memories: BaseMemory[] = [];
constructor(fields?: CombinedMemoryInput) {
super({
chatHistory: fields?.chatHistory,
returnMessages: fields?.returnMessages ?? false,
inputKey: fields?.inputKey,
outputKey: fields?.outputKey,
});
this.memories = fields?.memories ?? this.memories;
this.humanPrefix = fields?.humanPrefix ?? this.humanPrefix;
this.aiPrefix = fields?.aiPrefix ?? this.aiPrefix;
this.memoryKey = fields?.memoryKey ?? this.memoryKey;
this.checkRepeatedMemoryVariable();
this.checkInputKey();
}
/**
* Checks for repeated memory variables across all memory objects. Throws
* an error if any are found.
*/
checkRepeatedMemoryVariable() {
const allVariables: string[] = [];
for (const memory of this.memories) {
const overlap = allVariables.filter((x) => memory.memoryKeys.includes(x));
if (overlap.length > 0) {
throw new Error(
`The same variables ${[
...overlap,
]} are found in multiple memory objects, which is not allowed by CombinedMemory.`
);
}
allVariables.push(...memory.memoryKeys);
}
}
/**
* Checks if input keys are set for all memory objects. Logs a warning if
* any are missing.
*/
checkInputKey() {
for (const memory of this.memories) {
if (
(memory as BaseChatMemory).chatHistory !== undefined &&
(memory as BaseChatMemory).inputKey === undefined
) {
console.warn(
`When using CombinedMemory, input keys should be set so the input is known. Was not set on ${memory}.`
);
}
}
}
/**
* Loads memory variables from all memory objects.
* @param inputValues Input values to load memory variables from.
* @returns Promise that resolves with an object containing the loaded memory variables.
*/
async loadMemoryVariables(
inputValues: InputValues
): Promise<MemoryVariables> {
let memoryData: Record<string, unknown> = {};
for (const memory of this.memories) {
const data = await memory.loadMemoryVariables(inputValues);
memoryData = {
...memoryData,
...data,
};
}
return memoryData;
}
/**
* Saves the context to all memory objects.
* @param inputValues Input values to save.
* @param outputValues Output values to save.
* @returns Promise that resolves when the context has been saved to all memory objects.
*/
async saveContext(inputValues: InputValues, outputValues: OutputValues) {
for (const memory of this.memories) {
await memory.saveContext(inputValues, outputValues);
}
}
/**
* Clears all memory objects.
* @returns Promise that resolves when all memory objects have been cleared.
*/
async clear() {
for (const memory of this.memories) {
if (typeof (memory as BaseChatMemory).clear === "function") {
await (memory as BaseChatMemory).clear();
}
}
}
get memoryKeys() {
const memoryKeys: string[] = [];
for (const memory of this.memories) {
memoryKeys.push(...memory.memoryKeys);
}
return memoryKeys;
}
}