A TypeScript implementation of a Retrieval-Augmented Generation (RAG) module using node-llama-cpp for embeddings and SQLite with vector extensions for efficient similarity search.
- Text embedding generation using LLaMA-based models
- Vector storage and retrieval with SQLite
- Efficient similarity search for semantic queries
- Simple API for saving and querying documents
- Built with TypeScript for type safety
rag-module/
├── src/
│ ├── rag.ts # Core RAG functionality
│ ├── embed.ts # Text embedding utilities
│ ├── db.ts # Database connection and setup
│ ├── test-rag.ts # Integration tests for RAG
│ ├── test-embed.ts # Tests for embedding functionality
│ └── test-db.ts # Database setup tests
├── model/ # Directory for model files (not included in repo)
├── dist/ # Compiled JavaScript output
└── rag.db # SQLite database file (created at runtime)
- Node.js (v16 or later)
- npm or yarn
- SQLite3 development files
-
Clone the repository:
git clone <repository-url> cd rag-module
-
Install dependencies:
npm install
-
Download the embedding model:
- Create a
modeldirectory in the project root - Download the
nomic-embed-text-v1.5.Q5_K_M.ggufmodel file into themodeldirectory - Or modify
embed.tsto point to your preferred model
- Create a
-
Build the project:
npm run build
import { initEmbedder } from './embed.js';
import { loadVectorExtension } from './db.js';
import { Embed, Save, Search } from './rag.js';
async function example() {
// Initialize the embedding model and database
await initEmbedder();
await loadVectorExtension();
// Generate an embedding
const vector = await Embed('Hello world');
console.log('Embedding:', vector.slice(0, 5), '...');
// Save a document
await Save(1, 'Banana is yellow', 'fruits');
await Save(2, 'Apple is red and crunchy', 'fruits');
await Save(3, 'Orange is citrus and orange colored', 'fruits');
// Search for similar documents
const results = await Search('yellow fruit', 'fruits', 2);
console.log('Search results:', results);
}
example().catch(console.error);Generates an embedding vector for the given text.
Saves a text document with its embedding to the specified table.
Searches for documents similar to the query and returns matching document IDs.
-
Test the embedding functionality:
npm run test-embed
-
Test database operations:
npm run test-db
-
Test the complete RAG pipeline:
npm run test-rag
- Uses
node-llama-cppfor generating text embeddings - Supports any LLaMA-compatible model (default: nomic-embed-text-v1.5)
- Handles model loading and inference
- SQLite with vector extension for efficient similarity search
- Stores document text alongside their vector embeddings
- Uses
sqlite-vecfor vector operations
- Implements cosine similarity for finding similar documents
- Returns results ordered by relevance
- Supports configurable result limits
ISC
Contributions are welcome! Please open an issue or submit a pull request.
- node-llama-cpp - For efficient LLM inference
- SQLite - For embedded database storage
- sqlite-vec - For vector similarity search