vectra 0.1.0 → 0.1.2

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 CHANGED
@@ -1,10 +1,95 @@
1
- # vectra
1
+ # Vectra
2
2
  Vectra is a local vector database for Node.js with features similar to [Pinecone](https://www.pinecone.io/) or [Qdrant](https://qdrant.tech/) but built using local files. Each Vectra index is a folder on disk. There's an `index.json` file in the folder that contains all the vectors for the index along with any indexed metadata. When you create an index you can specify which metadata properties to index and only those fields will be stored in the `index.json` file. All of the other metadata for an item will be stored on disk in a separate file keyed by a GUID.
3
3
 
4
4
  When queryng Vectra you'll be able to use the same subset of [Mongo DB query operators](https://www.mongodb.com/docs/manual/reference/operator/query/) that Pinecone supports and the results will be returned sorted by simularity. Every item in the index will first be filtered by metadata and then ranked for simularity. Even though every item is evaluated its all in memory so it should by nearly instantanious. Likely 1ms - 2ms for even a rather large index. Smaller indexes should be <1ms.
5
5
 
6
- Keep in mind that your entire Vectra index is loaded into memory so it's not well suited for scenarios like long term chat bot memory. Use a really vector DB for that. Vectra is intended to be used in scenarios where you have a small corpus of mostly static data you'd like to include in your prompt. Infinite few shot examples would be a great use case for Vectra or even just a single document you want to ask questions over.
6
+ Keep in mind that your entire Vectra index is loaded into memory so it's not well suited for scenarios like long term chat bot memory. Use a real vector DB for that. Vectra is intended to be used in scenarios where you have a small corpus of mostly static data that you'd like to include in your prompt. Infinite few shot examples would be a great use case for Vectra or even just a single document you want to ask questions over.
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
- Sample code and the actual code coming the week. GPT and I are still writting it :)
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
@@ -2,7 +2,7 @@
2
2
  "name": "vectra",
3
3
  "author": "Steven Ickman",
4
4
  "description": "A vector database that uses the local file system for storage.",
5
- "version": "0.1.0",
5
+ "version": "0.1.2",
6
6
  "license": "MIT",
7
7
  "keywords": [
8
8
  "gpt"
@@ -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
@@ -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
@@ -1 +0,0 @@
1
- {"version":3,"file":"utilities.js","sourceRoot":"","sources":["../src/utilities.ts"],"names":[],"mappings":";;;AACA,SAAgB,UAAU,CAAC,IAAc,EAAE,IAAc;IACrD,yDAAyD;IACzD,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,0CAA0C;IAC1C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QAClC,8DAA8D;QAC9D,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;KAC5B;IACD,iBAAiB;IACjB,OAAO,GAAG,CAAC;AACf,CAAC;AAVD,gCAUC;AAED,SAAgB,SAAS,CAAC,GAAa;IACnC,wDAAwD;IACxD,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,yCAAyC;IACzC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QACjC,2CAA2C;QAC3C,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;KAC1B;IACD,oCAAoC;IACpC,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC1B,CAAC;AAVD,8BAUC;AAED,SAAgB,gBAAgB,CAAC,IAAc,EAAE,IAAc;IAC3D,sEAAsE;IACtE,OAAO,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;AACxE,CAAC;AAHD,4CAGC;AAED,SAAgB,0BAA0B,CAAC,IAAc,EAAE,KAAa,EAAE,IAAc,EAAE,KAAa;IACnG,sEAAsE;IACtE,OAAO,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC;AACpD,CAAC;AAHD,gEAGC"}