GithubHelp home page GithubHelp logo

chat-nova's Introduction


Logo

Kickstart Your Own ChatGPT Clone in Just 5 Minutes

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

React Next.js TypeScript Supabase Tailwind CSS Framer Motion

Thumbnail

Introduction

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.

Setup environment variables

  1. 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>
  2. Generate a new OpenAI API key: OpenAI Docs.

  3. Generate Supabase API keys, and service key: Supabase Docs.

Setup Supabase Database

Once you have successfully setup the environment variables, you can now setup the Supabase database. Follow these steps:

Users Table

  1. 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;
  2. 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;
    $$;
  3. 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();

Chats Table

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 )

  1. 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())
  2. 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())
  3. 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())
  4. 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())

Messages Table

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 )

  1. 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())
  2. 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())

Documents Table

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 )

  1. enable insert documents:

    CREATE POLICY "enable insert documents" ON "public"."documents"
    AS PERMISSIVE FOR INSERT
    TO public
    
    WITH CHECK (true)
  2. 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.

Run Locally

To run this project in your local development environment, follow these steps:

  1. Clone the repository to your local machine.

    [email protected]:Kyler18/chat-nova.git
  2. Open the cloned folder in your preferred code editor, install the required dependencies by running the following command in the terminal:

    npm install
  3. Start the development server by running the following command:

    npm run dev

You are now ready to go!

Contribute to this project

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

chat-nova's People

Contributors

wanderverse avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.