constitutionGPT icon indicating copy to clipboard operation
constitutionGPT copied to clipboard

AI chat over the US Constitution πŸ“œ πŸ’¬ πŸ‡ΊπŸ‡Έ


Logo Logo

Constitution GPT

AI chat over the US Constitution πŸ“œ πŸ’¬ πŸ‡ΊπŸ‡Έ

Website Github


Table of Contents

    πŸ“ About
πŸ’» How to build
  • Initial setup
  • Embeddings backend
  • Chat frontend
  • Run app
πŸš€ Next steps
πŸ”§ Tools used
πŸ‘€ Contact

πŸ“About

Chat with the US Constitution. Combines pgvector embeddings (Supabase), OpenAI, and NextJs to provide frontend UI chat interface.


πŸ’»How to Build

Initial setup

Clone and install dependencies:

git clone https://github.com/vdutts7/constitutionGPT
cd constitutionGPT
npm i

Copy .env.example and rename to .env in root directory. Fill out API keys:

SUPABASE_ANON_KEY=""
NEXT_PUBLIC_SUPABASE_URL=""
OPENAI_API_KEY=""

#optional- leave blank if unused. DO NOT DELETE
OPENAI_PROXY=""
SPLASH_URL=""

Get API keys:

IMPORTANT: Verify that .gitignore contains .env in it.

Embeddings backend

Create a Supabase account and project at Supabase:

  • Run this query Supabase's SQL editor:
    create extension vector;
    
  • Create a table to store embeddings with this query:
    create table documents (
      id bigserial primary key,
      content text,
      url text,
      embedding vector (1536)
    );
    
  • Add similarity search function with another query:
    create or replace function match_documents (
      query_embedding vector(1536),
      similarity_threshold float,
      match_count int
    )
    returns table (
      id bigint,
      content text,
      url text,
      similarity float
    )
    language plpgsql
    as $$
    begin
      return query
      select
        documents.id,
        documents.content,
        documents.url,
        1 - (documents.embedding <=> query_embedding) as similarity
      from documents
      where 1 - (documents.embedding <=> query_embedding) > similarity_threshold
      order by documents.embedding <=> query_embedding
      limit match_count;
    end;
    $$;
    

Chat frontend

  • NextJs styled with Tailwind CSS
  • Chats streamed using OpenAIStream. See utils/OpenAIStream.ts for details

πŸš€Next Steps

Use this project as a foundation and build on top of this!

πŸ’‘ Ideas πŸ’‘

  • Entire history of SCOTUS cases?
  • Specific data for each US president + administration?
  • Patriot Act? Chips Act?
  • CIA, DEA, FDA, CDC, etc. + all the confusing docs on the .gov websites

πŸ”§Tools Used

Next OpenAI Supabase Tailwind CSS Vercel

πŸ‘€Contact

Email Twitter