@futdevpro/nts-dynamo 1.14.64 → 1.14.67
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/_modules/local-vector-search/_enums/lvs-search-mode.enum.d.ts +19 -0
- package/build/_modules/local-vector-search/_enums/lvs-search-mode.enum.d.ts.map +1 -0
- package/build/_modules/local-vector-search/_enums/lvs-search-mode.enum.js +23 -0
- package/build/_modules/local-vector-search/_enums/lvs-search-mode.enum.js.map +1 -0
- package/build/_modules/local-vector-search/_models/lvs-search-result.interface.d.ts +17 -0
- package/build/_modules/local-vector-search/_models/lvs-search-result.interface.d.ts.map +1 -0
- package/build/_modules/local-vector-search/_models/lvs-search-result.interface.js +3 -0
- package/build/_modules/local-vector-search/_models/lvs-search-result.interface.js.map +1 -0
- package/build/_modules/local-vector-search/_services/lvs-doc-chunk-data.service.d.ts +27 -0
- package/build/_modules/local-vector-search/_services/lvs-doc-chunk-data.service.d.ts.map +1 -0
- package/build/_modules/local-vector-search/_services/lvs-doc-chunk-data.service.js +198 -0
- package/build/_modules/local-vector-search/_services/lvs-doc-chunk-data.service.js.map +1 -0
- package/build/_modules/local-vector-search/_services/lvs-local-vector-search.data-service.d.ts +130 -0
- package/build/_modules/local-vector-search/_services/lvs-local-vector-search.data-service.d.ts.map +1 -0
- package/build/_modules/local-vector-search/_services/lvs-local-vector-search.data-service.js +241 -0
- package/build/_modules/local-vector-search/_services/lvs-local-vector-search.data-service.js.map +1 -0
- package/build/_modules/local-vector-search/_services/lvs-vector-pool.control-service.d.ts +71 -0
- package/build/_modules/local-vector-search/_services/lvs-vector-pool.control-service.d.ts.map +1 -0
- package/build/_modules/local-vector-search/_services/lvs-vector-pool.control-service.js +182 -0
- package/build/_modules/local-vector-search/_services/lvs-vector-pool.control-service.js.map +1 -0
- package/build/_modules/local-vector-search/_services/lvs-vector-pool.control-service.spec.d.ts +2 -0
- package/build/_modules/local-vector-search/_services/lvs-vector-pool.control-service.spec.d.ts.map +1 -0
- package/build/_modules/local-vector-search/_services/lvs-vector-pool.control-service.spec.js +294 -0
- package/build/_modules/local-vector-search/_services/lvs-vector-pool.control-service.spec.js.map +1 -0
- package/build/_modules/local-vector-search/index.d.ts +6 -0
- package/build/_modules/local-vector-search/index.d.ts.map +1 -0
- package/build/_modules/local-vector-search/index.js +12 -0
- package/build/_modules/local-vector-search/index.js.map +1 -0
- package/build/_services/base/data.service.d.ts +1 -1
- package/build/_services/base/data.service.d.ts.map +1 -1
- package/build/_services/base/data.service.js +7 -1
- package/build/_services/base/data.service.js.map +1 -1
- package/package.json +12 -1
- package/src/_modules/local-vector-search/_enums/lvs-search-mode.enum.ts +19 -0
- package/src/_modules/local-vector-search/_models/lvs-search-result.interface.ts +17 -0
- package/src/_modules/local-vector-search/_services/lvs-doc-chunk-data.service.ts +276 -0
- package/src/_modules/local-vector-search/_services/lvs-local-vector-search.data-service.ts +330 -0
- package/src/_modules/local-vector-search/_services/lvs-vector-pool.control-service.spec.ts +393 -0
- package/src/_modules/local-vector-search/_services/lvs-vector-pool.control-service.ts +220 -0
- package/src/_modules/local-vector-search/index.ts +12 -0
- package/src/_services/base/data.service.ts +12 -3
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
import { LVS_Search_Mode } from '../_enums/lvs-search-mode.enum';
|
|
2
|
+
import { LVS_SearchResult } from '../_models/lvs-search-result.interface';
|
|
3
|
+
import { LVS_VectorPool_ControlService } from './lvs-vector-pool.control-service';
|
|
4
|
+
|
|
5
|
+
describe('| LVS_VectorPool_ControlService', () => {
|
|
6
|
+
let vectorPool: LVS_VectorPool_ControlService;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
vectorPool = new LVS_VectorPool_ControlService();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe('| Constructor/Initialization', () => {
|
|
13
|
+
it('| should initialize with empty pools', () => {
|
|
14
|
+
const all: Map<string, number[]> = vectorPool.getAll();
|
|
15
|
+
expect(all.size).toBe(0);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('| addVector', () => {
|
|
20
|
+
it('| should add a valid vector to the pool', () => {
|
|
21
|
+
const id: string = 'vector1';
|
|
22
|
+
const vector: number[] = [1, 2, 3];
|
|
23
|
+
|
|
24
|
+
vectorPool.addVector(id, vector);
|
|
25
|
+
|
|
26
|
+
const all: Map<string, number[]> = vectorPool.getAll();
|
|
27
|
+
expect(all.size).toBe(1);
|
|
28
|
+
expect(all.has(id)).toBeTrue();
|
|
29
|
+
expect(all.get(id)).toEqual(vector);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('| should add multiple vectors to the pool', () => {
|
|
33
|
+
vectorPool.addVector('vector1', [1, 2, 3]);
|
|
34
|
+
vectorPool.addVector('vector2', [4, 5, 6]);
|
|
35
|
+
vectorPool.addVector('vector3', [7, 8, 9]);
|
|
36
|
+
|
|
37
|
+
const all: Map<string, number[]> = vectorPool.getAll();
|
|
38
|
+
expect(all.size).toBe(3);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('| should throw error when ID is empty string', () => {
|
|
42
|
+
expect(() => {
|
|
43
|
+
vectorPool.addVector('', [1, 2, 3]);
|
|
44
|
+
}).toThrowError('Vector ID is required');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('| should throw error when vector is empty array', () => {
|
|
48
|
+
expect(() => {
|
|
49
|
+
vectorPool.addVector('vector1', []);
|
|
50
|
+
}).toThrowError('Vector must be a non-empty array');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('| should throw error when vector is not an array', () => {
|
|
54
|
+
expect(() => {
|
|
55
|
+
vectorPool.addVector('vector1', null as any);
|
|
56
|
+
}).toThrowError('Vector must be a non-empty array');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('| should throw error when vector contains non-finite number', () => {
|
|
60
|
+
expect(() => {
|
|
61
|
+
vectorPool.addVector('vector1', [1, 2, Infinity]);
|
|
62
|
+
}).toThrowError('Vector must contain only finite numbers at index 2');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('| should throw error when vector contains NaN', () => {
|
|
66
|
+
expect(() => {
|
|
67
|
+
vectorPool.addVector('vector1', [1, 2, NaN]);
|
|
68
|
+
}).toThrowError('Vector must contain only finite numbers at index 2');
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('| should store normalized vector internally', () => {
|
|
72
|
+
const id: string = 'vector1';
|
|
73
|
+
const vector: number[] = [3, 4]; // Magnitude = 5, normalized = [0.6, 0.8]
|
|
74
|
+
|
|
75
|
+
vectorPool.addVector(id, vector);
|
|
76
|
+
|
|
77
|
+
// Verify original vector is stored
|
|
78
|
+
const all: Map<string, number[]> = vectorPool.getAll();
|
|
79
|
+
expect(all.get(id)).toEqual(vector);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe('| removeVector', () => {
|
|
84
|
+
it('| should remove an existing vector from the pool', () => {
|
|
85
|
+
vectorPool.addVector('vector1', [1, 2, 3]);
|
|
86
|
+
vectorPool.addVector('vector2', [4, 5, 6]);
|
|
87
|
+
|
|
88
|
+
vectorPool.removeVector('vector1');
|
|
89
|
+
|
|
90
|
+
const all: Map<string, number[]> = vectorPool.getAll();
|
|
91
|
+
expect(all.size).toBe(1);
|
|
92
|
+
expect(all.has('vector1')).toBeFalse();
|
|
93
|
+
expect(all.has('vector2')).toBeTrue();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('| should not throw when removing non-existent vector', () => {
|
|
97
|
+
expect(() => {
|
|
98
|
+
vectorPool.removeVector('non-existent');
|
|
99
|
+
}).not.toThrow();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('| should remove vector from both pools', () => {
|
|
103
|
+
vectorPool.addVector('vector1', [1, 2, 3]);
|
|
104
|
+
vectorPool.removeVector('vector1');
|
|
105
|
+
|
|
106
|
+
const all: Map<string, number[]> = vectorPool.getAll();
|
|
107
|
+
expect(all.size).toBe(0);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe('| updateVector', () => {
|
|
112
|
+
it('| should update an existing vector', () => {
|
|
113
|
+
vectorPool.addVector('vector1', [1, 2, 3]);
|
|
114
|
+
const newVector: number[] = [4, 5, 6];
|
|
115
|
+
|
|
116
|
+
vectorPool.updateVector('vector1', newVector);
|
|
117
|
+
|
|
118
|
+
const all: Map<string, number[]> = vectorPool.getAll();
|
|
119
|
+
expect(all.get('vector1')).toEqual(newVector);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('| should throw error when updating non-existent vector', () => {
|
|
123
|
+
expect(() => {
|
|
124
|
+
vectorPool.updateVector('non-existent', [1, 2, 3]);
|
|
125
|
+
}).toThrowError('Vector with ID "non-existent" does not exist');
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe('| clearPool', () => {
|
|
130
|
+
it('| should clear a populated pool', () => {
|
|
131
|
+
vectorPool.addVector('vector1', [1, 2, 3]);
|
|
132
|
+
vectorPool.addVector('vector2', [4, 5, 6]);
|
|
133
|
+
vectorPool.addVector('vector3', [7, 8, 9]);
|
|
134
|
+
|
|
135
|
+
vectorPool.clearPool();
|
|
136
|
+
|
|
137
|
+
const all: Map<string, number[]> = vectorPool.getAll();
|
|
138
|
+
expect(all.size).toBe(0);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('| should not throw when clearing empty pool', () => {
|
|
142
|
+
expect(() => {
|
|
143
|
+
vectorPool.clearPool();
|
|
144
|
+
}).not.toThrow();
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
describe('| getAll', () => {
|
|
149
|
+
it('| should return a copy of the pool, not a reference', () => {
|
|
150
|
+
vectorPool.addVector('vector1', [1, 2, 3]);
|
|
151
|
+
const all1: Map<string, number[]> = vectorPool.getAll();
|
|
152
|
+
const all2: Map<string, number[]> = vectorPool.getAll();
|
|
153
|
+
|
|
154
|
+
expect(all1).not.toBe(all2);
|
|
155
|
+
expect(all1.size).toBe(all2.size);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('| should return empty map when pool is empty', () => {
|
|
159
|
+
const all: Map<string, number[]> = vectorPool.getAll();
|
|
160
|
+
expect(all.size).toBe(0);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
describe('| cosineSimilarity (static)', () => {
|
|
165
|
+
it('| should return 1 for identical vectors', () => {
|
|
166
|
+
const vector: number[] = [1, 2, 3];
|
|
167
|
+
const similarity: number = LVS_VectorPool_ControlService.cosineSimilarity(vector, vector);
|
|
168
|
+
expect(similarity).toBeCloseTo(1, 10);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('| should return approximately 0 for orthogonal vectors', () => {
|
|
172
|
+
const vector1: number[] = [1, 0, 0];
|
|
173
|
+
const vector2: number[] = [0, 1, 0];
|
|
174
|
+
const similarity: number =
|
|
175
|
+
LVS_VectorPool_ControlService.cosineSimilarity(vector1, vector2);
|
|
176
|
+
expect(similarity).toBeCloseTo(0, 10);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('| should throw error for vectors with different dimensions', () => {
|
|
180
|
+
const vector1: number[] = [1, 2, 3];
|
|
181
|
+
const vector2: number[] = [1, 2];
|
|
182
|
+
|
|
183
|
+
expect(() => {
|
|
184
|
+
LVS_VectorPool_ControlService.cosineSimilarity(vector1, vector2);
|
|
185
|
+
}).toThrowError('Vectors must have the same dimension. Got 3 and 2');
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('| should calculate correct cosine similarity for known vectors', () => {
|
|
189
|
+
// Two vectors pointing in similar direction
|
|
190
|
+
const vector1: number[] = [1, 1, 0];
|
|
191
|
+
const vector2: number[] = [1, 0, 0];
|
|
192
|
+
// Normalized: [1/√2, 1/√2, 0] and [1, 0, 0]
|
|
193
|
+
// Dot product: 1/√2 ≈ 0.707
|
|
194
|
+
const similarity: number =
|
|
195
|
+
LVS_VectorPool_ControlService.cosineSimilarity(vector1, vector2);
|
|
196
|
+
expect(similarity).toBeCloseTo(1 / Math.sqrt(2), 10);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('| should handle zero vector', () => {
|
|
200
|
+
const vector1: number[] = [0, 0, 0];
|
|
201
|
+
const vector2: number[] = [1, 2, 3];
|
|
202
|
+
const similarity: number =
|
|
203
|
+
LVS_VectorPool_ControlService.cosineSimilarity(vector1, vector2);
|
|
204
|
+
expect(similarity).toBeCloseTo(0, 10);
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
describe('| l2Distance (static)', () => {
|
|
209
|
+
it('| should return 0 for identical vectors', () => {
|
|
210
|
+
const vector: number[] = [1, 2, 3];
|
|
211
|
+
const distance: number = LVS_VectorPool_ControlService.l2Distance(vector, vector);
|
|
212
|
+
expect(distance).toBe(0);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('| should throw error for vectors with different dimensions', () => {
|
|
216
|
+
const vector1: number[] = [1, 2, 3];
|
|
217
|
+
const vector2: number[] = [1, 2];
|
|
218
|
+
|
|
219
|
+
expect(() => {
|
|
220
|
+
LVS_VectorPool_ControlService.l2Distance(vector1, vector2);
|
|
221
|
+
}).toThrowError('Vectors must have the same dimension. Got 3 and 2');
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('| should calculate correct L2 distance for known vectors', () => {
|
|
225
|
+
const vector1: number[] = [0, 0, 0];
|
|
226
|
+
const vector2: number[] = [3, 4, 0];
|
|
227
|
+
// Distance = √(3² + 4² + 0²) = √25 = 5
|
|
228
|
+
const distance: number = LVS_VectorPool_ControlService.l2Distance(vector1, vector2);
|
|
229
|
+
expect(distance).toBe(5);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('| should calculate correct L2 distance for 1D vectors', () => {
|
|
233
|
+
const vector1: number[] = [5];
|
|
234
|
+
const vector2: number[] = [2];
|
|
235
|
+
const distance: number = LVS_VectorPool_ControlService.l2Distance(vector1, vector2);
|
|
236
|
+
expect(distance).toBe(3);
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
describe('| search', () => {
|
|
241
|
+
beforeEach(() => {
|
|
242
|
+
// Add test vectors
|
|
243
|
+
vectorPool.addVector('vec1', [1, 0, 0]);
|
|
244
|
+
vectorPool.addVector('vec2', [0, 1, 0]);
|
|
245
|
+
vectorPool.addVector('vec3', [0, 0, 1]);
|
|
246
|
+
vectorPool.addVector('vec4', [1, 1, 0]);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it('| should return empty array for empty pool', () => {
|
|
250
|
+
const emptyPool: LVS_VectorPool_ControlService = new LVS_VectorPool_ControlService();
|
|
251
|
+
const results: LVS_SearchResult[] = emptyPool.search([1, 0, 0], 3, LVS_Search_Mode.cosineSimilarity);
|
|
252
|
+
expect(results.length).toBe(0);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it('| should return results in descending order for cosine similarity', () => {
|
|
256
|
+
const query: number[] = [1, 0, 0];
|
|
257
|
+
const results: LVS_SearchResult[] = vectorPool.search(
|
|
258
|
+
query,
|
|
259
|
+
4,
|
|
260
|
+
LVS_Search_Mode.cosineSimilarity
|
|
261
|
+
);
|
|
262
|
+
|
|
263
|
+
expect(results.length).toBe(4);
|
|
264
|
+
// vec1 should be most similar (identical)
|
|
265
|
+
expect(results[0].id).toBe('vec1');
|
|
266
|
+
expect(results[0].score).toBeCloseTo(1, 10);
|
|
267
|
+
// Results should be in descending order
|
|
268
|
+
for (let i: number = 0; i < results.length - 1; i++) {
|
|
269
|
+
expect(results[i].score).toBeGreaterThanOrEqual(results[i + 1].score);
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('| should return results in ascending order for L2 distance', () => {
|
|
274
|
+
const query: number[] = [1, 0, 0];
|
|
275
|
+
const results: LVS_SearchResult[] = vectorPool.search(
|
|
276
|
+
query,
|
|
277
|
+
4,
|
|
278
|
+
LVS_Search_Mode.l2Distance
|
|
279
|
+
);
|
|
280
|
+
|
|
281
|
+
expect(results.length).toBe(4);
|
|
282
|
+
// vec1 should be closest (distance = 0)
|
|
283
|
+
expect(results[0].id).toBe('vec1');
|
|
284
|
+
expect(results[0].score).toBe(0);
|
|
285
|
+
// Results should be in ascending order
|
|
286
|
+
for (let i: number = 0; i < results.length - 1; i++) {
|
|
287
|
+
expect(results[i].score).toBeLessThanOrEqual(results[i + 1].score);
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it('| should limit results to top-K when k < pool size', () => {
|
|
292
|
+
const query: number[] = [1, 0, 0];
|
|
293
|
+
const results: LVS_SearchResult[] = vectorPool.search(
|
|
294
|
+
query,
|
|
295
|
+
2,
|
|
296
|
+
LVS_Search_Mode.cosineSimilarity
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
expect(results.length).toBe(2);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
it('| should return all results when k > pool size', () => {
|
|
303
|
+
const query: number[] = [1, 0, 0];
|
|
304
|
+
const results: LVS_SearchResult[] = vectorPool.search(
|
|
305
|
+
query,
|
|
306
|
+
10,
|
|
307
|
+
LVS_Search_Mode.cosineSimilarity
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
expect(results.length).toBe(4);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it('| should return all results when k = pool size', () => {
|
|
314
|
+
const query: number[] = [1, 0, 0];
|
|
315
|
+
const results: LVS_SearchResult[] = vectorPool.search(
|
|
316
|
+
query,
|
|
317
|
+
4,
|
|
318
|
+
LVS_Search_Mode.cosineSimilarity
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
expect(results.length).toBe(4);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
it('| should throw error for dimension mismatch', () => {
|
|
325
|
+
const query: number[] = [1, 0]; // 2D query, pool has 3D vectors
|
|
326
|
+
|
|
327
|
+
expect(() => {
|
|
328
|
+
vectorPool.search(query, 3, LVS_Search_Mode.cosineSimilarity);
|
|
329
|
+
}).toThrowError('Query vector dimension (2) does not match pool vector dimension (3)');
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
it('| should throw error for empty query vector', () => {
|
|
333
|
+
expect(() => {
|
|
334
|
+
vectorPool.search([], 3, LVS_Search_Mode.cosineSimilarity);
|
|
335
|
+
}).toThrowError('Query vector must be a non-empty array');
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
it('| should throw error for non-array query', () => {
|
|
339
|
+
expect(() => {
|
|
340
|
+
vectorPool.search(null as any, 3, LVS_Search_Mode.cosineSimilarity);
|
|
341
|
+
}).toThrowError('Query vector must be a non-empty array');
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it('| should throw error for k = 0', () => {
|
|
345
|
+
expect(() => {
|
|
346
|
+
vectorPool.search([1, 0, 0], 0, LVS_Search_Mode.cosineSimilarity);
|
|
347
|
+
}).toThrowError('k must be a positive number');
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it('| should throw error for negative k', () => {
|
|
351
|
+
expect(() => {
|
|
352
|
+
vectorPool.search([1, 0, 0], -1, LVS_Search_Mode.cosineSimilarity);
|
|
353
|
+
}).toThrowError('k must be a positive number');
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it('| should throw error for unknown search mode', () => {
|
|
357
|
+
expect(() => {
|
|
358
|
+
vectorPool.search([1, 0, 0], 3, 'unknown-mode' as LVS_Search_Mode);
|
|
359
|
+
}).toThrowError('Unknown search mode: unknown-mode');
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
it('| should return correct scores for cosine similarity', () => {
|
|
363
|
+
const query: number[] = [1, 0, 0];
|
|
364
|
+
const results: LVS_SearchResult[] = vectorPool.search(
|
|
365
|
+
query,
|
|
366
|
+
4,
|
|
367
|
+
LVS_Search_Mode.cosineSimilarity
|
|
368
|
+
);
|
|
369
|
+
|
|
370
|
+
// vec1 should have score = 1 (identical)
|
|
371
|
+
const vec1Result: LVS_SearchResult | undefined =
|
|
372
|
+
results.find((r: LVS_SearchResult) => r.id === 'vec1');
|
|
373
|
+
expect(vec1Result).toBeDefined();
|
|
374
|
+
expect(vec1Result!.score).toBeCloseTo(1, 10);
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
it('| should return correct scores for L2 distance', () => {
|
|
378
|
+
const query: number[] = [1, 0, 0];
|
|
379
|
+
const results: LVS_SearchResult[] = vectorPool.search(
|
|
380
|
+
query,
|
|
381
|
+
4,
|
|
382
|
+
LVS_Search_Mode.l2Distance
|
|
383
|
+
);
|
|
384
|
+
|
|
385
|
+
// vec1 should have score = 0 (identical)
|
|
386
|
+
const vec1Result: LVS_SearchResult | undefined =
|
|
387
|
+
results.find((r: LVS_SearchResult) => r.id === 'vec1');
|
|
388
|
+
expect(vec1Result).toBeDefined();
|
|
389
|
+
expect(vec1Result!.score).toBe(0);
|
|
390
|
+
});
|
|
391
|
+
});
|
|
392
|
+
});
|
|
393
|
+
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { LVS_Search_Mode } from '../_enums/lvs-search-mode.enum';
|
|
2
|
+
import { LVS_SearchResult } from '../_models/lvs-search-result.interface';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Vektor pool kezelő utility class
|
|
6
|
+
* Instance-based class, amely in-memory vektor pool-t kezel és vektor hasonlósági számításokat végez
|
|
7
|
+
* Minden instance saját vektor pool-t tart fenn
|
|
8
|
+
*/
|
|
9
|
+
export class LVS_VectorPool_ControlService {
|
|
10
|
+
/**
|
|
11
|
+
* In-memory vektor pool tárolás
|
|
12
|
+
* Key: vektor ID, Value: vektor értékek
|
|
13
|
+
*/
|
|
14
|
+
private vectorPool: Map<string, number[]> = new Map<string, number[]>();
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Normalizált vektor pool tárolás (L2 normalizált másolatok)
|
|
18
|
+
* Key: vektor ID, Value: L2 normalizált vektor értékek
|
|
19
|
+
*/
|
|
20
|
+
private normalizedVectorPool: Map<string, number[]> = new Map<string, number[]>();
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Hozzáad egy vektort a pool-hoz
|
|
24
|
+
* Opcionálisan L2 normalizált másolatot is tárol
|
|
25
|
+
*/
|
|
26
|
+
addVector(id: string, vector: number[]): void {
|
|
27
|
+
if (!id) {
|
|
28
|
+
throw new Error('Vector ID is required');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (!Array.isArray(vector) || vector.length === 0) {
|
|
32
|
+
throw new Error('Vector must be a non-empty array');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Validáljuk, hogy a vektor számokból áll
|
|
36
|
+
for (let i: number = 0; i < vector.length; i++) {
|
|
37
|
+
if (typeof vector[i] !== 'number' || !isFinite(vector[i])) {
|
|
38
|
+
throw new Error(`Vector must contain only finite numbers at index ${i}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Tároljuk az eredeti vektort
|
|
43
|
+
this.vectorPool.set(id, vector);
|
|
44
|
+
|
|
45
|
+
// L2 normalizált másolatot is tárolunk a cosine similarity számításokhoz
|
|
46
|
+
const normalized: number[] = LVS_VectorPool_ControlService.l2Normalize(vector);
|
|
47
|
+
this.normalizedVectorPool.set(id, normalized);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Eltávolít egy vektort a pool-ból
|
|
52
|
+
*/
|
|
53
|
+
removeVector(id: string): void {
|
|
54
|
+
this.vectorPool.delete(id);
|
|
55
|
+
this.normalizedVectorPool.delete(id);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Frissít egy vektort a pool-ban
|
|
60
|
+
*/
|
|
61
|
+
updateVector(id: string, vector: number[]): void {
|
|
62
|
+
if (!this.vectorPool.has(id)) {
|
|
63
|
+
throw new Error(`Vector with ID "${id}" does not exist`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
this.addVector(id, vector);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Törli az összes vektort a pool-ból
|
|
71
|
+
*/
|
|
72
|
+
clearPool(): void {
|
|
73
|
+
this.vectorPool.clear();
|
|
74
|
+
this.normalizedVectorPool.clear();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Visszaadja az összes vektort a pool-ból
|
|
79
|
+
* Debugging és export céljára
|
|
80
|
+
*/
|
|
81
|
+
getAll(): Map<string, number[]> {
|
|
82
|
+
return new Map<string, number[]>(this.vectorPool);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* L2 normalizálás egy vektorra
|
|
87
|
+
* A vektor hosszát 1-re normalizálja
|
|
88
|
+
* Statikus metódus, mivel nincs szüksége instance state-re
|
|
89
|
+
*/
|
|
90
|
+
private static l2Normalize(vector: number[]): number[] {
|
|
91
|
+
const magnitude: number = Math.sqrt(
|
|
92
|
+
vector.reduce((sum: number, value: number) => sum + value * value, 0)
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
if (magnitude === 0) {
|
|
96
|
+
// Ha a vektor nullvektor, akkor nullvektort adunk vissza
|
|
97
|
+
return new Array<number>(vector.length).fill(0);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return vector.map((value: number) => value / magnitude);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Cosine similarity számítás két vektor között
|
|
105
|
+
* Normalizált vektorokat használ a számításhoz
|
|
106
|
+
* Eredmény: 0 és 1 közötti érték, ahol 1 a legnagyobb hasonlóság
|
|
107
|
+
* Statikus metódus, mivel nincs szüksége instance state-re
|
|
108
|
+
*/
|
|
109
|
+
static cosineSimilarity(a: number[], b: number[]): number {
|
|
110
|
+
if (a.length !== b.length) {
|
|
111
|
+
throw new Error(
|
|
112
|
+
`Vectors must have the same dimension. Got ${a.length} and ${b.length}`
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Normalizáljuk a vektorokat
|
|
117
|
+
const normalizedA: number[] = this.l2Normalize(a);
|
|
118
|
+
const normalizedB: number[] = this.l2Normalize(b);
|
|
119
|
+
|
|
120
|
+
// Dot product számítása
|
|
121
|
+
let dotProduct: number = 0;
|
|
122
|
+
for (let i: number = 0; i < normalizedA.length; i++) {
|
|
123
|
+
dotProduct += normalizedA[i] * normalizedB[i];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Cosine similarity = dot product of normalized vectors
|
|
127
|
+
// Mivel a vektorok normalizáltak, ez egyenlő a cosine similarity-vel
|
|
128
|
+
return dotProduct;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* L2 distance (Euklideszi távolság) számítás két vektor között
|
|
133
|
+
* Eredmény: 0 vagy pozitív érték, ahol 0 a legkisebb távolság
|
|
134
|
+
* Statikus metódus, mivel nincs szüksége instance state-re
|
|
135
|
+
*/
|
|
136
|
+
static l2Distance(a: number[], b: number[]): number {
|
|
137
|
+
if (a.length !== b.length) {
|
|
138
|
+
throw new Error(
|
|
139
|
+
`Vectors must have the same dimension. Got ${a.length} and ${b.length}`
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// L2 distance = sqrt(sum((a[i] - b[i])^2))
|
|
144
|
+
let sumSquaredDiff: number = 0;
|
|
145
|
+
for (let i: number = 0; i < a.length; i++) {
|
|
146
|
+
const diff: number = a[i] - b[i];
|
|
147
|
+
sumSquaredDiff += diff * diff;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return Math.sqrt(sumSquaredDiff);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Vektor keresés a pool-ban
|
|
155
|
+
* Brute-force megközelítés (lineáris keresés)
|
|
156
|
+
*
|
|
157
|
+
* @param query A keresési query vektor
|
|
158
|
+
* @param k A visszaadandó találatok száma (top-K)
|
|
159
|
+
* @param mode A keresési mód (cosine similarity vagy L2 distance)
|
|
160
|
+
* @returns A top-K találatok ID-val és score-rel, megfelelő sorrendben
|
|
161
|
+
*/
|
|
162
|
+
search(
|
|
163
|
+
query: number[],
|
|
164
|
+
k: number,
|
|
165
|
+
mode: LVS_Search_Mode
|
|
166
|
+
): LVS_SearchResult[] {
|
|
167
|
+
if (!Array.isArray(query) || query.length === 0) {
|
|
168
|
+
throw new Error('Query vector must be a non-empty array');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (k <= 0) {
|
|
172
|
+
throw new Error('k must be a positive number');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (this.vectorPool.size === 0) {
|
|
176
|
+
return [];
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// Validáljuk, hogy minden vektor ugyanolyan dimenziójú-e
|
|
180
|
+
const firstVector: number[] | undefined = Array.from(this.vectorPool.values())[0];
|
|
181
|
+
if (firstVector && firstVector.length !== query.length) {
|
|
182
|
+
throw new Error(
|
|
183
|
+
`Query vector dimension (${query.length}) does not match pool vector ` +
|
|
184
|
+
`dimension (${firstVector.length})`
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Számoljuk ki a hasonlóságot/távolságot minden vektorra
|
|
189
|
+
const results: LVS_SearchResult[] = [];
|
|
190
|
+
|
|
191
|
+
for (const [id, vector] of this.vectorPool.entries()) {
|
|
192
|
+
let score: number;
|
|
193
|
+
|
|
194
|
+
if (mode === LVS_Search_Mode.cosineSimilarity) {
|
|
195
|
+
// Cosine similarity: nagyobb érték = jobb hasonlóság
|
|
196
|
+
score = LVS_VectorPool_ControlService.cosineSimilarity(query, vector);
|
|
197
|
+
} else if (mode === LVS_Search_Mode.l2Distance) {
|
|
198
|
+
// L2 distance: kisebb érték = jobb hasonlóság
|
|
199
|
+
score = LVS_VectorPool_ControlService.l2Distance(query, vector);
|
|
200
|
+
} else {
|
|
201
|
+
throw new Error(`Unknown search mode: ${mode}`);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
results.push({ id, score });
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// Rendezzük az eredményeket
|
|
208
|
+
if (mode === LVS_Search_Mode.cosineSimilarity) {
|
|
209
|
+
// Cosine similarity: csökkenő sorrend (legnagyobb hasonlóság először)
|
|
210
|
+
results.sort((a: LVS_SearchResult, b: LVS_SearchResult) => b.score - a.score);
|
|
211
|
+
} else {
|
|
212
|
+
// L2 distance: növekvő sorrend (legkisebb távolság először)
|
|
213
|
+
results.sort((a: LVS_SearchResult, b: LVS_SearchResult) => a.score - b.score);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Visszaadjuk a top-K találatot
|
|
217
|
+
return results.slice(0, k);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
// ENUMS
|
|
4
|
+
export * from './_enums/lvs-search-mode.enum';
|
|
5
|
+
|
|
6
|
+
// INTERFACES
|
|
7
|
+
export * from './_models/lvs-search-result.interface';
|
|
8
|
+
|
|
9
|
+
// SERVICES
|
|
10
|
+
export * from './_services/lvs-doc-chunk-data.service';
|
|
11
|
+
export * from './_services/lvs-local-vector-search.data-service';
|
|
12
|
+
export * from './_services/lvs-vector-pool.control-service';
|
|
@@ -804,10 +804,8 @@ export class DyNTS_DataService<T extends DyFM_Metadata> {
|
|
|
804
804
|
return data;
|
|
805
805
|
}
|
|
806
806
|
|
|
807
|
-
async patchData(data?: T): Promise<T> {
|
|
807
|
+
async patchData(data?: Partial<T>): Promise<T> {
|
|
808
808
|
try {
|
|
809
|
-
data = this.ensureData(data);
|
|
810
|
-
|
|
811
809
|
if (!data._id) {
|
|
812
810
|
throw new DyFM_Error({
|
|
813
811
|
...this._getDefaultErrorSettings(
|
|
@@ -828,6 +826,17 @@ export class DyNTS_DataService<T extends DyFM_Metadata> {
|
|
|
828
826
|
});
|
|
829
827
|
}
|
|
830
828
|
|
|
829
|
+
for (const key in this.depSettings) {
|
|
830
|
+
if (data[key] && data[key] !== dataExists[key]) {
|
|
831
|
+
throw new DyFM_Error({
|
|
832
|
+
...this._getDefaultErrorSettings(
|
|
833
|
+
'patchData',
|
|
834
|
+
new Error(`Cannot patch data: dependency data mismatch! (${this.dataParams.dataName})`)
|
|
835
|
+
),
|
|
836
|
+
});
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
|
|
831
840
|
DyFM_Object.cleanAssign(dataExists, data);
|
|
832
841
|
|
|
833
842
|
await this.validateForSave(dataExists);
|