1
- import { getEmbedding } from "./embed-into-supabase.js"
2
- import { createClient } from ' @supabase/supabase-js'
3
- import dotenv from ' dotenv'
1
+ import { getEmbedding } from "./embed-into-supabase.js" ;
2
+ import { createClient } from " @supabase/supabase-js" ;
3
+ import dotenv from " dotenv" ;
4
4
5
- dotenv . config ( )
5
+ dotenv . config ( ) ;
6
6
7
- const supabaseUrl = process . env . SUPABASE_URL
8
- const supabaseKey = process . env . SUPABASE_KEY
7
+ const supabaseUrl = process . env . SUPABASE_URL ;
8
+ const supabaseKey = process . env . SUPABASE_KEY ;
9
9
const supabase = createClient ( supabaseUrl , supabaseKey , {
10
- auth : {
11
- persistSession : false
12
- }
13
- } )
10
+ auth : {
11
+ persistSession : false ,
12
+ } ,
13
+ } ) ;
14
14
15
15
/*
16
16
return similarity search in this format
@@ -22,35 +22,83 @@ return similarity search in this format
22
22
*/
23
23
24
24
export const similaritySearch = async ( query ) => {
25
- const embedding = await getEmbedding ( query )
26
- const res = [ ]
27
-
28
- const { data : documents , error } = await supabase . rpc ( 'match_highlights' , {
29
- query_embedding : embedding ,
30
- match_count : 3 ,
31
- match_threshold : 0.0
32
- } )
33
-
34
- for ( const document of documents ) {
35
- const { data : highlights , error } = await supabase . from ( 'highlights' ) . select ( '*' ) . eq ( 'id' , document . id )
36
- const book_id = highlights [ 0 ] . book_id
37
- const { data : books , error : bookError } = await supabase . from ( 'books' ) . select ( '*' ) . eq ( 'book_id' , book_id )
38
-
39
-
40
- res . push ( {
41
- text : highlights [ 0 ] . text ,
42
- title : books [ 0 ] . title ,
43
- similarity : document . similarity ,
44
- id : highlights [ 0 ] . id ,
45
- author : books [ 0 ] . author ,
46
- thoughts : highlights [ 0 ] . thoughts ,
47
- } )
48
- }
25
+ const embedding = await getEmbedding ( query ) ;
26
+ const res = [ ] ;
27
+
28
+ const { data : documents , error } = await supabase . rpc ( "match_highlights" , {
29
+ query_embedding : embedding ,
30
+ match_count : 3 ,
31
+ match_threshold : 0.0 ,
32
+ } ) ;
33
+
34
+ for ( const document of documents ) {
35
+ const { data : highlights , error } = await supabase
36
+ . from ( "highlights" )
37
+ . select ( "*" )
38
+ . eq ( "id" , document . id ) ;
39
+ const book_id = highlights [ 0 ] . book_id ;
40
+ const { data : books , error : bookError } = await supabase
41
+ . from ( "books" )
42
+ . select ( "*" )
43
+ . eq ( "book_id" , book_id ) ;
44
+
45
+ res . push ( {
46
+ text : highlights [ 0 ] . text ,
47
+ title : books [ 0 ] . title ,
48
+ similarity : document . similarity ,
49
+ id : highlights [ 0 ] . id ,
50
+ author : books [ 0 ] . author ,
51
+ thoughts : highlights [ 0 ] . thoughts ,
52
+ } ) ;
53
+ }
54
+
55
+ if ( error ) {
56
+ console . log ( error ) ;
57
+ return ;
58
+ }
49
59
50
- if ( error ) {
51
- console . log ( error )
52
- return
60
+ return res ;
61
+ } ;
62
+
63
+ export const similaritySearchWhereBookIDs = async ( query , bookIDs ) => {
64
+ const embedding = await getEmbedding ( query ) ;
65
+ const res = [ ] ;
66
+
67
+ const { data : documents , error } = await supabase . rpc (
68
+ "match_highlights_where_book_ids" ,
69
+ {
70
+ query_embedding : embedding ,
71
+ match_count : 3 ,
72
+ match_threshold : 0.0 ,
73
+ book_ids : bookIDs ,
53
74
}
75
+ ) ;
76
+
77
+ for ( const document of documents ) {
78
+ const { data : highlights , error } = await supabase
79
+ . from ( "highlights" )
80
+ . select ( "*" )
81
+ . eq ( "id" , document . id ) ;
82
+ const book_id = highlights [ 0 ] . book_id ;
83
+ const { data : books , error : bookError } = await supabase
84
+ . from ( "books" )
85
+ . select ( "*" )
86
+ . eq ( "book_id" , book_id ) ;
87
+
88
+ res . push ( {
89
+ text : highlights [ 0 ] . text ,
90
+ title : books [ 0 ] . title ,
91
+ similarity : document . similarity ,
92
+ id : highlights [ 0 ] . id ,
93
+ author : books [ 0 ] . author ,
94
+ thoughts : highlights [ 0 ] . thoughts ,
95
+ } ) ;
96
+ }
97
+
98
+ if ( error ) {
99
+ console . log ( error ) ;
100
+ return ;
101
+ }
54
102
55
- return res
56
- }
103
+ return res ;
104
+ } ;
0 commit comments