@futdevpro/nts-dynamo 1.14.63 → 1.14.65

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.
Files changed (36) hide show
  1. package/build/_modules/local-vector-search/_enums/lvs-search-mode.enum.d.ts +19 -0
  2. package/build/_modules/local-vector-search/_enums/lvs-search-mode.enum.d.ts.map +1 -0
  3. package/build/_modules/local-vector-search/_enums/lvs-search-mode.enum.js +23 -0
  4. package/build/_modules/local-vector-search/_enums/lvs-search-mode.enum.js.map +1 -0
  5. package/build/_modules/local-vector-search/_models/lvs-search-result.interface.d.ts +17 -0
  6. package/build/_modules/local-vector-search/_models/lvs-search-result.interface.d.ts.map +1 -0
  7. package/build/_modules/local-vector-search/_models/lvs-search-result.interface.js +3 -0
  8. package/build/_modules/local-vector-search/_models/lvs-search-result.interface.js.map +1 -0
  9. package/build/_modules/local-vector-search/_services/lvs-doc-chunk-data.service.d.ts +27 -0
  10. package/build/_modules/local-vector-search/_services/lvs-doc-chunk-data.service.d.ts.map +1 -0
  11. package/build/_modules/local-vector-search/_services/lvs-doc-chunk-data.service.js +198 -0
  12. package/build/_modules/local-vector-search/_services/lvs-doc-chunk-data.service.js.map +1 -0
  13. package/build/_modules/local-vector-search/_services/lvs-local-vector-search.data-service.d.ts +130 -0
  14. package/build/_modules/local-vector-search/_services/lvs-local-vector-search.data-service.d.ts.map +1 -0
  15. package/build/_modules/local-vector-search/_services/lvs-local-vector-search.data-service.js +241 -0
  16. package/build/_modules/local-vector-search/_services/lvs-local-vector-search.data-service.js.map +1 -0
  17. package/build/_modules/local-vector-search/_services/lvs-vector-pool.control-service.d.ts +71 -0
  18. package/build/_modules/local-vector-search/_services/lvs-vector-pool.control-service.d.ts.map +1 -0
  19. package/build/_modules/local-vector-search/_services/lvs-vector-pool.control-service.js +182 -0
  20. package/build/_modules/local-vector-search/_services/lvs-vector-pool.control-service.js.map +1 -0
  21. package/build/_modules/local-vector-search/index.d.ts +6 -0
  22. package/build/_modules/local-vector-search/index.d.ts.map +1 -0
  23. package/build/_modules/local-vector-search/index.js +12 -0
  24. package/build/_modules/local-vector-search/index.js.map +1 -0
  25. package/build/_services/base/data.service.d.ts +1 -0
  26. package/build/_services/base/data.service.d.ts.map +1 -1
  27. package/build/_services/base/data.service.js +26 -1
  28. package/build/_services/base/data.service.js.map +1 -1
  29. package/package.json +12 -1
  30. package/src/_modules/local-vector-search/_enums/lvs-search-mode.enum.ts +19 -0
  31. package/src/_modules/local-vector-search/_models/lvs-search-result.interface.ts +17 -0
  32. package/src/_modules/local-vector-search/_services/lvs-doc-chunk-data.service.ts +276 -0
  33. package/src/_modules/local-vector-search/_services/lvs-local-vector-search.data-service.ts +330 -0
  34. package/src/_modules/local-vector-search/_services/lvs-vector-pool.control-service.ts +220 -0
  35. package/src/_modules/local-vector-search/index.ts +12 -0
  36. package/src/_services/base/data.service.ts +40 -1
