quivr/scripts/supabase_new_store_documents.sql
Stan Girard e0de23e24d
Feat/single brain (#215)
* feat(llm): update

* feat(singlebrain): added new table with user-id

* feat(user): get user from email

* feat(user_id): added search

*  add user_id to most endpoints

* docs(readme): new script

---------

Co-authored-by: gozineb <zinebe@theodo.fr>
2023-05-31 13:51:23 +02:00

41 lines
1.2 KiB
PL/PgSQL

create extension if not exists vector;
-- Create a table to store your documents
create table if not exists vectors (
id bigserial primary key,
user_id text, -- new column added here
content text, -- corresponds to Document.pageContent
metadata jsonb, -- corresponds to Document.metadata
embedding vector(1536) -- 1536 works for OpenAI embeddings, change if needed
);
CREATE OR REPLACE FUNCTION match_vectors(query_embedding vector(1536), match_count int, p_user_id text) -- user_id changed to p_user_id here
RETURNS TABLE(
id bigint,
user_id text, -- new column added here
content text,
metadata jsonb,
-- we return matched vectors to enable maximal marginal relevance searches
embedding vector(1536),
similarity float)
LANGUAGE plpgsql
AS $$
# variable_conflict use_column
BEGIN
RETURN query
SELECT
id,
user_id, -- new column added here
content,
metadata,
embedding,
1 -(vectors.embedding <=> query_embedding) AS similarity
FROM
vectors
WHERE vectors.user_id = p_user_id -- filter changed here
ORDER BY
vectors.embedding <=> query_embedding
LIMIT match_count;
END;
$$;