Documentation
¶
Overview ¶
Package veclite provides an embeddable vector database with zero external dependencies. It stores vectors with metadata in a single file using gob encoding.
Basic usage:
db, err := veclite.Open("data.veclite")
if err != nil {
log.Fatal(err)
}
defer db.Close()
coll := db.Collection("embeddings")
id, err := coll.Insert(vector, map[string]any{"file": "main.go"})
results, err := coll.Search(queryVector, veclite.TopK(10))
Index ¶
- Constants
- Variables
- type Collection
- func (c *Collection) All() []*Record
- func (c *Collection) ArchiveRecord(id uint64) error
- func (c *Collection) CleanupExpired() (int, error)
- func (c *Collection) Clear() error
- func (c *Collection) Consolidate(config ConsolidationConfig) (*ConsolidationResult, error)
- func (c *Collection) Count() int
- func (c *Collection) CountExpired() int
- func (c *Collection) Delete(id uint64) error
- func (c *Collection) DeleteWhere(filters ...Filter) (int, error)
- func (c *Collection) Dimension() int
- func (c *Collection) DistanceType() floats.DistanceType
- func (c *Collection) ExpandConsolidation(consolidationID uint64) ([]*Record, error)
- func (c *Collection) Find(filters ...Filter) ([]*Record, error)
- func (c *Collection) FindOne(filters ...Filter) (*Record, error)
- func (c *Collection) FindSimilarClusters(config ConsolidationConfig) ([]MemoryCluster, error)
- func (c *Collection) ForEach(fn func(*Record) bool)
- func (c *Collection) Get(id uint64) (*Record, error)
- func (c *Collection) GetArchived() ([]*Record, error)
- func (c *Collection) GetConsolidations() ([]*Record, error)
- func (c *Collection) GetSession(sessionID string) ([]*Record, error)
- func (c *Collection) GetSessionStats(sessionID string) (SessionStats, error)
- func (c *Collection) GetThread(chunkID uint64) ([]*Record, error)
- func (c *Collection) GetVector(id uint64) ([]float32, error)
- func (c *Collection) HasIndex() bool
- func (c *Collection) HybridSearch(query []float32, text string, opts ...SearchOption) ([]Result, error)
- func (c *Collection) IndexStats() *hnsw.IndexStats
- func (c *Collection) IndexType() IndexType
- func (c *Collection) Insert(vector []float32, payload map[string]any) (uint64, error)
- func (c *Collection) InsertBatch(vectors [][]float32, payloads []map[string]any) ([]uint64, error)
- func (c *Collection) InsertDocument(vector []float32, content string, payload map[string]any) (uint64, error)
- func (c *Collection) InsertText(text string, payload map[string]any) (uint64, error)
- func (c *Collection) InsertTurn(turn ConversationTurn) (uint64, error)
- func (c *Collection) InsertWithOptions(vector []float32, payload map[string]any, opts ...InsertOption) (uint64, error)
- func (c *Collection) Iterate(opts ...IterOption) *Iterator
- func (c *Collection) ListSessions() []string
- func (c *Collection) Name() string
- func (c *Collection) Search(query []float32, opts ...SearchOption) ([]Result, error)
- func (c *Collection) SearchExplain(query []float32, opts ...SearchOption) (*SearchExplanation, error)
- func (c *Collection) SearchInSession(sessionID string, query []float32, opts ...SearchOption) ([]Result, error)
- func (c *Collection) SearchStream(query []float32, fn SearchFunc, opts ...SearchOption) error
- func (c *Collection) SearchText(text string, opts ...SearchOption) ([]Result, error)
- func (c *Collection) Stats() CollectionStats
- func (c *Collection) Subscribe(query []float32, opts ...SubscriptionOption) (*Subscription, error)
- func (c *Collection) TextSearch(query string, opts ...SearchOption) ([]Result, error)
- func (c *Collection) UnarchiveRecord(id uint64) error
- func (c *Collection) Unsubscribe(subscriptionID string) error
- func (c *Collection) Update(id uint64, payload map[string]any) error
- func (c *Collection) UpdateVector(id uint64, vector []float32) error
- func (c *Collection) Upsert(id uint64, vector []float32, payload map[string]any) (uint64, error)
- func (c *Collection) UpsertByKey(keyField string, keyValue any, vector []float32, payload map[string]any) (uint64, bool, error)
- type CollectionOption
- func WithDimension(dim int) CollectionOption
- func WithDistanceType(t floats.DistanceType) CollectionOption
- func WithEmbedder(e Embedder) CollectionOption
- func WithHNSW(m, efConstruction int) CollectionOption
- func WithHNSWConfig(config HNSWConfig) CollectionOption
- func WithTextIndex(fields ...string) CollectionOption
- type CollectionSnapshot
- type CollectionStats
- type ConsolidationConfig
- type ConsolidationResult
- type ConversationTurn
- type DB
- func (db *DB) Close() error
- func (db *DB) Collection(name string) *Collection
- func (db *DB) Collections() []string
- func (db *DB) CreateCollection(name string, opts ...CollectionOption) (*Collection, error)
- func (db *DB) CreateEpisodeStore(memoriesCollectionName string) (*EpisodeStore, error)
- func (db *DB) CreateKnowledgeGraph(name string) (*KnowledgeGraph, error)
- func (db *DB) DropCollection(name string) error
- func (db *DB) GetCollection(name string) (*Collection, error)
- func (db *DB) HasCollection(name string) bool
- func (db *DB) IsClosed() bool
- func (db *DB) Metrics() MetricsSnapshot
- func (db *DB) Path() string
- func (db *DB) Stats() DatabaseStats
- func (db *DB) Sync() error
- type DatabaseSnapshot
- type DatabaseStats
- type DecayConfig
- type DecayType
- type DimensionError
- type DistanceType
- type Embedder
- type Entity
- type Episode
- type EpisodeConfig
- type EpisodeResult
- type EpisodeStore
- func (es *EpisodeStore) CreateEpisode(recordIDs []uint64, title string) (*Episode, error)
- func (es *EpisodeStore) DeleteEpisode(episodeID string) error
- func (es *EpisodeStore) DetectEpisodes(config EpisodeConfig) ([]*Episode, error)
- func (es *EpisodeStore) ExpandEpisode(episodeID string) ([]*Record, error)
- func (es *EpisodeStore) FindRecordEpisode(recordID uint64) (*Episode, error)
- func (es *EpisodeStore) GetEpisode(episodeID string) (*Episode, error)
- func (es *EpisodeStore) ListEpisodes() []*Episode
- func (es *EpisodeStore) SearchEpisodes(query []float32, limit int) ([]*Episode, error)
- func (es *EpisodeStore) SearchWithEpisodeExpansion(query []float32, opts ...SearchOption) ([]EpisodeResult, error)
- type ExpandedSearchResult
- type FileStorage
- func (f *FileStorage) Close() error
- func (f *FileStorage) Delete() error
- func (f *FileStorage) Exists() bool
- func (f *FileStorage) Load() (*DatabaseSnapshot, error)
- func (f *FileStorage) Lock() error
- func (f *FileStorage) Path() string
- func (f *FileStorage) Save(snapshot *DatabaseSnapshot) error
- func (f *FileStorage) Unlock() error
- type Filter
- func AccessCountAbove(n uint64) Filter
- func AccessCountBelow(n uint64) Filter
- func AccessedAfter(t time.Time) Filter
- func AccessedBefore(t time.Time) Filter
- func AgeNewerThan(d time.Duration) Filter
- func AgeOlderThan(d time.Duration) Filter
- func And(filters ...Filter) Filter
- func Between(key string, min, max float64) Filter
- func Contains(key, substr string) Filter
- func CreatedAfter(t time.Time) Filter
- func CreatedBefore(t time.Time) Filter
- func Equal(key string, value any) Filter
- func Exists(key string) Filter
- func ExpiredBefore(t time.Time) Filter
- func GT(key string, value float64) Filter
- func GTE(key string, value float64) Filter
- func Glob(key, pattern string) Filter
- func GreaterThan(key string, value float64) Filter
- func GreaterThanOrEqual(key string, value float64) Filter
- func HasTTLFilter() Filter
- func ImportanceAbove(threshold float32) Filter
- func ImportanceBelow(threshold float32) Filter
- func ImportanceBetween(min, max float32) Filter
- func In(key string, values ...any) Filter
- func LT(key string, value float64) Filter
- func LTE(key string, value float64) Filter
- func LessThan(key string, value float64) Filter
- func LessThanOrEqual(key string, value float64) Filter
- func NeverAccessed() Filter
- func Not(filter Filter) Filter
- func NotEqual(key string, value any) Filter
- func NotExpired() Filter
- func NotIn(key string, values ...any) Filter
- func Or(filters ...Filter) Filter
- func Prefix(key, prefix string) Filter
- func Suffix(key, suffix string) Filter
- func UpdatedAfter(t time.Time) Filter
- func UpdatedBefore(t time.Time) Filter
- type FilterFunc
- type HNSWConfig
- type HNSWIndex
- func (h *HNSWIndex) Count() int
- func (h *HNSWIndex) Delete(id uint64) error
- func (h *HNSWIndex) HardDelete(id uint64) error
- func (h *HNSWIndex) Insert(id uint64, vector []float32) error
- func (h *HNSWIndex) Internal() *hnsw.Index
- func (h *HNSWIndex) Search(query []float32, k int) ([]IndexResult, error)
- func (h *HNSWIndex) SearchWithEf(query []float32, k int, ef int) ([]IndexResult, error)
- func (h *HNSWIndex) SetInternal(idx *hnsw.Index)
- func (h *HNSWIndex) Stats() hnsw.IndexStats
- func (h *HNSWIndex) Type() string
- type Index
- type IndexResult
- type IndexType
- type InsertOption
- type InvertedIndexSnapshot
- type IterOption
- type Iterator
- type KnowledgeGraph
- func (kg *KnowledgeGraph) AddEntity(entity Entity) error
- func (kg *KnowledgeGraph) AddRelationship(rel Relationship) error
- func (kg *KnowledgeGraph) DeleteEntity(entityID string) error
- func (kg *KnowledgeGraph) DeleteRelationship(relID string) error
- func (kg *KnowledgeGraph) GetEntity(entityID string) (*Entity, error)
- func (kg *KnowledgeGraph) GetRelationship(relID string) (*Relationship, error)
- func (kg *KnowledgeGraph) GetRelationships(entityID string, direction string) []*Relationship
- func (kg *KnowledgeGraph) ListEntities(entityType string) []*Entity
- func (kg *KnowledgeGraph) Name() string
- func (kg *KnowledgeGraph) SearchWithExpansion(query []float32, traversalConfig TraversalConfig, opts ...SearchOption) ([]ExpandedSearchResult, error)
- func (kg *KnowledgeGraph) Stats() KnowledgeGraphStats
- func (kg *KnowledgeGraph) Traverse(startIDs []string, config TraversalConfig) (*TraversalResult, error)
- func (kg *KnowledgeGraph) UpdateEntity(entity Entity) error
- type KnowledgeGraphStats
- type Logger
- type MatchEvent
- type MemoryCluster
- type MemoryStorage
- type Metrics
- type MetricsSnapshot
- type NopLogger
- type NotFoundError
- type Option
- type Record
- type RecordSnapshot
- type Relationship
- type Result
- type SearchExplanation
- type SearchFunc
- type SearchOption
- func Threshold(t float32) SearchOption
- func TopK(k int) SearchOption
- func WithAccessTracking(enabled bool) SearchOption
- func WithContent(include bool) SearchOption
- func WithDecay(decayType DecayType, halfLife time.Duration) SearchOption
- func WithEfSearch(ef int) SearchOption
- func WithFilter(f Filter) SearchOption
- func WithFilters(filters ...Filter) SearchOption
- func WithImportanceBoost(factor float32) SearchOption
- func WithLimit(n int) SearchOption
- func WithOffset(n int) SearchOption
- func WithTextWeight(w float64) SearchOption
- func WithVectorWeight(w float64) SearchOption
- type SessionStats
- type Storage
- type StorageError
- type Subscription
- type SubscriptionOption
- type TimeRange
- type TraversalConfig
- type TraversalResult
Constants ¶
const ( // PayloadKeyArchived indicates if a record has been archived. PayloadKeyArchived = "_archived" // PayloadKeyConsolidationGroup is the ID of the consolidation group. PayloadKeyConsolidationGroup = "_consolidation_group" // PayloadKeyConsolidatedFrom contains IDs of records this was consolidated from. PayloadKeyConsolidatedFrom = "_consolidated_from" // PayloadKeyIsConsolidation indicates this record is a consolidation of others. PayloadKeyIsConsolidation = "_is_consolidation" )
Reserved payload keys for memory consolidation.
const ( // PayloadKeySessionID identifies which session/conversation a record belongs to. PayloadKeySessionID = "_session_id" // PayloadKeyTurnNumber is the sequential turn number within a session. PayloadKeyTurnNumber = "_turn_number" // PayloadKeyRole indicates the role (e.g., "user", "assistant", "system"). PayloadKeyRole = "_role" // PayloadKeyParentChunk links to the parent chunk ID for threaded conversations. PayloadKeyParentChunk = "_parent_chunk" // PayloadKeyChildChunks contains IDs of child chunks. PayloadKeyChildChunks = "_child_chunks" // PayloadKeyThreadRoot is the ID of the root chunk in a thread. PayloadKeyThreadRoot = "_thread_root" )
Reserved payload keys for conversation tracking.
const ( // DistanceCosine uses cosine similarity (higher = more similar). DistanceCosine = floats.DistanceCosine // DistanceDot uses dot product (higher = more similar). DistanceDot = floats.DistanceDot // DistanceEuclidean uses Euclidean distance (lower = more similar). DistanceEuclidean = floats.DistanceEuclidean )
const Version = "0.2.0"
Version is the library version.
Variables ¶
var ( // ErrNotFound is returned when a record or collection is not found. ErrNotFound = errors.New("veclite: not found") // ErrDimensionMismatch is returned when vector dimensions don't match. ErrDimensionMismatch = errors.New("veclite: dimension mismatch") // ErrEmptyVector is returned when an empty vector is provided. ErrEmptyVector = errors.New("veclite: empty vector") // ErrCollectionExists is returned when trying to create a collection that already exists. ErrCollectionExists = errors.New("veclite: collection already exists") // ErrDatabaseClosed is returned when operations are attempted on a closed database. ErrDatabaseClosed = errors.New("veclite: database closed") // ErrInvalidPath is returned when an invalid file path is provided. ErrInvalidPath = errors.New("veclite: invalid path") // ErrCorruptedFile is returned when the database file is corrupted. ErrCorruptedFile = errors.New("veclite: corrupted file") // ErrInvalidVersion is returned when the file version is not supported. ErrInvalidVersion = errors.New("veclite: unsupported file version") // ErrBatchSizeMismatch is returned when batch operation input sizes don't match. ErrBatchSizeMismatch = errors.New("veclite: batch size mismatch") // ErrReadOnly is returned when a write operation is attempted on a read-only database. ErrReadOnly = errors.New("veclite: database is read-only") )
Sentinel errors for common conditions.
var ErrChecksumMismatch = errors.New("veclite: checksum mismatch")
ErrChecksumMismatch is returned when the file checksum does not match.
var ErrFileLocked = errors.New("veclite: database file is locked by another process")
ErrFileLocked is returned when the database file is already locked by another process.
var ErrNoEmbedder = errors.New("veclite: no embedder configured")
ErrNoEmbedder is returned when an embedding operation is attempted without an embedder configured on the collection.
Functions ¶
This section is empty.
Types ¶
type Collection ¶
type Collection struct {
// contains filtered or unexported fields
}
Collection represents a collection of vectors with the same dimension.
func (*Collection) All ¶
func (c *Collection) All() []*Record
All returns all records in the collection.
func (*Collection) ArchiveRecord ¶ added in v0.8.0
func (c *Collection) ArchiveRecord(id uint64) error
ArchiveRecord marks a record as archived. Archived records are excluded from normal searches but can be retrieved with GetArchived.
func (*Collection) CleanupExpired ¶ added in v0.8.0
func (c *Collection) CleanupExpired() (int, error)
CleanupExpired removes all expired records from the collection. Returns the number of records removed.
func (*Collection) Clear ¶
func (c *Collection) Clear() error
Clear removes all records from the collection. Returns an error if the database is read-only.
func (*Collection) Consolidate ¶ added in v0.8.0
func (c *Collection) Consolidate(config ConsolidationConfig) (*ConsolidationResult, error)
Consolidate finds similar memory clusters and optionally creates consolidated records.
func (*Collection) Count ¶
func (c *Collection) Count() int
Count returns the number of records in the collection.
func (*Collection) CountExpired ¶ added in v0.8.0
func (c *Collection) CountExpired() int
CountExpired returns the number of expired records in the collection.
func (*Collection) Delete ¶
func (c *Collection) Delete(id uint64) error
Delete removes a record by ID.
func (*Collection) DeleteWhere ¶
func (c *Collection) DeleteWhere(filters ...Filter) (int, error)
DeleteWhere removes all records matching the filters. Returns the number of deleted records.
func (*Collection) Dimension ¶
func (c *Collection) Dimension() int
Dimension returns the vector dimension. Returns 0 if no vectors have been inserted yet.
func (*Collection) DistanceType ¶
func (c *Collection) DistanceType() floats.DistanceType
DistanceType returns the distance metric type.
func (*Collection) ExpandConsolidation ¶ added in v0.8.0
func (c *Collection) ExpandConsolidation(consolidationID uint64) ([]*Record, error)
ExpandConsolidation retrieves the original records that were consolidated into a consolidation record.
func (*Collection) Find ¶
func (c *Collection) Find(filters ...Filter) ([]*Record, error)
Find retrieves all records matching the filters.
func (*Collection) FindOne ¶
func (c *Collection) FindOne(filters ...Filter) (*Record, error)
FindOne retrieves the first record matching the filters.
func (*Collection) FindSimilarClusters ¶ added in v0.8.0
func (c *Collection) FindSimilarClusters(config ConsolidationConfig) ([]MemoryCluster, error)
FindSimilarClusters identifies clusters of similar memories using single-linkage clustering.
func (*Collection) ForEach ¶ added in v0.6.0
func (c *Collection) ForEach(fn func(*Record) bool)
ForEach iterates over all records in the collection, calling fn for each. If fn returns false, iteration stops early. Records are cloned before being passed to fn.
func (*Collection) Get ¶
func (c *Collection) Get(id uint64) (*Record, error)
Get retrieves a record by ID.
func (*Collection) GetArchived ¶ added in v0.8.0
func (c *Collection) GetArchived() ([]*Record, error)
GetArchived retrieves all archived records.
func (*Collection) GetConsolidations ¶ added in v0.8.0
func (c *Collection) GetConsolidations() ([]*Record, error)
GetConsolidations retrieves all consolidation records.
func (*Collection) GetSession ¶ added in v0.8.0
func (c *Collection) GetSession(sessionID string) ([]*Record, error)
GetSession retrieves all records belonging to a session. Returns records in turn number order.
func (*Collection) GetSessionStats ¶ added in v0.8.0
func (c *Collection) GetSessionStats(sessionID string) (SessionStats, error)
GetSessionStats returns statistics about a session.
func (*Collection) GetThread ¶ added in v0.8.0
func (c *Collection) GetThread(chunkID uint64) ([]*Record, error)
GetThread retrieves all records in a thread starting from the given chunk ID. Returns records in chronological order.
func (*Collection) GetVector ¶
func (c *Collection) GetVector(id uint64) ([]float32, error)
GetVector retrieves just the vector for a record.
func (*Collection) HasIndex ¶ added in v0.2.0
func (c *Collection) HasIndex() bool
HasIndex returns true if this collection has an index.
func (*Collection) HybridSearch ¶ added in v0.6.0
func (c *Collection) HybridSearch(query []float32, text string, opts ...SearchOption) ([]Result, error)
HybridSearch performs both vector search and BM25 text search, then fuses results using Reciprocal Rank Fusion (RRF) with k=60. Requires text indexing to be enabled via WithTextIndex. Use WithVectorWeight and WithTextWeight to control the balance.
func (*Collection) IndexStats ¶ added in v0.2.0
func (c *Collection) IndexStats() *hnsw.IndexStats
IndexStats returns statistics about the collection's index. Returns nil if no index is configured.
func (*Collection) IndexType ¶ added in v0.2.0
func (c *Collection) IndexType() IndexType
IndexType returns the index type for this collection.
func (*Collection) Insert ¶
Insert adds a vector with optional payload to the collection. Returns the assigned record ID.
func (*Collection) InsertBatch ¶
InsertBatch adds multiple vectors with payloads to the collection. Returns the assigned record IDs. If payloads is nil or shorter than vectors, missing payloads are treated as nil.
func (*Collection) InsertDocument ¶ added in v0.6.0
func (c *Collection) InsertDocument(vector []float32, content string, payload map[string]any) (uint64, error)
InsertDocument inserts a vector with content text and payload. Content is automatically indexed for BM25 text search when text indexing is enabled.
func (*Collection) InsertText ¶ added in v0.6.0
InsertText embeds the text using the configured embedder and inserts the result. Requires an embedder to be set via WithEmbedder.
func (*Collection) InsertTurn ¶ added in v0.8.0
func (c *Collection) InsertTurn(turn ConversationTurn) (uint64, error)
InsertTurn inserts a conversation turn with conversation metadata. Returns the record ID.
func (*Collection) InsertWithOptions ¶ added in v0.8.0
func (c *Collection) InsertWithOptions(vector []float32, payload map[string]any, opts ...InsertOption) (uint64, error)
InsertWithOptions adds a vector with optional payload and insert options. Use this method to set TTL, importance, and other options.
func (*Collection) Iterate ¶ added in v0.6.0
func (c *Collection) Iterate(opts ...IterOption) *Iterator
Iterate returns an iterator over collection records. Options can control offset and limit for pagination.
func (*Collection) ListSessions ¶ added in v0.8.0
func (c *Collection) ListSessions() []string
ListSessions returns all unique session IDs in the collection.
func (*Collection) Search ¶
func (c *Collection) Search(query []float32, opts ...SearchOption) ([]Result, error)
Search finds the most similar vectors to the query vector.
func (*Collection) SearchExplain ¶ added in v0.2.0
func (c *Collection) SearchExplain(query []float32, opts ...SearchOption) (*SearchExplanation, error)
SearchExplain performs a search and returns detailed statistics.
func (*Collection) SearchInSession ¶ added in v0.8.0
func (c *Collection) SearchInSession(sessionID string, query []float32, opts ...SearchOption) ([]Result, error)
SearchInSession searches for similar vectors within a specific session.
func (*Collection) SearchStream ¶ added in v0.6.0
func (c *Collection) SearchStream(query []float32, fn SearchFunc, opts ...SearchOption) error
SearchStream performs a search and streams results to the callback function. The callback receives results one at a time and can return false to stop early.
func (*Collection) SearchText ¶ added in v0.6.0
func (c *Collection) SearchText(text string, opts ...SearchOption) ([]Result, error)
SearchText embeds the text query using the configured embedder and searches. Requires an embedder to be set via WithEmbedder.
func (*Collection) Stats ¶
func (c *Collection) Stats() CollectionStats
Stats returns statistics about the collection.
func (*Collection) Subscribe ¶ added in v0.8.0
func (c *Collection) Subscribe(query []float32, opts ...SubscriptionOption) (*Subscription, error)
Subscribe creates a subscription that receives events when new records match the query. Returns the subscription which can be used to receive events and close the subscription.
func (*Collection) TextSearch ¶ added in v0.6.0
func (c *Collection) TextSearch(query string, opts ...SearchOption) ([]Result, error)
TextSearch performs BM25 full-text search over indexed fields. Requires text indexing to be enabled via WithTextIndex.
func (*Collection) UnarchiveRecord ¶ added in v0.8.0
func (c *Collection) UnarchiveRecord(id uint64) error
UnarchiveRecord removes the archived flag from a record.
func (*Collection) Unsubscribe ¶ added in v0.8.0
func (c *Collection) Unsubscribe(subscriptionID string) error
Unsubscribe removes a subscription by ID.
func (*Collection) Update ¶
func (c *Collection) Update(id uint64, payload map[string]any) error
Update updates the payload for a record.
func (*Collection) UpdateVector ¶ added in v0.4.0
func (c *Collection) UpdateVector(id uint64, vector []float32) error
UpdateVector updates the vector for a record.
func (*Collection) Upsert ¶ added in v0.4.0
Upsert inserts a new record or updates an existing one by ID. If the ID is 0, a new record is created with an auto-generated ID. If the ID exists, the vector and payload are updated. If the ID doesn't exist, a new record is created with that ID. Returns the record ID (either the provided one or newly generated).
func (*Collection) UpsertByKey ¶ added in v0.4.0
func (c *Collection) UpsertByKey(keyField string, keyValue any, vector []float32, payload map[string]any) (uint64, bool, error)
UpsertByKey inserts a new record or updates an existing one based on a key field. If a record with payload[keyField] == keyValue exists, it is updated. Otherwise, a new record is inserted. Returns the record ID and whether it was an insert (true) or update (false).
type CollectionOption ¶
type CollectionOption interface {
// contains filtered or unexported methods
}
CollectionOption configures a collection.
func WithDimension ¶
func WithDimension(dim int) CollectionOption
WithDimension sets the vector dimension for the collection. If set, all vectors must match this dimension. If not set (0), the dimension is determined by the first insert.
func WithDistanceType ¶
func WithDistanceType(t floats.DistanceType) CollectionOption
WithDistanceType sets the distance metric for the collection. Default is cosine similarity.
func WithEmbedder ¶ added in v0.6.0
func WithEmbedder(e Embedder) CollectionOption
WithEmbedder sets an auto-embedding plugin for the collection. When set, InsertText and SearchText methods become available.
func WithHNSW ¶ added in v0.2.0
func WithHNSW(m, efConstruction int) CollectionOption
WithHNSW enables HNSW indexing for the collection. m is the maximum number of connections per node (default: 16, recommended: 12-48). efConstruction is the candidate list size during construction (default: 200).
func WithHNSWConfig ¶ added in v0.2.0
func WithHNSWConfig(config HNSWConfig) CollectionOption
WithHNSWConfig enables HNSW indexing with custom configuration.
func WithTextIndex ¶ added in v0.6.0
func WithTextIndex(fields ...string) CollectionOption
WithTextIndex enables BM25 full-text indexing on the specified payload fields. When enabled, string values in these fields are tokenized and indexed for text search. The Content field of records is always indexed when text indexing is enabled.
type CollectionSnapshot ¶
type CollectionSnapshot struct {
// Name is the collection name.
Name string
// Dimension is the vector dimension.
Dimension int
// DistanceType is the distance metric.
DistanceType floats.DistanceType
// NextID is the next record ID to assign.
NextID uint64
// Records contains all records in the collection.
Records []*RecordSnapshot
// CreatedAt is when the collection was created.
CreatedAt time.Time
// UpdatedAt is when the collection was last modified.
UpdatedAt time.Time
// IndexType is the type of index (none, hnsw).
IndexType IndexType
// HNSWConfig holds the HNSW configuration (if IndexType is hnsw).
HNSWConfig *HNSWConfig
// HNSWSnapshot holds the HNSW index state (if IndexType is hnsw).
HNSWSnapshot *hnsw.Snapshot
// TextIndexSnapshot holds the BM25 text index state (if text indexing is enabled).
TextIndexSnapshot *InvertedIndexSnapshot
}
CollectionSnapshot is the serializable state of a collection.
func NewCollectionSnapshot ¶
func NewCollectionSnapshot(name string, dimension int, distanceType floats.DistanceType) *CollectionSnapshot
NewCollectionSnapshot creates a new empty collection snapshot.
type CollectionStats ¶
type CollectionStats struct {
// Name is the collection name.
Name string
// Count is the number of records in the collection.
Count int
// Dimension is the vector dimension (0 if not yet set).
Dimension int
// DistanceType is the distance metric used.
DistanceType string
// IndexType is the index type (none, hnsw).
IndexType string
}
CollectionStats contains statistics about a collection.
type ConsolidationConfig ¶ added in v0.8.0
type ConsolidationConfig struct {
// SimilarityThreshold is the minimum similarity for records to be grouped (0.0-1.0).
// Higher values mean stricter grouping.
SimilarityThreshold float32
// MinGroupSize is the minimum number of records to form a cluster.
MinGroupSize int
// MaxGroupSize is the maximum number of records in a cluster.
MaxGroupSize int
// SummaryGenerator is a function that creates a summary from a group of records.
// It returns the summary text, additional payload, and any error.
// If nil, no consolidation records are created (only clustering is performed).
SummaryGenerator func([]*Record) (string, map[string]any, error)
// Embedder is used to generate embeddings for consolidated summaries.
// Required if SummaryGenerator is provided.
Embedder Embedder
// ArchiveOriginals determines whether to archive original records after consolidation.
ArchiveOriginals bool
// Filters can be used to limit which records are considered for consolidation.
Filters []Filter
}
ConsolidationConfig configures memory consolidation.
type ConsolidationResult ¶ added in v0.8.0
type ConsolidationResult struct {
// ClustersFound is the number of clusters identified.
ClustersFound int
// RecordsConsolidated is the total number of records that were consolidated.
RecordsConsolidated int
// ConsolidatedRecordIDs are the IDs of newly created consolidation records.
ConsolidatedRecordIDs []uint64
// ArchivedRecordIDs are the IDs of records that were archived.
ArchivedRecordIDs []uint64
// Clusters contains details about each cluster found.
Clusters []MemoryCluster
}
ConsolidationResult contains the results of a consolidation operation.
type ConversationTurn ¶ added in v0.8.0
type ConversationTurn struct {
// SessionID identifies the conversation session.
SessionID string
// TurnNumber is the sequential turn number (1-indexed).
TurnNumber int
// Role is the speaker role (e.g., "user", "assistant").
Role string
// Content is the text content of the turn.
Content string
// Vector is the embedding vector. If nil, the collection's embedder is used.
Vector []float32
// ParentChunkID links to a parent chunk for threaded conversations.
ParentChunkID uint64
// Payload contains additional metadata.
Payload map[string]any
// Importance is the importance score (0.0-1.0).
Importance float32
// TTL is the time-to-live for this turn.
TTL time.Duration
}
ConversationTurn represents a single turn in a conversation.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB represents a VecLite database.
func Open ¶
Open opens or creates a VecLite database at the given path. Use ":memory:" for an in-memory database that won't be persisted.
func (*DB) Collection ¶
func (db *DB) Collection(name string) *Collection
Collection returns a collection by name, creating it if it doesn't exist. This is the preferred way to get collections for most use cases. In read-only mode, returns nil if the collection doesn't exist (cannot create).
func (*DB) Collections ¶
Collections returns the names of all collections.
func (*DB) CreateCollection ¶
func (db *DB) CreateCollection(name string, opts ...CollectionOption) (*Collection, error)
CreateCollection creates a new collection with the given options. Returns an error if the collection already exists.
func (*DB) CreateEpisodeStore ¶ added in v0.8.0
func (db *DB) CreateEpisodeStore(memoriesCollectionName string) (*EpisodeStore, error)
CreateEpisodeStore creates a new episode store for a collection.
func (*DB) CreateKnowledgeGraph ¶ added in v0.8.0
func (db *DB) CreateKnowledgeGraph(name string) (*KnowledgeGraph, error)
CreateKnowledgeGraph creates a new knowledge graph.
func (*DB) DropCollection ¶
DropCollection removes a collection and all its data.
func (*DB) GetCollection ¶
func (db *DB) GetCollection(name string) (*Collection, error)
GetCollection returns an existing collection or ErrNotFound.
func (*DB) HasCollection ¶
HasCollection returns true if a collection exists.
func (*DB) Metrics ¶ added in v0.6.0
func (db *DB) Metrics() MetricsSnapshot
Metrics returns the current metrics snapshot.
type DatabaseSnapshot ¶
type DatabaseSnapshot struct {
// Version is the file format version.
Version uint32
// Collections maps collection names to their snapshots.
Collections map[string]*CollectionSnapshot
// CreatedAt is when the database was created.
CreatedAt time.Time
// UpdatedAt is when the database was last modified.
UpdatedAt time.Time
}
DatabaseSnapshot is the serializable state of the database.
func NewDatabaseSnapshot ¶
func NewDatabaseSnapshot() *DatabaseSnapshot
NewDatabaseSnapshot creates a new empty database snapshot.
type DatabaseStats ¶
type DatabaseStats struct {
// Path is the database file path (":memory:" for in-memory).
Path string
// Collections is the number of collections.
Collections int
// TotalRecords is the total number of records across all collections.
TotalRecords int
// CollectionStats contains stats for each collection.
CollectionStats []CollectionStats
}
DatabaseStats contains statistics about the database.
type DecayConfig ¶ added in v0.8.0
type DecayConfig struct {
// Type is the decay function type.
Type DecayType
// HalfLife is the time after which the score is halved (for exponential decay).
// For linear decay, this is the maximum age after which the score is zero.
// For gaussian decay, this is the sigma parameter.
HalfLife time.Duration
}
DecayConfig holds the configuration for temporal decay.
type DecayType ¶ added in v0.8.0
type DecayType string
DecayType specifies the type of temporal decay function to apply.
const ( // DecayNone applies no temporal decay. DecayNone DecayType = "none" // DecayExponential applies exponential decay: score * 2^(-age/halfLife) DecayExponential DecayType = "exponential" // DecayLinear applies linear decay: score * max(0, 1 - age/maxAge) DecayLinear DecayType = "linear" // DecayGaussian applies Gaussian decay: score * exp(-0.5 * (age/sigma)^2) DecayGaussian DecayType = "gaussian" )
type DimensionError ¶
DimensionError provides details about dimension mismatches.
func (*DimensionError) Error ¶
func (e *DimensionError) Error() string
func (*DimensionError) Unwrap ¶
func (e *DimensionError) Unwrap() error
type DistanceType ¶ added in v0.2.0
type DistanceType = floats.DistanceType
Re-export distance types for external use.
type Embedder ¶ added in v0.6.0
type Embedder interface {
// Embed converts a single text into a vector embedding.
Embed(text string) ([]float32, error)
// EmbedBatch converts multiple texts into vector embeddings.
EmbedBatch(texts []string) ([][]float32, error)
// Dimension returns the output vector dimension.
Dimension() int
}
Embedder is the interface for auto-embedding text to vectors. Implementations live in separate modules to maintain zero-dependency core.
type Entity ¶ added in v0.8.0
type Entity struct {
// ID is the unique identifier for this entity.
ID string
// Type categorizes the entity (e.g., "person", "company", "concept").
Type string
// Name is the human-readable name of the entity.
Name string
// Vector is the embedding that represents this entity.
Vector []float32
// Properties contains additional entity attributes.
Properties map[string]any
}
Entity represents a node in the knowledge graph.
type Episode ¶ added in v0.8.0
type Episode struct {
// ID is the unique identifier for this episode.
ID string
// Title is a human-readable summary of the episode.
Title string
// Vector is the embedding that represents the episode (centroid of contained records).
Vector []float32
// TimeRange is the span from the first to last record in the episode.
TimeRange TimeRange
// RecordIDs are the IDs of records that belong to this episode.
RecordIDs []uint64
// CreatedAt is when the episode was created.
CreatedAt time.Time
// Metadata contains additional episode information.
Metadata map[string]any
}
Episode represents a coherent group of related memories forming a discrete experience.
func (*Episode) RecordCount ¶ added in v0.8.0
RecordCount returns the number of records in the episode.
type EpisodeConfig ¶ added in v0.8.0
type EpisodeConfig struct {
// TimeGapThreshold is the maximum time gap between records in the same episode.
// Records separated by more than this are considered part of different episodes.
TimeGapThreshold time.Duration
// MinRecords is the minimum number of records to form an episode.
MinRecords int
// MaxRecords is the maximum number of records in an episode.
MaxRecords int
// SimilarityThreshold is the minimum similarity for records to be grouped.
// Used when temporal clustering alone isn't sufficient.
SimilarityThreshold float32
// Filters can be used to limit which records are considered for episodes.
Filters []Filter
}
EpisodeConfig configures episode detection.
type EpisodeResult ¶ added in v0.8.0
type EpisodeResult struct {
// Result is the underlying search result.
Result Result
// Episode is the episode containing this result (if any).
Episode *Episode
// EpisodeRecords are other records in the same episode.
EpisodeRecords []*Record
}
EpisodeResult represents a search result that includes episode context.
type EpisodeStore ¶ added in v0.8.0
type EpisodeStore struct {
// contains filtered or unexported fields
}
EpisodeStore manages episodes for a collection.
func (*EpisodeStore) CreateEpisode ¶ added in v0.8.0
func (es *EpisodeStore) CreateEpisode(recordIDs []uint64, title string) (*Episode, error)
CreateEpisode manually creates an episode from a set of record IDs.
func (*EpisodeStore) DeleteEpisode ¶ added in v0.8.0
func (es *EpisodeStore) DeleteEpisode(episodeID string) error
DeleteEpisode removes an episode (does not delete the underlying records).
func (*EpisodeStore) DetectEpisodes ¶ added in v0.8.0
func (es *EpisodeStore) DetectEpisodes(config EpisodeConfig) ([]*Episode, error)
DetectEpisodes automatically detects episodes using temporal and similarity clustering.
func (*EpisodeStore) ExpandEpisode ¶ added in v0.8.0
func (es *EpisodeStore) ExpandEpisode(episodeID string) ([]*Record, error)
ExpandEpisode retrieves all records belonging to an episode.
func (*EpisodeStore) FindRecordEpisode ¶ added in v0.8.0
func (es *EpisodeStore) FindRecordEpisode(recordID uint64) (*Episode, error)
FindRecordEpisode finds the episode containing a specific record (if any).
func (*EpisodeStore) GetEpisode ¶ added in v0.8.0
func (es *EpisodeStore) GetEpisode(episodeID string) (*Episode, error)
GetEpisode retrieves an episode by ID.
func (*EpisodeStore) ListEpisodes ¶ added in v0.8.0
func (es *EpisodeStore) ListEpisodes() []*Episode
ListEpisodes returns all episodes.
func (*EpisodeStore) SearchEpisodes ¶ added in v0.8.0
func (es *EpisodeStore) SearchEpisodes(query []float32, limit int) ([]*Episode, error)
SearchEpisodes searches for episodes by their vector representation.
func (*EpisodeStore) SearchWithEpisodeExpansion ¶ added in v0.8.0
func (es *EpisodeStore) SearchWithEpisodeExpansion(query []float32, opts ...SearchOption) ([]EpisodeResult, error)
SearchWithEpisodeExpansion performs a search and includes episode context for results.
type ExpandedSearchResult ¶ added in v0.8.0
type ExpandedSearchResult struct {
// Entity is the primary search result.
Entity *Entity
// Score is the similarity score.
Score float32
// RelatedEntities are entities connected to the primary result.
RelatedEntities []*Entity
// Relationships are the connections to related entities.
Relationships []*Relationship
}
ExpandedSearchResult contains search results with graph context.
type FileStorage ¶
type FileStorage struct {
// contains filtered or unexported fields
}
FileStorage is a file-based storage implementation. Uses gob encoding with atomic writes for durability. Acquires an exclusive file lock to prevent concurrent access from multiple processes.
func NewFileStorage ¶
func NewFileStorage(path string) *FileStorage
NewFileStorage creates a new file storage for the given path.
func (*FileStorage) Delete ¶
func (f *FileStorage) Delete() error
Delete removes the database file and any backup/lock files.
func (*FileStorage) Exists ¶
func (f *FileStorage) Exists() bool
Exists returns true if the database file exists.
func (*FileStorage) Load ¶
func (f *FileStorage) Load() (*DatabaseSnapshot, error)
Load reads the database from the file. Returns nil, nil if the file doesn't exist yet.
func (*FileStorage) Lock ¶ added in v0.5.0
func (f *FileStorage) Lock() error
Lock acquires an exclusive file lock on a .lock file adjacent to the database. This prevents multiple processes from opening the same database.
func (*FileStorage) Save ¶
func (f *FileStorage) Save(snapshot *DatabaseSnapshot) error
Save writes the database to the file using atomic write pattern. Writes to .tmp file, fsyncs, then renames old to .bak, then renames .tmp to final.
func (*FileStorage) Unlock ¶ added in v0.5.0
func (f *FileStorage) Unlock() error
Unlock releases the file lock.
type Filter ¶
type Filter interface {
// Match returns true if the record matches the filter criteria.
Match(r *Record) bool
}
Filter is an interface for filtering records based on payload values.
func AccessCountAbove ¶ added in v0.8.0
AccessCountAbove creates a filter that matches records accessed more than n times.
func AccessCountBelow ¶ added in v0.8.0
AccessCountBelow creates a filter that matches records accessed fewer than n times.
func AccessedAfter ¶ added in v0.8.0
AccessedAfter creates a filter that matches records last accessed after the given time.
func AccessedBefore ¶ added in v0.8.0
AccessedBefore creates a filter that matches records last accessed before the given time.
func AgeNewerThan ¶ added in v0.8.0
AgeNewerThan creates a filter that matches records created less than d ago.
func AgeOlderThan ¶ added in v0.8.0
AgeOlderThan creates a filter that matches records created more than d ago.
func Between ¶ added in v0.4.0
Between creates a filter that matches records where min <= payload[key] <= max.
func Contains ¶
Contains creates a filter that matches records where payload[key] contains the substring.
func CreatedAfter ¶ added in v0.8.0
CreatedAfter creates a filter that matches records created after the given time.
func CreatedBefore ¶ added in v0.8.0
CreatedBefore creates a filter that matches records created before the given time.
func ExpiredBefore ¶ added in v0.8.0
ExpiredBefore creates a filter that matches records that expired before the given time.
func GreaterThan ¶ added in v0.4.0
GreaterThan creates a filter that matches records where payload[key] > value.
func GreaterThanOrEqual ¶ added in v0.4.0
GreaterThanOrEqual creates a filter that matches records where payload[key] >= value.
func HasTTLFilter ¶ added in v0.8.0
func HasTTLFilter() Filter
HasTTLFilter creates a filter that matches records with a TTL set.
func ImportanceAbove ¶ added in v0.8.0
ImportanceAbove creates a filter that matches records with importance > threshold.
func ImportanceBelow ¶ added in v0.8.0
ImportanceBelow creates a filter that matches records with importance < threshold.
func ImportanceBetween ¶ added in v0.8.0
ImportanceBetween creates a filter that matches records with min <= importance <= max.
func LessThan ¶ added in v0.4.0
LessThan creates a filter that matches records where payload[key] < value.
func LessThanOrEqual ¶ added in v0.4.0
LessThanOrEqual creates a filter that matches records where payload[key] <= value.
func NeverAccessed ¶ added in v0.8.0
func NeverAccessed() Filter
NeverAccessed creates a filter that matches records that have never been accessed via search.
func NotEqual ¶
NotEqual creates a filter that matches records where payload[key] does not equal value.
func NotExpired ¶ added in v0.8.0
func NotExpired() Filter
NotExpired creates a filter that matches records that have not expired. This includes records with no TTL set.
func NotIn ¶
NotIn creates a filter that matches records where payload[key] is not in the given values.
func UpdatedAfter ¶ added in v0.8.0
UpdatedAfter creates a filter that matches records updated after the given time.
func UpdatedBefore ¶ added in v0.8.0
UpdatedBefore creates a filter that matches records updated before the given time.
type FilterFunc ¶
FilterFunc is a function adapter for the Filter interface.
func (FilterFunc) Match ¶
func (f FilterFunc) Match(r *Record) bool
Match implements Filter interface.
type HNSWConfig ¶ added in v0.2.0
type HNSWConfig struct {
// M is the maximum number of connections per node.
M int
// EfConstruction is the size of the candidate list during index construction.
EfConstruction int
// EfSearch is the default size of the candidate list during search.
EfSearch int
}
HNSWConfig holds HNSW index configuration.
type HNSWIndex ¶ added in v0.2.0
type HNSWIndex struct {
// contains filtered or unexported fields
}
HNSWIndex wraps the HNSW index to implement the Index interface.
func NewHNSWIndex ¶ added in v0.2.0
func NewHNSWIndex(dimension int, distanceType floats.DistanceType, m, efConstruction int) *HNSWIndex
NewHNSWIndex creates a new HNSW index with the given parameters.
func NewHNSWIndexWithConfig ¶ added in v0.2.0
func NewHNSWIndexWithConfig(dimension int, distanceType floats.DistanceType, config hnsw.Config) *HNSWIndex
NewHNSWIndexWithConfig creates a new HNSW index with a custom configuration.
func (*HNSWIndex) HardDelete ¶ added in v0.4.0
HardDelete removes a vector completely from the index. This is needed for update operations where we re-insert with the same ID.
func (*HNSWIndex) Internal ¶ added in v0.2.0
Internal returns the underlying HNSW index (for serialization).
func (*HNSWIndex) Search ¶ added in v0.2.0
func (h *HNSWIndex) Search(query []float32, k int) ([]IndexResult, error)
Search finds the k nearest neighbors to the query vector.
func (*HNSWIndex) SearchWithEf ¶ added in v0.2.0
SearchWithEf searches with a custom ef parameter.
func (*HNSWIndex) SetInternal ¶ added in v0.2.0
SetInternal sets the underlying HNSW index (for deserialization).
func (*HNSWIndex) Stats ¶ added in v0.2.0
func (h *HNSWIndex) Stats() hnsw.IndexStats
Stats returns statistics about the index.
type Index ¶ added in v0.2.0
type Index interface {
// Insert adds a vector with the given ID to the index.
Insert(id uint64, vector []float32) error
// Delete removes a vector from the index.
Delete(id uint64) error
// Search finds the k nearest neighbors to the query vector.
// Returns IDs and distances/similarities.
Search(query []float32, k int) ([]IndexResult, error)
// SearchWithEf searches with a custom ef parameter (for HNSW).
// For indexes that don't support ef, this should behave like Search.
SearchWithEf(query []float32, k int, ef int) ([]IndexResult, error)
// Count returns the number of vectors in the index.
Count() int
// Type returns the index type name.
Type() string
}
Index is the interface for vector search indexes. Implementations can provide different algorithms (brute-force, HNSW, etc.).
type IndexResult ¶ added in v0.2.0
IndexResult represents a search result from an index.
type InsertOption ¶ added in v0.8.0
type InsertOption interface {
// contains filtered or unexported methods
}
InsertOption configures record insertion behavior.
func WithContentOption ¶ added in v0.8.0
func WithContentOption(content string) InsertOption
WithContentOption sets the content field for the record. This is an alternative to using InsertDocument.
func WithExpiresAt ¶ added in v0.8.0
func WithExpiresAt(t time.Time) InsertOption
WithExpiresAt sets an explicit expiration time for the record. This takes precedence over WithTTL if both are specified.
func WithImportance ¶ added in v0.8.0
func WithImportance(score float32) InsertOption
WithImportance sets the importance score for the record. Value should be between 0.0 and 1.0. Values outside this range are clamped.
func WithTTL ¶ added in v0.8.0
func WithTTL(d time.Duration) InsertOption
WithTTL sets a time-to-live duration for the record. The record will expire after this duration from the time of insertion.
type InvertedIndexSnapshot ¶ added in v0.6.0
type InvertedIndexSnapshot struct {
Postings map[string][]uint64
DocLengths map[uint64]int
TotalDocLen int64
DocCount int
Fields []string
}
InvertedIndexSnapshot is the serializable state of the inverted index.
type IterOption ¶ added in v0.6.0
type IterOption interface {
// contains filtered or unexported methods
}
IterOption configures the iterator.
func IterLimit ¶ added in v0.6.0
func IterLimit(n int) IterOption
IterLimit sets the maximum number of records to return.
func IterOffset ¶ added in v0.6.0
func IterOffset(n int) IterOption
IterOffset sets the number of records to skip.
type Iterator ¶ added in v0.6.0
type Iterator struct {
// contains filtered or unexported fields
}
Iterator allows iterating over collection records one at a time.
type KnowledgeGraph ¶ added in v0.8.0
type KnowledgeGraph struct {
// contains filtered or unexported fields
}
KnowledgeGraph provides a graph-based knowledge base with vector search.
func (*KnowledgeGraph) AddEntity ¶ added in v0.8.0
func (kg *KnowledgeGraph) AddEntity(entity Entity) error
AddEntity adds an entity to the knowledge graph.
func (*KnowledgeGraph) AddRelationship ¶ added in v0.8.0
func (kg *KnowledgeGraph) AddRelationship(rel Relationship) error
AddRelationship adds a relationship between two entities.
func (*KnowledgeGraph) DeleteEntity ¶ added in v0.8.0
func (kg *KnowledgeGraph) DeleteEntity(entityID string) error
DeleteEntity removes an entity and all its relationships.
func (*KnowledgeGraph) DeleteRelationship ¶ added in v0.8.0
func (kg *KnowledgeGraph) DeleteRelationship(relID string) error
DeleteRelationship removes a relationship.
func (*KnowledgeGraph) GetEntity ¶ added in v0.8.0
func (kg *KnowledgeGraph) GetEntity(entityID string) (*Entity, error)
GetEntity retrieves an entity by ID.
func (*KnowledgeGraph) GetRelationship ¶ added in v0.8.0
func (kg *KnowledgeGraph) GetRelationship(relID string) (*Relationship, error)
GetRelationship retrieves a relationship by ID.
func (*KnowledgeGraph) GetRelationships ¶ added in v0.8.0
func (kg *KnowledgeGraph) GetRelationships(entityID string, direction string) []*Relationship
GetRelationships returns all relationships for an entity.
func (*KnowledgeGraph) ListEntities ¶ added in v0.8.0
func (kg *KnowledgeGraph) ListEntities(entityType string) []*Entity
ListEntities returns all entities, optionally filtered by type.
func (*KnowledgeGraph) Name ¶ added in v0.8.0
func (kg *KnowledgeGraph) Name() string
Name returns the name of the knowledge graph.
func (*KnowledgeGraph) SearchWithExpansion ¶ added in v0.8.0
func (kg *KnowledgeGraph) SearchWithExpansion(query []float32, traversalConfig TraversalConfig, opts ...SearchOption) ([]ExpandedSearchResult, error)
SearchWithExpansion searches for similar entities and expands results with graph context.
func (*KnowledgeGraph) Stats ¶ added in v0.8.0
func (kg *KnowledgeGraph) Stats() KnowledgeGraphStats
Stats returns statistics about the knowledge graph.
func (*KnowledgeGraph) Traverse ¶ added in v0.8.0
func (kg *KnowledgeGraph) Traverse(startIDs []string, config TraversalConfig) (*TraversalResult, error)
Traverse performs a graph traversal starting from the given entity IDs.
func (*KnowledgeGraph) UpdateEntity ¶ added in v0.8.0
func (kg *KnowledgeGraph) UpdateEntity(entity Entity) error
UpdateEntity updates an existing entity.
type KnowledgeGraphStats ¶ added in v0.8.0
type KnowledgeGraphStats struct {
// EntityCount is the total number of entities.
EntityCount int
// RelationshipCount is the total number of relationships.
RelationshipCount int
// EntityTypes maps entity types to counts.
EntityTypes map[string]int
// RelationshipTypes maps relationship types to counts.
RelationshipTypes map[string]int
}
KnowledgeGraphStats contains statistics about a knowledge graph.
type Logger ¶ added in v0.6.0
type Logger interface {
// Debug logs a debug message with key-value pairs.
Debug(msg string, keysAndValues ...any)
// Info logs an informational message with key-value pairs.
Info(msg string, keysAndValues ...any)
// Error logs an error message with key-value pairs.
Error(msg string, keysAndValues ...any)
}
Logger is the interface for structured logging in VecLite. Implementations can bridge to any logging library (slog, zap, zerolog, etc.).
type MatchEvent ¶ added in v0.8.0
type MatchEvent struct {
// Record is the matching record.
Record *Record
// Score is the similarity score to the subscription query.
Score float32
// Timestamp is when the match was detected.
Timestamp time.Time
// SubscriptionID is the ID of the subscription that matched.
SubscriptionID string
}
MatchEvent represents an event when a new record matches a subscription.
type MemoryCluster ¶ added in v0.8.0
type MemoryCluster struct {
// ID is a unique identifier for this cluster.
ID string
// Records are the records in this cluster.
Records []*Record
// Centroid is the average vector of all records in the cluster.
Centroid []float32
// AverageImportance is the average importance score of records.
AverageImportance float32
// TimeRange is the span from oldest to newest record.
TimeRange TimeRange
}
MemoryCluster represents a group of similar memories that could be consolidated.
type MemoryStorage ¶
type MemoryStorage struct {
// contains filtered or unexported fields
}
MemoryStorage is an in-memory storage implementation. Data is not persisted and will be lost when the database is closed.
func NewMemoryStorage ¶
func NewMemoryStorage() *MemoryStorage
NewMemoryStorage creates a new in-memory storage.
func (*MemoryStorage) Close ¶
func (m *MemoryStorage) Close() error
Close is a no-op for memory storage.
func (*MemoryStorage) Load ¶
func (m *MemoryStorage) Load() (*DatabaseSnapshot, error)
Load returns the stored snapshot or nil if none exists.
func (*MemoryStorage) Save ¶
func (m *MemoryStorage) Save(snapshot *DatabaseSnapshot) error
Save stores the snapshot in memory.
type Metrics ¶ added in v0.6.0
type Metrics struct {
// contains filtered or unexported fields
}
Metrics provides observable counters for database operations. All operations are atomic and safe for concurrent access.
func (*Metrics) Snapshot ¶ added in v0.6.0
func (m *Metrics) Snapshot() MetricsSnapshot
Snapshot returns a point-in-time snapshot of the metrics.
type MetricsSnapshot ¶ added in v0.6.0
type MetricsSnapshot struct {
SearchCount int64 `json:"search_count"`
InsertCount int64 `json:"insert_count"`
DeleteCount int64 `json:"delete_count"`
AvgSearchTime time.Duration `json:"avg_search_time_ns"`
}
MetricsSnapshot is a point-in-time snapshot of database metrics.
type NopLogger ¶ added in v0.6.0
type NopLogger struct{}
NopLogger is a no-op logger that discards all messages. This is the default logger used when none is configured, ensuring zero overhead.
type NotFoundError ¶
type NotFoundError struct {
Type string // "record", "collection", etc.
ID string // Identifier that was not found
}
NotFoundError provides details about what was not found.
func (*NotFoundError) Error ¶
func (e *NotFoundError) Error() string
func (*NotFoundError) Unwrap ¶
func (e *NotFoundError) Unwrap() error
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures the database.
func WithLogger ¶ added in v0.6.0
WithLogger sets a logger for the database. Pass nil or NopLogger{} to disable logging (default).
func WithReadOnly ¶
WithReadOnly opens the database in read-only mode. Write operations will return an error.
func WithSyncOnWrite ¶
WithSyncOnWrite enables automatic sync after each write operation. This is slower but ensures durability.
type Record ¶
type Record struct {
// ID is the unique identifier for this record.
ID uint64
// Vector is the embedding vector.
Vector []float32
// Payload contains arbitrary metadata associated with the vector.
Payload map[string]any
// Content is the optional original text content associated with this record.
// Used for document-oriented storage and automatically indexed by BM25 when text indexing is enabled.
Content string
// CreatedAt is when the record was inserted.
CreatedAt time.Time
// UpdatedAt is when the record was last updated.
UpdatedAt time.Time
// ExpiresAt is when this record expires. Zero value means never expires.
ExpiresAt time.Time
// Importance is a score from 0.0 to 1.0 indicating how important this record is.
// Higher values make the record more likely to be returned in search results.
Importance float32
// AccessCount tracks how many times this record has been accessed via search.
AccessCount uint64
// LastAccessedAt is when this record was last accessed via search.
LastAccessedAt time.Time
}
Record represents a stored vector with its metadata.
func (*Record) HasTTL ¶ added in v0.8.0
HasTTL returns true if the record has an expiration time set.
type RecordSnapshot ¶
type RecordSnapshot struct {
// ID is the record's unique identifier.
ID uint64
// Vector is the embedding vector.
Vector []float32
// Payload contains arbitrary metadata.
Payload map[string]any
// Content is the optional text content.
Content string
// CreatedAt is when the record was inserted.
CreatedAt time.Time
// UpdatedAt is when the record was last updated.
UpdatedAt time.Time
// ExpiresAt is when this record expires. Zero value means never expires.
ExpiresAt time.Time
// Importance is a score from 0.0 to 1.0.
Importance float32
// AccessCount tracks how many times this record has been accessed.
AccessCount uint64
// LastAccessedAt is when this record was last accessed.
LastAccessedAt time.Time
}
RecordSnapshot is the serializable state of a record.
type Relationship ¶ added in v0.8.0
type Relationship struct {
// ID is the unique identifier for this relationship.
ID string
// SourceID is the ID of the source entity.
SourceID string
// TargetID is the ID of the target entity.
TargetID string
// Type describes the relationship (e.g., "works_at", "knows", "related_to").
Type string
// Weight is the strength of the relationship (0.0-1.0).
Weight float32
// Properties contains additional relationship attributes.
Properties map[string]any
// Bidirectional indicates if the relationship goes both ways.
Bidirectional bool
}
Relationship represents an edge between two entities in the knowledge graph.
func (*Relationship) Clone ¶ added in v0.8.0
func (r *Relationship) Clone() *Relationship
Clone creates a copy of the relationship.
type Result ¶
type Result struct {
// Record is the matched record.
Record *Record
// Score is the similarity/distance score.
// For cosine/dot: higher is more similar.
// For euclidean: lower is more similar.
Score float32
}
Result represents a search result with its similarity score.
type SearchExplanation ¶ added in v0.2.0
type SearchExplanation struct {
// Results contains the search results.
Results []Result
// IndexType is the type of index used (none, hnsw).
IndexType string
// NodesVisited is the number of nodes visited during search.
// Only populated for HNSW searches.
NodesVisited int
// LayersVisited is the number of HNSW layers visited.
// Only populated for HNSW searches.
LayersVisited int
// Duration is how long the search took.
Duration time.Duration
// BruteForce indicates whether brute-force search was used.
BruteForce bool
}
SearchExplanation provides details about how a search was performed.
type SearchFunc ¶ added in v0.6.0
SearchFunc is a callback for streaming search results. Return false to stop receiving results.
type SearchOption ¶
type SearchOption interface {
// contains filtered or unexported methods
}
SearchOption configures search behavior.
func Threshold ¶
func Threshold(t float32) SearchOption
Threshold sets the minimum similarity score for results. For cosine/dot: results with score >= threshold are returned. For euclidean: results with score <= threshold are returned.
func TopK ¶
func TopK(k int) SearchOption
TopK sets the maximum number of results to return. Default is 10.
func WithAccessTracking ¶ added in v0.8.0
func WithAccessTracking(enabled bool) SearchOption
WithAccessTracking enables access tracking for search results. When enabled, accessing a record via search increments its access count and updates its last accessed timestamp.
func WithContent ¶ added in v0.6.0
func WithContent(include bool) SearchOption
WithContent controls whether the Content field is included in search results. By default, Content is included. Set to false to exclude it for smaller results.
func WithDecay ¶ added in v0.8.0
func WithDecay(decayType DecayType, halfLife time.Duration) SearchOption
WithDecay sets the temporal decay function for search results. The decay is applied based on record age (time since creation).
func WithEfSearch ¶ added in v0.2.0
func WithEfSearch(ef int) SearchOption
WithEfSearch sets the efSearch parameter for HNSW search. Higher values improve recall at the cost of speed. Has no effect on collections without HNSW index.
func WithFilter ¶
func WithFilter(f Filter) SearchOption
WithFilter adds a filter to the search. Multiple filters are combined with AND logic.
func WithFilters ¶
func WithFilters(filters ...Filter) SearchOption
WithFilters adds multiple filters to the search. All filters are combined with AND logic.
func WithImportanceBoost ¶ added in v0.8.0
func WithImportanceBoost(factor float32) SearchOption
WithImportanceBoost sets a boost factor for importance scores. The final score is: baseScore * (1 + importanceBoost * importance) A factor of 1.0 means importance can double the score.
func WithLimit ¶ added in v0.6.0
func WithLimit(n int) SearchOption
WithLimit sets the maximum number of results to return. This is an alias for TopK for use in pagination contexts.
func WithOffset ¶ added in v0.6.0
func WithOffset(n int) SearchOption
WithOffset sets the number of results to skip before returning. Use with TopK for pagination: WithOffset(20), TopK(10) returns results 21-30.
func WithTextWeight ¶ added in v0.6.0
func WithTextWeight(w float64) SearchOption
WithTextWeight sets the weight for the text search component in hybrid search. Default is 1.0.
func WithVectorWeight ¶ added in v0.6.0
func WithVectorWeight(w float64) SearchOption
WithVectorWeight sets the weight for the vector search component in hybrid search. Default is 1.0.
type SessionStats ¶ added in v0.8.0
type SessionStats struct {
// SessionID is the session identifier.
SessionID string
// TurnCount is the number of turns in the session.
TurnCount int
// FirstTurn is the timestamp of the first turn.
FirstTurn time.Time
// LastTurn is the timestamp of the last turn.
LastTurn time.Time
// Roles contains the count of turns by role.
Roles map[string]int
}
SessionStats returns statistics about a session.
type Storage ¶
type Storage interface {
// Load reads the database from storage.
// Returns nil, nil if the database doesn't exist yet.
Load() (*DatabaseSnapshot, error)
// Save writes the database to storage.
Save(snapshot *DatabaseSnapshot) error
// Close releases any resources held by the storage.
Close() error
}
Storage is the interface for database persistence.
type StorageError ¶
StorageError wraps storage-related errors with context.
func (*StorageError) Error ¶
func (e *StorageError) Error() string
func (*StorageError) Unwrap ¶
func (e *StorageError) Unwrap() error
type Subscription ¶ added in v0.8.0
type Subscription struct {
// ID is the unique identifier for this subscription.
ID string
// Query is the embedding vector to match against.
Query []float32
// Threshold is the minimum similarity score for a match.
Threshold float32
// Filters are additional filters to apply.
Filters []Filter
// contains filtered or unexported fields
}
Subscription represents an active subscription for matching records.
func (*Subscription) Close ¶ added in v0.8.0
func (s *Subscription) Close() error
Close closes the subscription and its event channel.
func (*Subscription) Events ¶ added in v0.8.0
func (s *Subscription) Events() <-chan MatchEvent
Events returns the channel for receiving match events.
func (*Subscription) IsClosed ¶ added in v0.8.0
func (s *Subscription) IsClosed() bool
IsClosed returns true if the subscription is closed.
type SubscriptionOption ¶ added in v0.8.0
type SubscriptionOption interface {
// contains filtered or unexported methods
}
SubscriptionOption configures a subscription.
func WithSubscriptionBufferSize ¶ added in v0.8.0
func WithSubscriptionBufferSize(size int) SubscriptionOption
WithSubscriptionBufferSize sets the event channel buffer size.
func WithSubscriptionFilter ¶ added in v0.8.0
func WithSubscriptionFilter(f Filter) SubscriptionOption
WithSubscriptionFilter adds a filter to the subscription.
func WithSubscriptionThreshold ¶ added in v0.8.0
func WithSubscriptionThreshold(threshold float32) SubscriptionOption
WithSubscriptionThreshold sets the minimum similarity threshold for matches.
type TraversalConfig ¶ added in v0.8.0
type TraversalConfig struct {
// MaxDepth is the maximum number of hops from start nodes.
MaxDepth int
// MaxNodes is the maximum number of nodes to visit.
MaxNodes int
// MinWeight is the minimum relationship weight to follow.
MinWeight float32
// RelationshipTypes limits traversal to specific relationship types.
// Empty means all types.
RelationshipTypes []string
// EntityTypes limits traversal to specific entity types.
// Empty means all types.
EntityTypes []string
// Direction controls traversal direction: "outgoing", "incoming", or "both".
Direction string
}
TraversalConfig configures graph traversal behavior.
type TraversalResult ¶ added in v0.8.0
type TraversalResult struct {
// Entities are the entities found during traversal.
Entities []*Entity
// Relationships are the relationships traversed.
Relationships []*Relationship
// Depths maps entity IDs to their depth from start nodes.
Depths map[string]int
}
TraversalResult contains the results of a graph traversal.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
veclite
command
Command veclite provides a CLI for interacting with VecLite databases.
|
Command veclite provides a CLI for interacting with VecLite databases. |
|
examples
|
|
|
basic
command
Example basic demonstrates the core VecLite operations: open a database, insert vectors, search, and close.
|
Example basic demonstrates the core VecLite operations: open a database, insert vectors, search, and close. |
|
batch
command
Example batch demonstrates batch operations, upsert, and iteration.
|
Example batch demonstrates batch operations, upsert, and iteration. |
|
filtering
command
Example filtering demonstrates VecLite's rich filter expressions.
|
Example filtering demonstrates VecLite's rich filter expressions. |
|
hnsw
command
Example hnsw demonstrates HNSW index configuration and performance.
|
Example hnsw demonstrates HNSW index configuration and performance. |
|
http-client
command
Example http-client demonstrates using VecLite's HTTP API.
|
Example http-client demonstrates using VecLite's HTTP API. |
|
integrations
|
|
|
openclaw
Package openclaw provides a high-level agent memory interface built on VecLite.
|
Package openclaw provides a high-level agent memory interface built on VecLite. |
|
internal
|
|
|
floats
Package floats provides optimized floating-point vector operations.
|
Package floats provides optimized floating-point vector operations. |
|
hnsw
Package hnsw implements the Hierarchical Navigable Small World graph algorithm for approximate nearest neighbor search.
|
Package hnsw implements the Hierarchical Navigable Small World graph algorithm for approximate nearest neighbor search. |