0.83.0 • Published 4 months ago

mongodb-rag v0.83.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

MongoDB RAG Logo

MongoDB-RAG

NPM Version License Issues Pull Requests Downloads MongoDB-RAG

Overview

MongoDB-RAG (Retrieval Augmented Generation) is an NPM module that simplifies vector search using MongoDB Atlas. This library enables developers to efficiently perform similarity search, caching, batch processing, and indexing for fast and accurate retrieval of relevant data.

šŸš€ Features

  • Vector Search: Efficiently retrieves similar documents using MongoDB's Atlas Vector Search.
  • Dynamic Database & Collection Selection: Supports flexible selection of multiple databases and collections.
  • Batch Processing: Handles bulk processing of documents with retry mechanisms.
  • Index Management: Ensures necessary indexes are available and optimized.
  • Caching Mechanism: Provides in-memory caching for frequently accessed data.
  • Advanced Chunking: Supports sliding window, semantic, and recursive chunking strategies.
  • CLI for Scaffolding RAG Apps

šŸš€ Getting Started

1ļøāƒ£ Install the Package

npm install mongodb-rag dotenv

2ļøāƒ£ Set Up MongoDB Atlas

  1. Initialize Your App using the CLI:
    npx mongodb-rag init
    This will guide you through setting up your MongoDB connection and save the configuration to .mongodb-rag.json. Make sure to add .mongodb-rag.json to your .gitignore file to keep your credentials secure.
   % npx mongodb-rag init
āœ” Enter your MongoDB connection string: Ā· mongodb+srv://<username>:<password>@cluster0.mongodb.net/
āœ” Enter the database name: Ā· mongodb-rag
āœ” Enter the collection name: Ā· documents
āœ” Select an embedding provider: Ā· openai
āœ” Enter your API key (skip if using Ollama): Ā· your-embedding-api-key
āœ” Enter the model name: Ā· text-embedding-3-small
āœ” Enter the embedding dimensions: Ā· 1536
āœ… Configuration saved to .mongodb-rag.json

šŸ” Next steps:
1. Run `npx mongodb-rag test-connection` to verify your setup
2. Run `npx mongodb-rag create-index` to create your vector search index
  1. Create a MongoDB Atlas Cluster (MongoDB Atlas)

  2. Enable Vector Search under Indexes:

    {
      "definition": {
        "fields": [
          { "path": "embedding", "type": "vector", "numDimensions": 1536, "similarity": "cosine" }
        ]
      }
    }

    or, use the CLI to create the index:

    npx mongodb-rag create-index
  3. Create a .env File using:
    npx mongodb-rag create-env
    This command reads the .mongodb-rag.json file and generates a .env file with the necessary environment variables.

3ļøāƒ£ Quick Start with CLI

You can generate a fully working RAG-enabled app with MongoDB Atlas Vector Search using:

npx mongodb-rag create-rag-app my-rag-app

This will:

  • Scaffold a new CRUD RAG app with Express and MongoDB Atlas.
  • Set up environment variables for embedding providers.
  • Create API routes for ingestion, search, and deletion.

Then, navigate into your project and run:

cd my-rag-app
npm install
npm run dev

4ļøāƒ£ Initialize MongoRAG

import { MongoRAG } from 'mongodb-rag';
import dotenv from 'dotenv';
dotenv.config();

const rag = new MongoRAG({
    mongoUrl: process.env.MONGODB_URI,
    database: 'my_rag_db',  // Default database
    collection: 'documents', // Default collection
    embedding: {
        provider: process.env.EMBEDDING_PROVIDER,
        apiKey: process.env.EMBEDDING_API_KEY,
        model: process.env.EMBEDDING_MODEL,
        dimensions: 1536
    }
});
await rag.connect();

5ļøāƒ£ Ingest Documents

const documents = [
    { id: 'doc1', content: 'MongoDB is a NoSQL database.', metadata: { source: 'docs' } },
    { id: 'doc2', content: 'Vector search is useful for semantic search.', metadata: { source: 'ai' } }
];

