hnswgo

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2024 License: MIT Imports: 5 Imported by: 0

README ¶

HNSWGO

A Go wrapper for hnswlib 📦

Installation

go get github.com/Eigen-DB/hnswgo

Usage

package examples

import (
	"fmt"
	"time"

	"github.com/Eigen-DB/hnswgo"
)

func main() {
	dimensions := 2
	maxElements := 10000
	m := 32
	efConstruction := 400
	spaceType := "l2"
	seed := int(time.Now().Unix())

	// instantiate the index
	index, err := hnswgo.New(
		dimensions,
		m,
		efConstruction,
		seed,
		uint32(maxElements),
		spaceType,
	)
	if err != nil {
		fmt.Printf("Error: %s\n", err.Error())
	}

	defer index.Free() // defer freeing the index from memory (don't forget in order ot prevent memory leaks)

	// sample vectors
	vectors := [][]float32{
		{1.2, 3.4},
		{2.1, 4.5},
		{0.5, 1.7},
		{3.3, 2.2},
		{4.8, 5.6},
		{7.1, 8.2},
		{9.0, 0.4},
		{6.3, 3.5},
		{2.9, 7.8},
		{5.0, 1.1},
	}

	// insert sample vectors
	for i, v := range vectors {
		err = index.InsertVector(v, uint32(i))
		if err != nil {
			fmt.Printf("Error: %s\n", err.Error())
		}
	}

	k := 5
	nnLabels, nnDists, err := index.SearchKNN(vectors[0], k) // perform similarity search where the first of our sample vectors is the query vector
	if err != nil {
		fmt.Printf("Error: %s\n", err.Error())
	}

	fmt.Printf("%d-nearest neighbors:\n", k)
	for i := range nnLabels {
		fmt.Printf("vector %d is %f units from query vector\n", nnLabels[i], nnDists[i])
	}
}

Visualize the vectors in this example here.

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

func Normalize ¶

func Normalize(vector []float32)

Normalizes a vector in place. Normalize(v) = (1/|v|)*v

- vector: the vector to Normalize in place

Types ¶

type Index ¶

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

func New ¶

func New(dim int, m int, efConstruction int, randSeed int, maxElements uint32, spaceType string) (*Index, error)

Returns a reference to an instance of an HNSW index.

- dim: dimension of the vector space

- maxElements: index's vector storage capacity

- m: `m` parameter in the HNSW algorithm

- efConstruction: `efConstruction` parameter in the HNSW algorithm

- randSeed: random seed

- spaceType: similarity metric to use in the index

Returns an instance of an HNSW index, or an error if there was a problem initializing the index.

func (*Index) DeleteVector ¶

func (i *Index) DeleteVector(label uint64) error

Marks a vector with the specified label as deleted, which omits it from KNN search.

- label: the vector's label

Returns an error if one occured.

func (*Index) Free ¶

func (i *Index) Free()

Frees the HNSW index from memory.

func (*Index) GetVector ¶

func (i *Index) GetVector(label uint64) ([]float32, error)

Returns a vector's components using its label

- label: the vector's label

Returns the vector's components with specified label

func (*Index) InsertVector ¶

func (i *Index) InsertVector(vector []float32, label uint64) error

Adds a vector to the HNSW index.

- vector: the vector to add to the index

- label: the vector's label

Returns an error if one occured.

func (*Index) SearchKNN ¶

func (i *Index) SearchKNN(vector []float32, k int) ([]uint64, []float32, error)

Performs similarity search on the HNSW index.

- vector: the query vector

- k: the k value

Returns the labels and distances of each of the nearest neighbors, and an error if one occured. Note: the size of both arrays can be < k if k > num of vectors in the index

func (*Index) SetEfConstruction ¶

func (i *Index) SetEfConstruction(efConstruction int) error

Set's the efConstruction parameter in the HNSW index.

- efConstruction: the new efConstruction parameter

Returns an error if one occured.

Directories ¶

Path Synopsis

Jump to

Keyboard shortcuts

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