Documentation
¶
Index ¶
- Constants
- func EnsureVecWASM()
- type Index
- func (i *Index) All(ctx context.Context, yield func(model.SectionID) bool) error
- func (i *Index) Close() error
- func (i *Index) DeleteByID(ctx context.Context, ids ...model.SectionID) error
- func (i *Index) DeleteBySource(ctx context.Context, source *url.URL) error
- func (i *Index) GenerateSnapshot(ctx context.Context) (io.ReadCloser, error)
- func (i *Index) Index(ctx context.Context, document model.Document, funcs ...index.OptionFunc) error
- func (i *Index) RestoreSnapshot(ctx context.Context, r io.Reader) error
- func (i *Index) Search(ctx context.Context, query string, opts index.SearchOptions) ([]*index.SearchResult, error)
- func (i *Index) SearchFiltered(ctx context.Context, query string, filter index.Filter, ...) ([]*index.SearchResult, error)
- func (i *Index) Semantic() bool
- type OptionFunc
- type Options
- type SnapshottedMetadata
- type SnapshottedRecord
Constants ¶
const DefaultEmbeddingsConcurrency int = 8
DefaultEmbeddingsConcurrency bounds, by default, how many embedding batches are computed in parallel for a single document. 8 gives most of the speedup on large documents while staying modest enough to avoid overwhelming the embeddings endpoint (rate limiting / 429s); lower it via config for stricter endpoints.
const DefaultMaxWords int = 500
DefaultMaxWords bounds, by default, the number of words per chunk sent to the embeddings model.
const DefaultReadPoolSize int = 4
DefaultReadPoolSize is the default number of read connections opened by NewIndexAtPath for concurrent searches.
const DefaultVectorSize int = 768
DefaultVectorSize is the default dimension of the vec0 embedding column, matching common 768-dimension embedding models.
Variables ¶
This section is empty.
Functions ¶
func EnsureVecWASM ¶ added in v0.0.3
func EnsureVecWASM()
EnsureVecWASM forces ncruces/go-sqlite3 to load the sqlite-vec-enabled WASM build, overriding any plain SQLite build that another package may have selected.
It is only needed when the same process opens a sqlite-vec index AND another ncruces/go-sqlite3 connection whose package imports the vanilla WASM build — most notably the gorm SQLite store (gorm.NewSQLiteStore), which transitively imports github.com/ncruces/go-sqlite3/embed. Both builds assign the global sqlite3.Binary from their init, so which one wins depends on undefined init order; without this call the sqlite-vec build may lose and vec0 virtual tables fail with "no such module: vec0".
Call it once at startup, before opening ANY sqlite connection (the store included). The vec0-enabled build is a superset of plain SQLite, so the store works on it unchanged. It is unnecessary when sqlite-vec is the only ncruces/go-sqlite3 user in the process.
Types ¶
type Index ¶
type Index struct {
// contains filtered or unexported fields
}
func NewIndex ¶
NewIndex creates a sqlite-vec backed vector index on top of the given connection. The connection remains owned by the caller. The embeddings model (WithEmbeddingsModel) and vector size (WithVectorSize) are recorded in the index on first use; reopening an existing index with a different model or size is rejected to prevent silently mixing incompatible vectors.
func NewIndexAtPath ¶ added in v0.2.0
NewIndexAtPath opens (and owns) a sqlite-vec index stored at path: one write connection plus a pool of read connections (WithReadPoolSize, default DefaultReadPoolSize), so concurrent searches run on their own connection under WAL snapshot isolation instead of serializing on a single one. Unlike NewIndex, migrations and the model/vector-size identity check run eagerly, and the returned index must be Closed by the caller.
func (*Index) Close ¶ added in v0.2.0
Close closes the connections owned by the index (NewIndexAtPath). An index built with NewIndex owns nothing and Close is a no-op. In-flight operations must have completed before calling it.
func (*Index) DeleteByID ¶
DeleteByID implements index.Index.
func (*Index) DeleteBySource ¶
DeleteBySource implements index.Index.
func (*Index) GenerateSnapshot ¶
GenerateSnapshot implements snapshot.Snapshotable.
func (*Index) Index ¶
func (i *Index) Index(ctx context.Context, document model.Document, funcs ...index.OptionFunc) error
Index implements index.Index.
func (*Index) RestoreSnapshot ¶
RestoreSnapshot implements snapshot.Snapshotable.
func (*Index) Search ¶
func (i *Index) Search(ctx context.Context, query string, opts index.SearchOptions) ([]*index.SearchResult, error)
Search implements index.Index.
func (*Index) SearchFiltered ¶ added in v0.7.0
func (i *Index) SearchFiltered(ctx context.Context, query string, filter index.Filter, opts index.SearchOptions) ([]*index.SearchResult, error)
SearchFiltered implements index.FilterableIndex: the metadata filter is evaluated inside the KNN query, so the k results returned are k results that already satisfy it — instead of k results from which the filter may leave nothing.
The translation is validated against the shared conformance suite (index/filtertest), which is what allows this index to advertise the capability: filtering here and filtering in Go must keep the same documents.
type OptionFunc ¶ added in v0.0.2
type OptionFunc func(opts *Options)
func WithCoarseQuantization ¶ added in v0.2.0
func WithCoarseQuantization(enabled bool) OptionFunc
WithCoarseQuantization toggles the two-stage binary-quantization search (see Options.CoarseQuantization).
func WithEmbeddingsConcurrency ¶ added in v0.0.6
func WithEmbeddingsConcurrency(n int) OptionFunc
WithEmbeddingsConcurrency bounds how many embedding batches are computed in parallel for a single document (see Options.EmbeddingsConcurrency).
func WithEmbeddingsModel ¶ added in v0.0.2
func WithEmbeddingsModel(model string) OptionFunc
WithEmbeddingsModel records the embeddings model backing the index.
func WithMaxWords ¶ added in v0.0.2
func WithMaxWords(maxWords int) OptionFunc
WithMaxWords bounds the size of the chunks sent to the embeddings model.
func WithReadPoolSize ¶ added in v0.2.0
func WithReadPoolSize(n int) OptionFunc
WithReadPoolSize sets the number of dedicated read connections opened by NewIndexAtPath (see Options.ReadPoolSize).
func WithVectorSize ¶ added in v0.0.2
func WithVectorSize(size int) OptionFunc
WithVectorSize sets the dimension of the vec0 embedding column.
type Options ¶ added in v0.0.2
type Options struct {
// EmbeddingsModel identifies the embeddings model. It is recorded in the
// index and opening an existing index with a different model is rejected
// (the stored vectors would no longer be comparable).
EmbeddingsModel string
// VectorSize is the dimension of the vec0 embedding column (default
// DefaultVectorSize). It is recorded in the index; opening an existing
// index with a different size is rejected.
VectorSize int
// MaxWords bounds the size of the chunks sent to the embeddings model.
MaxWords int
// EmbeddingsConcurrency bounds how many embedding batches are computed in
// parallel for a single document. Large documents split into many batches;
// computing them concurrently cuts indexing latency at the cost of more
// simultaneous requests to the embeddings endpoint. Defaults to
// DefaultEmbeddingsConcurrency when <= 0.
EmbeddingsConcurrency int
// ReadPoolSize is the number of dedicated read connections opened by
// NewIndexAtPath (defaults to DefaultReadPoolSize when <= 0). Ignored by
// NewIndex, which uses the single caller-owned connection.
ReadPoolSize int
// CoarseQuantization enables the two-stage search: a fast KNN on the
// binary-quantized vectors (Hamming distance) preselects k×8 candidates,
// re-scored with the full float vectors. ~30× faster scans at a marginal
// quality cost, worthwhile on large corpora. Requires a vector size
// divisible by 8.
CoarseQuantization bool
}
Options configures a sqlite-vec Index.
func NewOptions ¶ added in v0.0.2
func NewOptions(funcs ...OptionFunc) *Options
type SnapshottedMetadata ¶
type SnapshottedMetadata struct {
Model string
}
type SnapshottedRecord ¶
type SnapshottedRecord struct {
Source string
SectionID string
ChunkIndex int
Embeddings []byte
Collections []string
// Metadata is the document's metadata JSON (schema v3), repeated on each of
// its chunks. Snapshots taken before v3 decode it as "", which restores a
// document without metadata — the same state an index had back then.
Metadata string
}