|
| 1 | +# RAG Agent Demo — How to Create an AI Agent with Minimal Coding |
| 2 | + |
| 3 | +Companion project for the **"How to Create an AI Agent with Minimal Coding"** video. This demo builds a fully functional RAG (Retrieval-Augmented Generation) agent that answers questions about the MongoDB Brand Book using MongoDB Atlas Vector Search, Voyage AI embeddings, and the Vercel AI SDK's `ToolLoopAgent`. |
| 4 | + |
| 5 | +## What It Does |
| 6 | + |
| 7 | +A chat-based AI agent that: |
| 8 | + |
| 9 | +1. Receives a user question about MongoDB brand guidelines |
| 10 | +2. Autonomously decides whether to search the brand book |
| 11 | +3. Embeds the query with Voyage AI and runs a vector search on MongoDB Atlas |
| 12 | +4. Synthesizes a grounded answer from the retrieved documents |
| 13 | + |
| 14 | +The agent uses an **agentic loop** — it reasons, calls tools, inspects results, and repeats until it has enough context to respond. All in ~5 files of code. |
| 15 | + |
| 16 | +## Architecture |
| 17 | + |
| 18 | +```mermaid |
| 19 | +flowchart TD |
| 20 | + A[User] -->|Sends question| B[React Chat UI] |
| 21 | + B -->|POST /api/chat| C[Next.js API Route] |
| 22 | + C --> D[ToolLoopAgent] |
| 23 | + D -->|Reasoning step| E{Need more context?} |
| 24 | + E -->|Yes| F[searchDocumentation tool] |
| 25 | + F -->|Embed query| G[Voyage AI] |
| 26 | + G -->|Query vector| H[MongoDB Atlas Vector Search] |
| 27 | + H -->|Top 5 results| D |
| 28 | + E -->|No| I[Generate final response] |
| 29 | + I -->|Stream| B |
| 30 | +
|
| 31 | + style A fill:#00ed64,color:#000 |
| 32 | + style D fill:#00ed64,color:#000 |
| 33 | + style H fill:#00ed64,color:#000 |
| 34 | +``` |
| 35 | + |
| 36 | +## Tech Stack |
| 37 | + |
| 38 | +| Layer | Technology | |
| 39 | +|-------|-----------| |
| 40 | +| Frontend | React 19, Next.js 16 (App Router), Tailwind CSS v4 | |
| 41 | +| Agent Framework | Vercel AI SDK v6 (`ToolLoopAgent`) | |
| 42 | +| LLM | Google Gemini Flash | |
| 43 | +| Embeddings | Voyage AI (`voyage-4-large` for ingestion, `voyage-4-lite` for queries) | |
| 44 | +| Database | MongoDB Atlas with Vector Search | |
| 45 | +| Language | TypeScript | |
| 46 | + |
| 47 | +## Project Structure |
| 48 | + |
| 49 | +``` |
| 50 | +src/ |
| 51 | + app/ |
| 52 | + page.tsx # Chat UI (React client component) |
| 53 | + layout.tsx # Root layout, fonts, metadata |
| 54 | + globals.css # Tailwind theme (dark mode, MongoDB green) |
| 55 | + api/chat/route.ts # POST endpoint — streams agent responses |
| 56 | + lib/ |
| 57 | + agent.ts # ToolLoopAgent config (model, system prompt, tools) |
| 58 | + tools.ts # searchDocumentation tool (embeddings + vector search) |
| 59 | + mongodb.ts # MongoDB connection pooling |
| 60 | +scripts/ |
| 61 | + ingest.ts # Embeds and loads brand book chunks into MongoDB |
| 62 | +``` |
| 63 | + |
| 64 | +## Prerequisites |
| 65 | + |
| 66 | +- Node.js 18+ |
| 67 | +- A [MongoDB Atlas](https://www.mongodb.com/cloud/atlas) cluster |
| 68 | +- [Google AI Studio](https://aistudio.google.com/) API key (for Gemini Flash) |
| 69 | +- [Voyage AI](https://www.voyageai.com/) API key (for embeddings) |
| 70 | + |
| 71 | +## Setup |
| 72 | + |
| 73 | +### 1. Install dependencies |
| 74 | + |
| 75 | +```bash |
| 76 | +npm install |
| 77 | +``` |
| 78 | + |
| 79 | +### 2. Configure environment variables |
| 80 | + |
| 81 | +Create a `.env.local` file: |
| 82 | + |
| 83 | +``` |
| 84 | +MONGODB_URI=mongodb+srv://<user>:<password>@<cluster>.mongodb.net/ |
| 85 | +GOOGLE_GENERATIVE_AI_API_KEY=your-gemini-api-key |
| 86 | +VOYAGE_AI_API_KEY=your-voyage-ai-api-key |
| 87 | +``` |
| 88 | + |
| 89 | +### 3. Ingest the brand book data |
| 90 | + |
| 91 | +```bash |
| 92 | +npx ts-node scripts/ingest.ts |
| 93 | +``` |
| 94 | + |
| 95 | +This embeds 14 brand book sections with Voyage AI, inserts them into the `brand_demo.brand_book` collection, and automatically creates the Atlas Vector Search index. The index may take a minute to become ready after creation. |
| 96 | + |
| 97 | +### 4. Run the app |
| 98 | + |
| 99 | +```bash |
| 100 | +npm run dev |
| 101 | +``` |
| 102 | + |
| 103 | +Open [http://localhost:3000](http://localhost:3000). |
| 104 | + |
| 105 | +## How the Agent Works |
| 106 | + |
| 107 | +The core of the project is `src/lib/agent.ts` — a `ToolLoopAgent` that: |
| 108 | + |
| 109 | +- Uses **Gemini Flash** as the reasoning LLM |
| 110 | +- Has a single tool: `searchDocumentation` (vector search over the brand book) |
| 111 | +- Runs up to **10 steps** before stopping (safety limit) |
| 112 | +- **Streams** responses in real-time back to the UI |
| 113 | + |
| 114 | +When a user asks a brand-related question, the agent autonomously decides to call `searchDocumentation`, which: |
| 115 | + |
| 116 | +1. Embeds the query using Voyage AI (`voyage-4-lite` for fast query-time embedding) |
| 117 | +2. Runs a `$vectorSearch` aggregation on MongoDB Atlas (top 5 results, 15 candidates) |
| 118 | +3. Returns the matched sections with relevance scores |
| 119 | + |
| 120 | +The agent then uses the retrieved context to generate a grounded, accurate response. |
| 121 | + |
| 122 | +## License |
| 123 | + |
| 124 | +MIT |
0 commit comments