@@ -0,0 +1,330 @@
1
+ import { DyFM_Log, DyFM_Error } from '@futdevpro/fsm-dynamo';
2
+ import {
3
+ DyFM_DataModel_Params,
4
+ DyFM_DataProperty_Params,
5
+ DyFM_DBFilter,
6
+ DyFM_Metadata,
7
+ } from '@futdevpro/fsm-dynamo';
8
+ import { DyFM_OAI_Settings } from '@futdevpro/fsm-dynamo/ai/open-ai';
9
+
10
+ import { LVS_Search_Mode } from '../_enums/lvs-search-mode.enum';
11
+ import { LVS_SearchResult } from '../_models/lvs-search-result.interface';
12
+ import { LVS_VectorPool_ControlService } from './lvs-vector-pool.control-service';
13
+ import { DyNTS_OAI_VectorDataService } from '../../ai/_modules/open-ai/_services/data-services/oai-vector-data.service';
14
+ import { DyNTS_global_settings } from '../../../_collections/global-settings.const';
15
+
16
+ /**
17
+ * Local Vector Data Service
18
+ * Extends {@link DyNTS_OAI_VectorDataService} to perform in-memory vector similarity search
19
+ * instead of MongoDB vector search
20
+ *
21
+ * This service loads data from the database, builds an in-memory vector pool,
22
+ * and performs brute-force vector similarity search using cosine similarity or L2 distance
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * const service = new DyNTS_OAI_LocalVectorDataService(
27
+ * data,
28
+ * dataParams,
29
+ * openAISettings,
30
+ * issuer
31
+ * );
32
+ *
33
+ * const results = await service.vectorSearch({
34
+ * input: 'search query',
35
+ * searchInKey: 'contentVectorized',
36
+ * limit: 10,
37
+ * filterBy: { category: 'docs' }
38
+ * });
39
+ * ```
40
+ */
41
+ export class DyNTS_LVS_VectorDataService<T extends DyFM_Metadata> extends DyNTS_OAI_VectorDataService<T> {
42
+
43
+ /**
44
+ * Default search mode
45
+ * Cosine similarity is typically preferred for text embeddings
46
+ */
47
+ defaultSearchMode: LVS_Search_Mode = LVS_Search_Mode.cosineSimilarity;
48
+
49
+ /**
50
+ * Whether to use L2 normalization when storing vectors
51
+ * Normalized vectors are more efficient for cosine similarity calculations
52
+ */
53
+ useL2Normalization: boolean = true;
54
+
55
+ /**
56
+ * Instance-based vector pool utility
57
+ * Minden service instance saját vektor pool-t tart fenn
58
+ */
59
+ private vectorPool: LVS_VectorPool_ControlService = new LVS_VectorPool_ControlService();
60
+
61
+ constructor(
62
+ /**
63
+ * Initial data, this will be used by functions on default
64
+ */
65
+ data: T,
66
+ /**
67
+ * DB data params will be used to connect to usable dbService on GlobalService
68
+ */
69
+ dataParams: DyFM_DataModel_Params<T>,
70
+ /**
71
+ * OpenAI settings
72
+ */
73
+ openAISettings: DyFM_OAI_Settings,
74
+ /**
75
+ * Initial set for issuer to be able to follow the issuer's activity
76
+ */
77
+ issuer: string
78
+ ) {
79
+ super(data, dataParams, openAISettings, issuer);
80
+ }
81
+
82
+ /**
83
+ * Overrides the parent vectorSearch method to perform local in-memory vector search
84
+ *
85
+ * Process:
86
+ * 1. Load data from DB using filterBy (if provided) or getAll()
87
+ * 2. Extract vectorized properties from loaded data
88
+ * 3. Build in-memory vector pool
89
+ * 4. Vectorize input query
90
+ * 5. Perform local vector search
91
+ * 6. Map search results back to full data objects
92
+ *
93
+ * @param set Search parameters
94
+ * @returns Array of data objects sorted by similarity score
95
+ */
96
+ override async vectorSearch(
97
+ set: {
98
+ input: string;
99
+ /** this should be the vectorized property key */
100
+ searchInKey: string;
101
+ limit?: number;
102
+ /**
103
+ * Number of candidates that are used to find the best match
104
+ * (Not used in local search, but kept for compatibility)
105
+ */
106
+ numberOfCandidates?: number;
107
+ /**
108
+ * Filter to narrow down candidates from DB before vector search
109
+ */
110
+ filterBy?: DyFM_DBFilter<T>;
111
+ /**
112
+ * Search mode (cosine similarity or L2 distance)
113
+ * Defaults to this.defaultSearchMode
114
+ */
115
+ searchMode?: LVS_Search_Mode;
116
+ },
117
+ ): Promise<T[]> {
118
+ try {
119
+ if (!set.input) {
120
+ throw new DyFM_Error({
121
+ ...this.getDefaultErrorSettings(
122
+ 'vectorSearch',
123
+ new Error('input is required')
124
+ ),
125
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-LVS-VS1`,
126
+ });
127
+ }
128
+
129
+ set.limit ??= 3;
130
+ const { input, searchInKey, limit, filterBy, searchMode } = set;
131
+
132
+ // Validáljuk, hogy a searchInKey létezik-e
133
+ const property: DyFM_DataProperty_Params<any, T> =
134
+ this.dataParams.properties[searchInKey];
135
+
136
+ if (!property) {
137
+ throw new DyFM_Error({
138
+ ...this.getDefaultErrorSettings(
139
+ 'vectorSearch',
140
+ new Error(
141
+ `Property "${searchInKey}" not found! ` +
142
+ `while searching "${this.dataParams.dataName}" ` +
143
+ `(The searchable properties are: ` +
144
+ `${this.vectorizedProperties.map(p => `"${p.key}"`).join(', ')})`
145
+ )
146
+ ),
147
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-LVS-VS2`,
148
+ __localStack: this.dataParams.stackLocation,
149
+ });
150
+ }
151
+
152
+ // Ellenőrizzük, hogy a property vectorized-e
153
+ if (!this.vectorizedProperties.some(
154
+ (p: DyFM_DataProperty_Params<any, T>) => p.key === searchInKey
155
+ )) {
156
+ throw new DyFM_Error({
157
+ ...this.getDefaultErrorSettings(
158
+ 'vectorSearch',
159
+ new Error(
160
+ `Property "${searchInKey}" is not a vectorized property! ` +
161
+ `(The vectorized properties are: ` +
162
+ `${this.vectorizedProperties.map(p => `"${p.key}"`).join(', ')})`
163
+ )
164
+ ),
165
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-LVS-VS3`,
166
+ __localStack: this.dataParams.stackLocation,
167
+ });
168
+ }
169
+
170
+ // 1. Betöltjük az adatokat a DB-ből (filterBy-val szűrve, ha van)
171
+ let dataList: T[];
172
+ if (filterBy) {
173
+ // Ha van filterBy, akkor először szűrünk a DB-ben
174
+ if (this.debugLog) {
175
+ DyFM_Log.log(`Local vector search: filtering data with filterBy...`);
176
+ }
177
+ dataList = await this.findDataList(filterBy, true);
178
+ } else {
179
+ // Ha nincs filterBy, akkor minden adatot betöltünk
180
+ if (this.debugLog) {
181
+ DyFM_Log.log(`Local vector search: loading all data...`);
182
+ }
183
+ dataList = await this.getAll(true);
184
+ }
185
+
186
+ if (dataList.length === 0) {
187
+ if (this.debugLog) {
188
+ DyFM_Log.log(`Local vector search: no data found`);
189
+ }
190
+ return [];
191
+ }
192
+
193
+ // 2. Kinyerjük a vectorized property-ket és építjük a vector pool-t
194
+ if (this.debugLog) {
195
+ DyFM_Log.log(
196
+ `Local vector search: building vector pool from ${dataList.length} items...`
197
+ );
198
+ }
199
+
200
+ // Töröljük a korábbi pool-t
201
+ this.vectorPool.clearPool();
202
+
203
+ // Hozzáadjuk a vektorokat a pool-hoz
204
+ const dataMap: Map<string, T> = new Map<string, T>();
205
+ for (const dataItem of dataList) {
206
+ if (!dataItem._id) {
207
+ // Ha nincs _id, akkor kihagyjuk
208
+ if (this.debugLog) {
209
+ DyFM_Log.warn(
210
+ `Local vector search: skipping item without _id`
211
+ );
212
+ }
213
+ continue;
214
+ }
215
+
216
+ const vectorizedValue: number[] | undefined =
217
+ dataItem[searchInKey] as number[] | undefined;
218
+
219
+ if (!vectorizedValue || !Array.isArray(vectorizedValue)) {
220
+ // Ha nincs vectorized érték, akkor kihagyjuk
221
+ if (this.debugLog) {
222
+ DyFM_Log.warn(
223
+ `Local vector search: skipping item "${dataItem._id}" ` +
224
+ `without vectorized value for "${searchInKey}"`
225
+ );
226
+ }
227
+ continue;
228
+ }
229
+
230
+ // Hozzáadjuk a vektort a pool-hoz
231
+ this.vectorPool.addVector(dataItem._id, vectorizedValue);
232
+ dataMap.set(dataItem._id, dataItem);
233
+ }
234
+
235
+ if (dataMap.size === 0) {
236
+ if (this.debugLog) {
237
+ DyFM_Log.log(`Local vector search: no valid vectors found`);
238
+ }
239
+ return [];
240
+ }
241
+
242
+ // 3. Vectorizáljuk a query input-ot
243
+ if (this.debugLog) {
244
+ DyFM_Log.log(`Local vector search: vectorizing query input...`);
245
+ }
246
+
247
+ const queryVector: number[] = await this.vectorize(
248
+ input,
249
+ property.embeddingModel
250
+ );
251
+
252
+ // 4. Végrehajtjuk a local vector search-t
253
+ const mode: LVS_Search_Mode = searchMode ?? this.defaultSearchMode;
254
+
255
+ if (this.debugLog) {
256
+ DyFM_Log.log(
257
+ `Local vector search: searching with mode "${mode}", limit: ${limit}...`
258
+ );
259
+ }
260
+
261
+ const searchResults: LVS_SearchResult[] = this.vectorPool.search(
262
+ queryVector,
263
+ limit,
264
+ mode
265
+ );
266
+
267
+ if (this.debugLog) {
268
+ DyFM_Log.log(
269
+ `Local vector search: found ${searchResults.length} results`
270
+ );
271
+ }
272
+
273
+ // 5. Map-eljük vissza a search results-ot a teljes data objektumokra
274
+ const results: T[] = [];
275
+ for (const searchResult of searchResults) {
276
+ const dataItem: T | undefined = dataMap.get(searchResult.id);
277
+ if (dataItem) {
278
+ results.push(dataItem);
279
+ }
280
+ }
281
+
282
+ // Töröljük a pool-t a memória felszabadítása érdekében
283
+ this.vectorPool.clearPool();
284
+
285
+ return results;
286
+ } catch (error) {
287
+ // Töröljük a pool-t hiba esetén is
288
+ this.vectorPool.clearPool();
289
+
290
+ throw new DyFM_Error({
291
+ ...this.getDefaultErrorSettings('vectorSearch', error),
292
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-LVS-VS0`,
293
+ });
294
+ }
295
+ }
296
+ }
297
+
298
+ /**
299
+ * @example
300
+ * ```typescript
301
+ * // Create a local vector data service
302
+ * const localVectorService = new DyNTS_OAI_LocalVectorDataService<CodeChunk>(
303
+ * new CodeChunk(),
304
+ * codeChunk_dataParams,
305
+ * openAISettings,
306
+ * 'example-issuer'
307
+ * );
308
+ *
309
+ * // Perform vector search with filterBy
310
+ * const results = await localVectorService.vectorSearch({
311
+ * input: 'How to implement authentication?',
312
+ * searchInKey: 'chunkParentedContentVectorized',
313
+ * limit: 5,
314
+ * filterBy: {
315
+ * // Filter by category before vector search
316
+ * category: 'documentation'
317
+ * },
318
+ * searchMode: LVS_Search_Mode.cosineSimilarity
319
+ * });
320
+ *
321
+ * // Results are sorted by similarity score (highest first for cosine)
322
+ * console.log(`Found ${results.length} similar chunks`);
323
+ *
324
+ * // Access the results
325
+ * for (const result of results) {
326
+ * console.log(`Chunk ID: ${result._id}, Content: ${result.chunkContent}`);
327
+ * }
328
+ * ```
329
+ */
330
+
@@ -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';
@@ -17,6 +17,7 @@ import {
17
17
  DyFM_Log,
18
18
  DyFM_Metadata,
19
19
  DyFM_NestPropertySearch,
20
+ DyFM_Object,
20
21
  DyFM_RangeValue,
21
22
  DyFM_SearchQuery,
22
23
  DyFM_SearchResult,
@@ -792,7 +793,7 @@ export class DyNTS_DataService<T extends DyFM_Metadata> {
792
793
  if (!data) {
793
794
  throw new DyFM_Error({
794
795
  ...this._getDefaultErrorSettings(
795
- 'saveData',
796
+ 'ensureData',
796
797
  new Error(`no data to save! (${this.dataParams.dataName})`)
797
798
  ),
798
799
 
@@ -803,6 +804,44 @@ export class DyNTS_DataService<T extends DyFM_Metadata> {
803
804
  return data;
804
805
  }
805
806
 
807
+ async patchData(data?: T): Promise<T> {
808
+ try {
809
+ data = this.ensureData(data);
810
+
811
+ if (!data._id) {
812
+ throw new DyFM_Error({
813
+ ...this._getDefaultErrorSettings(
814
+ 'patchData',
815
+ new Error(`no ID to patch data! (${this.dataParams.dataName})`)
816
+ ),
817
+ });
818
+ }
819
+
820
+ const dataExists = await this.getDataById(data._id, true);
821
+
822
+ if (!dataExists) {
823
+ throw new DyFM_Error({
824
+ ...this._getDefaultErrorSettings(
825
+ 'patchData',
826
+ new Error(`data not found! (${this.dataParams.dataName})`)
827
+ ),
828
+ });
829
+ }
830
+
831
+ DyFM_Object.cleanAssign(dataExists, data);
832
+
833
+ await this.validateForSave(dataExists);
834
+
835
+ return await this.dataDBService.modifyData(dataExists, this.issuer);
836
+ } catch (error) {
837
+ throw new DyFM_Error({
838
+ ...this._getDefaultErrorSettings('patchData', error),
839
+
840
+ errorCode: `${DyNTS_global_settings.systemShortCodeName}|DyNTS-DS0-PD0`,
841
+ });
842
+ }
843
+ }
844
+
806
845
  /**
807
846
  * modifies data if the data have ID and already exists in the DB,
808
847
  * creates new if the ID is not present or cant find in DB,