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

fix: postgres embedding issues #425

Merged
merged 1 commit into from
Nov 20, 2024
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ twitter_cookies.json
timeline_cache.json

*.sqlite
agent
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this cant be git ignored

characters/

packages/core/src/providers/cache
Expand Down
43 changes: 7 additions & 36 deletions packages/adapter-postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,42 +290,13 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
match_count: number;
unique: boolean;
}): Promise<Memory[]> {
const client = await this.pool.connect();
try {
let sql = `
SELECT *,
1 - (embedding <-> $3) as similarity
FROM memories
WHERE type = $1 AND "roomId" = $2
`;

if (params.unique) {
sql += ` AND "unique" = true`;
}

sql += ` AND 1 - (embedding <-> $3) >= $4
ORDER BY embedding <-> $3
LIMIT $5`;

const { rows } = await client.query(sql, [
params.tableName,
params.roomId,
params.embedding,
params.match_threshold,
params.match_count,
]);

return rows.map((row) => ({
...row,
content:
typeof row.content === "string"
? JSON.parse(row.content)
: row.content,
similarity: row.similarity,
}));
} finally {
client.release();
}
return await this.searchMemoriesByEmbedding(params.embedding, {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these methods are equivalent. the current function signature for searchMemories doesn't scope by roomId, im not sure if that is intentional?

match_threshold: params.match_threshold,
count: params.match_count,
roomId: params.roomId,
unique: params.unique,
tableName: params.tableName,
});
}

async getMemories(params: {
Expand Down
2 changes: 1 addition & 1 deletion packages/client-discord/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,10 @@ export class MessageManager {
roomId,
content,
createdAt: message.createdTimestamp,
embedding: embeddingZeroVector,
};

if (content.text) {
await this.runtime.messageManager.addEmbeddingToMemory(memory);
await this.runtime.messageManager.createMemory(memory);
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export class MemoryManager implements IMemoryManager {
elizaLogger.debug("Memory already exists, skipping");
return;
}

await this.runtime.databaseAdapter.createMemory(
memory,
this.tableName,
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,14 @@ export class AgentRuntime implements IAgentRuntime {
text: knowledgeItem,
},
});

const fragments = await splitChunks(knowledgeItem, 1200, 200);
for (const fragment of fragments) {
const embedding = await embed(this, fragment);
await this.knowledgeManager.createMemory({
id: stringToUuid(fragment),
// We namespace the knowledge base uuid to avoid id
// collision with the document above.
id: stringToUuid(knowledgeId + fragment),
roomId: this.agentId,
agentId: this.agentId,
userId: this.agentId,
Expand Down