2023-06-11 22:11:43 +03:00
|
|
|
-- Create users table
|
2023-08-21 15:05:13 +03:00
|
|
|
CREATE TABLE IF NOT EXISTS user_daily_usage(
|
2023-06-28 20:39:27 +03:00
|
|
|
user_id UUID REFERENCES auth.users (id),
|
2023-06-11 22:11:43 +03:00
|
|
|
email TEXT,
|
|
|
|
date TEXT,
|
2023-08-21 15:05:13 +03:00
|
|
|
daily_requests_count INT,
|
2023-06-28 20:39:27 +03:00
|
|
|
PRIMARY KEY (user_id, date)
|
2023-06-11 22:11:43 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
-- Create chats table
|
|
|
|
CREATE TABLE IF NOT EXISTS chats(
|
|
|
|
chat_id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
|
2023-06-28 20:39:27 +03:00
|
|
|
user_id UUID REFERENCES auth.users (id),
|
2023-06-11 22:11:43 +03:00
|
|
|
creation_time TIMESTAMP DEFAULT current_timestamp,
|
|
|
|
history JSONB,
|
|
|
|
chat_name TEXT
|
|
|
|
);
|
|
|
|
|
2023-06-22 18:50:06 +03:00
|
|
|
|
2023-06-11 22:11:43 +03:00
|
|
|
-- Create vector extension
|
|
|
|
CREATE EXTENSION IF NOT EXISTS vector;
|
|
|
|
|
|
|
|
-- Create vectors table
|
|
|
|
CREATE TABLE IF NOT EXISTS vectors (
|
2023-07-12 13:44:34 +03:00
|
|
|
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
|
2023-06-11 22:11:43 +03:00
|
|
|
content TEXT,
|
2023-09-16 00:39:29 +03:00
|
|
|
file_sha1 TEXT,
|
2023-06-11 22:11:43 +03:00
|
|
|
metadata JSONB,
|
|
|
|
embedding VECTOR(1536)
|
|
|
|
);
|
|
|
|
|
|
|
|
-- Create function to match vectors
|
2023-06-28 20:39:27 +03:00
|
|
|
CREATE OR REPLACE FUNCTION match_vectors(query_embedding VECTOR(1536), match_count INT, p_brain_id UUID)
|
2023-06-11 22:11:43 +03:00
|
|
|
RETURNS TABLE(
|
2023-07-12 13:44:34 +03:00
|
|
|
id UUID,
|
2023-06-28 20:39:27 +03:00
|
|
|
brain_id UUID,
|
2023-06-11 22:11:43 +03:00
|
|
|
content TEXT,
|
|
|
|
metadata JSONB,
|
|
|
|
embedding VECTOR(1536),
|
|
|
|
similarity FLOAT
|
|
|
|
) LANGUAGE plpgsql AS $$
|
|
|
|
#variable_conflict use_column
|
|
|
|
BEGIN
|
|
|
|
RETURN QUERY
|
|
|
|
SELECT
|
2023-06-28 20:39:27 +03:00
|
|
|
vectors.id,
|
|
|
|
brains_vectors.brain_id,
|
|
|
|
vectors.content,
|
|
|
|
vectors.metadata,
|
|
|
|
vectors.embedding,
|
2023-06-11 22:11:43 +03:00
|
|
|
1 - (vectors.embedding <=> query_embedding) AS similarity
|
|
|
|
FROM
|
|
|
|
vectors
|
2023-06-28 20:39:27 +03:00
|
|
|
INNER JOIN
|
|
|
|
brains_vectors ON vectors.id = brains_vectors.vector_id
|
|
|
|
WHERE brains_vectors.brain_id = p_brain_id
|
2023-06-11 22:11:43 +03:00
|
|
|
ORDER BY
|
|
|
|
vectors.embedding <=> query_embedding
|
|
|
|
LIMIT match_count;
|
|
|
|
END;
|
|
|
|
$$;
|
|
|
|
|
|
|
|
-- Create stats table
|
2023-06-12 18:58:05 +03:00
|
|
|
CREATE TABLE IF NOT EXISTS stats (
|
2023-06-11 22:11:43 +03:00
|
|
|
time TIMESTAMP,
|
|
|
|
chat BOOLEAN,
|
|
|
|
embedding BOOLEAN,
|
|
|
|
details TEXT,
|
|
|
|
metadata JSONB,
|
|
|
|
id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY
|
|
|
|
);
|
|
|
|
|
|
|
|
-- Create summaries table
|
|
|
|
CREATE TABLE IF NOT EXISTS summaries (
|
|
|
|
id BIGSERIAL PRIMARY KEY,
|
2023-07-12 13:44:34 +03:00
|
|
|
document_id UUID REFERENCES vectors(id),
|
2023-06-11 22:11:43 +03:00
|
|
|
content TEXT,
|
|
|
|
metadata JSONB,
|
|
|
|
embedding VECTOR(1536)
|
|
|
|
);
|
|
|
|
|
|
|
|
-- Create function to match summaries
|
|
|
|
CREATE OR REPLACE FUNCTION match_summaries(query_embedding VECTOR(1536), match_count INT, match_threshold FLOAT)
|
|
|
|
RETURNS TABLE(
|
|
|
|
id BIGINT,
|
2023-07-12 13:44:34 +03:00
|
|
|
document_id UUID,
|
2023-06-11 22:11:43 +03:00
|
|
|
content TEXT,
|
|
|
|
metadata JSONB,
|
|
|
|
embedding VECTOR(1536),
|
|
|
|
similarity FLOAT
|
|
|
|
) LANGUAGE plpgsql AS $$
|
|
|
|
#variable_conflict use_column
|
|
|
|
BEGIN
|
|
|
|
RETURN QUERY
|
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
document_id,
|
|
|
|
content,
|
|
|
|
metadata,
|
|
|
|
embedding,
|
|
|
|
1 - (summaries.embedding <=> query_embedding) AS similarity
|
|
|
|
FROM
|
|
|
|
summaries
|
|
|
|
WHERE 1 - (summaries.embedding <=> query_embedding) > match_threshold
|
|
|
|
ORDER BY
|
|
|
|
summaries.embedding <=> query_embedding
|
|
|
|
LIMIT match_count;
|
|
|
|
END;
|
|
|
|
$$;
|
2023-06-14 22:21:13 +03:00
|
|
|
|
|
|
|
-- Create api_keys table
|
|
|
|
CREATE TABLE IF NOT EXISTS api_keys(
|
|
|
|
key_id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
2023-06-28 20:39:27 +03:00
|
|
|
user_id UUID REFERENCES auth.users (id),
|
2023-06-14 22:21:13 +03:00
|
|
|
api_key TEXT UNIQUE,
|
|
|
|
creation_time TIMESTAMP DEFAULT current_timestamp,
|
|
|
|
deleted_time TIMESTAMP,
|
|
|
|
is_active BOOLEAN DEFAULT true
|
|
|
|
);
|
|
|
|
|
2023-08-03 16:41:24 +03:00
|
|
|
--- Create prompts table
|
|
|
|
CREATE TABLE IF NOT EXISTS prompts (
|
|
|
|
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
|
|
|
|
title VARCHAR(255),
|
|
|
|
content TEXT,
|
|
|
|
status VARCHAR(255) DEFAULT 'private'
|
|
|
|
);
|
|
|
|
|
|
|
|
--- Create brains table
|
2023-07-25 16:22:17 +03:00
|
|
|
CREATE TABLE IF NOT EXISTS brains (
|
2023-06-17 00:36:53 +03:00
|
|
|
brain_id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
2023-07-25 16:22:17 +03:00
|
|
|
name TEXT NOT NULL,
|
2023-06-17 00:36:53 +03:00
|
|
|
status TEXT,
|
2023-07-25 16:22:17 +03:00
|
|
|
description TEXT,
|
2023-06-17 00:36:53 +03:00
|
|
|
model TEXT,
|
2023-07-25 16:22:17 +03:00
|
|
|
max_tokens INT,
|
|
|
|
temperature FLOAT,
|
2023-08-03 11:37:13 +03:00
|
|
|
openai_api_key TEXT,
|
|
|
|
prompt_id UUID REFERENCES prompts(id)
|
2023-06-17 00:36:53 +03:00
|
|
|
);
|
|
|
|
|
2023-08-03 11:37:13 +03:00
|
|
|
|
2023-08-10 11:25:08 +03:00
|
|
|
-- Create chat_history table
|
|
|
|
CREATE TABLE IF NOT EXISTS chat_history (
|
|
|
|
message_id UUID DEFAULT uuid_generate_v4(),
|
|
|
|
chat_id UUID REFERENCES chats(chat_id),
|
|
|
|
user_message TEXT,
|
|
|
|
assistant TEXT,
|
|
|
|
message_time TIMESTAMP DEFAULT current_timestamp,
|
|
|
|
PRIMARY KEY (chat_id, message_id),
|
|
|
|
prompt_id UUID REFERENCES prompts(id),
|
|
|
|
brain_id UUID REFERENCES brains(brain_id)
|
|
|
|
);
|
|
|
|
|
2023-09-07 14:22:06 +03:00
|
|
|
-- Create notification table
|
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS notifications (
|
|
|
|
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
|
|
|
datetime TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
|
|
chat_id UUID REFERENCES chats(chat_id),
|
|
|
|
message TEXT,
|
|
|
|
action VARCHAR(255) NOT NULL,
|
|
|
|
status VARCHAR(255) NOT NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2023-06-17 00:36:53 +03:00
|
|
|
-- Create brains X users table
|
|
|
|
CREATE TABLE IF NOT EXISTS brains_users (
|
|
|
|
brain_id UUID,
|
|
|
|
user_id UUID,
|
|
|
|
rights VARCHAR(255),
|
2023-06-28 20:39:27 +03:00
|
|
|
default_brain BOOLEAN DEFAULT false,
|
2023-06-17 00:36:53 +03:00
|
|
|
PRIMARY KEY (brain_id, user_id),
|
2023-06-28 20:39:27 +03:00
|
|
|
FOREIGN KEY (user_id) REFERENCES auth.users (id),
|
2023-07-12 13:44:34 +03:00
|
|
|
FOREIGN KEY (brain_id) REFERENCES brains (brain_id)
|
2023-06-17 00:36:53 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
-- Create brains X vectors table
|
|
|
|
CREATE TABLE IF NOT EXISTS brains_vectors (
|
|
|
|
brain_id UUID,
|
2023-07-12 13:44:34 +03:00
|
|
|
vector_id UUID,
|
2023-06-30 11:09:50 +03:00
|
|
|
file_sha1 TEXT,
|
2023-06-17 00:36:53 +03:00
|
|
|
PRIMARY KEY (brain_id, vector_id),
|
2023-06-28 20:39:27 +03:00
|
|
|
FOREIGN KEY (vector_id) REFERENCES vectors (id),
|
|
|
|
FOREIGN KEY (brain_id) REFERENCES brains (brain_id)
|
2023-06-17 00:36:53 +03:00
|
|
|
);
|
2023-07-03 12:11:29 +03:00
|
|
|
|
2023-07-11 19:20:31 +03:00
|
|
|
-- Create brains X vectors table
|
|
|
|
CREATE TABLE IF NOT EXISTS brain_subscription_invitations (
|
|
|
|
brain_id UUID,
|
|
|
|
email VARCHAR(255),
|
|
|
|
rights VARCHAR(255),
|
|
|
|
PRIMARY KEY (brain_id, email),
|
2023-07-12 13:44:34 +03:00
|
|
|
FOREIGN KEY (brain_id) REFERENCES brains (brain_id)
|
2023-07-11 19:20:31 +03:00
|
|
|
);
|
|
|
|
|
2023-08-01 10:24:57 +03:00
|
|
|
--- Create user_identity table
|
|
|
|
CREATE TABLE IF NOT EXISTS user_identity (
|
|
|
|
user_id UUID PRIMARY KEY,
|
|
|
|
openai_api_key VARCHAR(255)
|
|
|
|
);
|
|
|
|
|
2023-08-03 10:53:38 +03:00
|
|
|
|
2023-07-17 18:24:31 +03:00
|
|
|
CREATE OR REPLACE FUNCTION public.get_user_email_by_user_id(user_id uuid)
|
|
|
|
RETURNS TABLE (email text)
|
|
|
|
SECURITY definer
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
|
|
|
RETURN QUERY SELECT au.email::text FROM auth.users au WHERE au.id = user_id;
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
|
2023-07-18 15:30:19 +03:00
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION public.get_user_id_by_user_email(user_email text)
|
|
|
|
RETURNS TABLE (user_id uuid)
|
|
|
|
SECURITY DEFINER
|
|
|
|
AS $$
|
|
|
|
BEGIN
|
|
|
|
RETURN QUERY SELECT au.id::uuid FROM auth.users au WHERE au.email = user_email;
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-07-03 12:11:29 +03:00
|
|
|
CREATE TABLE IF NOT EXISTS migrations (
|
2023-07-03 15:41:33 +03:00
|
|
|
name VARCHAR(255) PRIMARY KEY,
|
2023-07-03 12:11:29 +03:00
|
|
|
executed_at TIMESTAMPTZ DEFAULT current_timestamp
|
|
|
|
);
|
|
|
|
|
2023-09-13 14:47:12 +03:00
|
|
|
CREATE TABLE IF NOT EXISTS user_settings (
|
|
|
|
user_id UUID PRIMARY KEY,
|
|
|
|
models JSONB DEFAULT '["gpt-3.5-turbo"]'::jsonb,
|
|
|
|
max_requests_number INT DEFAULT 50,
|
|
|
|
max_brains INT DEFAULT 5,
|
2023-09-20 18:43:46 +03:00
|
|
|
max_brain_size INT DEFAULT 10000000
|
2023-09-13 14:47:12 +03:00
|
|
|
);
|
|
|
|
|
2023-09-20 10:35:37 +03:00
|
|
|
-- knowledge table
|
|
|
|
CREATE TABLE IF NOT EXISTS knowledge (
|
|
|
|
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
|
|
|
file_name TEXT,
|
|
|
|
url TEXT,
|
|
|
|
brain_id UUID NOT NULL REFERENCES brains(brain_id),
|
|
|
|
extension TEXT NOT NULL,
|
|
|
|
CHECK ((file_name IS NOT NULL AND url IS NULL) OR (file_name IS NULL AND url IS NOT NULL))
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
-- knowledge_vectors table
|
|
|
|
CREATE TABLE IF NOT EXISTS knowledge_vectors (
|
|
|
|
knowledge_id UUID NOT NULL REFERENCES knowledge(id),
|
|
|
|
vector_id UUID NOT NULL REFERENCES vectors(id),
|
|
|
|
embedding_model TEXT NOT NULL,
|
|
|
|
PRIMARY KEY (knowledge_id, vector_id, embedding_model)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2023-09-14 12:56:59 +03:00
|
|
|
insert into
|
|
|
|
storage.buckets (id, name)
|
|
|
|
values
|
2023-09-14 17:45:15 +03:00
|
|
|
('quivr', 'quivr');
|
2023-09-14 12:56:59 +03:00
|
|
|
|
|
|
|
CREATE POLICY "Access Quivr Storage 1jccrwz_0" ON storage.objects FOR INSERT TO anon WITH CHECK (bucket_id = 'quivr');
|
|
|
|
|
|
|
|
CREATE POLICY "Access Quivr Storage 1jccrwz_1" ON storage.objects FOR SELECT TO anon USING (bucket_id = 'quivr');
|
|
|
|
|
|
|
|
CREATE POLICY "Access Quivr Storage 1jccrwz_2" ON storage.objects FOR UPDATE TO anon USING (bucket_id = 'quivr');
|
|
|
|
|
|
|
|
CREATE POLICY "Access Quivr Storage 1jccrwz_3" ON storage.objects FOR DELETE TO anon USING (bucket_id = 'quivr');
|
|
|
|
|
2023-07-03 12:11:29 +03:00
|
|
|
INSERT INTO migrations (name)
|
2023-09-20 10:35:37 +03:00
|
|
|
SELECT '202309151054032_add_knowledge_tables'
|
2023-07-03 12:11:29 +03:00
|
|
|
WHERE NOT EXISTS (
|
2023-09-20 10:35:37 +03:00
|
|
|
SELECT 1 FROM migrations WHERE name = '202309151054032_add_knowledge_tables'
|
2023-07-12 13:44:34 +03:00
|
|
|
);
|
2023-09-14 12:56:59 +03:00
|
|
|
|
|
|
|
|