This is a repo that has newest vercel ai, chatGpt streaming and postgres features. The App has a quick response times because we are using vercel ai streaming configuration. In addition, I am persisting the messages using postgres.
This is a good starter kit, but please keep in mind that in order to make this a production level app, you need to make app more secure with authentication
To run the example locally you need to:
- Sign up at OpenAI's Developer Platform.
- Go to OpenAI's dashboard and create an API KEY.
- Set the required OpenAI environment variable as the token value as shown the example env file but in a new file called
.env.local
pnpm install
to install the required dependencies.pnpm dev
to launch the development server.
Using vercel.com, it is easy to deploy. Here are the main steps I took.
- Publish app on github.com
- Go on vercel.com, connect your github repo to vercel.
- Set up Postgres Storage by going to
Storage
Settings
- Setup Next.js Repo. You can use one of the templates likes this from vercel
- Add Open AI Key. You can generate a new API Key here
- Validate that the front-end works. Run
npm dev
and play with app locally - Setup github + deploy. Go to vercel.com and set up account and connect to the github repo you setup in step #1 . You can then deploy app with a few clicks.
- Make sure environment variables are setup in vercel.com. within vercel repo, go to
Settings
=>Environment Variables
and addOPENAI_API_KEY
key
Exta Credit (Postgres)
- Wihin vercel.com, navigate to your repo again and go to
Storage
& Setup a Postgres instance. Follow the instruction to connect the instance to your repo & pull the credentials down locally. If you don't have vercel installed, install globallynpm i --g vercel
- Add the necessary postgres code to fetch, update, etc. (I already added some basics)
To learn more about OpenAI, Next.js, and the Vercel AI SDK take a look at the following resources:
- Vercel AI SDK docs
- Vercel AI Playground
- OpenAI Documentation - learn about OpenAI features and API.
- Next.js Documentation - learn about Next.js features and API.