Live Demo (deployed on fly.io)
![Screen Shot 2023-06-20 at 1 30 56 PM](https://private-user-images.githubusercontent.com/3489963/247438724-bcc762d2-68f5-4c4e-8c49-14602bee4995.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ3MjYwNTcsIm5iZiI6MTcyNDcyNTc1NywicGF0aCI6Ii8zNDg5OTYzLzI0NzQzODcyNC1iY2M3NjJkMi02OGY1LTRjNGUtOGM0OS0xNDYwMmJlZTQ5OTUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjdUMDIyOTE3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MTE1MjVhNjI0MmYzNzA0OGQzMzc0NjE5NWU1Y2U2OTNkOGVhNGVhMjdmYmM5ZTQ3NGMwZGMxYWVjYjk3YWUzNSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.m42hM7aejqHAy5J3_1Qk0IT1idNhe_TsUYEYrpEKppg)
- Auth: Clerk
- App logic: Next.js
- VectorDB: Pinecone / Supabase pgvector
- LLM Orchestration: Langchain.js
- Image Model: Replicate
- Text Model: OpenAI
- Text streaming: ai sdk
- Deployment: Fly
The simplest way to try out this stack is to test it out locally and traverse through code files to understand how each component work. Here are the steps to get started.
Fork the repo to your Github account, then run the following command to clone the repo:
git clone git@github.com:[YOUR_GITHUB_ACCOUNT_NAME]/ai-getting-started.git
cd ai-getting-started
npm install
cp .env.local.example .env.local
a. Clerk Secrets
Go to https://dashboard.clerk.com/ -> "Add Application" -> Fill in Application name/select how your users should sign in -> Create Application
Now you should see both NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
and CLERK_SECRET_KEY
on the screen
b. OpenAI API key
Visit https://platform.openai.com/account/api-keys to get your OpenAI API key
c. Replicate API key
Visit https://replicate.com/account/api-tokens to get your Replicate API key
NOTE: By default, this template uses Pinecone as vector store, but you can turn on Supabase pgvector easily. This means you only need to fill out either Pinecone API key or Supabase API key.
d. Pinecone API key
- Create a Pinecone index by visiting https://app.pinecone.io/ and click on "Create Index"
- Give it an index name (this will be the environment variable
PINECONE_INDEX
) - Fill in Dimension as
1536
- Once the index is successfully created, click on "API Keys" on the left side nav and create an API key: copy "Environment" value to
PINECONE_ENVIRONMENT
variable, and "Value" toPINECONE_API_KEY
e. Supabase API key
- Create a Supabase instance here; then go to Project Settings -> API
SUPABASE_URL
is the URL value under "Project URL"SUPABASE_PRIVATE_KEY
is the key starts withey
under Project API Keys- Now, you should enable pgvector on Supabase and create a schema. You can do this easily by clicking on "SQL editor" on the left hand side on supabase UI and then clicking on "+New Query". Copy paste this code snippet in the SQL editor and click "Run".
There are a few markdown files under /blogs
directory as examples so you can do Q&A on them. To generate embeddings and store them in the vector database for future queries, you can run the following command:
Run the following command to generate embeddings and store them in Pinecone:
npm run generate-embeddings-pinecone
In QAModel.tsx
, replace /api/qa-pinecone
with /api/qa-pg-vector
. Then run the following command to generate embeddings and store them in Supabase pgvector:
npm run generate-embeddings-supabase
Now you are ready to test out the app locally! To do this, simply run npm run dev
under the project root.
- Register an account on fly.io and then install flyctl
- Run
fly launch
under project root -- this will generate afly.toml
that includes all the configurations you will need - Run
fly deploy -ha=false
to deploy the app -- the -ha flag makes sure fly only spins up one instance, which is included in the free plan. You also want to runfly scale memory 512
to scale up the fly vm memory for this app. - For any other non-localhost environment, the existing Clerk development instance should continue to work. You can upload the secrets to Fly by running
cat .env.local | fly secrets import
- If you are ready to deploy to production, you should create a prod environment under the current Clerk instance. For more details on deploying a production app with Clerk, check out their documentation here. Note that you will likely need to manage your own domain and do domain verification as part of the process.
- Create a new file
.env.prod
locally and fill in all the production-environment secrets. Remember to updateNEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
andCLERK_SECRET_KEY
by copying secrets from Clerk's production instance -cat .env.prod | fly secrets import
to upload secrets
You can fork this repo, make changes, and create a PR. Add @ykhli or @timqian as reviewers.
If you are new to contributing on github, here is a step-by-step guide:
- Clcik on
Fork
on the top right of this page - Work on your change and push it to your forked repo. Now when you navigate to the forked repo's UI, you should see something like the following:
![pr-preview](https://private-user-images.githubusercontent.com/3489963/247439582-631e5f45-39ec-4b54-b9d1-b963e279dcc6.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ3MjYwNTcsIm5iZiI6MTcyNDcyNTc1NywicGF0aCI6Ii8zNDg5OTYzLzI0NzQzOTU4Mi02MzFlNWY0NS0zOWVjLTRiNTQtYjlkMS1iOTYzZTI3OWRjYzYucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjdUMDIyOTE3WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9OGU2MTk3NGQxODJiZDA1ZTQ4YmFhMDA0ZDZjOTNjNTgxODU5NmFjYTM5OTRkYzExMjE3MjM3OTI1ZTJiOTdiMSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.1XskEJ49mekEXi2nGoZHj8P687IeuViSg6-A124HJi4)
- Click on "Contribute" -> "Open Pull Request".
- Once you have a PR, you can add reviewers.
Feel free to open feature requests, bug reports etc under Issues.