Full Retrieval-Augmented Generation pipeline using the AI Gateway and MongoDB Atlas Vector Search.
Markdown
Docs
ingest.ts
Chunk & > AI Gateway
Embed < (embeddings)
MongoDB
Atlas
(vectors)
search.ts / chat.ts
Query -> > AI Gateway
Retrieve -> < (LLM chat)
Generate
| Script | Description |
|---|---|
pnpm ingest |
Load sample docs, chunk, embed, store in MongoDB |
pnpm search |
Vector similarity search (pnpm search "your query") |
pnpm chat |
Full RAG chat (pnpm chat "your question") |
- MongoDB Atlas cluster with vector search enabled
- AI Gateway running with an embedding model and a chat model
- Node.js 18+
Create a vector search index on your collection:
{
"fields": [
{
"type": "vector",
"path": "embedding",
"numDimensions": 1024,
"similarity": "cosine"
}
]
}Name the index vector_index. Adjust numDimensions to match your embedding model.
cp .env.example .env
# Edit .env with your MongoDB URI, gateway URL, and model namespnpm install
# Step 1: Ingest sample documents
pnpm ingest
# Step 2: Search
pnpm search "What pricing plans are available?"
# Step 3: Full RAG chat
pnpm chat "How do I handle rate limit errors?"The sample-data/ directory contains fake product documentation for "AcmeCloud":
product-overview.md- Features and pricinggetting-started.md- Installation and quick starttroubleshooting.md- Common issues and solutions
Replace these with your own documents for real use cases.
| Variable | Description | Default |
|---|---|---|
GATEWAY_URL |
AI Gateway URL | http://localhost:3000 |
GATEWAY_API_KEY |
Gateway API key | - |
MONGODB_URI |
MongoDB connection string | mongodb://localhost:27017 |
MONGODB_DATABASE |
Database name | rag_demo |
MONGODB_COLLECTION |
Collection name | documents |
EMBEDDING_MODEL |
Model for embeddings | bedrock/titan-embed-v2 |
CHAT_MODEL |
Model for chat generation | bedrock/claude-3-sonnet |