Skip to content

Commit 1b9d1fa

Browse files
committed
Add search-by-book-ids endpoint
1 parent dd7ec82 commit 1b9d1fa

File tree

3 files changed

+103
-41
lines changed

3 files changed

+103
-41
lines changed

get-random-highlight.js

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export async function fetchRandomHighlightInBookID(bookIds, amount) {
2525

2626
const ids = ids_table.map((id) => id.id);
2727

28-
2928
if (error) throw error;
3029

3130
const highlights = [];

server.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express from 'express';
22
import bodyParser from 'body-parser';
3-
import { similaritySearch } from './similarity-search.js';
3+
import { similaritySearch, similaritySearchWhereBookIDs } from './similarity-search.js';
44
import { sharePic } from './share-pic.js';
55
import { fetchRandomHighlight, fetchRandomHighlightInBookID } from './get-random-highlight.js';
66
import { addThoughtToHighlight } from './add-thought-to-highlight.js';
@@ -46,6 +46,21 @@ app.post('/search', async (req, res) => {
4646
}
4747
})
4848

49+
app.post('/search-by-book-ids', async (req, res) => {
50+
try {
51+
console.log(req.body)
52+
const query = req.body.query
53+
const book_ids = req.body.book_ids
54+
const results = await similaritySearchWhereBookIDs(query, book_ids)
55+
res.send(results)
56+
} catch (err) {
57+
console.log(err)
58+
res.status(500).send({
59+
error: err
60+
})
61+
}
62+
})
63+
4964
app.post('/dalle-to-cf', async (req, res) => {
5065
console.log(req.body)
5166
const url = req.body.url

similarity-search.js

+87-39
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
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";
44

5-
dotenv.config()
5+
dotenv.config();
66

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;
99
const supabase = createClient(supabaseUrl, supabaseKey, {
10-
auth: {
11-
persistSession: false
12-
}
13-
})
10+
auth: {
11+
persistSession: false,
12+
},
13+
});
1414

1515
/*
1616
return similarity search in this format
@@ -22,35 +22,83 @@ return similarity search in this format
2222
*/
2323

2424
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+
}
4959

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,
5374
}
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+
}
54102

55-
return res
56-
}
103+
return res;
104+
};

0 commit comments

Comments
 (0)