vectoria

package module
v0.0.0-...-b6ef178 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

README

Vectoria

Vectoria is an embedded vector database for simple use cases. It implements LSH (Locality-Sensitive Hashing).

Here’s a complete example:

package main

import (
	"log"

	"github.com/caiodallaqua/vectoria"
)

func main() {
	var (
		indexName = "hello-world"
		// Vector to be stored.
		vec = []float64{1.1, 0.2}
		// Key to be retrieved.
		key = "my-key"

		// Let's add a small deviation to vec,
		// so we can see a match.
		queryVec = []float64{vec[0] + 0.1, vec[1] + 0.1}
		// Only similarities >= thresold.
		threshold = 0.9
		// Upper limit on how many items to retrieve.
		k uint32 = 1
	)

	dbConfig := vectoria.DBConfig{
		Path: "", // In-memory storage.
		LSH: []vectoria.LSHConfig{{
			IndexName:      indexName,
			NumRounds:      5,
			NumHyperPlanes: 2,
			SpaceDim:       2, // Length of your vectors.
		}},
	}

	db, err := vectoria.New(dbConfig)
	if err != nil {
		log.Fatalf("unable to create database: %s", err)
		return
	}

	if err = db.Add(key, vec, indexName); err != nil {
		log.Fatalf("unable to add key: %s", err)
		return
	}

	res, err := db.Get(queryVec, threshold, k, indexName)
	if err != nil {
		log.Fatalf("unable to get key: %s", err)
		return
	}

	log.Println("retrieval results:")
	for index, key := range res {
		log.Printf("\tindex: %v", index)
		log.Printf("\tkeys: %v", key)
	}
}
License

Apache License 2.0

Documentation

Overview

Package vectoria provides an embedded vector database for simple use cases.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	// contains filtered or unexported fields
}

func New

func New(config DBConfig) (db *DB, err error)

func (*DB) Add

func (db *DB) Add(itemID string, itemVec []float64, indexNames ...string) error

TODO: rollback on err

func (*DB) Get

func (db *DB) Get(queryVec []float64, threshold float64, k uint32, indexNames ...string) (res map[string][]string, err error)

func (*DB) Indexes

func (db *DB) Indexes() []string

func (*DB) NumIndexes

func (db *DB) NumIndexes() uint32

type DBConfig

type DBConfig struct {
	Path string

	LSH []LSHConfig
	// contains filtered or unexported fields
}

type LSHConfig

type LSHConfig struct {
	IndexName string `json:"index_name"`

	// Number of rounds. More rounds improve quality, but also adds computation overhead.
	// It must be at least 1.
	// If invalid value is given, default value is used.
	NumRounds uint32 `json:"num_rounds"`

	// Number of hyperplanes to split the space on. It must be at least 1.
	// If invalid value is given, default value is used.
	NumHyperPlanes uint32 `json:"num_hyper_planes"`

	// Dimension of the space (vector length). It must be at least 2.
	// If invalid value is given, default value is used.
	SpaceDim uint32 `json:"space_dim"`
}

type Options

type Options func(*DB) error

Directories

Path Synopsis
internal
lsh

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL