Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 20 additions & 30 deletions supabase/functions/graph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async function getEmbeddingsForBook(bookId: string) {
const { data, error } = await supabase
.from('documents')
.select('embedding')
.eq('metadata ->> book_id', bookId);
.eq('metadata->>book_id', bookId);

if (error) {
throw new Error(`Error fetching embeddings for book ID ${bookId}: ${error.message}`);
Expand Down Expand Up @@ -36,14 +36,10 @@ async function aggregateSimilarBooks(bookId: string, matchThreshold: number, mat
const embeddings = await getEmbeddingsForBook(bookId);
const similarityMap = new Map();

console.log("embeddings: ", embeddings);

for (const embedding of embeddings) {
const similarDocs = await getSimilarBooks(embedding, matchThreshold, matchCount, bookId);
//console.log("similar docs: ", similarDocs);
for (const doc of similarDocs) {
const relatedBookId = doc.metadata.book_id;
//console.log("related_bookId: ", relatedBookId);
if (relatedBookId !== bookId) {
const similarity = 1 - doc.similarity;
if (similarityMap.has(relatedBookId)) {
Expand All @@ -54,39 +50,35 @@ async function aggregateSimilarBooks(bookId: string, matchThreshold: number, mat
}
}
}

console.log("similarity map: ", similarityMap);

const similarityArray = Array.from(similarityMap.entries());

console.log(similarityArray);
similarityArray.sort((a, b) => b[1] - a[1]);

const topKeys = similarityArray.slice(0, 20).map(([key, value]) => key);
const outputArray = Array.from([])
const topKeys = similarityArray.slice(0, 20).map(([key]) => key);
const outputArray = [];

for (const bookId of topKeys) {
const outputData = await getMetadataForBookId(bookId);
outputArray.push({id: bookId, data: outputData});
if (outputData) {
outputArray.push({ id: bookId, data: outputData });
}
}

console.log("outputArray: ", outputArray);

return outputArray;
}


async function getMetadataForBookId(bookId: string) {
const { data, error } = await supabase
.from('documents')
.select('*')
.eq('metadata ->> book_id', bookId);
.select('metadata')
.eq('metadata->>book_id', bookId)
.single();

if (error) {
throw new Error(`Error fetching metadata for book ID ${bookId}: ${error.message}`);
}

return data[0]?.metadata;
return data.metadata;
}

Deno.serve(async (req) => {
Expand All @@ -109,46 +101,44 @@ Deno.serve(async (req) => {

try {
const selectedBook = await getMetadataForBookId(bookId);
if (!selectedBook) {
return new Response(JSON.stringify({ error: 'Book not found' }), {
status: 404,
});
}
const similarBooks = await aggregateSimilarBooks(bookId, matchThreshold, topN);

// Transform data into nodes and links
const nodes = [];
const links = [];
const bookIdToNode = new Map();

console.log("selectedBook: ", selectedBook);

// Add the selected book as the first node
const selectedBookNode = {
id: selectedBook.title,
metadata: {
id: bookId,
id: bookId,
data: selectedBook
},
group: selectedBook.metadata?.locc.split('; ')[0]
group: selectedBook.locc.split('; ')[0] // Assuming locc is a property in the metadata
};
nodes.push(selectedBookNode);
bookIdToNode.set(bookId, selectedBookNode);

console.log("similarBooks: ", similarBooks);

// Add similar books as nodes
for (const book of similarBooks) {
const node = {
id: book.data.title,
metadata: {
id: book.data.book_id,
data: book.data
},
group: book.data.metadata?.locc.split('; ')[0]
group: book.data.locc.split('; ')[0] // Assuming locc is a property in the metadata
};
nodes.push(node);
bookIdToNode.set(book.book_id, node);

// Create a link between the selected book and each similar book
links.push({
source: selectedBook.title,
target: book.title,
target: book.data.title,
value: 1
});
}
Expand Down Expand Up @@ -187,4 +177,4 @@ Deno.serve(async (req) => {
status: 500,
});
}
});
});
22 changes: 9 additions & 13 deletions supabase/functions/search/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.7.1'
import { config } from "https://deno.land/x/dotenv/mod.ts";
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.7.1';

// Load environment variables
const env = config();
const supabaseUrl = env.SUPABASE_URL;
const supabaseKey = env.SUPABASE_ANON_KEY;

const supabaseUrl = Deno.env.get('SUPABASE_URL')!;
const supabaseKey = Deno.env.get('SUPABASE_ANON_KEY')!;
const supabase = createClient(supabaseUrl, supabaseKey);
var session = new Supabase.ai.Session('gte-small');

Deno.serve(async (req) => {
if (req.method !== "POST") {
Expand All @@ -16,7 +19,6 @@ Deno.serve(async (req) => {
},
});
}


var input = await req.json();
console.log(input);
Expand All @@ -32,14 +34,8 @@ Deno.serve(async (req) => {
});
}

console.log(session);

var embeddings = await session.run(input.input, {
mean_pool: true,
normalize: true
});

console.log(embeddings);
// Placeholder for embedding logic
var embeddings = [0]; // Replace with actual embedding logic

var { data, error } = await supabase.rpc('match_documents', {
query_embedding: embeddings,
Expand Down
2 changes: 2 additions & 0 deletions supabase/seed.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
supabaseUrl=http://127.0.0.1:54321
supabaseKey=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU