API Reference
MongoRAG Classβ
Constructorβ
Creates a new instance of the MongoRAG class.
const rag = new MongoRAG({
mongoUrl: string,
database: string,
collection: string,
embedding: {
provider: 'openai',
apiKey: string,
model?: string,
batchSize?: number,
dimensions?: number
},
search?: {
maxResults?: number,
minScore?: number,
similarityMetric?: 'cosine' | 'dotProduct' | 'euclidean'
}
});
Parametersβ
config.mongoUrl(string, required): MongoDB connection URI.config.database(string, required): Default MongoDB database name.config.collection(string, required): Default MongoDB collection name.config.embedding(object, required):provider(string, required): Embedding provider (openai,ollama, orvoyageare supported).apiKey(string, required): API key for the embedding provider (not required forollama).model(string, optional): Model name. Defaults depend on provider:- OpenAI:
'text-embedding-3-small' - Voyage:
'voyage-3'(other options:voyage-3-large,voyage-3-lite,voyage-code-3,voyage-finance-2,voyage-law-2) - Ollama: requires model specification
- OpenAI:
baseUrl(string, optional): Base URL for Ollama API (default:'http://localhost:11434').batchSize(number, optional): Batch size for embedding generation (default:100).dimensions(number, optional): Number of dimensions in the embedding space (default:1536).
config.search(object, optional):maxResults(number, optional): Maximum number of results to return (default:5).minScore(number, optional): Minimum similarity score threshold (default:0.7).similarityMetric(string, optional): Similarity function for search (cosine,dotProduct,euclidean). Defaults to'cosine'.
Methodsβ
connect()β
Establishes connection to MongoDB.
await rag.connect();
disconnect()β
Closes the MongoDB connection.
await rag.disconnect();
ingestBatch()β
Ingests a batch of documents into the vector store.
await rag.ingestBatch(documents, {
batchSize?: number,
preprocessor?: Function
});
Parameters:
documents(array, required): Array of documents to ingestoptions(object, optional):batchSize: Override default batch sizepreprocessor: Custom preprocessor for this batch
Returns: Promise<void>
search()β
Performs a vector search for similar documents.
const results = await rag.search(query, {
database?: string,
collection?: string,
maxResults?: number,
minScore?: number
});
Parameters:
query(string, required): Search queryoptions(object, optional):maxResults: Maximum number of resultsminScore: Minimum similarity scorefilter: MongoDB filter for metadata
Returns: Promise<Array<SearchResult>>
Type Definitionsβ
interface SearchResult {
content: string;
documentId: string;
metadata?: Record<string, any>;
score: number;
}
Configuration Examplesβ
Basic Configurationβ
const rag = new MongoRAG({
mongoUrl: 'mongodb+srv://your-connection-string',
database: 'ragdb',
collection: 'documents',
embedding: {
provider: 'openai',
apiKey: process.env.EMBEDDING_API_KEY
}
});
Advanced Configurationβ
const rag = new MongoRAG({
mongoUrl: 'mongodb+srv://your-connection-string',
database: 'ragdb',
collection: 'documents',
embedding: {
provider: 'openai',
apiKey: process.env.EMBEDDING_API_KEY,
model: 'text-embedding-ada-002',
batchSize: 100,
dimensions: 1536
},
search: {
maxResults: 10,
minScore: 0.8,
similarityMetric: 'dotProduct'
}
});
Error Handlingβ
The library provides specific error types:
try {
await rag.search('query');
} catch (error) {
console.error('An error occurred:', error.message);
// Handle the error appropriately
}
For more detailed examples and use cases, refer to: