-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers-memory.js
218 lines (196 loc) · 7.03 KB
/
helpers-memory.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
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
const logger = require("./src/logger")("helpers-memory");
const {
getRelevantMemories,
getUserMemory,
getUserMessageHistory,
getAllMemories,
} = require("./src/remember");
const { supabase } = require("./src/supabaseclient");
const { Chance } = require("chance");
const chance = new Chance();
// async function getUserMemory(username, limit = 10) {
// const { data, error } = await supabase
// .from("memories")
// .select("*")
// .eq("user_id", username)
// .order("created_at", { ascending: false })
// .limit(limit);
// if (error) {
// logger.error(
// `Error retrieving user memory for ${username}: ${JSON.stringify(error)}`
// );
// return [];
// }
// return data;
// }
// async function getUserMessageHistory(username, limit = 10) {
// const { data, error } = await supabase
// .from("messages")
// .select("*")
// .eq("user_id", username)
// .order("created_at", { ascending: false })
// .limit(limit);
// if (error) {
// logger.error(
// `Error retrieving message history for ${username}: ${JSON.stringify(
// error
// )}`
// );
// return [];
// }
// return data;
// }
// async function getAllMemories(limit = 10) {
// const { data, error } = await supabase
// .from("memories")
// .select("*")
// .order("created_at", { ascending: false })
// .limit(limit);
// if (error) {
// logger.error(`Error retrieving general memories: ${JSON.stringify(error)}`);
// return [];
// }
// return data;
// }
/**
* Retrieves previous messages for a user and adds them to the messages array.
* @param {string} username - The username of the user.
* @param {Array} messages - The array to which the user messages will be added.
* @param {Object} options - The options for generating user messages.
* @param {number} [options.minCount=1] - The minimum number of user messages to retrieve.
* @param {number} [options.maxCount=10] - The maximum number of user messages to retrieve.
* @returns {Promise<void>} - A promise that resolves when the user messages have been added to the array.
*/
async function addUserMessages(username, messages, options = {}) {
const { minCount = 2, maxCount = 8 } = options;
const userMessageCount = chance.integer({ min: minCount, max: maxCount });
// Retrieve user messages and add them to the messages array
logger.info(
`🔧 Retrieving ${userMessageCount} previous messages for ${username}`
);
try {
const userMessages = await getUserMessageHistory(
username,
userMessageCount
);
if (!userMessages) {
logger.warn(`No previous messages found for ${username}`);
return;
}
userMessages.reverse();
userMessages.forEach((message) => {
messages.push({
role: "user",
content: `${message.value}`,
});
});
} catch (error) {
logger.error("Error getting previous user messages:", error);
}
}
/**
* Adds user memories to the messages array.
* @param {string} username - The username of the user.
* @param {Array} messages - The array of messages to add user memories to.
* @param {Object} options - The options for retrieving user memories.
* @param {number} [options.minCount=8] - The minimum number of user memories to retrieve.
* @param {number} [options.maxCount=32] - The maximum number of user memories to retrieve.
* @returns {Promise<void>} - A promise that resolves when the user memories are added to the messages array.
*/
async function addUserMemories(username, messages, options = {}) {
const { minCount = 2, maxCount = 6 } = options;
const userMemoryCount = chance.integer({ min: minCount, max: maxCount });
try {
const userMemories = await getUserMemory(username, userMemoryCount);
logger.info(`🔧 Retrieving ${userMemoryCount} memories for ${username}`);
userMemories.forEach((memory) => {
messages.push({
role: "system",
content: `You remember from a previous interaction on ${memory.created_at}: ${memory.value}`,
});
});
} catch (err) {
logger.error(err);
}
}
/**
* Adds relevant memories to the messages array.
* @param {string} username - The username of the user.
* @param {Array} messages - The array of messages to add relevant memories to.
* @param {Object} options - The options for retrieving relevant memories.
* @param {number} [options.minCount=6] - The minimum number of relevant memories to retrieve.
* @param {number} [options.maxCount=32] - The maximum number of relevant memories to retrieve.
* @returns {Promise<void>} - A promise that resolves when the relevant memories are added to the messages array.
*/
async function addRelevantMemories(username, messages, options = {}) {
const { minCount = 1, maxCount = 4 } = options;
const relevantMemoryCount = chance.integer({ min: minCount, max: maxCount });
// get the last user message to use as the query for relevant memories
const mostRecentUserMessage = messages
.slice()
.reverse()
.find((message) => message.role === "user");
if (!mostRecentUserMessage) {
logger.info(`No last user message found for ${username}`);
return;
}
const queryString = mostRecentUserMessage.content;
try {
const relevantMemories = await getRelevantMemories(
queryString,
relevantMemoryCount
);
logger.info(
`🔧 Retrieving ${relevantMemoryCount} relevant memories for ${queryString.slice(
0,
40
)}...`
);
if (relevantMemories.length != 0) {
relevantMemories.forEach((memory) => {
// log out the memories
// logger.info("relevant memory " + JSON.stringify(memory));
messages.push({
role: "system",
content: `${memory.created_at}: ${memory.value}`,
});
});
}
} catch (err) {
logger.error(`${err} - Error retrieving relevant memories`);
}
}
/**
* Adds general memories to the messages array.
* @param {Array} messages - The array of messages to add general memories to.
* @param {Object} options - The options for retrieving general memories.
* @param {number} [options.minCount=2] - The minimum number of general memories to retrieve.
* @param {number} [options.maxCount=8] - The maximum number of general memories to retrieve.
* @returns {Promise<void>} - A promise that resolves when the general memories are added to the messages array.
*/
async function addGeneralMemories(messages, options = {}) {
const { minCount = 1, maxCount = 3 } = options;
const generalMemoryCount = chance.integer({ min: minCount, max: maxCount });
try {
const generalMemories = await getAllMemories(generalMemoryCount);
logger.info(`🔧 Retrieving ${generalMemoryCount} general memories`);
generalMemories.forEach((memory) => {
messages.push({
role: "system",
content: `${memory.created_at}: ${memory.value}`,
});
});
} catch (err) {
logger.error(err);
}
}
module.exports = {
getUserMemory,
getUserMessageHistory,
getAllMemories,
getRelevantMemories,
addUserMessages,
addUserMemories,
addRelevantMemories,
addGeneralMemories,
};