forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmemory.ts
252 lines (226 loc) · 8.21 KB
/
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
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
import { embed, getEmbeddingZeroVector } from "./embedding.ts";
import elizaLogger from "./logger.ts";
import type {
IAgentRuntime,
IMemoryManager,
Memory,
UUID,
} from "./types.ts";
const defaultMatchThreshold = 0.1;
const defaultMatchCount = 10;
/**
* Manage memories in the database.
*/
export class MemoryManager implements IMemoryManager {
/**
* The AgentRuntime instance associated with this manager.
*/
runtime: IAgentRuntime;
/**
* The name of the database table this manager operates on.
*/
tableName: string;
/**
* Constructs a new MemoryManager instance.
* @param opts Options for the manager.
* @param opts.tableName The name of the table this manager will operate on.
* @param opts.runtime The AgentRuntime instance associated with this manager.
*/
constructor(opts: { tableName: string; runtime: IAgentRuntime }) {
this.runtime = opts.runtime;
this.tableName = opts.tableName;
}
/**
* Adds an embedding vector to a memory object. If the memory already has an embedding, it is returned as is.
* @param memory The memory object to add an embedding to.
* @returns A Promise resolving to the memory object, potentially updated with an embedding vector.
*/
/**
* Adds an embedding vector to a memory object if one doesn't already exist.
* The embedding is generated from the memory's text content using the runtime's
* embedding model. If the memory has no text content, an error is thrown.
*
* @param memory The memory object to add an embedding to
* @returns The memory object with an embedding vector added
* @throws Error if the memory content is empty
*/
async addEmbeddingToMemory(memory: Memory): Promise<Memory> {
// Return early if embedding already exists
if (memory.embedding) {
return memory;
}
const memoryText = memory.content.text;
// Validate memory has text content
if (!memoryText) {
throw new Error(
"Cannot generate embedding: Memory content is empty"
);
}
try {
// Generate embedding from text content
memory.embedding = await embed(this.runtime, memoryText);
} catch (error) {
elizaLogger.error("Failed to generate embedding:", error);
// Fallback to zero vector if embedding fails
memory.embedding = getEmbeddingZeroVector().slice();
}
return memory;
}
/**
* Retrieves a list of memories by user IDs, with optional deduplication.
* @param opts Options including user IDs, count, and uniqueness.
* @param opts.roomId The room ID to retrieve memories for.
* @param opts.count The number of memories to retrieve.
* @param opts.unique Whether to retrieve unique memories only.
* @returns A Promise resolving to an array of Memory objects.
*/
async getMemories({
roomId,
count = 10,
unique = true,
start,
end,
}: {
roomId: UUID;
count?: number;
unique?: boolean;
start?: number;
end?: number;
}): Promise<Memory[]> {
return await this.runtime.databaseAdapter.getMemories({
roomId,
count,
unique,
tableName: this.tableName,
agentId: this.runtime.agentId,
start,
end,
});
}
async getCachedEmbeddings(content: string): Promise<
{
embedding: number[];
levenshtein_score: number;
}[]
> {
if (!this.runtime.databaseAdapter){
throw Error("Database adapter not initialized");
}
return await this.runtime.databaseAdapter.getCachedEmbeddings({
query_table_name: this.tableName,
query_threshold: 2,
query_input: content,
query_field_name: "content",
query_field_sub_name: "text",
query_match_count: 10,
});
}
/**
* Searches for memories similar to a given embedding vector.
* @param embedding The embedding vector to search with.
* @param opts Options including match threshold, count, user IDs, and uniqueness.
* @param opts.match_threshold The similarity threshold for matching memories.
* @param opts.count The maximum number of memories to retrieve.
* @param opts.roomId The room ID to retrieve memories for.
* @param opts.unique Whether to retrieve unique memories only.
* @returns A Promise resolving to an array of Memory objects that match the embedding.
*/
async searchMemoriesByEmbedding(
embedding: number[],
opts: {
match_threshold?: number;
count?: number;
roomId: UUID;
unique?: boolean;
}
): Promise<Memory[]> {
const {
match_threshold = defaultMatchThreshold,
count = defaultMatchCount,
roomId,
unique,
} = opts;
const result = await this.runtime.databaseAdapter.searchMemories({
tableName: this.tableName,
roomId,
agentId: this.runtime.agentId,
embedding: embedding,
match_threshold: match_threshold,
match_count: count,
unique: !!unique,
});
return result;
}
/**
* Creates a new memory in the database, with an option to check for similarity before insertion.
* @param memory The memory object to create.
* @param unique Whether to check for similarity before insertion.
* @returns A Promise that resolves when the operation completes.
*/
async createMemory(memory: Memory, unique = false): Promise<void> {
// TODO: check memory.agentId == this.runtime.agentId
const existingMessage =
await this.runtime.databaseAdapter.getMemoryById(memory.id);
if (existingMessage) {
elizaLogger.debug("Memory already exists, skipping");
return;
}
elizaLogger.log("Creating Memory", memory.id, memory.content.text);
await this.runtime.databaseAdapter.createMemory(
memory,
this.tableName,
unique
);
}
async getMemoriesByRoomIds(params: { roomIds: UUID[], limit?: number; }): Promise<Memory[]> {
return await this.runtime.databaseAdapter.getMemoriesByRoomIds({
tableName: this.tableName,
agentId: this.runtime.agentId,
roomIds: params.roomIds,
limit: params.limit
});
}
async getMemoriesByIds(ids: UUID[]): Promise<Memory[]> {
return this.runtime.databaseAdapter.getMemoriesByIds(ids);
}
async getMemoryById(id: UUID): Promise<Memory | null> {
const result = await this.runtime.databaseAdapter.getMemoryById(id);
if (result && result.agentId !== this.runtime.agentId) return null;
return result;
}
/**
* Removes a memory from the database by its ID.
* @param memoryId The ID of the memory to remove.
* @returns A Promise that resolves when the operation completes.
*/
async removeMemory(memoryId: UUID): Promise<void> {
await this.runtime.databaseAdapter.removeMemory(
memoryId,
this.tableName
);
}
/**
* Removes all memories associated with a set of user IDs.
* @param roomId The room ID to remove memories for.
* @returns A Promise that resolves when the operation completes.
*/
async removeAllMemories(roomId: UUID): Promise<void> {
await this.runtime.databaseAdapter.removeAllMemories(
roomId,
this.tableName
);
}
/**
* Counts the number of memories associated with a set of user IDs, with an option for uniqueness.
* @param roomId The room ID to count memories for.
* @param unique Whether to count unique memories only.
* @returns A Promise resolving to the count of memories.
*/
async countMemories(roomId: UUID, unique = true): Promise<number> {
return await this.runtime.databaseAdapter.countMemories(
roomId,
unique,
this.tableName
);
}
}