Next.js
GitHub Repository
You can find the project source code on GitHub.
This guide provides detailed, step-by-step instructions on how to use Upstash RAG Chat with Next.js. You can also explore our Next.js Chat to Website example for detailed, end-to-end examples and best practices.
Project Setup
Create a new Next.js application and install @upstash/rag-chat
package.
Setup Upstash Redis
Create a Redis database using Upstash Console or Upstash CLI and copy the UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
into your .env
file.
Setup Upstash Vector
Create a Vector index using Upstash Console or Upstash CLI and copy the UPSTASH_VECTOR_REST_URL
and UPSTASH_VECTOR_REST_TOKEN
into your .env
file.
Setup QStash LLM
Navigate to QStash Console and copy the QSTASH_TOKEN
into your .env
file.
Configure Middleware for Cookies
Create a middleware in /app/middleware.ts
to generate a session ID cookie. This cookie will be used to track the user’s chat session.
Create a Chat Streaming API Route
We will create a RAG Chat instance in /app/lib/rag-chat.ts
to later use in our API route and Home page.
Using the RAG Chat instance we created, we can now create a chat streaming API route in /app/api/chat-stream/route.ts
.
Create a Chat Component
We can now create a chat component in /app/components/chat.tsx
using the chat streaming API route we created.
Update Home Page
Update /app/page.tsx
using the Chat component we created.
Run & Deploy
Run the app locally with npm run dev
, check http://localhost:3000/
Deploy your app with vercel
Was this page helpful?