1.4.1 • Published 4 months ago

rag-module v1.4.1

Weekly downloads
-
License
ISC
Repository
-
Last release
4 months ago

RAG Module Documentation

A TypeScript/JavaScript module for implementing Retrieval-Augmented Generation (RAG) using Qdrant vector database, Google's Generative AI embeddings, and Groq LLM.

Github Repo

Check out the Github Repository Link for this module here.

Features

  • Query classification using LLM
  • Vector storage and retrieval using Qdrant
  • Text embeddings using Google's Generative AI
  • Customizable prompts for classification and responses
  • Automatic text chunking and vector store creation
  • TypeScript support with full type definitions

Installation

npm install rag-module @qdrant/js-client-rest @langchain/google-genai groq-sdk langchain

Prerequisites

You'll need the following API keys:

  • Google API key for embeddings, Get it from here
  • Qdrant API key and URL for vector storage, Get it from your Database's Clusters Api Keys section.
  • Groq API key for LLM capabilities, Get it from here

Basic Usage

import { createRAG, RAGConfig } from 'rag-module';

const config: RAGConfig = {
    googleApiKey: 'your-google-api-key',
    qdrantUrl: 'your-qdrant-url',
    qdrantApiKey: 'your-qdrant-api-key',
    groqApiKey: 'your-groq-api-key',
    collectionName: 'custom-collection',
    fallbackResponse: 'Sorry, I could not find relevant information.'
};

const rag = createRAG(config);

async function main() {
    try {
        const result = await rag.processQuery(
            "Your context text here...",
            "Your query here..."
        );
        console.log('Response:', result.response);
        console.log('Category:', result.category);
    } catch (error) {
        console.error('Error:', error);
    }
}

Advanced Configuration

Custom Prompts

You can customize the classification and response prompts:

const config: RAGConfig = {
    // ... other config options
    prompts: {
        classification: `Analyze this query and categorize it as either 'technical', 'historical', 
                        or 'general': {query}. Reply with just one word.`,
        response: `Based on this context: '{context}', please answer this question: '{query}'.
                  Include relevant quotes when appropriate.`
    }
};

Configuration Options

OptionTypeRequiredDefaultDescription
googleApiKeystringYes-Google API key for embeddings
qdrantUrlstringYes-Qdrant server URL
qdrantApiKeystringYes-Qdrant API key
groqApiKeystringYes-Groq API key
collectionNamestringNo'default_collection'Name of the Qdrant collection
fallbackResponsestringNo'I could not find relevant information.'Response when no relevant information is found
promptsobjectNoSee belowCustom prompts for classification and response

Default Prompts

{
    classification: `I am providing you a query, based on the query your work is detect whether 
                    that is related to marks, events or general information. Query: {query}`,
    response: `You are a helpful assistant. Based on this context: "{context}", 
              please answer this question: "{query}"`
}

API Reference

createRAG(config: RAGConfig)

Creates a new RAG implementation instance with the provided configuration.

processQuery(contextText: string, query: string): Promise

Processes a query against the provided context and returns a response with classification.

Returns:

interface RAGResponse {
    response: string;  // The generated response
    category: string;  // The classified category
}

updatePrompts(newPrompts: Partial)

Updates the classification and response prompts after initialization.

Error Handling

The module throws errors for:

  • Missing required configuration parameters
  • Vector store creation failures
  • Query processing errors
  • Collection creation/management issues

TypeScript Support

The module includes TypeScript definitions for all exports. Import types as needed:

import { RAGConfig, RAGResponse } from 'rag-module';

Examples

Custom Classification Categories

const config: RAGConfig = {
    // ... other config options
    prompts: {
        classification: `Categorize as 'technical', 'historical', or 'general': {query}`,
        response: `Based on this context: '{context}', answer: '{query}'`
    }
};

const rag = createRAG(config);
const result = await rag.processQuery(
    "Technical documentation about TypeScript...",
    "What are TypeScript interfaces?"
);
// result.category will be 'technical'