quivr/supabase/migrations/20240207034043_related.sql
Stan Girard 03c49693b7
feat(chunks): now chunk size is saved in database dynamically and not just 500 (#2164)
# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):
2024-02-06 23:23:37 -08:00

40 lines
1.2 KiB
PL/PgSQL

alter table "public"."brains" drop constraint "brains_prompt_id_fkey";
alter table "public"."chat_history" drop constraint "chat_history_prompt_id_fkey";
alter table "public"."brains" add constraint "brains_prompt_id_fkey" FOREIGN KEY (prompt_id) REFERENCES prompts(id) ON UPDATE CASCADE ON DELETE CASCADE not valid;
alter table "public"."brains" validate constraint "brains_prompt_id_fkey";
alter table "public"."chat_history" add constraint "chat_history_prompt_id_fkey" FOREIGN KEY (prompt_id) REFERENCES prompts(id) ON UPDATE CASCADE ON DELETE CASCADE not valid;
alter table "public"."chat_history" validate constraint "chat_history_prompt_id_fkey";
set check_function_bodies = off;
CREATE OR REPLACE FUNCTION public.match_brain(query_embedding vector, match_count integer, p_user_id uuid)
RETURNS TABLE(id uuid, name text, similarity double precision)
LANGUAGE plpgsql
AS $function$
BEGIN
RETURN QUERY
SELECT
b.brain_id,
b.name,
1 - (b.meaning <=> query_embedding) as similarity
FROM
brains b
LEFT JOIN
brains_users bu ON b.brain_id = bu.brain_id
WHERE
(bu.user_id = p_user_id AND bu.rights IN ('Owner', 'Editor', 'Viewer'))
ORDER BY
b.meaning <=> query_embedding
LIMIT
match_count;
END;
$function$
;