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 embedding calculation for sqlite #261

Merged
merged 2 commits into from
Nov 13, 2024
Merged
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
45 changes: 34 additions & 11 deletions packages/adapter-sqlite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,28 +337,51 @@ export class SqliteDatabaseAdapter extends DatabaseAdapter {
query_field_sub_name: string;
query_match_count: number;
}): Promise<{ embedding: number[]; levenshtein_score: number }[]> {
// First get content text and calculate Levenshtein distance
const sql = `
WITH content_text AS (
SELECT
embedding,
json_extract(
json(content),
'$.' || ? || '.' || ?
) as content_text
FROM memories
WHERE type = ?
AND json_extract(
json(content),
'$.' || ? || '.' || ?
) IS NOT NULL
)
SELECT
embedding,
0 as levenshtein_score -- Using 0 as placeholder score
FROM memories
WHERE type = ?
AND json_extract(content, '$.' || ? || '.' || ?) IS NOT NULL
length(?) + length(content_text) - (
length(?) + length(content_text) - (
length(replace(lower(?), lower(content_text), '')) +
length(replace(lower(content_text), lower(?), ''))
) / 2
) as levenshtein_score
FROM content_text
ORDER BY levenshtein_score ASC
LIMIT ?
`;

const params = [
const rows = this.db.prepare(sql).all(
opts.query_field_name,
opts.query_field_sub_name,
opts.query_table_name,
opts.query_field_name,
opts.query_field_sub_name,
opts.query_input,
opts.query_input,
opts.query_input,
opts.query_input,
opts.query_match_count
];

const rows = this.db.prepare(sql).all(...params);
) as { embedding: Buffer; levenshtein_score: number }[];

return rows.map((row) => ({
embedding: row.embedding,
levenshtein_score: 0
return rows.map(row => ({
embedding: Array.from(new Float32Array(row.embedding as Buffer)),
levenshtein_score: row.levenshtein_score
}));
}

Expand Down
Loading