@svector/client
TypeScript icon, indicating that this package has built-in type declarations

0.0.6 • Public • Published

SvectorDB Client

For more information on SvectorDB, visit the website.

Creating a database

To create a database, visit the dashboard and navigate to the databases page. Click the green plus button and create a database with dimension set to 4, type set to "sandbox" and region set to "us-east-2".

Sandbox databases are free but have limitations. View the limits page for more information.

Connecting

To authenticate with the database, you will need to generate an API key. To do this, navigate to the databases page and click the key symbol next to the database you want to access. Take note of the API key and the database ID.

Install the client library using npm:

npm install @svectordb/client

Next create a file called index.js and add the following code:

const { DatabaseService } = require('@svector/client');

const region = 'us-east-2';
const apiKey = 'f4ead19a-bac7-4ff0-b379-d3f634ed180f'; // replace with your API key
const databaseId = 'f39434565486ee15'; // replace with your database ID

const client = new DatabaseService({
    endpoint: `https://${region}.api.svectordb.com`,
    apiKey: apiKey
});

Writing data

To add data use the setItem method, this will upsert the data into the database. Let's insert 10 random documents:

for (let i = 0; i < 10; i++) {
    await client.setItem({
        databaseId,
        key: `random-${i}`,
        value: Buffer.from(`Hello world #${i}!`), // Value is stored as bytes, use Buffer.from to convert a string to bytes
        vector: [Math.random(), Math.random(), Math.random(), Math.random()]
    });
}

Now let's see the data we just inserted:

const documents = await client.listItems({databaseId});
console.log(documents);

You can also get a single document by key:

const document = await client.getItem({databaseId, key: 'random-1'});
console.log(document);

To delete a document, use the deleteItem method:

await client.deleteItem({databaseId, key: 'random-1'});

Querying data

To query data, use the queryItems method. Let's query the 5 closest documents to the vector [0.5, 0.5, 0.5, 0.5]:

const results = await client.query({
    databaseId,
    query: {
        vector: [0.5, 0.5, 0.5, 0.5]
    },
    maxResults: 5,
});

console.log(results);

You can also query for vectors closest to an existing document:

const results = await client.query({
    databaseId,
    query: {
        key: 'random-0'
    },
    maxResults: 5,
});

console.log(results);

Full example

Here is the full example:

const { DatabaseService } = require('@svector/client');

const region = 'us-east-2';
const apiKey = 'f4ead19a-bac7-4ff0-b379-d3f634ed180f'; // replace with your API key
const databaseId = 'f39434565486ee15'; // replace with your database ID

const client = new DatabaseService({
    endpoint: `https://${region}.api.svectordb.com`,
    apiKey: apiKey
});

(async () => {
    // Insert 10 random documents
    for (let i = 0; i < 10; i++) {
        await client.setItem({
            databaseId,
            key: `random-${i}`,
            value: Buffer.from(`Hello world #${i}!`), // Value is stored as bytes, use Buffer.from to convert a string to bytes
            vector: [Math.random(), Math.random(), Math.random(), Math.random()]
        });
    }

    // List documents
    const documents = await client.listItems({databaseId});
    console.log(`Found ${documents.items.length} documents`)
    console.log(documents);

    // Get a single document
    const document = await client.getItem({databaseId, key: 'random-1'});
    console.log('Document with key "random-1"');
    console.log(document);

    // Delete a document
    await client.deleteItem({databaseId, key: 'random-1'});

    // Query documents
    const queryByVector = await client.query({
        databaseId,
        query: {
            vector: [0.5, 0.5, 0.5, 0.5]
        },
        maxResults: 5,
    });

    console.log('Query by vector');
    console.log(queryByVector);

    const queryByKey = await client.query({
        databaseId,
        query: {
            key: 'random-0'
        },
        maxResults: 5,
    });

    console.log('Query by key');
    console.log(queryByKey);
})();

Readme

Keywords

none

Package Sidebar

Install

npm i @svector/client

Weekly Downloads

4

Version

0.0.6

License

none

Unpacked Size

157 kB

Total Files

75

Last publish

Collaborators

  • svector