vectra 0.1.0 → 0.1.1
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/README.md +86 -1
- package/package.json +1 -1
- package/lib/ItemSelector.d.ts +0 -41
- package/lib/ItemSelector.d.ts.map +0 -1
- package/lib/ItemSelector.js +0 -156
- package/lib/ItemSelector.js.map +0 -1
- package/lib/LocalIndex.d.ts +0 -184
- package/lib/LocalIndex.d.ts.map +0 -1
- package/lib/LocalIndex.js +0 -392
- package/lib/LocalIndex.js.map +0 -1
- package/lib/index.d.ts +0 -3
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -15
- package/lib/index.js.map +0 -1
- package/lib/utilities.d.ts +0 -5
- package/lib/utilities.d.ts.map +0 -1
- package/lib/utilities.js +0 -38
- package/lib/utilities.js.map +0 -1
package/README.md
CHANGED
|
@@ -7,4 +7,89 @@ Keep in mind that your entire Vectra index is loaded into memory so it's not wel
|
|
|
7
7
|
|
|
8
8
|
Pinecone style namespaces aren't directly supported but you could easily mimic them by creating a separate Vectra index (and folder) for each namespace.
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
$ npm install vectra
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
First create an instance of `LocalIndex` with the path to the folder where you want you're items stored:
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import { LocalIndex } from 'vectra';
|
|
22
|
+
|
|
23
|
+
const index = new LocalIndex(path.join(__dirname, '..', 'index'));
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Next, from inside an async function, create your index:
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
if (!await index.isIndexCreated()) {
|
|
30
|
+
await index.createIndex();
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Add some items to your index:
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { OpenAIApi, Configuration } from 'openai';
|
|
38
|
+
|
|
39
|
+
const configuration = new Configuration({
|
|
40
|
+
apiKey: `<YOUR_KEY>`,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const api = new OpenAIApi(configuration);
|
|
44
|
+
|
|
45
|
+
async function getVector(text: string) {
|
|
46
|
+
const response = await api.createEmbedding({
|
|
47
|
+
'model': 'text-embedding-ada-002',
|
|
48
|
+
'input': text,
|
|
49
|
+
});
|
|
50
|
+
return response.data.data[0].embedding;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function addItem(text: string) {
|
|
54
|
+
await index.insertItem({
|
|
55
|
+
vector: await getVector(text),
|
|
56
|
+
metadata: { text }
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Add items
|
|
61
|
+
await addItem('apple');
|
|
62
|
+
await addItem('oranges');
|
|
63
|
+
await addItem('red');
|
|
64
|
+
await addItem('blue');
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Then query for items:
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
async function query(text: string) {
|
|
71
|
+
const vector = await getVector(input);
|
|
72
|
+
const results = await index.queryItems(vector, 3);
|
|
73
|
+
if (results.length > 0) {
|
|
74
|
+
for (const result of results) {
|
|
75
|
+
console.log(`[${result.score}] ${result.item.metadata.text}`);
|
|
76
|
+
}
|
|
77
|
+
} else {
|
|
78
|
+
console.log(`No results found.`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
await query('green');
|
|
83
|
+
/*
|
|
84
|
+
[0.9036569942401076] blue
|
|
85
|
+
[0.8758153664568566] red
|
|
86
|
+
[0.8323828606103998] apple
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
await query('banana');
|
|
90
|
+
/*
|
|
91
|
+
[0.9033128691220631] apple
|
|
92
|
+
[0.8493374123092652] oranges
|
|
93
|
+
[0.8415324469533297] blue
|
|
94
|
+
*/
|
|
95
|
+
```
|
package/package.json
CHANGED
package/lib/ItemSelector.d.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { MetadataFilter, MetadataTypes } from './LocalIndex';
|
|
2
|
-
export declare class ItemSelector {
|
|
3
|
-
/**
|
|
4
|
-
* Returns the similarity between two vectors using the cosine similarity.
|
|
5
|
-
* @param vector1 Vector 1
|
|
6
|
-
* @param vector2 Vector 2
|
|
7
|
-
* @returns Similarity between the two vectors
|
|
8
|
-
*/
|
|
9
|
-
static cosineSimilarity(vector1: number[], vector2: number[]): number;
|
|
10
|
-
/**
|
|
11
|
-
* Normalizes a vector.
|
|
12
|
-
* @remarks
|
|
13
|
-
* The norm of a vector is the square root of the sum of the squares of the elements.
|
|
14
|
-
* The LocalIndex pre-normalizes all vectors to improve performance.
|
|
15
|
-
* @param vector Vector to normalize
|
|
16
|
-
* @returns Normalized vector
|
|
17
|
-
*/
|
|
18
|
-
static normalize(vector: number[]): number;
|
|
19
|
-
/**
|
|
20
|
-
* Returns the similarity between two vectors using cosine similarity.
|
|
21
|
-
* @remarks
|
|
22
|
-
* The LocalIndex pre-normalizes all vectors to improve performance.
|
|
23
|
-
* This method uses the pre-calculated norms to improve performance.
|
|
24
|
-
* @param vector1 Vector 1
|
|
25
|
-
* @param norm1 Norm of vector 1
|
|
26
|
-
* @param vector2 Vector 2
|
|
27
|
-
* @param norm2 Norm of vector 2
|
|
28
|
-
* @returns Similarity between the two vectors
|
|
29
|
-
*/
|
|
30
|
-
static normalizedCosineSimilarity(vector1: number[], norm1: number, vector2: number[], norm2: number): number;
|
|
31
|
-
/**
|
|
32
|
-
* Applies a filter to the metadata of an item.
|
|
33
|
-
* @param metadata Metadata of the item
|
|
34
|
-
* @param filter Filter to apply
|
|
35
|
-
* @returns True if the item matches the filter, false otherwise
|
|
36
|
-
*/
|
|
37
|
-
static select(metadata: Record<string, MetadataTypes>, filter: MetadataFilter): boolean;
|
|
38
|
-
private static dotProduct;
|
|
39
|
-
private static metadataFilter;
|
|
40
|
-
}
|
|
41
|
-
//# sourceMappingURL=ItemSelector.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ItemSelector.d.ts","sourceRoot":"","sources":["../src/ItemSelector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7D,qBAAa,YAAY;IACrB;;;;;OAKG;WACW,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE;IAKnE;;;;;;;OAOG;WACW,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE;IAYxC;;;;;;;;;;OAUG;WACW,0BAA0B,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM;IAK3G;;;;;OAKG;WACW,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,EAAE,MAAM,EAAE,cAAc,GAAG,OAAO;IAoC9F,OAAO,CAAC,MAAM,CAAC,UAAU;IAYzB,OAAO,CAAC,MAAM,CAAC,cAAc;CAqDhC"}
|
package/lib/ItemSelector.js
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ItemSelector = void 0;
|
|
4
|
-
class ItemSelector {
|
|
5
|
-
/**
|
|
6
|
-
* Returns the similarity between two vectors using the cosine similarity.
|
|
7
|
-
* @param vector1 Vector 1
|
|
8
|
-
* @param vector2 Vector 2
|
|
9
|
-
* @returns Similarity between the two vectors
|
|
10
|
-
*/
|
|
11
|
-
static cosineSimilarity(vector1, vector2) {
|
|
12
|
-
// Return the quotient of the dot product and the product of the norms
|
|
13
|
-
return this.dotProduct(vector1, vector2) / (this.normalize(vector1) * this.normalize(vector2));
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Normalizes a vector.
|
|
17
|
-
* @remarks
|
|
18
|
-
* The norm of a vector is the square root of the sum of the squares of the elements.
|
|
19
|
-
* The LocalIndex pre-normalizes all vectors to improve performance.
|
|
20
|
-
* @param vector Vector to normalize
|
|
21
|
-
* @returns Normalized vector
|
|
22
|
-
*/
|
|
23
|
-
static normalize(vector) {
|
|
24
|
-
// Initialize a variable to store the sum of the squares
|
|
25
|
-
let sum = 0;
|
|
26
|
-
// Loop through the elements of the array
|
|
27
|
-
for (let i = 0; i < vector.length; i++) {
|
|
28
|
-
// Square the element and add it to the sum
|
|
29
|
-
sum += vector[i] * vector[i];
|
|
30
|
-
}
|
|
31
|
-
// Return the square root of the sum
|
|
32
|
-
return Math.sqrt(sum);
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Returns the similarity between two vectors using cosine similarity.
|
|
36
|
-
* @remarks
|
|
37
|
-
* The LocalIndex pre-normalizes all vectors to improve performance.
|
|
38
|
-
* This method uses the pre-calculated norms to improve performance.
|
|
39
|
-
* @param vector1 Vector 1
|
|
40
|
-
* @param norm1 Norm of vector 1
|
|
41
|
-
* @param vector2 Vector 2
|
|
42
|
-
* @param norm2 Norm of vector 2
|
|
43
|
-
* @returns Similarity between the two vectors
|
|
44
|
-
*/
|
|
45
|
-
static normalizedCosineSimilarity(vector1, norm1, vector2, norm2) {
|
|
46
|
-
// Return the quotient of the dot product and the product of the norms
|
|
47
|
-
return this.dotProduct(vector1, vector2) / (norm1 * norm2);
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Applies a filter to the metadata of an item.
|
|
51
|
-
* @param metadata Metadata of the item
|
|
52
|
-
* @param filter Filter to apply
|
|
53
|
-
* @returns True if the item matches the filter, false otherwise
|
|
54
|
-
*/
|
|
55
|
-
static select(metadata, filter) {
|
|
56
|
-
if (filter === undefined || filter === null) {
|
|
57
|
-
return true;
|
|
58
|
-
}
|
|
59
|
-
for (const key in filter) {
|
|
60
|
-
switch (key) {
|
|
61
|
-
case '$and':
|
|
62
|
-
if (!filter[key].every((f) => this.select(metadata, f))) {
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
break;
|
|
66
|
-
case '$or':
|
|
67
|
-
if (!filter[key].some((f) => this.select(metadata, f))) {
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
break;
|
|
71
|
-
default:
|
|
72
|
-
const value = filter[key];
|
|
73
|
-
if (value === undefined || value === null) {
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
else if (typeof value == 'object') {
|
|
77
|
-
if (!this.metadataFilter(metadata[key], value)) {
|
|
78
|
-
return false;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
if (metadata[key] !== value) {
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
break;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return true;
|
|
90
|
-
}
|
|
91
|
-
static dotProduct(arr1, arr2) {
|
|
92
|
-
// Initialize a variable to store the sum of the products
|
|
93
|
-
let sum = 0;
|
|
94
|
-
// Loop through the elements of the arrays
|
|
95
|
-
for (let i = 0; i < arr1.length; i++) {
|
|
96
|
-
// Multiply the corresponding elements and add them to the sum
|
|
97
|
-
sum += arr1[i] * arr2[i];
|
|
98
|
-
}
|
|
99
|
-
// Return the sum
|
|
100
|
-
return sum;
|
|
101
|
-
}
|
|
102
|
-
static metadataFilter(value, filter) {
|
|
103
|
-
if (value === undefined || value === null) {
|
|
104
|
-
return false;
|
|
105
|
-
}
|
|
106
|
-
for (const key in filter) {
|
|
107
|
-
switch (key) {
|
|
108
|
-
case '$eq':
|
|
109
|
-
if (value !== filter[key]) {
|
|
110
|
-
return false;
|
|
111
|
-
}
|
|
112
|
-
break;
|
|
113
|
-
case '$ne':
|
|
114
|
-
if (value === filter[key]) {
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
break;
|
|
118
|
-
case '$gt':
|
|
119
|
-
if (typeof value != 'number' || value <= filter[key]) {
|
|
120
|
-
return false;
|
|
121
|
-
}
|
|
122
|
-
break;
|
|
123
|
-
case '$gte':
|
|
124
|
-
if (typeof value != 'number' || value < filter[key]) {
|
|
125
|
-
return false;
|
|
126
|
-
}
|
|
127
|
-
break;
|
|
128
|
-
case '$lt':
|
|
129
|
-
if (typeof value != 'number' || value >= filter[key]) {
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
break;
|
|
133
|
-
case '$lte':
|
|
134
|
-
if (typeof value != 'number' || value > filter[key]) {
|
|
135
|
-
return false;
|
|
136
|
-
}
|
|
137
|
-
break;
|
|
138
|
-
case '$in':
|
|
139
|
-
if (typeof value == 'boolean' || !filter[key].includes(value)) {
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
|
-
break;
|
|
143
|
-
case '$nin':
|
|
144
|
-
if (typeof value == 'boolean' || filter[key].includes(value)) {
|
|
145
|
-
return false;
|
|
146
|
-
}
|
|
147
|
-
break;
|
|
148
|
-
default:
|
|
149
|
-
return value === filter[key];
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
return true;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
exports.ItemSelector = ItemSelector;
|
|
156
|
-
//# sourceMappingURL=ItemSelector.js.map
|
package/lib/ItemSelector.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ItemSelector.js","sourceRoot":"","sources":["../src/ItemSelector.ts"],"names":[],"mappings":";;;AAEA,MAAa,YAAY;IACrB;;;;;OAKG;IACI,MAAM,CAAC,gBAAgB,CAAC,OAAiB,EAAE,OAAiB;QAC/D,sEAAsE;QACtE,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,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@
|
|
|
1
|
-
export interface CreateIndexConfig {
|
|
2
|
-
version: number;
|
|
3
|
-
deleteIfExists?: boolean;
|
|
4
|
-
metadata_config?: {
|
|
5
|
-
indexed?: string[];
|
|
6
|
-
};
|
|
7
|
-
}
|
|
8
|
-
export interface IndexStats {
|
|
9
|
-
version: number;
|
|
10
|
-
metadata_config: {
|
|
11
|
-
indexed?: string[];
|
|
12
|
-
};
|
|
13
|
-
items: number;
|
|
14
|
-
}
|
|
15
|
-
export interface IndexItem<TMetadata = Record<string, MetadataTypes>> {
|
|
16
|
-
id: string;
|
|
17
|
-
metadata: TMetadata;
|
|
18
|
-
vector: number[];
|
|
19
|
-
norm: number;
|
|
20
|
-
metadataFile?: string;
|
|
21
|
-
}
|
|
22
|
-
export interface QueryResult<TMetadata = Record<string, MetadataTypes>> {
|
|
23
|
-
item: IndexItem<TMetadata>;
|
|
24
|
-
score: number;
|
|
25
|
-
}
|
|
26
|
-
export interface MetadataFilter {
|
|
27
|
-
[key: string]: MetadataTypes | MetadataFilter | (number | string)[] | MetadataFilter[];
|
|
28
|
-
/**
|
|
29
|
-
* Equal to (number, string, boolean)
|
|
30
|
-
*/
|
|
31
|
-
'$eq': number | string | boolean;
|
|
32
|
-
/**
|
|
33
|
-
* Not equal to (number, string, boolean)
|
|
34
|
-
*/
|
|
35
|
-
'$ne': number | string | boolean;
|
|
36
|
-
/**
|
|
37
|
-
* Greater than (number)
|
|
38
|
-
*/
|
|
39
|
-
'$gt': number;
|
|
40
|
-
/**
|
|
41
|
-
* Greater than or equal to (number)
|
|
42
|
-
*/
|
|
43
|
-
'$gte': number;
|
|
44
|
-
/**
|
|
45
|
-
* Less than (number)
|
|
46
|
-
*/
|
|
47
|
-
'$lt': number;
|
|
48
|
-
/**
|
|
49
|
-
* Less than or equal to (number)
|
|
50
|
-
*/
|
|
51
|
-
'$lte': number;
|
|
52
|
-
/**
|
|
53
|
-
* In array (string or number)
|
|
54
|
-
*/
|
|
55
|
-
'$in': (number | string)[];
|
|
56
|
-
/**
|
|
57
|
-
* Not in array (string or number)
|
|
58
|
-
*/
|
|
59
|
-
'$nin': (number | string)[];
|
|
60
|
-
/**
|
|
61
|
-
* AND (MetadataFilter[])
|
|
62
|
-
*/
|
|
63
|
-
'$and': MetadataFilter[];
|
|
64
|
-
/**
|
|
65
|
-
* OR (MetadataFilter[])
|
|
66
|
-
*/
|
|
67
|
-
'$or': MetadataFilter[];
|
|
68
|
-
}
|
|
69
|
-
export declare type MetadataTypes = number | string | boolean;
|
|
70
|
-
/**
|
|
71
|
-
* Local vector index instance.
|
|
72
|
-
* @remarks
|
|
73
|
-
* This class is used to create, update, and query a local vector index.
|
|
74
|
-
* Each index is a folder on disk containing an index.json file and an optional set of metadata files.
|
|
75
|
-
*/
|
|
76
|
-
export declare class LocalIndex {
|
|
77
|
-
private readonly _folderPath;
|
|
78
|
-
private _data?;
|
|
79
|
-
private _update?;
|
|
80
|
-
/**
|
|
81
|
-
* Creates a new instance of LocalIndex.
|
|
82
|
-
* @param folderPath - Path to the index folder
|
|
83
|
-
*/
|
|
84
|
-
constructor(folderPath: string);
|
|
85
|
-
/**
|
|
86
|
-
* Begins an update to the index.
|
|
87
|
-
* @remarks
|
|
88
|
-
* This method loads the index into memory and prepares it for updates.
|
|
89
|
-
*/
|
|
90
|
-
beginUpdate(): Promise<void>;
|
|
91
|
-
/**
|
|
92
|
-
* Cancels an update to the index.
|
|
93
|
-
* @remarks
|
|
94
|
-
* This method discards any changes made to the index since the update began.
|
|
95
|
-
*/
|
|
96
|
-
cancelUpdate(): void;
|
|
97
|
-
/**
|
|
98
|
-
* Creates a new index.
|
|
99
|
-
* @remarks
|
|
100
|
-
* This method creates a new folder on disk containing an index.json file.
|
|
101
|
-
* @param config - Index configuration
|
|
102
|
-
*/
|
|
103
|
-
createIndex(config?: CreateIndexConfig): Promise<void>;
|
|
104
|
-
/**
|
|
105
|
-
* Deletes the index.
|
|
106
|
-
* @remarks
|
|
107
|
-
* This method deletes the index folder from disk.
|
|
108
|
-
*/
|
|
109
|
-
deleteIndex(): Promise<void>;
|
|
110
|
-
/**
|
|
111
|
-
* Deletes an item from the index.
|
|
112
|
-
* @param id - Item id
|
|
113
|
-
*/
|
|
114
|
-
deleteItem(id: string): Promise<void>;
|
|
115
|
-
/**
|
|
116
|
-
* Ends an update to the index.
|
|
117
|
-
* @remarks
|
|
118
|
-
* This method saves the index to disk.
|
|
119
|
-
*/
|
|
120
|
-
endUpdate(): Promise<void>;
|
|
121
|
-
/**
|
|
122
|
-
* Loads an index from disk and returns its stats.
|
|
123
|
-
* @returns Index stats
|
|
124
|
-
*/
|
|
125
|
-
getIndexStats(): Promise<IndexStats>;
|
|
126
|
-
/**
|
|
127
|
-
* Returns an item from the index given its ID.
|
|
128
|
-
* @param id Item id
|
|
129
|
-
* @returns Item or undefined if not found
|
|
130
|
-
*/
|
|
131
|
-
getItem<TMetadata = Record<string, MetadataTypes>>(id: string): Promise<IndexItem<TMetadata> | undefined>;
|
|
132
|
-
/**
|
|
133
|
-
* Adds an item to the index.
|
|
134
|
-
* @remarks
|
|
135
|
-
* A new update is started if one is not already in progress. If an item with the same ID
|
|
136
|
-
* already exists, an error will be thrown.
|
|
137
|
-
* @param item Item to insert
|
|
138
|
-
* @returns Inserted item
|
|
139
|
-
*/
|
|
140
|
-
insertItem<TMetadata = Record<string, MetadataTypes>>(item: Partial<IndexItem<TMetadata>>): Promise<IndexItem<TMetadata>>;
|
|
141
|
-
/**
|
|
142
|
-
* Returns true if the index exists.
|
|
143
|
-
*/
|
|
144
|
-
isIndexCreated(): Promise<boolean>;
|
|
145
|
-
/**
|
|
146
|
-
* Returns all items in the index.
|
|
147
|
-
* @remarks
|
|
148
|
-
* This method loads the index into memory and returns all its items. A copy of the items
|
|
149
|
-
* array is returned so no modifications should be made to the array.
|
|
150
|
-
* @returns All items in the index
|
|
151
|
-
*/
|
|
152
|
-
listItems<TMetadata = Record<string, MetadataTypes>>(): Promise<IndexItem<TMetadata>[]>;
|
|
153
|
-
/**
|
|
154
|
-
* Returns all items in the index matching the filter.
|
|
155
|
-
* @remarks
|
|
156
|
-
* This method loads the index into memory and returns all its items matching the filter.
|
|
157
|
-
* @param filter Filter to apply
|
|
158
|
-
* @returns Items matching the filter
|
|
159
|
-
*/
|
|
160
|
-
listItemsByMetadata<TMetadata = Record<string, MetadataTypes>>(filter: MetadataFilter): Promise<IndexItem<TMetadata>[]>;
|
|
161
|
-
/**
|
|
162
|
-
* Finds the top k items in the index that are most similar to the vector.
|
|
163
|
-
* @remarks
|
|
164
|
-
* This method loads the index into memory and returns the top k items that are most similar.
|
|
165
|
-
* An optional filter can be applied to the metadata of the items.
|
|
166
|
-
* @param vector Vector to query against
|
|
167
|
-
* @param topK Number of items to return
|
|
168
|
-
* @param filter Optional filter to apply
|
|
169
|
-
* @returns Similar items to the vector that matches the filter
|
|
170
|
-
*/
|
|
171
|
-
queryItems<TMetadata = Record<string, MetadataTypes>>(vector: number[], topK: number, filter?: MetadataFilter): Promise<QueryResult<TMetadata>[]>;
|
|
172
|
-
/**
|
|
173
|
-
* Adds or replaces an item in the index.
|
|
174
|
-
* @remarks
|
|
175
|
-
* A new update is started if one is not already in progress. If an item with the same ID
|
|
176
|
-
* already exists, it will be replaced.
|
|
177
|
-
* @param item Item to insert or replace
|
|
178
|
-
* @returns Upserted item
|
|
179
|
-
*/
|
|
180
|
-
upsertItem<TMetadata = Record<string, MetadataTypes>>(item: Partial<IndexItem<TMetadata>>): Promise<IndexItem<TMetadata>>;
|
|
181
|
-
private loadIndexData;
|
|
182
|
-
private addItemToUpdate;
|
|
183
|
-
}
|
|
184
|
-
//# sourceMappingURL=LocalIndex.d.ts.map
|
package/lib/LocalIndex.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"LocalIndex.d.ts","sourceRoot":"","sources":["../src/LocalIndex.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,iBAAiB;IAC9B,OAAO,EAAE,MAAM,CAAC;IAChB,cAA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,OAAO,CAAC;IAS/C;;;;;;OAMG;IACU,SAAS,CAAC,SAAS,GAAG,MAAM,CAAC,MAAM,EAAC,aAAa,CAAC,KAAK,OAAO,CAAC,SAAS,CAAC,SAAS,CAAC,EAAE,CAAC;IAKnG;;;;;;OAMG;IACU,mBAAmB,CAAC,SAAS,GAAG,MAAM,CAAC,MAAM,EAAC,aAAa,CAAC,EAAE,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,SAAS,CAAC,SAAS,CAAC,EAAE,CAAC;IAKnI;;;;;;;;;OASG;IACU,UAAU,CAAC,SAAS,GAAG,MAAM,CAAC,MAAM,EAAC,aAAa,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,WAAW,CAAC,SAAS,CAAC,EAAE,CAAC;IAyC7J;;;;;;;OAOG;IACU,UAAU,CAAC,SAAS,GAAG,MAAM,CAAC,MAAM,EAAC,aAAa,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,GAAG,OAAO,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;YAWvH,aAAa;YAab,eAAe;CA8DhC"}
|
package/lib/LocalIndex.js
DELETED
|
@@ -1,392 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
-
}) : function(o, v) {
|
|
12
|
-
o["default"] = v;
|
|
13
|
-
});
|
|
14
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
-
if (mod && mod.__esModule) return mod;
|
|
16
|
-
var result = {};
|
|
17
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
-
__setModuleDefault(result, mod);
|
|
19
|
-
return result;
|
|
20
|
-
};
|
|
21
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
22
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
23
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
25
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
26
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
27
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
-
});
|
|
29
|
-
};
|
|
30
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
-
exports.LocalIndex = void 0;
|
|
32
|
-
const fs = __importStar(require("fs/promises"));
|
|
33
|
-
const path = __importStar(require("path"));
|
|
34
|
-
const uuid_1 = require("uuid");
|
|
35
|
-
const ItemSelector_1 = require("./ItemSelector");
|
|
36
|
-
/**
|
|
37
|
-
* Local vector index instance.
|
|
38
|
-
* @remarks
|
|
39
|
-
* This class is used to create, update, and query a local vector index.
|
|
40
|
-
* Each index is a folder on disk containing an index.json file and an optional set of metadata files.
|
|
41
|
-
*/
|
|
42
|
-
class LocalIndex {
|
|
43
|
-
/**
|
|
44
|
-
* Creates a new instance of LocalIndex.
|
|
45
|
-
* @param folderPath - Path to the index folder
|
|
46
|
-
*/
|
|
47
|
-
constructor(folderPath) {
|
|
48
|
-
this._folderPath = folderPath;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Begins an update to the index.
|
|
52
|
-
* @remarks
|
|
53
|
-
* This method loads the index into memory and prepares it for updates.
|
|
54
|
-
*/
|
|
55
|
-
beginUpdate() {
|
|
56
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
-
if (this._update) {
|
|
58
|
-
throw new Error('Update already in progress');
|
|
59
|
-
}
|
|
60
|
-
yield this.loadIndexData();
|
|
61
|
-
this._update = Object.assign({}, this._data);
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Cancels an update to the index.
|
|
66
|
-
* @remarks
|
|
67
|
-
* This method discards any changes made to the index since the update began.
|
|
68
|
-
*/
|
|
69
|
-
cancelUpdate() {
|
|
70
|
-
this._update = undefined;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Creates a new index.
|
|
74
|
-
* @remarks
|
|
75
|
-
* This method creates a new folder on disk containing an index.json file.
|
|
76
|
-
* @param config - Index configuration
|
|
77
|
-
*/
|
|
78
|
-
createIndex(config = { version: 1 }) {
|
|
79
|
-
var _a;
|
|
80
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
-
// Delete if exists
|
|
82
|
-
if (yield this.isIndexCreated()) {
|
|
83
|
-
if (config.deleteIfExists) {
|
|
84
|
-
yield this.deleteIndex();
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
throw new Error('Index already exists');
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
try {
|
|
91
|
-
// Create folder for index
|
|
92
|
-
yield fs.mkdir(this._folderPath, { recursive: true });
|
|
93
|
-
// Initialize index.json file
|
|
94
|
-
this._data = {
|
|
95
|
-
version: config.version,
|
|
96
|
-
metadata_config: (_a = config.metadata_config) !== null && _a !== void 0 ? _a : {},
|
|
97
|
-
items: []
|
|
98
|
-
};
|
|
99
|
-
yield fs.writeFile(path.join(this._folderPath, 'index.json'), JSON.stringify(this._data));
|
|
100
|
-
}
|
|
101
|
-
catch (err) {
|
|
102
|
-
yield this.deleteIndex();
|
|
103
|
-
throw new Error('Error creating index');
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Deletes the index.
|
|
109
|
-
* @remarks
|
|
110
|
-
* This method deletes the index folder from disk.
|
|
111
|
-
*/
|
|
112
|
-
deleteIndex() {
|
|
113
|
-
this._data = undefined;
|
|
114
|
-
return fs.rm(this._folderPath, {
|
|
115
|
-
recursive: true,
|
|
116
|
-
maxRetries: 3
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Deletes an item from the index.
|
|
121
|
-
* @param id - Item id
|
|
122
|
-
*/
|
|
123
|
-
deleteItem(id) {
|
|
124
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
-
if (this._update) {
|
|
126
|
-
const index = this._update.items.findIndex(i => i.id === id);
|
|
127
|
-
if (index >= 0) {
|
|
128
|
-
this._update.items.splice(index, 1);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
yield this.beginUpdate();
|
|
133
|
-
const index = this._update.items.findIndex(i => i.id === id);
|
|
134
|
-
if (index >= 0) {
|
|
135
|
-
this._update.items.splice(index, 1);
|
|
136
|
-
}
|
|
137
|
-
yield this.endUpdate();
|
|
138
|
-
}
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Ends an update to the index.
|
|
143
|
-
* @remarks
|
|
144
|
-
* This method saves the index to disk.
|
|
145
|
-
*/
|
|
146
|
-
endUpdate() {
|
|
147
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
148
|
-
if (!this._update) {
|
|
149
|
-
throw new Error('No update in progress');
|
|
150
|
-
}
|
|
151
|
-
try {
|
|
152
|
-
// Save index
|
|
153
|
-
yield fs.writeFile(path.join(this._folderPath, 'index.json'), JSON.stringify(this._update));
|
|
154
|
-
this._data = this._update;
|
|
155
|
-
this._update = undefined;
|
|
156
|
-
}
|
|
157
|
-
catch (err) {
|
|
158
|
-
throw new Error(`Error saving index: ${err.toString()}`);
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Loads an index from disk and returns its stats.
|
|
164
|
-
* @returns Index stats
|
|
165
|
-
*/
|
|
166
|
-
getIndexStats() {
|
|
167
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
168
|
-
yield this.loadIndexData();
|
|
169
|
-
return {
|
|
170
|
-
version: this._data.version,
|
|
171
|
-
metadata_config: this._data.metadata_config,
|
|
172
|
-
items: this._data.items.length
|
|
173
|
-
};
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Returns an item from the index given its ID.
|
|
178
|
-
* @param id Item id
|
|
179
|
-
* @returns Item or undefined if not found
|
|
180
|
-
*/
|
|
181
|
-
getItem(id) {
|
|
182
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
183
|
-
yield this.loadIndexData();
|
|
184
|
-
return this._data.items.find(i => i.id === id);
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Adds an item to the index.
|
|
189
|
-
* @remarks
|
|
190
|
-
* A new update is started if one is not already in progress. If an item with the same ID
|
|
191
|
-
* already exists, an error will be thrown.
|
|
192
|
-
* @param item Item to insert
|
|
193
|
-
* @returns Inserted item
|
|
194
|
-
*/
|
|
195
|
-
insertItem(item) {
|
|
196
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
197
|
-
if (this._update) {
|
|
198
|
-
return yield this.addItemToUpdate(item, true);
|
|
199
|
-
}
|
|
200
|
-
else {
|
|
201
|
-
yield this.beginUpdate();
|
|
202
|
-
const newItem = yield this.addItemToUpdate(item, true);
|
|
203
|
-
yield this.endUpdate();
|
|
204
|
-
return newItem;
|
|
205
|
-
}
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Returns true if the index exists.
|
|
210
|
-
*/
|
|
211
|
-
isIndexCreated() {
|
|
212
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
213
|
-
try {
|
|
214
|
-
yield fs.access(path.join(this._folderPath, 'index.json'));
|
|
215
|
-
return true;
|
|
216
|
-
}
|
|
217
|
-
catch (err) {
|
|
218
|
-
return false;
|
|
219
|
-
}
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Returns all items in the index.
|
|
224
|
-
* @remarks
|
|
225
|
-
* This method loads the index into memory and returns all its items. A copy of the items
|
|
226
|
-
* array is returned so no modifications should be made to the array.
|
|
227
|
-
* @returns All items in the index
|
|
228
|
-
*/
|
|
229
|
-
listItems() {
|
|
230
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
231
|
-
yield this.loadIndexData();
|
|
232
|
-
return this._data.items.slice();
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Returns all items in the index matching the filter.
|
|
237
|
-
* @remarks
|
|
238
|
-
* This method loads the index into memory and returns all its items matching the filter.
|
|
239
|
-
* @param filter Filter to apply
|
|
240
|
-
* @returns Items matching the filter
|
|
241
|
-
*/
|
|
242
|
-
listItemsByMetadata(filter) {
|
|
243
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
244
|
-
yield this.loadIndexData();
|
|
245
|
-
return this._data.items.filter(i => ItemSelector_1.ItemSelector.select(i.metadata, filter));
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Finds the top k items in the index that are most similar to the vector.
|
|
250
|
-
* @remarks
|
|
251
|
-
* This method loads the index into memory and returns the top k items that are most similar.
|
|
252
|
-
* An optional filter can be applied to the metadata of the items.
|
|
253
|
-
* @param vector Vector to query against
|
|
254
|
-
* @param topK Number of items to return
|
|
255
|
-
* @param filter Optional filter to apply
|
|
256
|
-
* @returns Similar items to the vector that matches the filter
|
|
257
|
-
*/
|
|
258
|
-
queryItems(vector, topK, filter) {
|
|
259
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
260
|
-
yield this.loadIndexData();
|
|
261
|
-
// Filter items
|
|
262
|
-
let items = this._data.items;
|
|
263
|
-
if (filter) {
|
|
264
|
-
items = items.filter(i => ItemSelector_1.ItemSelector.select(i.metadata, filter));
|
|
265
|
-
}
|
|
266
|
-
// Calculate distances
|
|
267
|
-
const norm = ItemSelector_1.ItemSelector.normalize(vector);
|
|
268
|
-
const distances = [];
|
|
269
|
-
for (let i = 0; i < items.length; i++) {
|
|
270
|
-
const item = items[i];
|
|
271
|
-
const distance = ItemSelector_1.ItemSelector.normalizedCosineSimilarity(vector, norm, item.vector, item.norm);
|
|
272
|
-
distances.push({ index: i, distance: distance });
|
|
273
|
-
}
|
|
274
|
-
// Sort by distance DESCENDING
|
|
275
|
-
distances.sort((a, b) => b.distance - a.distance);
|
|
276
|
-
// Find top k
|
|
277
|
-
const top = distances.slice(0, topK).map(d => {
|
|
278
|
-
return {
|
|
279
|
-
item: Object.assign({}, items[d.index]),
|
|
280
|
-
score: d.distance
|
|
281
|
-
};
|
|
282
|
-
});
|
|
283
|
-
// Load external metadata
|
|
284
|
-
for (const item of top) {
|
|
285
|
-
if (item.item.metadataFile) {
|
|
286
|
-
const metadataPath = path.join(this._folderPath, item.item.metadataFile);
|
|
287
|
-
const metadata = yield fs.readFile(metadataPath);
|
|
288
|
-
item.item.metadata = JSON.parse(metadata.toString());
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
return top;
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* Adds or replaces an item in the index.
|
|
296
|
-
* @remarks
|
|
297
|
-
* A new update is started if one is not already in progress. If an item with the same ID
|
|
298
|
-
* already exists, it will be replaced.
|
|
299
|
-
* @param item Item to insert or replace
|
|
300
|
-
* @returns Upserted item
|
|
301
|
-
*/
|
|
302
|
-
upsertItem(item) {
|
|
303
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
304
|
-
if (this._update) {
|
|
305
|
-
return yield this.addItemToUpdate(item, false);
|
|
306
|
-
}
|
|
307
|
-
else {
|
|
308
|
-
yield this.beginUpdate();
|
|
309
|
-
const newItem = yield this.addItemToUpdate(item, false);
|
|
310
|
-
yield this.endUpdate();
|
|
311
|
-
return newItem;
|
|
312
|
-
}
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
loadIndexData() {
|
|
316
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
317
|
-
if (this._data) {
|
|
318
|
-
return;
|
|
319
|
-
}
|
|
320
|
-
if (!(yield this.isIndexCreated())) {
|
|
321
|
-
throw new Error('Index does not exist');
|
|
322
|
-
}
|
|
323
|
-
const data = yield fs.readFile(path.join(this._folderPath, 'index.json'));
|
|
324
|
-
this._data = JSON.parse(data.toString());
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
addItemToUpdate(item, unique) {
|
|
328
|
-
var _a;
|
|
329
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
330
|
-
// Ensure vector is provided
|
|
331
|
-
if (!item.vector) {
|
|
332
|
-
throw new Error('Vector is required');
|
|
333
|
-
}
|
|
334
|
-
// Ensure unique
|
|
335
|
-
const id = (_a = item.id) !== null && _a !== void 0 ? _a : (0, uuid_1.v4)();
|
|
336
|
-
if (unique) {
|
|
337
|
-
const existing = this._update.items.find(i => i.id === id);
|
|
338
|
-
if (existing) {
|
|
339
|
-
throw new Error(`Item with id ${id} already exists`);
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
// Check for indexed metadata
|
|
343
|
-
let metadata = {};
|
|
344
|
-
let metadataFile;
|
|
345
|
-
if (this._update.metadata_config.indexed && this._update.metadata_config.indexed.length > 0 && item.metadata) {
|
|
346
|
-
// Copy only indexed metadata
|
|
347
|
-
for (const key of this._update.metadata_config.indexed) {
|
|
348
|
-
if (item.metadata && item.metadata[key]) {
|
|
349
|
-
metadata[key] = item.metadata[key];
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
// Save remaining metadata to disk
|
|
353
|
-
metadataFile = `${uuid_1.v4}.json`;
|
|
354
|
-
const metadataPath = path.join(this._folderPath, metadataFile);
|
|
355
|
-
yield fs.writeFile(metadataPath, JSON.stringify(item.metadata));
|
|
356
|
-
}
|
|
357
|
-
else if (item.metadata) {
|
|
358
|
-
metadata = item.metadata;
|
|
359
|
-
}
|
|
360
|
-
// Create new item
|
|
361
|
-
const newItem = {
|
|
362
|
-
id: id,
|
|
363
|
-
metadata: metadata,
|
|
364
|
-
vector: item.vector,
|
|
365
|
-
norm: ItemSelector_1.ItemSelector.normalize(item.vector)
|
|
366
|
-
};
|
|
367
|
-
if (metadataFile) {
|
|
368
|
-
newItem.metadataFile = metadataFile;
|
|
369
|
-
}
|
|
370
|
-
// Add item to index
|
|
371
|
-
if (!unique) {
|
|
372
|
-
const existing = this._update.items.find(i => i.id === id);
|
|
373
|
-
if (existing) {
|
|
374
|
-
existing.metadata = newItem.metadata;
|
|
375
|
-
existing.vector = newItem.vector;
|
|
376
|
-
existing.metadataFile = newItem.metadataFile;
|
|
377
|
-
return existing;
|
|
378
|
-
}
|
|
379
|
-
else {
|
|
380
|
-
this._update.items.push(newItem);
|
|
381
|
-
return newItem;
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
else {
|
|
385
|
-
this._update.items.push(newItem);
|
|
386
|
-
return newItem;
|
|
387
|
-
}
|
|
388
|
-
});
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
exports.LocalIndex = LocalIndex;
|
|
392
|
-
//# sourceMappingURL=LocalIndex.js.map
|
package/lib/LocalIndex.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"LocalIndex.js","sourceRoot":"","sources":["../src/LocalIndex.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,gDAAkC;AAClC,2CAA6B;AAC7B,+BAA0B;AAC1B,iDAA8C;AAuF9C;;;;;GAKG;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IAAI,YAAY,EAAE;gBACd,OAAO,CAAC,YAAY,GAAG,YAAY,CAAC;aACvC;YAED,oBAAoB;YACpB,IAAI,CAAC,MAAM,EAAE;gBACT,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;gBAC5D,IAAI,QAAQ,EAAE;oBACV,QAAQ,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;oBACrC,QAAQ,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;oBACjC,QAAQ,CAAC,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC;oBAC7C,OAAO,QAAQ,CAAC;iBACnB;qBAAM;oBACH,IAAI,CAAC,OAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;oBAClC,OAAO,OAAO,CAAC;iBAClB;aACJ;iBAAM;gBACH,IAAI,CAAC,OAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;gBAClC,OAAO,OAAO,CAAC;aAClB;;KACJ;CACJ;AAzVD,gCAyVC"}
|
package/lib/index.d.ts
DELETED
package/lib/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC"}
|
package/lib/index.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
-
};
|
|
12
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
__exportStar(require("./ItemSelector"), exports);
|
|
14
|
-
__exportStar(require("./LocalIndex"), exports);
|
|
15
|
-
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iDAA+B;AAC/B,+CAA6B"}
|
package/lib/utilities.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
export declare function dotProduct(arr1: number[], arr2: number[]): number;
|
|
2
|
-
export declare function normalize(arr: number[]): number;
|
|
3
|
-
export declare function cosineSimilarity(arr1: number[], arr2: number[]): number;
|
|
4
|
-
export declare function normalizedCosineSimilarity(arr1: number[], norm1: number, arr2: number[], norm2: number): number;
|
|
5
|
-
//# sourceMappingURL=utilities.d.ts.map
|
package/lib/utilities.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utilities.d.ts","sourceRoot":"","sources":["../src/utilities.ts"],"names":[],"mappings":"AACA,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,UAUxD;AAED,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,UAUtC;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,UAG9D;AAED,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,MAAM,UAGtG"}
|
package/lib/utilities.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.normalizedCosineSimilarity = exports.cosineSimilarity = exports.normalize = exports.dotProduct = void 0;
|
|
4
|
-
function dotProduct(arr1, arr2) {
|
|
5
|
-
// Initialize a variable to store the sum of the products
|
|
6
|
-
let sum = 0;
|
|
7
|
-
// Loop through the elements of the arrays
|
|
8
|
-
for (let i = 0; i < arr1.length; i++) {
|
|
9
|
-
// Multiply the corresponding elements and add them to the sum
|
|
10
|
-
sum += arr1[i] * arr2[i];
|
|
11
|
-
}
|
|
12
|
-
// Return the sum
|
|
13
|
-
return sum;
|
|
14
|
-
}
|
|
15
|
-
exports.dotProduct = dotProduct;
|
|
16
|
-
function normalize(arr) {
|
|
17
|
-
// Initialize a variable to store the sum of the squares
|
|
18
|
-
let sum = 0;
|
|
19
|
-
// Loop through the elements of the array
|
|
20
|
-
for (let i = 0; i < arr.length; i++) {
|
|
21
|
-
// Square the element and add it to the sum
|
|
22
|
-
sum += arr[i] * arr[i];
|
|
23
|
-
}
|
|
24
|
-
// Return the square root of the sum
|
|
25
|
-
return Math.sqrt(sum);
|
|
26
|
-
}
|
|
27
|
-
exports.normalize = normalize;
|
|
28
|
-
function cosineSimilarity(arr1, arr2) {
|
|
29
|
-
// Return the quotient of the dot product and the product of the norms
|
|
30
|
-
return dotProduct(arr1, arr2) / (normalize(arr1) * normalize(arr2));
|
|
31
|
-
}
|
|
32
|
-
exports.cosineSimilarity = cosineSimilarity;
|
|
33
|
-
function normalizedCosineSimilarity(arr1, norm1, arr2, norm2) {
|
|
34
|
-
// Return the quotient of the dot product and the product of the norms
|
|
35
|
-
return dotProduct(arr1, arr2) / (norm1 * norm2);
|
|
36
|
-
}
|
|
37
|
-
exports.normalizedCosineSimilarity = normalizedCosineSimilarity;
|
|
38
|
-
//# sourceMappingURL=utilities.js.map
|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