npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vectorcore

v1.0.0

Published

A vector database with the vector generation included

Downloads

80

Readme

VectorCore

VectorCore is a TypeScript library that provides a simple and efficient way to store and query vector data using vectra. It includes a built-in vector generator that uses fastembed, making it easy to work with vector data for free.

Features

  • Vector database with support for storing and querying vector data
  • Built-in vector generator for generating vectors from text data
  • Support for different vector types (Query, Passage, Any)

Installation

To install VectorCore, run the following command:

npm install vectorcore

Usage

To use VectorCore, import the DataBase and VectorGenerator classes.

import { DataBase, VectorGenerator } from "vectorcore";
//For CommonJS
// const {DataBase,VectorGenerator} = require("vectorcore");

Create a new instance of the DataBase class, passing in the folder path where you want to store the database.

const db = new DataBase("./database");

Create a new instance of the VectorGenerator class, passing in the embedding model you want to use.

import { EmbeddingModel } from "vectorcore"; //This imports an Enum with all the possible embedding models
const vectorGenerator = new VectorGenerator(EmbeddingModel.AllMiniLML6V2);

Now we need to initialize the generator and the database

await db.initialize(); //Resolves to a promise
await vectorGenerator.initialize(); //Resolves to a promise

And now we are free to use the generator and the database to save or retrieve items

Note: The generateVector method can be passed a VectorType to increase accuracy in this case we are gonna use Query cause is a single word but for larger texts you could use Passage or if you dont want to specify anything you can use Any.

import { VectorTypes } from "vectorcore"; //Import the vectortypes

const vectorApple = await vectorGenerator.generateVector(
	"apple",
	VectorTypes.Query
);
const vectorOranges = await vectorGenerator.generateVector(
	"oranges",
	VectorTypes.Query
);
const vectorRed = await vectorGenerator.generateVector(
	"red",
	VectorTypes.Query
);
const vectorBlue = await vectorGenerator.generateVector(
	"blue",
	VectorTypes.Query
);
await db.addItem(vectorApple, {
	metadata: {
		name: "Apple",
	},
}); //The metadata is an object that contains whatever you want to store like name,descriptions or anything else
await db.addItem(vectorOranges, {
	metadata: {
		name: "Oranges",
	},
});
await db.addItem(vectorRed, {
	metadata: {
		name: "Red",
	},
});
await db.addItem(vectorBlue, {
	metadata: {
		name: "Blue",
	},
});

const searchVector = await vectorGenerator.generateVector("fruit");

const items = await db.getItems(searchVector, 2);
console.log(items.map((e) => e.item)); // We need to map the item because the returned object has {item: Item, score: number}. Score is used to sort the results.

/*
   We search the database with the search vector and get the top 2 results.
   The expected output is an array containing two items with the metadata:
   [
       {
           id: "randomId if not defined at item",
           metadata: { name: "Oranges" },
           vector: number[],
           norm: number
       },
       {
           id: "randomId if not defined at item",
           metadata: { name: "Apple" },
           vector: number[],
           norm: number
       }
   ]
*/

Generating array of vectors

To reduce vector generation calls you can pass an array of strings to the generateVectors method

Note: This method has less accuracy for Queries than generateVector

Warning: this method will return an asynchronous iterable with promises

const Texts = ["apple", "oranges", "red", "blue"];

const vector = await vectorGenerator.generateVectors(Texts);
/*
You can specify the batch size to process some vectors at the same time the vectors are generating
Example: batchSize 1 will execute the for loop at every vector it generates
but batchSize 2 will execute the for loop at every 2 vectors it generates
*/
for await (const Batch of vector) {
	console.log(Batch);
}

Retrieving item by vector

const items = await db.getItems(vector, results);
const itemsFiltered = await db.getItems(vector, results, MetadataFilter);

Retrieving item by id

const item = await db.getItem("ID");

Deleting item by id

await db.deleteItem("ID");

Updating item by id

const item = { id: "ID", metadata: { name: "NewName" } };
await db.updateItem(item);

getAllItems

const items = await db.getAllItems();
const itemsFiltered = await db.getAllItems(MetadataFilter);

Delete database

await db.deleteDB();

MetadataFilter

you'll be able to use the same subset of Mongo DB query operators that Pinecone supports

Example EQ

const items = await db.getAllItems({ name: { $eq: "apple" } });

Example AND/OR

const items = await db.getAllItems({
	$and: [{ name: { $eq: "apple" } }, { color: { $eq: "blue" } }],
}); //or operator syntax is the same as and operator

Contributing

Contributions are welcome! If you'd like to contribute to VectorCore, please fork the repository and submit a pull request.