Skip to content

Eigen-DB/hnswgo

 
 

Repository files navigation

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]