-
Notifications
You must be signed in to change notification settings - Fork 2k
/
zep_cloud.ts
152 lines (134 loc) Β· 3.84 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
import { Zep, ZepClient } from "@getzep/zep-cloud";
import { Memory, NotFoundError, RoleType } from "@getzep/zep-cloud/api";
import { BaseChatMessageHistory } from "@langchain/core/chat_history";
import {
AIMessage,
BaseMessage,
HumanMessage,
MessageType,
} from "@langchain/core/messages";
import { condenseZepMemoryIntoHumanMessage } from "../../memory/zep_cloud.js";
export const getZepMessageRoleType = (role: MessageType): RoleType => {
switch (role) {
case "human":
return "user";
case "ai":
return "assistant";
case "system":
return "system";
case "function":
return "function";
case "tool":
return "tool";
default:
return "norole";
}
};
/**
* Interface defining the structure of the input data for the ZepMemory
* class. It includes properties like humanPrefix, aiPrefix, memoryKey, sessionId, memoryType and apiKey.
*/
interface ZepMemoryInput {
sessionId: string;
client: ZepClient;
memoryType: Zep.MemoryGetRequestMemoryType;
humanPrefix?: string;
aiPrefix?: string;
}
/**
* 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.
*
*/
export class ZepCloudChatMessageHistory
extends BaseChatMessageHistory
implements ZepMemoryInput
{
lc_namespace: string[] = [];
sessionId: string;
client: ZepClient;
memoryType: Zep.MemoryGetRequestMemoryType;
humanPrefix = "human";
aiPrefix = "ai";
constructor(fields: ZepMemoryInput) {
super();
this.sessionId = fields.sessionId;
this.memoryType = fields.memoryType;
this.client = fields.client;
if (fields.humanPrefix) {
this.humanPrefix = fields.humanPrefix;
}
if (fields.aiPrefix) {
this.aiPrefix = fields.aiPrefix;
}
}
private async getMemory(): Promise<Memory | null> {
try {
return this.client.memory.get(this.sessionId, {
memoryType: this.memoryType,
});
} catch (error) {
// eslint-disable-next-line no-instanceof/no-instanceof
if (error instanceof NotFoundError) {
console.warn(
`Session ${this.sessionId} not found in Zep. Returning None`
);
} else {
console.error("Error getting memory: ", error);
}
return null;
}
}
async getMessages(): Promise<BaseMessage[]> {
const memory = await this.getMemory();
if (!memory) {
return [];
}
return [condenseZepMemoryIntoHumanMessage(memory)];
}
async addAIChatMessage(
message: string,
metadata?: Record<string, unknown>
): Promise<void> {
await this.addMessage(new AIMessage({ content: message }), metadata);
}
async addMessage(
message: BaseMessage,
metadata?: Record<string, unknown>
): Promise<void> {
const messageToSave = message;
if (message._getType() === "ai") {
messageToSave.name = this.aiPrefix;
} else if (message._getType() === "human") {
messageToSave.name = this.humanPrefix;
}
if (message.content === null) {
throw new Error("Message content cannot be null");
}
if (Array.isArray(message.content)) {
throw new Error("Message content cannot be a list");
}
await this.client.memory.add(this.sessionId, {
messages: [
{
content: message.content,
role: message.name ?? message._getType(),
roleType: getZepMessageRoleType(message._getType()),
metadata,
},
],
});
}
async addUserMessage(
message: string,
metadata?: Record<string, unknown>
): Promise<void> {
await this.addMessage(new HumanMessage({ content: message }, metadata));
}
clear(): Promise<void> {
console.warn("Clearing memory", this.sessionId);
return Promise.resolve(undefined);
}
}