• Stars
    star
    1,249
  • Rank 37,414 (Top 0.8 %)
  • Language
    TypeScript
  • Created over 1 year ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Template for building your own custom ChatGPT style doc search powered by Next.js, OpenAI, and Supabase.

Next.js OpenAI Doc Search Starter

This starter takes all the .mdx files in the pages directory and processes them to use as custom context within OpenAI Text Completion prompts.

Deploy

Deploy this starter to Vercel. The Supabase integration will automatically set the required environment variables and configure your Database Schema. All you have to do is set your OPENAI_KEY and you're ready to go!

Deploy with Vercel

Technical Details

Building your own custom ChatGPT involves four steps:

  1. [👷 Build time] Pre-process the knowledge base (your .mdx files in your pages folder).
  2. [👷 Build time] Store embeddings in Postgres with pgvector.
  3. [🏃 Runtime] Perform vector similarity search to find the content that's relevant to the question.
  4. [🏃 Runtime] Inject content into OpenAI GPT-3 text completion prompt and stream response to the client.

👷 Build time

Step 1. and 2. happen at build time, e.g. when Vercel builds your Next.js app. During this time the generate-embeddings script is being executed which performs the following tasks:

sequenceDiagram
    participant Vercel
    participant DB (pgvector)
    participant OpenAI (API)
    loop 1. Pre-process the knowledge base
        Vercel->>Vercel: Chunk .mdx pages into sections
        loop 2. Create & store embeddings
            Vercel->>OpenAI (API): create embedding for page section
            OpenAI (API)->>Vercel: embedding vector(1536)
            Vercel->>DB (pgvector): store embedding for page section
        end
    end

In addition to storing the embeddings, this script generates a checksum for each of your .mdx files and stores this in another database table to make sure the embeddings are only regenerated when the file has changed.

🏃 Runtime

Step 3. and 4. happen at runtime, anytime the user submits a question. When this happens, the following sequence of tasks is performed:

sequenceDiagram
    participant Client
    participant Edge Function
    participant DB (pgvector)
    participant OpenAI (API)
    Client->>Edge Function: { query: lorem ispum }
    critical 3. Perform vector similarity search
        Edge Function->>OpenAI (API): create embedding for query
        OpenAI (API)->>Edge Function: embedding vector(1536)
        Edge Function->>DB (pgvector): vector similarity search
        DB (pgvector)->>Edge Function: relevant docs content
    end
    critical 4. Inject content into prompt
        Edge Function->>OpenAI (API): completion request prompt: query + relevant docs content
        OpenAI (API)-->>Client: text/event-stream: completions response
    end

The relevant files for this are the SearchDialog (Client) component and the vector-search (Edge Function).

The initialization of the database, including the setup of the pgvector extension is stored in the supabase/migrations folder which is automatically applied to your local Postgres instance when running supabase start.

Local Development

Configuration

  • cp .env.example .env
  • Set your OPENAI_KEY in the newly created .env file.
  • Set NEXT_PUBLIC_SUPABASE_ANON_KEY and SUPABASE_SERVICE_ROLE_KEY run:

    Note: You have to run supabase to retrieve the keys.

Start Supabase

Make sure you have Docker installed and running locally. Then run

supabase start

To retrieve NEXT_PUBLIC_SUPABASE_ANON_KEY and SUPABASE_SERVICE_ROLE_KEY run:

supabase status

Start the Next.js App

In a new terminal window, run

pnpm dev

Using your custom .mdx docs

  1. By default your documentation will need to be in .mdx format. This can be done by renaming existing (or compatible) markdown .md file.
  2. Run pnpm run embeddings to regenerate embeddings.

    Note: Make sure supabase is running. To check, run supabase status. If is not running run supabase start.

  3. Run pnpm dev again to refresh NextJS localhost:3000 rendered page.

Learn More

Video: How I Built Supabase’s OpenAI Doc Search

More Repositories

1

