mirror of
https://github.com/hanzoai/semantic.git
synced 2026-10-30 15:04:19 +00:00
The first commit was the shape. This is the implementation: every stage of the pipeline, the graph and its measures, the type system over it, entailment, deduplication and conflict resolution, the decision layer, the stores and the export formats. Twenty-six thousand lines of Go and sixteen thousand of tests, against no dependency but the standard library. Where the Python reaches for torch, spacy or a database driver, the Go states an interface and reports what is missing rather than pretending. A caller can tell "this build cannot serve that" from "nobody has heard of it", which is the difference between a port and a stub. The deterministic paths are complete and are what the tests exercise; the model-shaped ones are one adapter away. Porting a bug is not parity, so several places now disagree with the original on purpose. The JSON repair that closed a truncated reply counted each bracket kind separately and closed them backwards on the one input it exists for; it now works off a stack and ignores brackets inside strings. The entity patterns joined words with \s, so a heading ran into the sentence beneath it and became one name while the real relation was lost; a name is separated by horizontal space now. Eigenvector centrality ran plain power iteration, which on a bipartite graph swings between the two ends of a sign-paired eigenvalue forever and reported every node in a star as equally central; it iterates with A+I, which moves no eigenvector and settles. Similarity summed its weighted parts in map order, and float addition is not associative, so the same comparison scored differently between runs and duplicate detection returned a different answer each time. An inferred ontology named object-property endpoints by raw source type and data-property endpoints by class, so the schema referred to classes it did not contain. A property with no IRI of its own was minted as a class and came back Pascal-cased, renaming the term on the way out of the document. The command built a pipeline with every stage nil and panicked on each invocation. It is now the composition the package documents, and Run reports a missing ingester instead of dereferencing it. Co-authored-by: Hanzo Dev <[email protected]>
105 lines
3.2 KiB
Go
105 lines
3.2 KiB
Go
package store
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"sync"
|
|
)
|
|
|
|
// 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.
|
|
type Drivers[T any] struct {
|
|
mu sync.RWMutex
|
|
by map[string]func(string) (T, error)
|
|
}
|
|
|
|
// Vectors, Graphs and Triples hold the drivers of each kind. A store that
|
|
// answers more than one question registers under each of them.
|
|
var (
|
|
Vectors = &Drivers[Vector]{}
|
|
Graphs = &Drivers[Graph]{}
|
|
Triples = &Drivers[Triple]{}
|
|
)
|
|
|
|
// 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 (d *Drivers[T]) Add(name string, open func(dsn string) (T, error)) {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
if d.by == nil {
|
|
d.by = map[string]func(string) (T, error){}
|
|
}
|
|
d.by[name] = open
|
|
}
|
|
|
|
// Open builds the store a driver names from a data source name, whose shape
|
|
// the driver decides: a path, a URL, or nothing at all.
|
|
func (d *Drivers[T]) Open(name, dsn string) (T, error) {
|
|
d.mu.RLock()
|
|
open, ok := d.by[name]
|
|
d.mu.RUnlock()
|
|
var zero T
|
|
if !ok {
|
|
return zero, fmt.Errorf("%w: no driver %q", ErrDriver, name)
|
|
}
|
|
if open == nil {
|
|
return zero, fmt.Errorf("%w: %q is not built in", ErrDriver, name)
|
|
}
|
|
return open(dsn)
|
|
}
|
|
|
|
// Has reports whether a name is registered, whether or not it can be opened.
|
|
func (d *Drivers[T]) Has(name string) bool {
|
|
d.mu.RLock()
|
|
defer d.mu.RUnlock()
|
|
_, ok := d.by[name]
|
|
return ok
|
|
}
|
|
|
|
// Names lists every registered driver, in order.
|
|
func (d *Drivers[T]) Names() []string {
|
|
d.mu.RLock()
|
|
defer d.mu.RUnlock()
|
|
out := make([]string, 0, len(d.by))
|
|
for n := range d.by {
|
|
out = append(out, n)
|
|
}
|
|
sort.Strings(out)
|
|
return out
|
|
}
|
|
|
|
// The stores this package implements, and the names of the ones it does
|
|
// not. The names are here so the set of stores semantica knows about is one
|
|
// list a program can read, rather than something a caller learns by an
|
|
// import failing. Each unnamed one is a driver in its own package, waiting
|
|
// to be written against the same three interfaces.
|
|
func init() {
|
|
mem := func(string) (*Mem, error) { return &Mem{}, nil }
|
|
Vectors.Add("mem", func(dsn string) (Vector, error) { return mem(dsn) })
|
|
Graphs.Add("mem", func(dsn string) (Graph, error) { return mem(dsn) })
|
|
Triples.Add("mem", func(dsn string) (Triple, error) { return mem(dsn) })
|
|
|
|
Vectors.Add("file", func(dsn string) (Vector, error) { return Open(dsn) })
|
|
Graphs.Add("file", func(dsn string) (Graph, error) { return Open(dsn) })
|
|
Triples.Add("file", func(dsn string) (Triple, error) { return Open(dsn) })
|
|
|
|
for _, n := range []string{"faiss", "milvus", "pgvector", "pinecone", "sqlite", "weaviate"} {
|
|
Vectors.Add(n, nil)
|
|
}
|
|
for _, n := range []string{"age", "falkordb", "neo4j", "neptune"} {
|
|
Graphs.Add(n, nil)
|
|
}
|
|
for _, n := range []string{"anzo", "oxigraph"} {
|
|
Triples.Add(n, nil)
|
|
}
|
|
}
|