diff options
| author | Fuwn <[email protected]> | 2026-02-03 21:46:26 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-03 21:46:26 -0800 |
| commit | dcaa0a42d1cca1b0f04cf5c8c9aeb212efe289f0 (patch) | |
| tree | b0b0970e0f4a206507bf60a8e40bd085d8c3359d /supabase | |
| parent | refactor(web): Use self-documenting variable names (diff) | |
| download | archived-imemio-dcaa0a42d1cca1b0f04cf5c8c9aeb212efe289f0.tar.xz archived-imemio-dcaa0a42d1cca1b0f04cf5c8c9aeb212efe289f0.zip | |
feat(sdk): Support variable embedding dimensions
Diffstat (limited to 'supabase')
| -rw-r--r-- | supabase/migrations/002_variable_embedding_dimensions.sql | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/supabase/migrations/002_variable_embedding_dimensions.sql b/supabase/migrations/002_variable_embedding_dimensions.sql new file mode 100644 index 0000000..522df47 --- /dev/null +++ b/supabase/migrations/002_variable_embedding_dimensions.sql @@ -0,0 +1,62 @@ +drop index if exists memories_embedding_idx; + +alter table public.memories + drop column if exists embedding; + +alter table public.memories + add column embedding vector, + add column embedding_dimensions integer; + +create index memories_embedding_idx on public.memories using hnsw (embedding vector_cosine_ops); + +drop function if exists public.search_memories(vector(1536), float, int, uuid, uuid); + +create or replace function public.search_memories( + query_embedding vector, + match_threshold float default 0.7, + match_count int default 10, + filter_project_id uuid default null, + filter_folder_id uuid default null, + required_dimensions int default null +) +returns table ( + id uuid, + content text, + project_id uuid, + folder_id uuid, + tags jsonb, + metadata jsonb, + similarity float, + created_at timestamptz, + updated_at timestamptz +) +language plpgsql +security definer +as $$ +declare + query_dimensions int; +begin + query_dimensions := array_length(query_embedding::float[], 1); + + return query + select + m.id, + m.content, + m.project_id, + m.folder_id, + m.tags, + m.metadata, + 1 - (m.embedding <=> query_embedding) as similarity, + m.created_at, + m.updated_at + from public.memories m + where m.user_id = auth.uid() + and m.embedding is not null + and m.embedding_dimensions = query_dimensions + and 1 - (m.embedding <=> query_embedding) > match_threshold + and (filter_project_id is null or m.project_id = filter_project_id) + and (filter_folder_id is null or m.folder_id = filter_folder_id) + order by m.embedding <=> query_embedding + limit match_count; +end; +$$; |