@@ -336,34 +336,52 @@ export class SqliteDatabaseAdapter extends DatabaseAdapter {
336
336
query_field_name : string ;
337
337
query_field_sub_name : string ;
338
338
query_match_count : number ;
339
- } ) : Promise <
340
- {
341
- embedding : number [ ] ;
342
- levenshtein_score : number ;
343
- } [ ]
344
- > {
339
+ } ) : Promise < { embedding : number [ ] ; levenshtein_score : number } [ ] > {
340
+ // First get content text and calculate Levenshtein distance
345
341
const sql = `
346
- SELECT *
347
- FROM memories
348
- WHERE type = ?
349
- AND vec_distance_L2(${ opts . query_field_name } , ?) <= ?
350
- ORDER BY vec_distance_L2(${ opts . query_field_name } , ?) ASC
351
- LIMIT ?
352
- ` ;
353
- console . log ( "sql" , sql )
354
- console . log ( "opts.query_input" , opts . query_input )
355
- const memories = this . db . prepare ( sql ) . all (
342
+ WITH content_text AS (
343
+ SELECT
344
+ embedding,
345
+ json_extract(
346
+ json(content),
347
+ '$.' || ? || '.' || ?
348
+ ) as content_text
349
+ FROM memories
350
+ WHERE type = ?
351
+ AND json_extract(
352
+ json(content),
353
+ '$.' || ? || '.' || ?
354
+ ) IS NOT NULL
355
+ )
356
+ SELECT
357
+ embedding,
358
+ length(?) + length(content_text) - (
359
+ length(?) + length(content_text) - (
360
+ length(replace(lower(?), lower(content_text), '')) +
361
+ length(replace(lower(content_text), lower(?), ''))
362
+ ) / 2
363
+ ) as levenshtein_score
364
+ FROM content_text
365
+ ORDER BY levenshtein_score ASC
366
+ LIMIT ?
367
+ ` ;
368
+
369
+ const rows = this . db . prepare ( sql ) . all (
370
+ opts . query_field_name ,
371
+ opts . query_field_sub_name ,
356
372
opts . query_table_name ,
357
- new Float32Array ( opts . query_input . split ( "," ) . map ( Number ) ) , // Convert string to Float32Array
373
+ opts . query_field_name ,
374
+ opts . query_field_sub_name ,
375
+ opts . query_input ,
358
376
opts . query_input ,
359
- new Float32Array ( opts . query_input . split ( "," ) . map ( Number ) )
360
- ) as Memory [ ] ;
377
+ opts . query_input ,
378
+ opts . query_input ,
379
+ opts . query_match_count
380
+ ) as { embedding : Buffer ; levenshtein_score : number } [ ] ;
361
381
362
- return memories . map ( ( memory ) => ( {
363
- embedding : Array . from (
364
- new Float32Array ( memory . embedding as unknown as Buffer )
365
- ) , // Convert Buffer to number[]
366
- levenshtein_score : 0 ,
382
+ return rows . map ( row => ( {
383
+ embedding : Array . from ( new Float32Array ( row . embedding as Buffer ) ) ,
384
+ levenshtein_score : row . levenshtein_score
367
385
} ) ) ;
368
386
}
369
387
0 commit comments