-
Notifications
You must be signed in to change notification settings - Fork 2k
/
zep_cloud.ts
286 lines (249 loc) Β· 8.18 KB
/
zep_cloud.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import { Zep, ZepClient } from "@getzep/zep-cloud";
import { Memory, NotFoundError } from "@getzep/zep-cloud/api";
import {
InputValues,
OutputValues,
MemoryVariables,
getInputValue,
getOutputValue,
} from "@langchain/core/memory";
import {
AIMessage,
BaseMessage,
ChatMessage,
getBufferString,
HumanMessage,
SystemMessage,
} from "@langchain/core/messages";
import { BaseChatMemory, BaseChatMemoryInput } from "./chat_memory.js";
// Extract Summary and Facts from Zep memory, if present and compose a system prompt
export const zepMemoryContextToSystemPrompt = (memory: Memory) => {
let systemPrompt = "";
// Extract conversation facts, if present
if (memory.facts) {
systemPrompt += memory.facts.join("\n");
}
// Extract summary, if present
if (memory.summary && memory.summary?.content) {
systemPrompt += memory.summary.content;
}
return systemPrompt;
};
// We are condensing the Zep context into a human message in order to satisfy
// some models' input requirements and allow more flexibility for devs.
// (for example, Anthropic only supports one system message, and does not support multiple user messages in a row)
export const condenseZepMemoryIntoHumanMessage = (memory: Memory) => {
const systemPrompt = zepMemoryContextToSystemPrompt(memory);
let concatMessages = "";
// Add message history to the prompt, if present
if (memory.messages) {
concatMessages = memory.messages
.map((msg) => `${msg.role ?? msg.roleType}: ${msg.content}`)
.join("\n");
}
return new HumanMessage(`${systemPrompt}\n${concatMessages}`);
};
// Convert Zep Memory to a list of BaseMessages
export const zepMemoryToMessages = (memory: Memory) => {
const systemPrompt = zepMemoryContextToSystemPrompt(memory);
let messages: BaseMessage[] = systemPrompt
? [new SystemMessage(systemPrompt)]
: [];
if (memory && memory.messages) {
messages = messages.concat(
memory.messages
.filter((m) => m.content)
.map((message) => {
const { content, role, roleType } = message;
const messageContent = content as string;
if (roleType === "user") {
return new HumanMessage(messageContent);
} else if (role === "assistant") {
return new AIMessage(messageContent);
} else {
// default to generic ChatMessage
return new ChatMessage(
messageContent,
(roleType ?? role) as string
);
}
})
);
}
return messages;
};
/**
* Interface defining the structure of the input data for the ZepMemory
* class. It includes properties like humanPrefix, aiPrefix, memoryKey, memoryType
* sessionId, and apiKey.
*/
export interface ZepCloudMemoryInput extends BaseChatMemoryInput {
humanPrefix?: string;
aiPrefix?: string;
memoryKey?: string;
sessionId: string;
apiKey: string;
memoryType?: Zep.MemoryType;
// Whether to return separate messages for chat history with a SystemMessage containing (facts and summary) or return a single HumanMessage with the entire memory context.
// Defaults to false (return a single HumanMessage) in order to allow more flexibility with different models.
separateMessages?: boolean;
}
/**
* Class used to manage the memory of a chat session, including loading
* and saving the chat history, and clearing the memory when needed. It
* uses the ZepClient to interact with the Zep service for managing the
* chat session's memory.
* @example
* ```typescript
* const sessionId = randomUUID();
*
* // Initialize ZepCloudMemory with session ID and API key
* const memory = new ZepCloudMemory({
* sessionId,
* apiKey: "<zep api key>",
* });
*
* // Create a ChatOpenAI model instance with specific parameters
* const model = new ChatOpenAI({
* modelName: "gpt-3.5-turbo",
* temperature: 0,
* });
*
* // Create a ConversationChain with the model and memory
* const chain = new ConversationChain({ llm: model, memory });
*
* // Example of calling the chain with an input
* const res1 = await chain.call({ input: "Hi! I'm Jim." });
* console.log({ res1 });
*
* // Follow-up call to the chain to demonstrate memory usage
* const res2 = await chain.call({ input: "What did I just say my name was?" });
* console.log({ res2 });
*
* // Output the session ID and the current state of memory
* console.log("Session ID: ", sessionId);
* console.log("Memory: ", await memory.loadMemoryVariables({}));
*
* ```
*/
export class ZepCloudMemory
extends BaseChatMemory
implements ZepCloudMemoryInput
{
humanPrefix = "Human";
aiPrefix = "AI";
memoryKey = "history";
apiKey: string;
sessionId: string;
zepClient: ZepClient;
memoryType: Zep.MemoryType;
separateMessages: boolean;
constructor(fields: ZepCloudMemoryInput) {
super({
returnMessages: fields?.returnMessages ?? false,
inputKey: fields?.inputKey,
outputKey: fields?.outputKey,
});
this.humanPrefix = fields.humanPrefix ?? this.humanPrefix;
this.aiPrefix = fields.aiPrefix ?? this.aiPrefix;
this.memoryKey = fields.memoryKey ?? this.memoryKey;
this.apiKey = fields.apiKey;
this.sessionId = fields.sessionId;
this.memoryType = fields.memoryType ?? "perpetual";
this.separateMessages = fields.separateMessages ?? false;
this.zepClient = new ZepClient({
apiKey: this.apiKey,
});
}
get memoryKeys() {
return [this.memoryKey];
}
/**
* Method that retrieves the chat history from the Zep service and formats
* it into a list of messages.
* @param values Input values for the method.
* @returns Promise that resolves with the chat history formatted into a list of messages.
*/
async loadMemoryVariables(values: InputValues): Promise<MemoryVariables> {
const memoryType = values.memoryType ?? "perpetual";
let memory: Memory | null = null;
try {
memory = await this.zepClient.memory.get(this.sessionId, {
memoryType,
});
} catch (error) {
// eslint-disable-next-line no-instanceof/no-instanceof
if (error instanceof NotFoundError) {
return this.returnMessages
? { [this.memoryKey]: [] }
: { [this.memoryKey]: "" };
}
throw error;
}
if (this.returnMessages) {
return {
[this.memoryKey]: this.separateMessages
? zepMemoryToMessages(memory)
: [condenseZepMemoryIntoHumanMessage(memory)],
};
}
return {
[this.memoryKey]: this.separateMessages
? getBufferString(
zepMemoryToMessages(memory),
this.humanPrefix,
this.aiPrefix
)
: condenseZepMemoryIntoHumanMessage(memory).content,
};
}
/**
* Method that saves the input and output messages to the Zep service.
* @param inputValues Input messages to be saved.
* @param outputValues Output messages to be saved.
* @returns Promise that resolves when the messages have been saved.
*/
async saveContext(
inputValues: InputValues,
outputValues: OutputValues
): Promise<void> {
const input = getInputValue(inputValues, this.inputKey);
const output = getOutputValue(outputValues, this.outputKey);
// Add the new memory to the session using the ZepClient
if (this.sessionId) {
try {
await this.zepClient.memory.add(this.sessionId, {
messages: [
{
role: this.humanPrefix,
roleType: "user",
content: `${input}`,
},
{
role: this.aiPrefix,
roleType: "assistant",
content: `${output}`,
},
],
});
} catch (error) {
console.error("Error adding memory: ", error);
}
}
// Call the superclass's saveContext method
await super.saveContext(inputValues, outputValues);
}
/**
* Method that deletes the chat history from the Zep service.
* @returns Promise that resolves when the chat history has been deleted.
*/
async clear(): Promise<void> {
try {
await this.zepClient.memory.delete(this.sessionId);
} catch (error) {
console.error("Error deleting session: ", error);
}
// Clear the superclass's chat history
await super.clear();
}
}