Skip to content

Commit 438f7ce

Browse files
authored
Merge pull request #187 from mongodb-developer/minimal-ts-agent
Adding project minimal-ts-agent
2 parents 2c71678 + 1fdc1b4 commit 438f7ce

23 files changed

+7881
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Google Gemini (LLM)
2+
GOOGLE_GENERATIVE_AI_API_KEY=<your-google-generative-ai-api-key-here>
3+
4+
# Voyage AI (Embeddings)
5+
VOYAGE_AI_API_KEY=<your-voyage-ai-api-key-here>
6+
7+
# MongoDB Atlas
8+
MONGODB_URI=<your-mongodb-uri-here>

apps/minimal-ts-agent/.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# dependencies
2+
node_modules/
3+
4+
# next.js
5+
.next/
6+
out/
7+
8+
# production
9+
build/
10+
11+
# env files (keep .example)
12+
.env
13+
.env.local
14+
.env.*.local
15+
16+
# debug
17+
npm-debug.log*
18+
19+
# typescript
20+
*.tsbuildinfo
21+
next-env.d.ts

apps/minimal-ts-agent/README.md

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { defineConfig, globalIgnores } from "eslint/config";
2+
import nextVitals from "eslint-config-next/core-web-vitals";
3+
import nextTs from "eslint-config-next/typescript";
4+
5+
const eslintConfig = defineConfig([
6+
...nextVitals,
7+
...nextTs,
8+
// Override default ignores of eslint-config-next.
9+
globalIgnores([
10+
// Default ignores of eslint-config-next:
11+
".next/**",
12+
"out/**",
13+
"build/**",
14+
"next-env.d.ts",
15+
]),
16+
]);
17+
18+
export default eslintConfig;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { NextConfig } from "next";
2+
3+
const nextConfig: NextConfig = {
4+
/* config options here */
5+
};
6+
7+
export default nextConfig;

0 commit comments

Comments
 (0)