@coworker-agency/rag
Version:
Retrieval Augmented Generation (RAG) library for document indexing, vector storage, and AI-powered question answering
145 lines (129 loc) • 3.86 kB
JavaScript
/**
* @coworker-agency/rag
*
* A Retrieval Augmented Generation (RAG) library for document indexing,
* vector storage, and AI-powered question answering.
*/
import { createClient } from '@supabase/supabase-js';
import OpenAI from 'openai';
import { searchFaq, getAnswer } from './retriever/index.js';
import { queryClassifier } from './classifier/index.js';
import { getContextAwareVectors, extractRelevantContext } from './cav/index.js';
import { processDocuments } from './indexer/index.js';
// Global configuration
let config = {
supabaseUrl: null,
supabaseKey: null,
supabaseSecretKey: null,
supabaseBucket: 'docs',
tableName: 'vector_documents',
openaiApiKey: null,
openaiLlmModel: 'gpt-4o',
openaiEmbeddingModel: 'text-embedding-3-small'
};
// Client instances
let supabaseClient = null;
let openaiClient = null;
/**
* Initialize the RAG library with configuration
* @param {Object} options - Configuration options
* @param {string} options.supabaseUrl - Supabase project URL
* @param {string} options.supabaseKey - Supabase public key
* @param {string} options.supabaseSecretKey - Supabase service role key
* @param {string} options.supabaseBucket - Supabase storage bucket name
* @param {string} options.tableName - Vector store table name
* @param {string} options.openaiApiKey - OpenAI API key
* @param {string} options.openaiLlmModel - OpenAI LLM model
* @param {string} options.openaiEmbeddingModel - OpenAI Embedding model
* @returns {Object} - Configured RAG library instance
*/
function init({
supabaseUrl,
supabaseKey,
supabaseSecretKey,
supabaseBucket,
tableName = 'vector_documents',
openaiApiKey,
openaiLlmModel = 'gpt-4o',
openaiEmbeddingModel = 'text-embedding-3-small'
}) {
// Validate required parameters
if (!supabaseUrl || !supabaseSecretKey || !openaiApiKey) {
throw new Error('Missing required configuration: supabaseUrl, supabaseSecretKey, and openaiApiKey are required');
}
// Update configuration
config = {
supabaseUrl,
supabaseKey: supabaseKey || supabaseSecretKey, // Fall back to secret key if public key not provided
supabaseSecretKey,
supabaseBucket,
tableName,
openaiApiKey,
openaiLlmModel,
openaiEmbeddingModel
};
// Initialize Supabase client
supabaseClient = createClient(supabaseUrl, supabaseSecretKey, {
auth: { persistSession: false }
});
// Initialize OpenAI client
openaiClient = new OpenAI({
apiKey: openaiApiKey,
});
return {
searchFaq: (query, limit = 5) => searchFaq(
config.supabaseUrl,
config.supabaseSecretKey,
query,
limit,
config.tableName,
config.openaiApiKey
),
getAnswer: (question) => getAnswer(
config.supabaseUrl,
config.supabaseSecretKey,
question,
config.tableName,
config.openaiApiKey
),
processDocuments: (options = {}) => processDocuments(
config.supabaseUrl,
config.supabaseSecretKey,
config.supabaseBucket,
{
openaiApiKey: config.openaiApiKey,
tableName: config.tableName,
...options
}
),
queryClassifier: (query, classifications, options = {}) => queryClassifier(
query,
classifications,
{
openaiApiKey: config.openaiApiKey,
modelName: config.openaiLlmModel,
...options
}
),
getContextAwareVectors: (documentContent, options = {}) => getContextAwareVectors(
documentContent,
config.openaiApiKey,
{
modelName: config.openaiLlmModel,
embeddingsModel: config.openaiEmbeddingModel,
...options
}
),
extractRelevantContext
};
}
// Export functions
export {
init,
searchFaq,
getAnswer,
processDocuments,
queryClassifier,
getContextAwareVectors,
extractRelevantContext
};