Chat Nova will help you to build a custom AI Chatbot with full authentication and database management flow in a just few minutes
What is Chat Nova »
View Demo
·
Request Feature
·
Report Bug
Chat Nova is a full-stack chatbot that allows you to build a custom AI chatbot with full authentication and database management flow in a just few minutes. It is built with React, Next.js, TypeScript, Supabase, Tailwind CSS, and Framer Motion.
-
Create
env.local
file inside the root directory of the project, and add the following environment variables:OPENAI_API_KEY= <YOUR API KEY HERE> NEXT_PUBLIC_SUPABASE_URL= <SUPABASE URL> NEXT_PUBLIC_SUPABASE_ANON_KEY= <ANON KEY> SUPABASE_SERVICE_KEY= <SERVICE KEY>
-
Generate a new OpenAI API key: OpenAI Docs.
-
Generate Supabase API keys, and service key: Supabase Docs.
Once you have successfully setup the environment variables, you can now setup the Supabase database. Follow these steps:
-
Open Supabase SQL Editor, and run the following SQL query to create the users table:
create table public.users ( id uuid not null, created_at timestamp with time zone not null default now(), email text not null, avatar_url text null, role text not null default 'user'::text, constraint users_pkey primary key (id), constraint users_id_fkey foreign key (id) references auth.users (id) on update cascade on delete cascade ) tablespace pg_default;
-
Create PostgreSQL Function to create user:
create function public.create_profile_for_user() returns trigger language plpgsql security definer set search_path = public as $$ begin insert into public.users (id, email, avatar_url) values ( new.id, new.raw_user_meta_data->'email', new.raw_user_meta_data->'avatar_url' ); return new; end; $$;
-
Create PostgreSQL Trigger to create profile:
create trigger on_auth_user_created after insert on auth.users for each row execute procedure public.create_profile_for_user();
Open Supabase SQL Editor, and run the following SQL query to create the chats table:
create table
public.chats (
id uuid not null default gen_random_uuid (),
created_at timestamp with time zone not null default now(),
user_id uuid not null,
title text null,
constraint chats_pkey primary key (id),
constraint chats_user_id_fkey foreign key (user_id) references users (id) on update cascade on delete cascade
) tablespace pg_default;
Once you create the chats table, you need to create the necessary RLS policies for this table. ( What is RLS? Supabase Docs )
-
authenticated users can create new chats:
CREATE POLICY "authenticated users can create new chats" ON "public"."chats" AS PERMISSIVE FOR INSERT TO authenticated WITH CHECK (user_id = auth.uid())
-
authenticated users can delete their own chat:
CREATE POLICY "authenticated users can delete their own chat" ON "public"."chats" AS PERMISSIVE FOR DELETE TO authenticated USING (user_id = auth.uid())
-
authenticated users can update their own chats:
CREATE POLICY "authenticated users can update their own chats" ON "public"."chats" AS PERMISSIVE FOR UPDATE TO authenticated USING (user_id = auth.uid()) WITH CHECK (user_id = auth.uid())
-
authenticated users can view their own chats:
CREATE POLICY "authenticated users can view their own chats" ON "public"."chats" AS PERMISSIVE FOR SELECT TO authenticated USING (user_id = auth.uid())
Open Supabase SQL Editor, and run the following SQL query to create the messages table:
create table
public.messages (
id uuid not null default gen_random_uuid (),
created_at timestamp with time zone not null default now(),
role text not null,
content text not null,
chat_id uuid not null,
user_id uuid not null,
constraint messages_pkey primary key (id),
constraint messages_chat_id_fkey foreign key (chat_id) references chats (id) on update cascade on delete cascade,
constraint messages_user_id_fkey foreign key (user_id) references users (id) on update cascade on delete cascade
) tablespace pg_default;
Once you create the messages table, you need to create the necessary RLS policies for this table. ( What is RLS? Supabase Docs )
-
enable insert messages for authenticated users only:
CREATE POLICY "enable insert for authenticated users only" ON "public"."messages" AS PERMISSIVE FOR INSERT TO authenticated WITH CHECK (user_id = auth.uid())
-
users can view their own messages:
CREATE POLICY "users can view their own messages" ON "public"."messages" AS PERMISSIVE FOR SELECT TO authenticated USING (user_id = auth.uid())
Open Supabase SQL Editor, and run the following SQL query to create the vector
extenstion and the documents table:
-- Create the 'vector' extension
create extension if not exists vector;
-- Create a function to search for documents
create function match_documents (
query_embedding vector (1536),
match_count int default null,
filter jsonb default '{}'
) returns table (
id bigint,
content text,
metadata jsonb,
embedding jsonb,
similarity float
) language plpgsql as $$
#variable_conflict use_column
begin
return query
select
id,
content,
metadata,
(embedding::text)::jsonb as embedding,
1 - (documents.embedding <=> query_embedding) as similarity
from documents
where metadata @> filter
order by documents.embedding <=> query_embedding
limit match_count;
end;
$$;
Once you create the documents table, you need to create the necessary RLS policies for this table. ( What is RLS? Supabase Docs )
-
enable insert documents:
CREATE POLICY "enable insert documents" ON "public"."documents" AS PERMISSIVE FOR INSERT TO public WITH CHECK (true)
-
enable read access for all users:
CREATE POLICY "enable read access for all users" ON "public"."documents" AS PERMISSIVE FOR SELECT TO public USING (true)
Now you have successfully setup the Supabase database. You can now run the project locally.
To run this project in your local development environment, follow these steps:
-
Clone the repository to your local machine.
git@github.com:Kyler18/chat-nova.git
-
Open the cloned folder in your preferred code editor, install the required dependencies by running the following command in the terminal:
npm install
-
Start the development server by running the following command:
npm run dev
You are now ready to go!
Thank you for browsing this repo. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request