Documentation
¶
Index ¶
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.
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.
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 { // M is the maximum number of neighbors to keep for each node. M int // Distance is the distance function used to compare embeddings. Distance DistanceFunc // Ml is the level generation factor. E.g. 1 / log(Ml) is the probability // of adding a node to a level. Ml float64 // EfSearch is the number of nodes to consider in the search phase. EfSearch int // 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 // 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 the affected neighborhoods.
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.