Documentation
¶
Overview ¶
Package store holds the three ways a graph is kept: by similarity, by edge, and by assertion. They are separate interfaces because they answer separate questions, and a deployment may back them with one engine or three.
Index ¶
- Constants
- Variables
- func Cos(a, b []float32) float64
- func Dist(a, b []float32) float64
- func Dot(a, b []float32) float64
- func Norm(v []float32) float64
- func Unit(v []float32) []float32
- type Cond
- type Drivers
- type File
- func (s *File) Assert(ctx context.Context, sub, pred, obj string) error
- func (s *File) Close() error
- func (s *File) Compact() error
- func (s *File) Components() [][]string
- func (s *File) Cut(ctx context.Context, id string) error
- func (s *File) Degree(id string) (in, out int)
- func (s *File) Drop(ctx context.Context, ids ...string) error
- func (s *File) Dump() Snap
- func (s *File) Edge(ctx context.Context, from, to, label string) error
- func (s *File) Edges(from, to, label string) []Link
- func (s *File) Get(id string) ([]float32, map[string]any, bool)
- func (s *File) In(ctx context.Context, id string) ([]string, error)
- func (s *File) Match(ctx context.Context, sub, pred, obj string) ([][3]string, error)
- func (s *File) Metric() Metric
- func (s *File) Name() string
- func (s *File) Near(ctx context.Context, v []float32, k int) ([]Match, error)
- func (s *File) Node(ctx context.Context, id string, props map[string]any) error
- func (s *File) Nodes() []string
- func (s *File) Out(ctx context.Context, id string) ([]string, error)
- func (s *File) Path(ctx context.Context, from, to string) ([]string, error)
- func (s *File) Props(id string) (map[string]any, bool)
- func (s *File) Put(ctx context.Context, id string, v []float32, meta map[string]any) error
- func (s *File) Rank(v Metric) error
- func (s *File) Reach(ctx context.Context, id string, hops int) ([]string, error)
- func (s *File) Retract(ctx context.Context, sub, pred, obj string) error
- func (s *File) Scan(offset, limit int) []Match
- func (s *File) Search(ctx context.Context, q Query) ([]Match, error)
- func (s *File) Stat() Stat
- func (s *File) Unlink(ctx context.Context, from, to, label string) error
- type Filter
- func (f Filter) Add(field string, op Op, v any) Filter
- func (f Filter) Eq(field string, v any) Filter
- func (f Filter) Ge(field string, v any) Filter
- func (f Filter) Gt(field string, v any) Filter
- func (f Filter) Has(field string, v any) Filter
- func (f Filter) In(field string, list ...any) Filter
- func (f Filter) Le(field string, v any) Filter
- func (f Filter) Lt(field string, v any) Filter
- func (f Filter) Match(meta map[string]any) bool
- func (f Filter) Ne(field string, v any) Filter
- type Graph
- type Item
- type Link
- type Match
- type Mem
- func (m *Mem) Assert(ctx context.Context, s, p, o string) error
- func (m *Mem) Components() [][]string
- func (m *Mem) Cut(ctx context.Context, id string) error
- func (m *Mem) Degree(id string) (in, out int)
- func (m *Mem) Drop(ctx context.Context, ids ...string) error
- func (m *Mem) Dump() Snap
- func (m *Mem) Edge(ctx context.Context, from, to, label string) error
- func (m *Mem) Edges(from, to, label string) []Link
- func (m *Mem) Get(id string) ([]float32, map[string]any, bool)
- func (m *Mem) In(ctx context.Context, id string) ([]string, error)
- func (m *Mem) Load(ctx context.Context, s Snap) error
- func (m *Mem) Match(ctx context.Context, s, p, o string) ([][3]string, error)
- func (m *Mem) Metric() Metric
- func (m *Mem) Near(ctx context.Context, v []float32, k int) ([]Match, error)
- func (m *Mem) Node(ctx context.Context, id string, props map[string]any) error
- func (m *Mem) Nodes() []string
- func (m *Mem) Out(ctx context.Context, id string) ([]string, error)
- func (m *Mem) Path(ctx context.Context, from, to string) ([]string, error)
- func (m *Mem) Props(id string) (map[string]any, bool)
- func (m *Mem) Put(ctx context.Context, id string, v []float32, meta map[string]any) error
- func (m *Mem) Rank(v Metric)
- func (m *Mem) Reach(ctx context.Context, id string, hops int) ([]string, error)
- func (m *Mem) Retract(ctx context.Context, s, p, o string) error
- func (m *Mem) Scan(offset, limit int) []Match
- func (m *Mem) Search(ctx context.Context, q Query) ([]Match, error)
- func (m *Mem) Stat() Stat
- func (m *Mem) Unlink(ctx context.Context, from, to, label string) error
- type Metric
- type Op
- type Query
- type Snap
- type Stat
- type Triple
- type Vector
Constants ¶
const Space = "space"
Space is the metadata key a namespace is written under. A namespace is a filter, not a second kind of store: tag a vector with it on the way in and ask for it on the way out.
m.Put(ctx, "a", v, map[string]any{store.Space: "docs"})
m.Search(ctx, store.Query{Vec: q, K: 5, Filter: store.Filter{}.Eq(store.Space, "docs")})
Variables ¶
var ( Vectors = &Drivers[Vector]{} Graphs = &Drivers[Graph]{} Triples = &Drivers[Triple]{} )
Vectors, Graphs and Triples hold the drivers of each kind. A store that answers more than one question registers under each of them.
var ( // ErrID means an identifier was blank. ErrID = errors.New("store: empty id") // ErrTerm means a triple was missing a subject, predicate or object. ErrTerm = errors.New("store: empty term") // ErrDim means a vector's length differs from the store's. ErrDim = errors.New("store: vector dimension") // ErrDriver means a driver is named but this build cannot serve it. ErrDriver = errors.New("store: driver unavailable") )
What a store refuses to do. Test for these with errors.Is.
Functions ¶
func Cos ¶ added in v0.2.0
Cos is the cosine of the angle between two vectors. A zero vector points nowhere, so it scores 0 against everything rather than dividing by zero.
Types ¶
type Drivers ¶ added in v0.2.0
type Drivers[T any] struct { // contains filtered or unexported fields }
Drivers names the implementations of one kind of store, so a program can be told at run time which one to use. A driver in another package adds itself in an init, and a caller reaches it by importing that package for its effect:
import _ "github.com/hanzoai/semantic/store/qdrant"
v, err := store.Vectors.Open("qdrant", "http://localhost:6333/docs")
The zero value is an empty set of drivers, ready to use.
func (*Drivers[T]) Add ¶ added in v0.2.0
Add registers a driver under a name, replacing whatever was there. A nil open registers the name alone: Open then reports ErrDriver, so a caller can tell a store this build cannot serve from one nobody has heard of.
func (*Drivers[T]) Has ¶ added in v0.2.0
Has reports whether a name is registered, whether or not it can be opened.
type File ¶ added in v0.2.0
type File struct {
// contains filtered or unexported fields
}
File keeps a store in one file on disk. It answers every question Mem does, from memory, and owes its durability to a log: each write is appended as one JSON record and flushed before the call returns, so reopening the file replays exactly what was acknowledged.
A process that dies mid-write leaves a partial last line. A record counts only if it is terminated and decodes, so the partial line is discarded on the next open and the file is truncated back to the last whole record — the write that never returned never happened.
The log grows with every write. Compact folds it into a single snapshot record, written beside the file and renamed over it, so a crash during compaction leaves the old log intact.
func Open ¶ added in v0.2.0
Open opens the store at path, creating it if it is not there, and replays what was written.
func (*File) Compact ¶ added in v0.2.0
Compact rewrites the file as one snapshot, discarding a log that has grown longer than the state it describes. The snapshot is written beside the file and renamed over it, so an interrupted compaction leaves the store as it was.
func (*File) Components ¶ added in v0.2.0
Components groups the nodes into connected components. See Mem.Components.
func (*File) Degree ¶ added in v0.2.0
Degree counts the edges into and out of a node. See Mem.Degree.
func (*File) Name ¶ added in v0.2.0
Name is the path the store is kept at, as os.File reports its own.
func (*File) Rank ¶ added in v0.2.0
Rank sets how nearness is measured and records the choice, so a store reopened later ranks the way it was told to.
func (*File) Retract ¶ added in v0.2.0
Retract removes the triples that match a pattern. See Mem.Retract.
type Filter ¶ added in v0.2.0
type Filter []Cond
Filter is a conjunction: metadata passes when every condition holds. The zero value passes everything, so a query with no filter is not a special case.
Conditions are added by the builder methods, each returning the extended filter, so a filter reads as the question it asks:
f := store.Filter{}.Eq("lang", "en").Ge("year", 2020)
func (Filter) Has ¶ added in v0.2.0
Has requires the field to contain v: a substring of a string field, or a member of a list field.
type Graph ¶
type Graph interface {
Node(ctx context.Context, id string, props map[string]any) error
Edge(ctx context.Context, from, to, label string) error
Out(ctx context.Context, id string) ([]string, error)
}
Graph answers "what is connected to this".
type Item ¶ added in v0.2.0
Item is an identifier and what is known about it: a vector and its metadata, or — with no vector — a graph node and its properties.
type Match ¶
Match is a neighbour and how near it is.
func Blend ¶ added in v0.2.0
Blend merges ranked lists by weighted sum of the scores they already carry, which is the right combination when the sources score on one scale. A weight missing for a list counts as an equal share, 1/n.
func Fuse ¶ added in v0.2.0
Fuse merges ranked lists by reciprocal rank: an item scores the sum, over the lists it appears in, of 1/(k+rank), counting rank from 1. It needs no scores, only orders, which is what makes it the way to combine answers from sources that score on different scales — a vector store and a text index, say.
k damps how much the head of any one list decides the outcome; 60 is the constant from the original description and is used when k is not positive. Metadata comes from the first list an item appeared in.
type Mem ¶ added in v0.2.0
type Mem struct {
// contains filtered or unexported fields
}
Mem keeps a store in memory and implements all three of Vector, Graph and Triple, so a caller that wants one of them and a caller that wants all three use the same object. It is the default every other driver is measured against, and the store a program uses when it has no database.
The zero value is an empty store, ready to use, ranking by cosine. Every method is safe for concurrent use.
func (*Mem) Assert ¶ added in v0.2.0
Assert records one triple. The same triple asserted twice is one fact.
func (*Mem) Components ¶ added in v0.2.0
Components groups the nodes into connected components, ignoring edge direction. Each group is sorted, and the groups are ordered by their first node, so the answer is the same on every run.
func (*Mem) Cut ¶ added in v0.2.0
Cut removes a node and every edge that touched it, because an edge to a node that is gone is not a fact about anything.
func (*Mem) Degree ¶ added in v0.2.0
Degree counts the edges into and out of a node, the simplest measure of how central it is.
func (*Mem) Dump ¶ added in v0.2.0
Dump returns everything the store holds, in the order it was written.
func (*Mem) Edge ¶ added in v0.2.0
Edge links two nodes under a label, creating either endpoint if it is new. The same link asserted twice is one edge.
func (*Mem) Edges ¶ added in v0.2.0
Edges lists the links that match a pattern, first written first. An empty from, to or label matches any.
func (*Mem) Get ¶ added in v0.2.0
Get returns a vector and its metadata, and whether the store holds the id.
func (*Mem) Load ¶ added in v0.2.0
Load replaces everything the store holds with a snapshot. It is the other half of Dump: what one writes, the other reads back.
func (*Mem) Match ¶ added in v0.2.0
Match returns the triples that fit a pattern, first asserted first. An empty subject, predicate or object matches any, so Match(ctx, "", "", "") returns everything.
func (*Mem) Near ¶ added in v0.2.0
Near returns the k nearest vectors to v, nearest first. A k of zero or less returns every vector the store holds, ranked.
func (*Mem) Node ¶ added in v0.2.0
Node writes a node and its properties, creating it if the store has not heard of it. Properties given replace those of the same name; the rest are left alone, so a caller may set one field without reading the others.
func (*Mem) Path ¶ added in v0.2.0
Path returns a shortest path from one node to another, both ends included, following edges in their own direction. It is nil when there is no path; a node to itself is a path of one.
func (*Mem) Props ¶ added in v0.2.0
Props returns a node's properties and whether the store holds it.
func (*Mem) Put ¶ added in v0.2.0
Put writes a vector under an id, replacing whatever was there. The first vector fixes the store's dimension; a later one of a different length is refused with ErrDim, because a store whose vectors do not share a space cannot rank them. A vector of no components names no point and is refused for the same reason.
func (*Mem) Rank ¶ added in v0.2.0
Rank sets how nearness is measured. The zero value of a store ranks by cosine.
func (*Mem) Reach ¶ added in v0.2.0
Reach lists the nodes within hops steps of one, in either direction and excluding the node itself, nearest first. Zero hops reaches nothing.
func (*Mem) Retract ¶ added in v0.2.0
Retract removes the triples that fit a pattern, on the same wildcards as Match.
func (*Mem) Scan ¶ added in v0.2.0
Scan pages through the stored vectors in the order they were written, which is how a caller walks a whole store without ranking it. Scores are zero: nothing was asked for, so nothing is near.
type Metric ¶ added in v0.2.0
type Metric string
Metric names how nearness is measured. The zero value is Cosine, so a store that states no metric compares directions.
const ( // Cosine is the angle between two vectors: 1 when they point the same // way, 0 when they are orthogonal, -1 when opposed. Length is ignored. Cosine Metric = "cosine" // Inner is the dot product, so length counts as well as direction. Inner Metric = "inner" // Euclid is straight-line distance, negated so that larger still means // nearer and one comparison orders every metric. Euclid Metric = "euclid" )
type Op ¶ added in v0.2.0
type Op string
Op is how a condition compares a metadata field to a value.
const ( Eq Op = "eq" // field equals value Ne Op = "ne" // field differs from value Gt Op = "gt" // field is greater than value Ge Op = "ge" // field is greater than or equal to value Lt Op = "lt" // field is less than value Le Op = "le" // field is less than or equal to value In Op = "in" // field is one of the values in a list Has Op = "has" // field contains value: a substring, or a member of a list )
type Query ¶ added in v0.2.0
type Query struct {
Vec []float32
K int
Filter Filter
// Metric overrides the store's own. Empty uses the store's.
Metric Metric
}
Query asks for the nearest vectors that also match a filter.
A nil Vec ranks nothing and returns whatever the filter admits, in the order it was written, which is how a metadata-only lookup is asked for. A K of zero or less asks for every match rather than none, so a filter stands on its own.
type Snap ¶ added in v0.2.0
Snap is everything a store holds. A durable store writes one to make a snapshot and reads one back to restore.