Skip to content

Commit

Permalink
Add search-by-book-ids endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bramses committed Mar 13, 2024
1 parent dd7ec82 commit 1b9d1fa
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 41 deletions.
1 change: 0 additions & 1 deletion get-random-highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export async function fetchRandomHighlightInBookID(bookIds, amount) {

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


if (error) throw error;

const highlights = [];
Expand Down
17 changes: 16 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand Down
126 changes: 87 additions & 39 deletions similarity-search.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
return res;
};

0 comments on commit 1b9d1fa

Please sign in to comment.