npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ubc-genai-toolkit-rag

v0.1.7

Published

RAG module for the UBC GenAI Toolkit

Readme

UBC GenAI Toolkit - RAG Module

Overview

This module provides a standardized interface for building Retrieval-Augmented Generation (RAG) systems. It uses the Facade pattern to simplify interactions with vector stores, such as Qdrant, allowing you to easily store, manage, and retrieve documents for use in generative AI applications.

The module handles the complexities of connecting to a vector database, creating collections, and upserting document embeddings and metadata.

Installation

npm install ubc-genai-toolkit-rag ubc-genai-toolkit-core ubc-genai-toolkit-embeddings ubc-genai-toolkit-chunking

Core Concepts

  • RAGModule: The main class and entry point for interacting with a vector store.
  • VectorStore: An interface representing the operations of a vector database (e.g., Qdrant).
  • RAGConfig: The configuration object for the RAGModule, specifying the provider, embeddings, and chunking strategy.
  • Document: A standardized format for text data, including content and optional metadata.

Configuration

The RAGModule is configured by passing a RAGConfig object to the static RAGModule.create() method. This method asynchronously initializes the module and its dependencies.

A complete configuration requires specifying the vector store provider (e.g., qdrant), the embeddings model, and optionally, a chunking strategy.

import { RAGModule, RAGConfig } from 'ubc-genai-toolkit-rag';
import { ConsoleLogger } from 'ubc-genai-toolkit-core';

// Example complete configuration for the RAGModule
const ragConfig: RAGConfig = {
	provider: 'qdrant',
	qdrantConfig: {
		url: process.env.QDRANT_URL || 'http://localhost:6333',
		collectionName: 'my-test-collection',
		vectorSize: 384, // Dimensionality of the embeddings model
		distanceMetric: 'Cosine',
	},
	embeddingsConfig: {
		provider: 'fastembed',
		model: 'bge-small-en-v1.5',
	},
	// Optional: Add chunking configuration (see below)
	// chunkingConfig: { ... }

	logger: new ConsoleLogger(),
	debug: true,
};

// Asynchronously create and initialize the module
const ragModule = await RAGModule.create(ragConfig);

Chunking Configuration

When adding documents, the RAGModule automatically splits them into smaller chunks. You can control this behavior using the chunkingConfig property.

1. Default Behavior (No Configuration)

If you do not provide a chunkingConfig, the module uses a basic, internal chunker that splits text by a fixed character length. This is suitable for quick tests but is not recommended for production.

2. Using the ChunkingModule

For more advanced chunking, you can leverage the ubc-genai-toolkit-chunking module. This gives you access to multiple strategies like recursiveCharacter or token-based splitting.

import { RAGConfig } from 'ubc-genai-toolkit-rag';

const ragConfig: RAGConfig = {
	// ... other properties (provider, qdrantConfig, etc.)
	chunkingConfig: {
		strategy: 'recursiveCharacter',
		defaultOptions: {
			chunkSize: 500,
			chunkOverlap: 50,
		},
	},
};

3. Using a Custom Chunking Function

For maximum flexibility, you can provide your own function to handle chunking. The function should accept a string and return an array of string chunks.

import { RAGConfig } from 'ubc-genai-toolkit-rag';

const myCustomChunker = (text: string): string[] => {
	// Your custom logic to split text into chunks
	return text.split('\n\n'); // Example: split by paragraph
};

const ragConfig: RAGConfig = {
	// ... other properties (provider, qdrantConfig, etc.)
	chunkingConfig: myCustomChunker,
};

Usage Examples

Initialization

First, configure and create an instance of the RAGModule. The module handles the initialization of its internal dependencies, like the embeddings model.

import { RAGModule, RAGConfig } from 'ubc-genai-toolkit-rag';
import { ConsoleLogger } from 'ubc-genai-toolkit-core';

async function initializeRagModule() {
	const ragConfig: RAGConfig = {
		provider: 'qdrant',
		qdrantConfig: {
			url: 'http://localhost:6333',
			collectionName: 'my-test-collection',
			vectorSize: 384, // e.g., for bge-small-en-v1.5
			distanceMetric: 'Cosine',
		},
		embeddingsConfig: {
			provider: 'fastembed',
			model: 'bge-small-en-v1.5',
		},
		logger: new ConsoleLogger(),
	};

	const ragModule = await RAGModule.create(ragConfig);
	return ragModule;
}

const ragModule = await initializeRagModule();

Adding Documents to the Vector Store

You can add a document as a single string. The RAGModule will automatically handle chunking and embedding before storing the vectorized chunks.

async function addDocument(ragModule: RAGModule) {
	const documentContent = `UBC is a public research university in British Columbia, Canada.
It was established in 1908.
The Vancouver campus is located on the traditional, ancestral, and unceded territory of the Musqueam people.`;

	console.log('Adding document to the vector store...');
	const chunkIds = await ragModule.addDocument(documentContent, {
		source: 'ubc-facts.txt',
	});
	console.log(
		`Document added successfully. Chunk IDs: ${chunkIds.join(', ')}`
	);
}

Querying for Similar Documents

Use a query string to find the most relevant document chunks in the vector store.

async function findSimilarDocuments(ragModule: RAGModule, query: string) {
	console.log(`Querying for documents similar to: "${query}"`);

	const results = await ragModule.retrieveContext(query, {
		limit: 2, // Find the top 2 most similar document chunks
	});

	console.log('Query Results:');
	results.forEach((result, index) => {
		console.log(`  ${index + 1}. Content: "${result.content}"`);
		console.log(`     Score: ${result.score}`);
		console.log(`     Metadata:`, result.metadata);
	});
}

// Example usage
await addDocument(ragModule);
await findSimilarDocuments(ragModule, 'Tell me about UBC');

Error Handling

The module uses the common error types from ubc-genai-toolkit-core.