@@ -12,32 +12,38 @@ const { NativeEmbeddingReranker } = require("../../EmbeddingRerankers/native");
1212 * @typedef {import('@lancedb/lancedb').Connection } LanceClient
1313 */
1414
15- const LanceDb = {
16- uri : `${
17- ! ! process . env . STORAGE_DIR ? `${ process . env . STORAGE_DIR } /` : "./storage/"
18- } lancedb`,
19- name : "LanceDb" ,
15+ class LanceDb {
16+ constructor ( ) {
17+ this . uri = `${
18+ ! ! process . env . STORAGE_DIR ? `${ process . env . STORAGE_DIR } /` : "./storage/"
19+ } lancedb`;
20+ this . name = "LanceDb" ;
21+ }
2022
2123 /** @returns {Promise<{client: LanceClient}> } */
22- connect : async function ( ) {
24+ async connect ( ) {
2325 const client = await lancedb . connect ( this . uri ) ;
2426 return { client } ;
25- } ,
26- distanceToSimilarity : function ( distance = null ) {
27+ }
28+
29+ distanceToSimilarity ( distance = null ) {
2730 if ( distance === null || typeof distance !== "number" ) return 0.0 ;
2831 if ( distance >= 1.0 ) return 1 ;
2932 if ( distance < 0 ) return 1 - Math . abs ( distance ) ;
3033 return 1 - distance ;
31- } ,
32- heartbeat : async function ( ) {
34+ }
35+
36+ async heartbeat ( ) {
3337 await this . connect ( ) ;
3438 return { heartbeat : Number ( new Date ( ) ) } ;
35- } ,
36- tables : async function ( ) {
39+ }
40+
41+ async tables ( ) {
3742 const { client } = await this . connect ( ) ;
3843 return await client . tableNames ( ) ;
39- } ,
40- totalVectors : async function ( ) {
44+ }
45+
46+ async totalVectors ( ) {
4147 const { client } = await this . connect ( ) ;
4248 const tables = await client . tableNames ( ) ;
4349 let count = 0 ;
@@ -46,15 +52,17 @@ const LanceDb = {
4652 count += await table . countRows ( ) ;
4753 }
4854 return count ;
49- } ,
50- namespaceCount : async function ( _namespace = null ) {
55+ }
56+
57+ async namespaceCount ( _namespace = null ) {
5158 const { client } = await this . connect ( ) ;
5259 const exists = await this . namespaceExists ( client , _namespace ) ;
5360 if ( ! exists ) return 0 ;
5461
5562 const table = await client . openTable ( _namespace ) ;
5663 return ( await table . countRows ( ) ) || 0 ;
57- } ,
64+ }
65+
5866 /**
5967 * Performs a SimilaritySearch + Reranking on a namespace.
6068 * @param {Object } params - The parameters for the rerankedSimilarityResponse.
@@ -67,7 +75,7 @@ const LanceDb = {
6775 * @param {string[] } params.filterIdentifiers - The identifiers of the documents to filter out.
6876 * @returns
6977 */
70- rerankedSimilarityResponse : async function ( {
78+ async rerankedSimilarityResponse ( {
7179 client,
7280 namespace,
7381 query,
@@ -138,7 +146,7 @@ const LanceDb = {
138146 } ) ;
139147
140148 return result ;
141- } ,
149+ }
142150
143151 /**
144152 * Performs a SimilaritySearch on a give LanceDB namespace.
@@ -151,7 +159,7 @@ const LanceDb = {
151159 * @param {string[] } params.filterIdentifiers
152160 * @returns
153161 */
154- similarityResponse : async function ( {
162+ async similarityResponse ( {
155163 client,
156164 namespace,
157165 queryVector,
@@ -192,30 +200,32 @@ const LanceDb = {
192200 } ) ;
193201
194202 return result ;
195- } ,
203+ }
204+
196205 /**
197206 *
198207 * @param {LanceClient } client
199208 * @param {string } namespace
200209 * @returns
201210 */
202- namespace : async function ( client , namespace = null ) {
211+ async namespace ( client , namespace = null ) {
203212 if ( ! namespace ) throw new Error ( "No namespace value provided." ) ;
204213 const collection = await client . openTable ( namespace ) . catch ( ( ) => false ) ;
205214 if ( ! collection ) return null ;
206215
207216 return {
208217 ...collection ,
209218 } ;
210- } ,
219+ }
220+
211221 /**
212222 *
213223 * @param {LanceClient } client
214224 * @param {number[] } data
215225 * @param {string } namespace
216226 * @returns
217227 */
218- updateOrCreateCollection : async function ( client , data = [ ] , namespace ) {
228+ async updateOrCreateCollection ( client , data = [ ] , namespace ) {
219229 const hasNamespace = await this . hasNamespace ( namespace ) ;
220230 if ( hasNamespace ) {
221231 const collection = await client . openTable ( namespace ) ;
@@ -225,35 +235,39 @@ const LanceDb = {
225235
226236 await client . createTable ( namespace , data ) ;
227237 return true ;
228- } ,
229- hasNamespace : async function ( namespace = null ) {
238+ }
239+
240+ async hasNamespace ( namespace = null ) {
230241 if ( ! namespace ) return false ;
231242 const { client } = await this . connect ( ) ;
232243 const exists = await this . namespaceExists ( client , namespace ) ;
233244 return exists ;
234- } ,
245+ }
246+
235247 /**
236248 *
237249 * @param {LanceClient } client
238250 * @param {string } namespace
239251 * @returns
240252 */
241- namespaceExists : async function ( client , namespace = null ) {
253+ async namespaceExists ( client , namespace = null ) {
242254 if ( ! namespace ) throw new Error ( "No namespace value provided." ) ;
243255 const collections = await client . tableNames ( ) ;
244256 return collections . includes ( namespace ) ;
245- } ,
257+ }
258+
246259 /**
247260 *
248261 * @param {LanceClient } client
249262 * @param {string } namespace
250263 * @returns
251264 */
252- deleteVectorsInNamespace : async function ( client , namespace = null ) {
265+ async deleteVectorsInNamespace ( client , namespace = null ) {
253266 await client . dropTable ( namespace ) ;
254267 return true ;
255- } ,
256- deleteDocumentFromNamespace : async function ( namespace , docId ) {
268+ }
269+
270+ async deleteDocumentFromNamespace ( namespace , docId ) {
257271 const { client } = await this . connect ( ) ;
258272 const exists = await this . namespaceExists ( client , namespace ) ;
259273 if ( ! exists ) {
@@ -272,8 +286,9 @@ const LanceDb = {
272286 if ( vectorIds . length === 0 ) return ;
273287 await table . delete ( `id IN (${ vectorIds . map ( ( v ) => `'${ v } '` ) . join ( "," ) } )` ) ;
274288 return true ;
275- } ,
276- addDocumentToNamespace : async function (
289+ }
290+
291+ async addDocumentToNamespace (
277292 namespace ,
278293 documentData = { } ,
279294 fullFilePath = null ,
@@ -376,8 +391,9 @@ const LanceDb = {
376391 console . error ( "addDocumentToNamespace" , e . message ) ;
377392 return { vectorized : false , error : e . message } ;
378393 }
379- } ,
380- performSimilaritySearch : async function ( {
394+ }
395+
396+ async performSimilaritySearch ( {
381397 namespace = null ,
382398 input = "" ,
383399 LLMConnector = null ,
@@ -427,8 +443,9 @@ const LanceDb = {
427443 sources : this . curateSources ( sources ) ,
428444 message : false ,
429445 } ;
430- } ,
431- "namespace-stats" : async function ( reqBody = { } ) {
446+ }
447+
448+ async "namespace-stats" ( reqBody = { } ) {
432449 const { namespace = null } = reqBody ;
433450 if ( ! namespace ) throw new Error ( "namespace required" ) ;
434451 const { client } = await this . connect ( ) ;
@@ -438,8 +455,9 @@ const LanceDb = {
438455 return stats
439456 ? stats
440457 : { message : "No stats were able to be fetched from DB for namespace" } ;
441- } ,
442- "delete-namespace" : async function ( reqBody = { } ) {
458+ }
459+
460+ async "delete-namespace" ( reqBody = { } ) {
443461 const { namespace = null } = reqBody ;
444462 const { client } = await this . connect ( ) ;
445463 if ( ! ( await this . namespaceExists ( client , namespace ) ) )
@@ -449,14 +467,16 @@ const LanceDb = {
449467 return {
450468 message : `Namespace ${ namespace } was deleted.` ,
451469 } ;
452- } ,
453- reset : async function ( ) {
470+ }
471+
472+ async reset ( ) {
454473 const { client } = await this . connect ( ) ;
455474 const fs = require ( "fs" ) ;
456475 fs . rm ( `${ client . uri } ` , { recursive : true } , ( ) => null ) ;
457476 return { reset : true } ;
458- } ,
459- curateSources : function ( sources = [ ] ) {
477+ }
478+
479+ curateSources ( sources = [ ] ) {
460480 const documents = [ ] ;
461481 for ( const source of sources ) {
462482 const { text, vector : _v , _distance : _d , ...rest } = source ;
@@ -470,7 +490,7 @@ const LanceDb = {
470490 }
471491
472492 return documents ;
473- } ,
474- } ;
493+ }
494+ }
475495
476496module . exports . LanceDb = LanceDb ;
0 commit comments