postgres_lsp

A Language Server for Postgres
Rust
3,188
star
2

copycat

Generate deterministic fake values: The same input will always generate the same fake-output.
TypeScript
734
star
3

supabase-csharp

A C# Client library for Supabase
C#
452
star
4

supabase-on-aws

Self-hosted Supabase on AWS
TypeScript
382
star
5

supabase-kt

A Kotlin Multiplatform Client for Supabase.
Kotlin
364
star
6

sql-examples

Curated list of SQL to help you find useful script easily 🚀
Vue
349
star
7

postgrest-rs

Rust client for PostgREST
Rust
328
star
8

chatgpt-your-files

Production-ready MVP for securely chatting with your documents using pgvector
TypeScript
277
star
9

supabase-custom-claims

How to implement custom claims with Supabase
JavaScript
254
star
10

snapshot

Capture a snapshot (or subset) of your Postgres database whilst transforming the data.
TypeScript
211
star
11

supabase-graphql-example

A HackerNews-like clone built with Supabase and pg_graphql
PLpgSQL
207
star
12

supabase-kubernetes

Helm 3 charts to deploy a Supabase on Kubernetes
Smarty
206
star
13

svelte-kanban

PLpgSQL
203
star
14

nuxt-supabase

A supa simple wrapper around Supabase.js to enable usage within Nuxt.
TypeScript
166
star
15

godot-engine.supabase

A lightweight addon which integrates Supabase APIs for Godot Engine out of the box.
GDScript
159
star
16

postgrest-go

Isomorphic Go client for PostgREST. (Now Updating)
Go
125
star
17

supabase-ui-svelte

Supabase authentication UI for Svelte
Svelte
120
star
18

postgrest-csharp

A C# Client library for Postgrest
C#
114
star
19

firebase-to-supabase

Firebase to Supabase Migration Guide
JavaScript
100
star
20

vue-supabase

A supa simple wrapper around Supabase.js to enable usage within Vue.
TypeScript
86
star
21

expo-stripe-payments-with-supabase-functions

Bring the Func(🕺)
TypeScript
79
star
22

svelte-supabase

JavaScript
71
star
23

realtime-csharp

A C# client library for supabase/realtime.
C#
69
star
24

partner-gallery-example

Supabase Partner Gallery Example
TypeScript
67
star
25

supabase-sveltekit-example

Svelte
65
star
26

supabase-flutter-quickstart

Flutter implementation of the Quickstart Supabase User Management app.
C++
61
star
27

deno-fresh-openai-doc-search

Template for building your own custom ChatGPT style doc search powered by Fresh, Deno, OpenAI, and Supabase.
TypeScript
60
star
28

supabase-traefik

Python
57
star
29

flutter-stripe-payments-with-supabase-functions

Dart
55
star
30

auth-py

Python client implementation for Supabase Auth
Python
54
star
31

supabase-terraform

HCL
53
star
32

postgrest-kt

Postgrest Kotlin Client
Kotlin
52
star
33

flutter-auth-ui

Supabase Auth UI library for Flutter
Dart
49
star
34

supabase-rb

An isomorphic Ruby client for Supabase.
Ruby
46
star
35

realtime-swift

A Swift client for Supabase Realtime server.
Swift
45
star
36

supabase-by-example

TypeScript
44
star
37

flutter-chat

Simple chat application built with Flutter and Supabase.
Dart
44
star
38

postgrest-swift

Swift client for PostgREST
Swift
43
star
39

gotrue-csharp

C# implementation of Supabase's GoTrue
C#
38
star
40

supabase-vscode-extension

Supabase Extension for VS Code and GitHub Copilot.
TypeScript
35
star
41

postgrest-rb

Isomorphic Ruby client for PostgREST.
Ruby
32
star
42

gotrue-swift

A Swift client library for GoTrue.
Swift
32
star
43

storage-go

Storage util client for Supabase in Go
Go
31
star
44

