Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Optimize memory fetching by moving sorting and slicing to DB #1531

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/adapter-postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ export class PostgresDatabaseAdapter
roomIds: UUID[];
agentId?: UUID;
tableName: string;
limit?: number;
}): Promise<Memory[]> {
return this.withDatabase(async () => {
if (params.roomIds.length === 0) return [];
Expand All @@ -322,6 +323,13 @@ export class PostgresDatabaseAdapter
queryParams = [...queryParams, params.agentId];
}

// Add sorting, and conditionally add LIMIT if provided
query += ` ORDER BY "createdAt" DESC`;
if (params.limit) {
query += ` LIMIT $${queryParams.length + 1}`;
queryParams.push(params.limit.toString());
}

const { rows } = await this.pool.query(query, queryParams);
return rows.map((row) => ({
...row,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export abstract class DatabaseAdapter<DB = any> implements IDatabaseAdapter {
agentId: UUID;
roomIds: UUID[];
tableName: string;
limit?: number;
}): Promise<Memory[]>;

abstract getMemoryById(id: UUID): Promise<Memory | null>;
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,12 @@ export class MemoryManager implements IMemoryManager {
);
}

async getMemoriesByRoomIds(params: { roomIds: UUID[] }): Promise<Memory[]> {
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
});
}

Expand Down
17 changes: 5 additions & 12 deletions packages/core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,18 +937,11 @@ Text: ${attachment.text}
]);

// Check the existing memories in the database
const existingMemories =
await this.messageManager.getMemoriesByRoomIds({
// filter out the current room id from rooms
roomIds: rooms.filter((room) => room !== roomId),
});

// Sort messages by timestamp in descending order
existingMemories.sort((a, b) => b.createdAt - a.createdAt);

// Take the most recent messages
const recentInteractionsData = existingMemories.slice(0, 20);
return recentInteractionsData;
return await this.messageManager.getMemoriesByRoomIds({
// filter out the current room id from rooms
roomIds: rooms.filter((room) => room !== roomId),
limit: 20
});
};

const recentInteractions =
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,7 @@ export interface IDatabaseAdapter {
tableName: string;
agentId: UUID;
roomIds: UUID[];
limit?: number;
}): Promise<Memory[]>;

getCachedEmbeddings(params: {
Expand Down Expand Up @@ -1038,7 +1039,7 @@ export interface IMemoryManager {
): Promise<{ embedding: number[]; levenshtein_score: number }[]>;

getMemoryById(id: UUID): Promise<Memory | null>;
getMemoriesByRoomIds(params: { roomIds: UUID[] }): Promise<Memory[]>;
getMemoriesByRoomIds(params: { roomIds: UUID[], limit?: number }): Promise<Memory[]>;
searchMemoriesByEmbedding(
embedding: number[],
opts: {
Expand Down
Loading