0.7.6 • Published 2 months ago

vectra v0.7.6

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

Vectra

Vectra is a local vector database for Node.js with features similar to Pinecone or Qdrant 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.

When queryng Vectra you'll be able to use the same subset of Mongo DB query operators 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.

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.

Pinecone style namespaces aren't directly supported but you could easily mimic them by creating a separate Vectra index (and folder) for each namespace.

Other Language Bindings

This repo contains the TypeScript/JavaScript binding for Vectra but other language bindings are being created. Since Vectra is file based, any language binding can be used to read or write a Vectra index. That means you can build a Vectra index using JS and then read it using Python.

Installation

$ npm install vectra

Usage

First create an instance of LocalIndex with the path to the folder where you want you're items stored:

import { LocalIndex } from 'vectra';

const index = new LocalIndex(path.join(__dirname, '..', 'index'));

Next, from inside an async function, create your index:

if (!await index.isIndexCreated()) {
    await index.createIndex();
}

Add some items to your index:

import { OpenAIApi, Configuration } from 'openai';

const configuration = new Configuration({
    apiKey: `<YOUR_KEY>`,
});

const api = new OpenAIApi(configuration);

async function getVector(text: string) {
    const response = await api.createEmbedding({
        'model': 'text-embedding-ada-002',
        'input': text,
    });
    return response.data.data[0].embedding;
}

async function addItem(text: string) {
    await index.insertItem({
        vector: await getVector(text),
        metadata: { text }
    });
}

// Add items
await addItem('apple');
await addItem('oranges');
await addItem('red');
await addItem('blue');

Then query for items:

async function query(text: string) {
    const vector = await getVector(text);
    const results = await index.queryItems(vector, 3);
    if (results.length > 0) {
        for (const result of results) {
            console.log(`[${result.score}] ${result.item.metadata.text}`);
        }
    } else {
        console.log(`No results found.`);
    }
}

await query('green');
/*
[0.9036569942401076] blue
[0.8758153664568566] red
[0.8323828606103998] apple
*/

await query('banana');
/*
[0.9033128691220631] apple
[0.8493374123092652] oranges
[0.8415324469533297] blue
*/
0.7.6

2 months ago

0.7.5

2 months ago

0.7.2

2 months ago

0.7.1

2 months ago

0.7.4

2 months ago

0.7.3

2 months ago

0.7.0

2 months ago

0.6.0

2 months ago

0.5.4

5 months ago

0.5.5

5 months ago

0.5.3

6 months ago

0.5.0

6 months ago

0.5.2

6 months ago

0.5.1

6 months ago

0.3.0

8 months ago

0.2.1

9 months ago

0.1.2

12 months ago

0.2.0

9 months ago

0.4.4

8 months ago

0.4.1

8 months ago

0.4.0

8 months ago

0.2.2

9 months ago

0.4.3

8 months ago

0.4.2

8 months ago

0.1.1

1 year ago

0.1.0

1 year ago