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
11 changes: 6 additions & 5 deletions apps/roam/src/utils/discourseNodeSearchProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,11 +512,12 @@ const runSupabaseSemanticSearch = async ({
}

const queryEmbedding = await createEmbedding(trimmedQuery);
const { data, error } = await supabase.rpc("match_content_embeddings", {
query_embedding: JSON.stringify(queryEmbedding),
match_threshold: SUPABASE_MATCH_THRESHOLD,
match_count: SEARCH_TEST_RESULT_LIMIT,
});
const { data, error } = await supabase
.rpc("match_content_embeddings", {
query_embedding: JSON.stringify(queryEmbedding),
match_threshold: SUPABASE_MATCH_THRESHOLD,
})
.limit(SEARCH_TEST_RESULT_LIMIT);

if (error) {
throw new Error(error.message);
Expand Down
11 changes: 6 additions & 5 deletions apps/roam/src/utils/hyde.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,12 @@ export const findSimilarNodesVectorOnly = async ({

const queryEmbedding = await createEmbedding(text);

const { data, error } = await supabase.rpc("match_content_embeddings", {
query_embedding: JSON.stringify(queryEmbedding),
match_threshold: threshold,
match_count: limit,
});
const { data, error } = await supabase
.rpc("match_content_embeddings", {
query_embedding: JSON.stringify(queryEmbedding),
match_threshold: threshold,
})
.limit(limit);

if (error) {
console.error("Vector search failed:", error);
Expand Down
1 change: 0 additions & 1 deletion packages/database/src/dbTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,6 @@ export type Database = {
match_content_embeddings: {
Args: {
current_document_id?: number
match_count: number
match_threshold: number
query_embedding: string
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- Optimize match_content_embeddings by removing LIMIT from function body
-- This improves query planner performance as the LIMIT parameter was killing the planner

set search_path to public, extensions ;

DROP FUNCTION IF EXISTS public.match_content_embeddings(extensions.vector, double precision, integer, integer) ;

CREATE OR REPLACE FUNCTION public.match_content_embeddings (
Comment thread
mdroidian marked this conversation as resolved.
query_embedding extensions.vector,
match_threshold double precision,
current_document_id integer DEFAULT NULL::integer)
RETURNS TABLE (
content_id bigint,
roam_uid Text,
text_content Text,
similarity double precision)
SET search_path = 'extensions'
LANGUAGE sql STABLE
AS $$
SELECT
c.id AS content_id,
c.source_local_id AS roam_uid,
c.text AS text_content,
1 - (c.vector <=> query_embedding) AS similarity
FROM public.my_contents_with_embedding_openai_text_embedding_3_small_1536 AS c
WHERE 1 - (c.vector <=> query_embedding) > match_threshold
AND (current_document_id IS NULL OR c.document_id = current_document_id)
ORDER BY
c.vector <=> query_embedding ASC;
$$ ;
Comment thread
mdroidian marked this conversation as resolved.

RESET ALL ;
5 changes: 1 addition & 4 deletions packages/database/supabase/schemas/embedding.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ set search_path to public, extensions ;
CREATE OR REPLACE FUNCTION public.match_content_embeddings (
query_embedding extensions.vector,
match_threshold double precision,
match_count integer,
current_document_id integer DEFAULT NULL::integer)
RETURNS TABLE (
content_id bigint,
Expand All @@ -76,14 +75,12 @@ FROM public.my_contents_with_embedding_openai_text_embedding_3_small_1536 AS c
WHERE 1 - (c.vector <=> query_embedding) > match_threshold
AND (current_document_id IS NULL OR c.document_id = current_document_id)
ORDER BY
c.vector <=> query_embedding ASC
LIMIT match_count;
c.vector <=> query_embedding ASC;
$$ ;

ALTER FUNCTION public.match_content_embeddings (
query_embedding extensions.vector,
match_threshold double precision,
match_count integer,
current_document_id integer) OWNER TO "postgres" ;

CREATE OR REPLACE FUNCTION public.match_embeddings_for_subset_nodes (
Expand Down