await rag.ingestBatch(documents, { database: 'dynamic_db', collection: 'dynamic_docs' });
console.log('Documents ingested.');

6ļøāƒ£ Perform a Vector Search

const query = 'How does vector search work?';

const results = await rag.search(query, {
    database: 'dynamic_db',
    collection: 'dynamic_docs',
    maxResults: 3
});

console.log('Search Results:', results);

7ļøāƒ£ Close Connection

await rag.close();

⚔ Additional Features

šŸŒ Multi-Database & Collection Support

Store embeddings in multiple databases and collections dynamically.

await rag.ingestBatch(docs, { database: 'finance_db', collection: 'reports' });

šŸ”Ž Hybrid Search (Vector + Metadata Filtering)

const results = await rag.search('AI topics', {
    database: 'my_rag_db',
    collection: 'documents',
    maxResults: 5,
    filter: { 'metadata.source': 'ai' }
});

šŸ¤ Contributing

Contributions are welcome! Please fork the repository and submit a pull request.


šŸ“œ License

This project is licensed under the MIT License.

šŸ’” Examples

šŸ”— Links

0.83.0

4 months ago

0.82.0

4 months ago

0.81.0

4 months ago

0.80.0

4 months ago

0.79.0

4 months ago

0.78.0

4 months ago

0.77.0

4 months ago

0.76.0

4 months ago

0.75.0

4 months ago

0.74.0

4 months ago

0.73.0

4 months ago

0.72.0

4 months ago

0.71.0

4 months ago

0.70.5-alpha.9

4 months ago

0.70.5-alpha.8

4 months ago

0.70.5-alpha.7

4 months ago

0.70.5-alpha.6

4 months ago

0.70.5-alpha.5

4 months ago

0.70.5-alpha.4

4 months ago

0.70.5-alpha.3

4 months ago

0.70.4-alpha.0

4 months ago

0.70.3-alpha.0

4 months ago

0.70.1-alpha.0

4 months ago

0.70.0

4 months ago

0.69.0

4 months ago

0.68.0

4 months ago

0.67.0

4 months ago

0.66.0

4 months ago

0.65.0

4 months ago

0.64.0

4 months ago

0.63.0

4 months ago

0.62.0

4 months ago

0.61.0

4 months ago

0.60.0

4 months ago

0.59.0

4 months ago

0.58.0

4 months ago

0.57.0

4 months ago

0.56.0

4 months ago

0.55.0

4 months ago

0.54.0

4 months ago

0.53.0

4 months ago

0.52.0

4 months ago

0.51.0

4 months ago

0.50.0

4 months ago

0.49.0

4 months ago

0.48.0

4 months ago

0.47.0

4 months ago

0.46.0

4 months ago

0.45.0

4 months ago

0.44.0

4 months ago

0.43.0

4 months ago

0.42.0

4 months ago

0.41.0

4 months ago

0.40.0

4 months ago

0.39.0

4 months ago

0.38.0

4 months ago

0.37.0

4 months ago

0.36.0

4 months ago

0.35.0

4 months ago

0.34.0

4 months ago

0.33.0

4 months ago

0.32.0

4 months ago

0.31.0

4 months ago

0.30.0

4 months ago

0.29.0

4 months ago

0.28.0

4 months ago

0.27.0

4 months ago

0.26.0

4 months ago

0.25.0

4 months ago

0.24.0

4 months ago

0.23.0

4 months ago

0.22.0

4 months ago

0.21.0

4 months ago

0.20.0

4 months ago

0.19.0

4 months ago

0.18.0

4 months ago

0.17.0

4 months ago

0.16.0

4 months ago

0.15.0

4 months ago

0.13.0

4 months ago

0.12.0

4 months ago

0.11.0

4 months ago

0.10.0

4 months ago

0.9.0

4 months ago

0.8.0

4 months ago

0.7.0

4 months ago

0.6.0

4 months ago

0.5.0

4 months ago

0.4.0

4 months ago

0.3.0

4 months ago

0.2.0

4 months ago

0.1.0

4 months ago