Documentation ¶
Index ¶
- func CosineDistance(a, b []float32) float32
- func EuclideanDistance(a, b []float32) float32
- func RegisterDistanceFunc(name string, fn DistanceFunc)
- type Analyzer
- type DistanceFunc
- type Embeddable
- type Embedding
- type Graph
- func (g *Graph[T]) Add(nodes ...T)
- func (h *Graph[T]) Delete(id string) bool
- func (g *Graph[T]) Dims() int
- func (h *Graph[T]) Export(w io.Writer) error
- func (h *Graph[T]) Import(r io.Reader) error
- func (h *Graph[T]) Len() int
- func (h *Graph[T]) Lookup(id string) (T, bool)
- func (h *Graph[T]) Search(near Embedding, k int) []T
- type SavedGraph
- type Vector
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CosineDistance ¶
CosineDistance computes the cosine distance between two vectors.
func EuclideanDistance ¶
EuclideanDistance computes the Euclidean distance between two vectors.
func RegisterDistanceFunc ¶ added in v0.3.0
func RegisterDistanceFunc(name string, fn DistanceFunc)
RegisterDistanceFunc registers a distance function with a name. A distance function must be registered here before a graph can be exported and imported.
Types ¶
type Analyzer ¶
type Analyzer[T Embeddable] struct { Graph *Graph[T] }
Analyzer is a struct that holds a graph and provides methods for analyzing it. It offers no compatibility guarantee as the methods of measuring the graph's health with change with the implementation.
func (*Analyzer[T]) Connectivity ¶
Connectivity returns the average number of edges in the graph for each non-empty layer.
func (*Analyzer[T]) Topography ¶
Topography returns the number of nodes in each layer of the graph.
type DistanceFunc ¶
DistanceFunc is a function that computes the distance between two vectors.
type Embeddable ¶
type Embeddable interface { // ID returns a unique identifier for the object. ID() string // Embedding returns the embedding of the object. // float32 is used for compatibility with OpenAI embeddings. Embedding() Embedding }
Embeddable describes a type that can be embedded in a HNSW graph.
type Graph ¶
type Graph[T Embeddable] struct { // Distance is the distance function used to compare embeddings. Distance DistanceFunc // Rng is used for level generation. It may be set to a deterministic value // for reproducibility. Note that deterministic number generation can lead to // degenerate graphs when exposed to adversarial inputs. Rng *rand.Rand // M is the maximum number of neighbors to keep for each node. // A good default for OpenAI embeddings is 16. M int // Ml is the level generation factor. // E.g., for Ml = 0.25, each layer is 1/4 the size of the previous layer. Ml float64 // EfSearch is the number of nodes to consider in the search phase. // 20 is a reasonable default. Higher values improve search accuracy at // the expense of memory. EfSearch int // contains filtered or unexported fields }
Graph is a Hierarchical Navigable Small World graph. All public parameters must be set before adding nodes to the graph.
func NewGraph ¶
func NewGraph[T Embeddable]() *Graph[T]
NewGraph returns a new graph with default parameters, roughly designed for storing OpenAI embeddings.
func (*Graph[T]) Add ¶
func (g *Graph[T]) Add(nodes ...T)
Add inserts nodes into the graph. If another node with the same ID exists, it is replaced.
func (*Graph[T]) Delete ¶
Delete removes a node from the graph by ID. It tries to preserve the clustering properties of the graph by replenishing connectivity in the affected neighborhoods.
func (*Graph[T]) Dims ¶ added in v0.3.0
Dims returns the number of dimensions in the graph, or 0 if the graph is empty.
func (*Graph[T]) Export ¶ added in v0.3.0
Export writes the graph to a writer.
T must implement io.WriterTo.
func (*Graph[T]) Import ¶ added in v0.3.0
Import reads the graph from a reader. T must implement io.ReaderFrom. The imported graph does not have to match the exported graph's parameters (except for dimensionality). The graph will converge onto the new parameters.
type SavedGraph ¶ added in v0.3.0
type SavedGraph[T Embeddable] struct { *Graph[T] Path string }
SavedGraph is a wrapper around a graph that persists changes to a file upon calls to Save. It is more convenient but less powerful than calling Graph.Export and Graph.Import directly.
func LoadSavedGraph ¶ added in v0.3.0
func LoadSavedGraph[T Embeddable](path string) (*SavedGraph[T], error)
LoadSavedGraph opens a graph from a file, reads it, and returns it.
If the file does not exist (i.e. this is a new graph), the equivalent of NewGraph is returned.
It does not hold open a file descriptor, so SavedGraph can be forgotten without ever calling Save.
func (*SavedGraph[T]) Save ¶ added in v0.3.0
func (g *SavedGraph[T]) Save() error
Save writes the graph to the file.
type Vector ¶
type Vector struct {
// contains filtered or unexported fields
}
Vector is a struct that holds an ID and an embedding and implements the Embeddable interface.
func MakeVector ¶
MakeVector creates a new Vector with the given ID and embedding.