vector

package
v0.0.0-...-071fe3b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func APIEmbeddingFunc

func APIEmbeddingFunc(provider EmbeddingProvider, model string) chromem.EmbeddingFunc

APIEmbeddingFunc returns an EmbeddingFunc that uses a Provider API endpoint. Only enabled when the user explicitly configures an embedding_model.

func CosineSimilarity

func CosineSimilarity(a, b []float32) float64

CosineSimilarity computes cosine similarity between two vectors.

func LocalEmbeddingFunc

func LocalEmbeddingFunc(dim int) chromem.EmbeddingFunc

LocalEmbeddingFunc returns a pure Go character N-gram hash embedding function. Zero token cost, zero external dependencies, fully offline.

Algorithm: tokenize → character trigram extraction → FNV dual-hash mapping → L2 normalization

func NewEmbeddingFunc

func NewEmbeddingFunc(cfg StoreConfig, provider EmbeddingProvider, dim int) chromem.EmbeddingFunc

NewEmbeddingFunc selects the appropriate embedding function based on configuration. It guarantees that the returned EmbeddingFunc is NEVER nil.

Priority:

Tier 1: ONNX plugin (PluginPath exists) → best quality, fully offline
Tier 2: Provider API (APIModel set + provider non-nil) → good quality, costs tokens
Tier 3: Local hash (default) → zero cost, fully offline

Types

type EmbeddingPlugin

type EmbeddingPlugin interface {
	// Init initializes the plugin with the model path and output dimension.
	Init(modelPath string, dim int) error
	// Embed produces an embedding vector for the given text.
	Embed(text string) ([]float32, error)
	// Close releases plugin resources.
	Close()
}

EmbeddingPlugin defines the interface for ONNX embedding plugins loaded via DLL/SO.

func LoadPlugin

func LoadPlugin(path string) (EmbeddingPlugin, error)

LoadPlugin returns an error on non-Windows platforms. Linux/macOS support requires dlopen-based implementation.

type EmbeddingProvider

type EmbeddingProvider interface {
	CreateEmbedding(ctx context.Context, model, text string) ([]float32, error)
}

EmbeddingProvider is an interface for external embedding API providers.

type Entry

type Entry struct {
	ID        string            `json:"id"`
	Type      string            `json:"type"`
	Content   string            `json:"content"`
	Metadata  map[string]string `json:"metadata,omitempty"`
	Tags      []string          `json:"tags,omitempty"`
	Score     float64           `json:"score,omitempty"`
	CreatedAt time.Time         `json:"created_at"`
	UpdatedAt time.Time         `json:"updated_at"`
}

Entry represents a memory entry in the vector store. Mirrors memory.Entry but avoids importing the memory package.

type QueryResult

type QueryResult struct {
	Entries []Entry
	Total   int
	Query   string
}

QueryResult represents the result of a vector search.

type StoreConfig

type StoreConfig struct {
	EmbeddingTier       string
	LocalDim            int
	PluginPath          string
	PluginModelPath     string
	APIModel            string
	MaxResults          int
	SimilarityThreshold float64
	StoragePath         string
}

StoreConfig holds the configuration for creating a VectorStore. This is defined here to avoid an import cycle with the memory package.

type VectorStore

type VectorStore struct {
	// contains filtered or unexported fields
}

VectorStore wraps chromem-go and provides semantic vector search. It uses JSONL for full-entry persistence and chromem-go for similarity search.

func NewVectorStore

func NewVectorStore(cfg StoreConfig, provider EmbeddingProvider) (*VectorStore, error)

NewVectorStore creates a new VectorStore backed by chromem-go. The EmbeddingFunc is always explicitly set (never nil) to prevent chromem-go from calling OpenAI by default.

func (*VectorStore) Close

func (s *VectorStore) Close() error

Close releases resources.

func (*VectorStore) DeleteEntry

func (s *VectorStore) DeleteEntry(ctx context.Context, id string) error

DeleteEntry removes an entry by ID.

func (*VectorStore) GetByID

func (s *VectorStore) GetByID(ctx context.Context, id string) (*Entry, error)

GetByID retrieves a memory entry by ID.

func (*VectorStore) ListEntries

func (s *VectorStore) ListEntries(typeFilter []string, offset, limit int) (*QueryResult, error)

ListEntries returns entries optionally filtered by type, with pagination.

func (*VectorStore) Query

func (s *VectorStore) Query(ctx context.Context, query string, limit int, typeFilter []string) (*QueryResult, error)

Query performs semantic search against the vector store.

func (*VectorStore) StoreEntry

func (s *VectorStore) StoreEntry(ctx context.Context, entry *Entry) error

StoreEntry saves an entry to the vector store and persists it to JSONL.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL