vecgo

package module
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2024 License: MIT Imports: 8 Imported by: 0

README

🧬🔍🗄️ Vecgo

Build Status Go Reference goreportcard

Vecgo is a Go library designed for efficient vector indexing and searching, supporting various index types and emphasizing approximate nearest neighbor search. It provides a versatile and user-friendly interface for managing and querying vast collections of high-dimensional vectors.

⚠ This is experimental and subject to breaking changes.

Features

  • Support for multiple index types, including flat and HNSW (Hierarchical Navigable Small World) algorithms.
  • Customizable options for memory usage and search performance.
  • Efficient handling of high-dimensional vectors.
  • Embeddable vector store for seamless integration with Go applications.

Usage

Here's a basic example demonstrating how to perform nearest neighbor search with Vecgo:

package main

import (
	"fmt"
	"log"

	"github.com/hupe1980/vecgo"
)

func main() {
	vg := vecgo.NewHNSW[string]()
	// vg := vecgo.NewFlat[string]()

	_, err := vg.Insert(vecgo.VectorWithData[string]{
		Vector: []float32{1.0, 2.0, 2.5},
		Data:   "Hello World!",
	})
	if err != nil {
		log.Fatal(err)
	}

	k := 5
	
	result, err := vg.KNNSearch([]float32{1.0, 2.0, 2.5}, k)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(result[0].Data)
}

For more information on each method, please refer to the GoDoc documentation.

Contributing

Contributions to Vecgo are welcome! Feel free to open issues for bug reports, feature requests, or submit pull requests with improvements.

License

Vecgo is licensed under the MIT License. See the LICENSE file for details.

Documentation

Overview

Package vecgo provides functionalities for an embedded vector store database.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned when an item is not found.
	ErrNotFound = errors.New("not found")

	// ErrInvalidEFValue is returned when the explore factor (ef) is less than the value of k.
	ErrInvalidEFValue = errors.New("explore factor (ef) must be at least the value of k")
)

Functions

This section is empty.

Types

type BruteSearchOptions added in v0.0.5

type BruteSearchOptions struct {
	// FilterFunc is a function used to filter search results.
	FilterFunc FilterFunc
}

BruteSearchOptions contains options for brute-force search.

type FilterFunc added in v0.0.5

type FilterFunc func(id uint32) bool

FilterFunc is a function type used for filtering search results.

type KNNSearchOptions

type KNNSearchOptions struct {
	// EF (Explore Factor) specifies the size of the dynamic list for the nearest neighbors during the search.
	// Higher EF leads to more accurate but slower search.
	// EF cannot be set lower than the number of queried nearest neighbors (k).
	// The value of EF can be anything between k and the size of the dataset.
	EF int

	// FilterFunc is a function used to filter search results.
	FilterFunc FilterFunc
}

KNNSearchOptions contains options for KNN search.

type SearchResult

type SearchResult[T any] struct {
	index.SearchResult

	// Data is the associated data of the search result.
	Data T
}

SearchResult represents a search result.

type Vecgo

type Vecgo[T any] struct {
	// contains filtered or unexported fields
}

Vecgo is a vector store database.

func New

func New[T any](i index.Index) *Vecgo[T]

New creates a new Vecgo instance with the given index.

func NewFlat added in v0.0.8

func NewFlat[T any](optFns ...func(o *flat.Options)) *Vecgo[T]

NewFlat creates a new Vecgo instance with a flat index.

func NewFromFilename

func NewFromFilename[T any](filename string) (*Vecgo[T], error)

NewFromFilename creates a new Vecgo instance from a file.

func NewFromReader

func NewFromReader[T any](r io.Reader) (*Vecgo[T], error)

NewFromReader creates a new Vecgo instance from an io.Reader.

func NewHNSW added in v0.0.8

func NewHNSW[T any](optFns ...func(o *hnsw.Options)) *Vecgo[T]

NewHNSW creates a new Vecgo instance with an HNSW index.

func (*Vecgo[T]) BruteSearch

func (vg *Vecgo[T]) BruteSearch(query []float32, k int, optFns ...func(o *BruteSearchOptions)) ([]SearchResult[T], error)

BruteSearch performs a brute-force search.

func (*Vecgo[T]) Get

func (vg *Vecgo[T]) Get(id uint32) (T, error)

Get retrieves an item by ID.

func (*Vecgo[T]) Insert

func (vg *Vecgo[T]) Insert(item VectorWithData[T]) (uint32, error)

Insert inserts a vector along with associated data into the database.

func (*Vecgo[T]) KNNSearch

func (vg *Vecgo[T]) KNNSearch(query []float32, k int, optFns ...func(o *KNNSearchOptions)) ([]SearchResult[T], error)

KNNSearch performs a K-nearest neighbor search.

func (*Vecgo[T]) PrintStats

func (vg *Vecgo[T]) PrintStats()

PrintStats prints statistics about the database.

func (*Vecgo[T]) SaveToFile

func (vg *Vecgo[T]) SaveToFile(filename string) error

SaveToFile saves the Vecgo database to a file.

func (*Vecgo[T]) SaveToWriter

func (vg *Vecgo[T]) SaveToWriter(w io.Writer) error

SaveToWriter saves the Vecgo database to an io.Writer.

type VectorWithData

type VectorWithData[T any] struct {
	Vector []float32
	Data   T
}

VectorWithData represents a vector along with associated data.

Directories

Path Synopsis
_examples
Package index provides interfaces and types for vector search indexes.
Package index provides interfaces and types for vector search indexes.
flat
Package flat provides an implementation of a flat index for vector storage and search.
Package flat provides an implementation of a flat index for vector storage and search.
hnsw
Package hnsw implements the Hierarchical Navigable Small World (HNSW) graph for approximate nearest neighbor search.
Package hnsw implements the Hierarchical Navigable Small World (HNSW) graph for approximate nearest neighbor search.
internal

Jump to

Keyboard shortcuts

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