hnswgo

package module
v2.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: MIT Imports: 6 Imported by: 0

README

hnswgo Go Reference

A Go wrapper for hnswlib

Installation

go get github.com/Eigen-DB/hnswgo/v2

Usage

package examples

import (
	"fmt"
	"time"

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

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, uint64(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.

References

Yu. A. Malkov, D. A. Yashunin. "Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs" TPAMI, preprint: [https://arxiv.org/abs/1603.09320]

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 LoadIndex added in v2.1.0

func LoadIndex(location string, dim int, spaceType string, maxElements uint32) (*Index, error)

Loads a saved index and returns a reference to it.

- location: the file path of the saved index

- dim: dimension of the vector space

- spaceType: similarity metric to use in the index ("ip", "cosine", "l2". default: "l2")

- maxElements: index's vector storage capacity

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

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 ("ip", "cosine", "l2". default: "l2")

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. If the a vector with the same label already exists, the function returns an error

- vector: the vector to add to the index

- label: the vector's label

Returns an error if one occured.

func (*Index) ReplaceVector added in v2.2.0

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

Replaces an existing vector in the HNSW index.

- label: the vector's label

- newVector: the new vector used to replace the old vector

Returns an error if one occured.

func (*Index) SaveToDisk added in v2.1.0

func (i *Index) SaveToDisk(location string) error

Saves the index to the disk.

- location: the file path in which to save the index

Returns an error if there was a problem.

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
JackTT - Gopher 🇻🇳