---
title: "RAG with Vercel AI SDK"
description: "A retrieval-augmented generation chatbot built with Next.js, the Vercel AI SDK, Drizzle ORM, and PostgreSQL."
url: "https://vercel.com/templates/next.js/ai-sdk-rag"
links:
  repository: "https://github.com/vercel-labs/ai-sdk-preview-rag"
  demo: "https://ai-sdk-preview-rag.vercel.app"
  deploy: "https://vercel.com/new/clone?demo-title=RAG+with+Vercel+AI+SDK&demo-description=A+retrieval-augmented+generation+chatbot+built+with+Next.js%2C+the+Vercel+AI+SDK%2C+Drizzle+ORM%2C+and+PostgreSQL.&demo-url=https%3A%2F%2Fai-sdk-preview-rag.vercel.app&demo-image=%2F%2Fimages.ctfassets.net%2Fe5382hct74si%2F2BpbyD7YKkx7hSJw1pvjWv%2Fb64716fadf2c1e8b0baee59d8a70fac1%2FUntitled_design__5_.png&project-name=RAG+with+Vercel+AI+SDK&repository-name=ai-sdk-rag&repository-url=https%3A%2F%2Fgithub.com%2Fvercel-labs%2Fai-sdk-preview-rag&from=templates&skippable-integrations=1&env=OPENAI_API_KEY&envDescription=OpenAI+API+Key+Required"
---

# RAG with Vercel AI SDK

A retrieval-augmented generation chatbot built with Next.js, the Vercel AI SDK, Drizzle ORM, and PostgreSQL.

# AI SDK RAG Template

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fnicoalbanese%2Fai-sdk-rag-template&env=OPENAI_API_KEY&envDescription=You%20will%20need%20an%20OPENAI%20API%20Key.&project-name=ai-sdk-rag&repository-name=ai-sdk-rag&stores=%5B%7B%22type%22%3A%22postgres%22%7D%5D&skippable-integrations=1)

A [Next.js](https://nextjs.org/) application, powered by the Vercel AI SDK, that uses retrieval-augmented generation (RAG) to reason and respond with information outside of the model's training data.

## Features

- Information retrieval and addition through tool calls using the [`streamText`](https://ai-sdk.dev/docs/reference/ai-sdk-core/stream-text) function
- Real-time streaming of model responses to the frontend using the [`useChat`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat) hook
- Vector embedding storage with [DrizzleORM](https://orm.drizzle.team/) and [PostgreSQL](https://www.postgresql.org/)
- Animated UI with [Framer Motion](https://www.framer.com/motion/)

## Getting Started

To get the project up and running, follow these steps:

1. Install dependencies:

   ```bash
   npm install
   ```

2. Copy the example environment file:

   ```bash
   cp .env.example .env
   ```

3. Add your Vercel AI Gateway API key and PostgreSQL connection string to the `.env` file:

   ```
   AI_GATEWAY_API_KEY=your_api_key_here
   DATABASE_URL=your_postgres_connection_string_here
   ```

4. Migrate the database schema:

   ```bash
   npm run db:migrate
   ```

5. Start the development server:
   ```bash
   npm run dev
   ```

Your project should now be running on [http://localhost:3000](http://localhost:3000).

## Related Templates

### [Dynamic Model Usage with AI SDK](https://vercel.com/templates/next.js/ai-sdk-feature-flags-edge-config)

A chatbot that allows you to dynamically set the LLM using Vercel AI SDK with Feature Flags and Edge Config

### [Chatbot](https://vercel.com/templates/next.js/chatbot)

A full-featured, hackable Next.js AI chatbot built by Vercel

---

## Additional documentation

The [Vercel Knowledge Base](https://vercel.com/kb) contains guides and answers to common questions about Vercel, including deployment, framework configuration, domains, caching, and troubleshooting.
