@@ -17,41 +17,36 @@ const rag = new MongoRAG({
1717 mongoUrl: string,
1818 database: string,
1919 collection: string,
20- embedding? : {
20+ embedding: {
2121 provider: ' openai' ,
2222 apiKey: string,
2323 model?: string,
24- batchSize?: number
25- },
26- preprocessing?: {
27- documentPreprocessor?: Function ,
28- chunkSize?: number,
29- chunkOverlap?: number
24+ batchSize?: number,
25+ dimensions?: number
3026 },
3127 search?: {
3228 maxResults?: number,
33- minScore?: number
29+ minScore?: number,
30+ similarityMetric?: ' cosine' | ' dotProduct' | ' euclidean'
3431 }
3532});
3633```
3734
3835#### Parameters
3936
40- - ` config.mongoUrl ` (string, required): MongoDB connection URI
41- - ` config.database ` (string, required): MongoDB database name
42- - ` config.collection ` (string, required): MongoDB collection name
43- - ` config.embedding ` (object, optional):
44- - ` provider ` : Embedding provider (currently supports 'openai')
45- - ` apiKey ` : API key for the embedding provider
46- - ` model ` : Model name (default: 'text-embedding-ada-002')
47- - ` batchSize ` : Number of documents to embed in each batch
48- - ` config.preprocessing ` (object, optional):
49- - ` documentPreprocessor ` : Custom function for document preprocessing
50- - ` chunkSize ` : Maximum chunk size in tokens
51- - ` chunkOverlap ` : Number of overlapping tokens between chunks
37+ - ` config.mongoUrl ` (string, required): MongoDB connection URI.
38+ - ` config.database ` (string, required): Default MongoDB database name.
39+ - ` config.collection ` (string, required): Default MongoDB collection name.
40+ - ` config.embedding ` (object, required):
41+ - ` provider ` (string, required): Embedding provider (` openai ` is supported).
42+ - ` apiKey ` (string, required): API key for the embedding provider.
43+ - ` model ` (string, optional): Model name (default: ` 'text-embedding-3-small' ` ).
44+ - ` batchSize ` (number, optional): Batch size for embedding generation (default: ` 100 ` ).
45+ - ` dimensions ` (number, optional): Number of dimensions in the embedding space (default: ` 1536 ` ).
5246- ` config.search ` (object, optional):
53- - ` maxResults ` : Maximum number of results to return
54- - ` minScore ` : Minimum similarity score threshold
47+ - ` maxResults ` (number, optional): Maximum number of results to return (default: ` 5 ` ).
48+ - ` minScore ` (number, optional): Minimum similarity score threshold (default: ` 0.7 ` ).
49+ - ` similarityMetric ` (string, optional): Similarity function for search (` cosine ` , ` dotProduct ` , ` euclidean ` ). Defaults to ` 'cosine' ` .
5550
5651### Methods
5752
@@ -96,9 +91,10 @@ Performs a vector search for similar documents.
9691
9792``` javascript
9893const results = await rag .search (query, {
94+ database?: string,
95+ collection?: string,
9996 maxResults?: number,
100- minScore?: number,
101- filter?: object
97+ minScore?: number
10298});
10399```
104100
@@ -116,8 +112,9 @@ const results = await rag.search(query, {
116112``` typescript
117113interface SearchResult {
118114 content: string ;
119- score : number ;
115+ documentId : string ;
120116 metadata? : Record <string , any >;
117+ score: number ;
121118}
122119```
123120
@@ -148,23 +145,13 @@ const rag = new MongoRAG({
148145 provider: ' openai' ,
149146 apiKey: process .env .OPENAI_API_KEY ,
150147 model: ' text-embedding-ada-002' ,
151- batchSize: 100
152- },
153- preprocessing: {
154- documentPreprocessor : (doc ) => ({
155- ... doc,
156- content: doc .content .toLowerCase ().trim (),
157- metadata: {
158- ... doc .metadata ,
159- processedAt: new Date ()
160- }
161- }),
162- chunkSize: 500 ,
163- chunkOverlap: 50
148+ batchSize: 100 ,
149+ dimensions: 1536
164150 },
165151 search: {
166- maxResults: 5 ,
167- minScore: 0.7
152+ maxResults: 10 ,
153+ minScore: 0.8 ,
154+ similarityMetric: ' dotProduct'
168155 }
169156});
170157```
@@ -177,42 +164,11 @@ The library provides specific error types:
177164try {
178165 await rag .search (' query' );
179166} catch (error) {
180- if (error instanceof ConnectionError) {
181- // Handle connection errors
182- } else if (error instanceof EmbeddingError) {
183- // Handle embedding generation errors
184- } else if (error instanceof SearchError) {
185- // Handle search errors
186- }
167+ console .error (' An error occurred:' , error .message );
168+ // Handle the error appropriately
187169}
188170```
189171
190- ### Events
191-
192- MongoRAG emits events you can listen to:
193-
194- ``` javascript
195- rag .on (' connect' , () => {
196- console .log (' Connected to MongoDB' );
197- });
198-
199- rag .on (' ingest:start' , (batchSize ) => {
200- console .log (` Starting ingestion of ${ batchSize} documents` );
201- });
202-
203- rag .on (' ingest:complete' , (count ) => {
204- console .log (` Completed ingestion of ${ count} documents` );
205- });
206-
207- rag .on (' search:start' , (query ) => {
208- console .log (` Starting search for: ${ query} ` );
209- });
210-
211- rag .on (' error' , (error ) => {
212- console .error (' An error occurred:' , error);
213- });
214- ```
215-
216172For more detailed examples and use cases, refer to:
217173- [ Basic Example] ( ./examples/basic-example.md )
218174- [ Advanced Example] ( ./examples/advanced-example.md )
0 commit comments