From 1b9d1fadaded98b3834148c419afba54d823dacf Mon Sep 17 00:00:00 2001 From: Bram Adams <3282661+bramses@users.noreply.github.com> Date: Wed, 13 Mar 2024 14:02:33 -0400 Subject: [PATCH] Add search-by-book-ids endpoint --- get-random-highlight.js | 1 - server.js | 17 +++++- similarity-search.js | 126 +++++++++++++++++++++++++++------------- 3 files changed, 103 insertions(+), 41 deletions(-) diff --git a/get-random-highlight.js b/get-random-highlight.js index 8332a51..7d97011 100644 --- a/get-random-highlight.js +++ b/get-random-highlight.js @@ -25,7 +25,6 @@ export async function fetchRandomHighlightInBookID(bookIds, amount) { const ids = ids_table.map((id) => id.id); - if (error) throw error; const highlights = []; diff --git a/server.js b/server.js index 88e49fe..1f68bb4 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,6 @@ import express from 'express'; import bodyParser from 'body-parser'; -import { similaritySearch } from './similarity-search.js'; +import { similaritySearch, similaritySearchWhereBookIDs } from './similarity-search.js'; import { sharePic } from './share-pic.js'; import { fetchRandomHighlight, fetchRandomHighlightInBookID } from './get-random-highlight.js'; import { addThoughtToHighlight } from './add-thought-to-highlight.js'; @@ -46,6 +46,21 @@ app.post('/search', async (req, res) => { } }) +app.post('/search-by-book-ids', async (req, res) => { + try { + console.log(req.body) + const query = req.body.query + const book_ids = req.body.book_ids + const results = await similaritySearchWhereBookIDs(query, book_ids) + res.send(results) + } catch (err) { + console.log(err) + res.status(500).send({ + error: err + }) + } +}) + app.post('/dalle-to-cf', async (req, res) => { console.log(req.body) const url = req.body.url diff --git a/similarity-search.js b/similarity-search.js index 698be16..1e53878 100644 --- a/similarity-search.js +++ b/similarity-search.js @@ -1,16 +1,16 @@ -import { getEmbedding } from "./embed-into-supabase.js" -import { createClient } from '@supabase/supabase-js' -import dotenv from 'dotenv' +import { getEmbedding } from "./embed-into-supabase.js"; +import { createClient } from "@supabase/supabase-js"; +import dotenv from "dotenv"; -dotenv.config() +dotenv.config(); -const supabaseUrl = process.env.SUPABASE_URL -const supabaseKey = process.env.SUPABASE_KEY +const supabaseUrl = process.env.SUPABASE_URL; +const supabaseKey = process.env.SUPABASE_KEY; const supabase = createClient(supabaseUrl, supabaseKey, { - auth: { - persistSession: false - } -}) + auth: { + persistSession: false, + }, +}); /* return similarity search in this format @@ -22,35 +22,83 @@ return similarity search in this format */ export const similaritySearch = async (query) => { - const embedding = await getEmbedding(query) - const res = [] - - const { data: documents, error } = await supabase.rpc('match_highlights', { - query_embedding: embedding, - match_count: 3, - match_threshold: 0.0 - }) - - for (const document of documents) { - const { data: highlights, error } = await supabase.from('highlights').select('*').eq('id', document.id) - const book_id = highlights[0].book_id - const { data: books, error: bookError } = await supabase.from('books').select('*').eq('book_id', book_id) - - - res.push({ - text: highlights[0].text, - title: books[0].title, - similarity: document.similarity, - id: highlights[0].id, - author: books[0].author, - thoughts: highlights[0].thoughts, - }) - } + const embedding = await getEmbedding(query); + const res = []; + + const { data: documents, error } = await supabase.rpc("match_highlights", { + query_embedding: embedding, + match_count: 3, + match_threshold: 0.0, + }); + + for (const document of documents) { + const { data: highlights, error } = await supabase + .from("highlights") + .select("*") + .eq("id", document.id); + const book_id = highlights[0].book_id; + const { data: books, error: bookError } = await supabase + .from("books") + .select("*") + .eq("book_id", book_id); + + res.push({ + text: highlights[0].text, + title: books[0].title, + similarity: document.similarity, + id: highlights[0].id, + author: books[0].author, + thoughts: highlights[0].thoughts, + }); + } + + if (error) { + console.log(error); + return; + } - if (error) { - console.log(error) - return + return res; +}; + +export const similaritySearchWhereBookIDs = async (query, bookIDs) => { + const embedding = await getEmbedding(query); + const res = []; + + const { data: documents, error } = await supabase.rpc( + "match_highlights_where_book_ids", + { + query_embedding: embedding, + match_count: 3, + match_threshold: 0.0, + book_ids: bookIDs, } + ); + + for (const document of documents) { + const { data: highlights, error } = await supabase + .from("highlights") + .select("*") + .eq("id", document.id); + const book_id = highlights[0].book_id; + const { data: books, error: bookError } = await supabase + .from("books") + .select("*") + .eq("book_id", book_id); + + res.push({ + text: highlights[0].text, + title: books[0].title, + similarity: document.similarity, + id: highlights[0].id, + author: books[0].author, + thoughts: highlights[0].thoughts, + }); + } + + if (error) { + console.log(error); + return; + } - return res -} \ No newline at end of file + return res; +};