vec2pg

Migrate vector workloads to Postgres
Python
29
star
45

chatgpt-plugin-template-deno

Template for building ChatGPT Plugins in TypeScript that run on Supabase's custom Deno Edge Runtime.
TypeScript
27
star
46

storage-swift

Swift client library to interact with Supabase Storage
Swift
25
star
47

base64url-js

Pure TypeScript implementation of Base64-URL encoding for JavaScript strings.
TypeScript
22
star
48

gotrue-kt

Kotlin Client for GoTrue API
Kotlin
21
star
49

storage-csharp

A C# implementation of Supabase's Object Storage API
C#
19
star
50

postgrest-ex

Elixir Client library for PostgREST
Elixir
18
star
51

gotrue-ex

An Elixir client for the GoTrue authentication service
Elixir
16
star
52

heroku-to-supabase

Heroku to Supabase Migration Guide
16
star
53

sql-to-rest

SQL to PostgREST translator
TypeScript
15
star
54

nuxt3-quickstarter

CSS
13
star
55

gotrue-go

Typed Golang cilent for the Supabase fork of GoTrue
Go
12
star
56

sveltekit-subscription-payments

Clone, deploy, and fully customize a SaaS subscription application with SvelteKit.
TypeScript
11
star
57

supabase-php

PHP
11
star
58

pg_headerkit

A set of functions for adding special features to your application that uses PostgREST API calls to your PostgreSQL database.
PLpgSQL
11
star
59

functions-csharp

C# client for interacting with Supabase Functions
C#
11
star
60

gotrue-java

A Java client library for the GoTrue API.
Java
10
star
61

onesignal

Next.js app showcasing how you can implement push notification using OneSignal and Supabase
TypeScript
10
star
62

postgres-wasm-fdw

A WebAssembly foreign data wrapper example project
Rust
9
star
63

functions-swift

Swift Client library to interact with Supabase Functions.
Swift
8
star
64

supabase-adminpack

A Trusted Language Extension containing a variety of useful databse admin queries.
8
star
65

functions-go

Golang client library to interact with Supabase Functions.
Go
7
star
66

unboring.sg

A website and browser extensions to discover things to eat, do, and learn.
TypeScript
7
star
67

supabase-community-bot

TypeScript
5
star
68

ai-writer

An AI writing assistant powered by OpenAI, Supabase, and Next.js
TypeScript
5
star
69

functions-py

Python
5
star
70

storage-java

A Java client library for the Supabase Storage API
Java
4
star
71

supabase-management-js

Convenience wrapper for the Supabase Management API: https://supabase.com/docs/reference/api/introduction
TypeScript
4
star
72

supabase-go

Go
4
star
73

deno-supabase-js

Using supabase-js in Deno.
4
star
74

launchweek.dev

Tracking launch weeks across the industry, for product discovery and inspiration.
JavaScript
3
star
75

.github

Supabase Community
3
star
76

core-swift

Shared interfaces and helpers for Swift libraries
Swift
2
star
77

supa-jam

TypeScript
2
star
78

nftree-garden

Mint an NFT, plant a Tree!
JavaScript
1
star
79

realtime-go

1
star
80

functions-rs

Rust
1
star
81

squad

1
star
82

gotrue-php

PHP
1
star
83

storage-php

PHP
1
star
84

postgrest-php

PHP
1
star
85

deno-storage-js

Repo to overwrite https://deno.land/x/storagee listing.
1
star
86

supabase-fastify-api

TypeScript
1
star
87

gotrue-fsharp

F# client for interacting with Supabase GoTrue
F#
1
star
88

realtime-php

1
star
89

core-py

Shared interfaces and helpers for the supabase-py libs
Python
1
star
90

hacktoberfest-hackathon-template

Template for the Supabase Hacktoberfest Hackathon that follows https://hacktoberfest.digitalocean.com/resources/maintainers
1
star
91

postgrest-java

1
star