vector

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package vector provides common interfaces for vector storage implementations.

Package vector provides common interfaces and types for vector storage implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildWhereFilter

func BuildWhereFilter(docType DocType, project string) map[string]interface{}

BuildWhereFilter creates a where filter map for vector queries.

func CopyMetadata

func CopyMetadata(base map[string]any, key string, value any) map[string]any

CopyMetadata creates a new metadata map with an additional key-value pair.

func CopyMetadataMulti

func CopyMetadataMulti(base map[string]any, extra map[string]any) map[string]any

CopyMetadataMulti creates a new metadata map merging base and extra.

func DistanceToSimilarity

func DistanceToSimilarity(distance float64) float64

DistanceToSimilarity converts cosine distance to similarity score. Cosine distance: 0 = identical, 2 = opposite. Similarity: 1.0 = identical, 0.0 = opposite.

func ExtractObservationIDs

func ExtractObservationIDs(results []QueryResult, project string) []int64

ExtractObservationIDs extracts observation database IDs from query results, optionally filtering by project (including global scope).

func ExtractPromptIDs

func ExtractPromptIDs(results []QueryResult, project string) []int64

ExtractPromptIDs extracts user prompt database IDs from query results.

func ExtractSummaryIDs

func ExtractSummaryIDs(results []QueryResult, project string) []int64

ExtractSummaryIDs extracts session summary database IDs from query results.

func JoinStrings

func JoinStrings(strs []string, sep string) string

JoinStrings joins strings with a separator.

Types

type CacheStatsSnapshot

type CacheStatsSnapshot struct {
	EmbeddingHits   int64 `json:"embedding_hits"`
	EmbeddingMisses int64 `json:"embedding_misses"`
	ResultHits      int64 `json:"result_hits"`
	ResultMisses    int64 `json:"result_misses"`
}

CacheStatsSnapshot is an exported snapshot of cache performance metrics. For backends without a local cache (e.g. pgvector), all counters are zero.

func (CacheStatsSnapshot) HitRate

func (s CacheStatsSnapshot) HitRate() float64

HitRate returns the overall cache hit rate as a percentage (0–100).

type Client

type Client interface {
	// AddDocuments adds documents with their embeddings to the vector store.
	AddDocuments(ctx context.Context, docs []Document) error

	// DeleteDocuments removes documents by their IDs.
	DeleteDocuments(ctx context.Context, ids []string) error

	// Query performs a vector similarity search.
	Query(ctx context.Context, query string, limit int, where map[string]any) ([]QueryResult, error)

	// IsConnected checks if the vector store is available.
	IsConnected() bool

	// Close releases resources.
	Close() error

	// Count returns the total number of vectors in the store.
	Count(ctx context.Context) (int64, error)

	// ModelVersion returns the current embedding model version.
	ModelVersion() string

	// NeedsRebuild checks if vectors need to be rebuilt due to model version change.
	NeedsRebuild(ctx context.Context) (bool, string)

	// GetStaleVectors returns info about vectors with mismatched or null model versions.
	GetStaleVectors(ctx context.Context) ([]StaleVectorInfo, error)

	// DeleteVectorsByDocIDs removes vectors by their doc_ids.
	DeleteVectorsByDocIDs(ctx context.Context, docIDs []string) error

	// GetHealthStats returns comprehensive health statistics about the vector store.
	GetHealthStats(ctx context.Context) (*HealthStats, error)

	// GetCacheStats returns cache performance statistics.
	// Backends without a local cache return a zero-value CacheStatsSnapshot.
	GetCacheStats() CacheStatsSnapshot

	// DeleteByObservationID removes all vectors associated with an observation ID.
	DeleteByObservationID(ctx context.Context, obsID int64) error

	// CacheStats returns basic cache size info for backward compatibility.
	// Deprecated: Use GetCacheStats for comprehensive statistics.
	CacheStats() (size int, maxSize int)
}

Client defines the interface for vector storage operations. Both sqlitevec.Client and hybrid.Client implement this interface.

type DocType

type DocType string

DocType represents the type of document stored in the vector table.

const (
	DocTypeObservation    DocType = "observation"
	DocTypeSessionSummary DocType = "session_summary"
	DocTypeUserPrompt     DocType = "user_prompt"
)

type Document

type Document struct {
	Metadata map[string]any
	ID       string
	Content  string
}

Document represents a document to store with vector embedding.

type ExtractedIDs

type ExtractedIDs struct {
	ObservationIDs []int64
	SummaryIDs     []int64
	PromptIDs      []int64
}

ExtractedIDs contains database IDs extracted from query results, grouped by document type.

func ExtractIDsByDocType

func ExtractIDsByDocType(results []QueryResult) *ExtractedIDs

ExtractIDsByDocType extracts database IDs from query results, grouped by document type.

type HealthStats

type HealthStats struct {
	TotalVectors  int64  `json:"total_vectors"`
	StaleVectors  int64  `json:"stale_vectors"`
	CurrentModel  string `json:"current_model"`
	NeedsRebuild  bool   `json:"needs_rebuild"`
	RebuildReason string `json:"rebuild_reason"`
}

HealthStats contains comprehensive health information about the vector store.

type QueryResult

type QueryResult struct {
	Metadata   map[string]any
	ID         string
	Distance   float64
	Similarity float64
}

QueryResult represents a search result from vector search.

func FilterByThreshold

func FilterByThreshold(results []QueryResult, threshold float64, maxResults int) []QueryResult

FilterByThreshold filters results to only include those above the similarity threshold. If maxResults > 0, also caps the number of results.

type StaleVectorInfo

type StaleVectorInfo struct {
	DocID     string
	DocType   string
	FieldType string
	Project   string
	Scope     string
	SQLiteID  int64
}

StaleVectorInfo contains information about a vector that needs rebuilding.

Directories

Path Synopsis
Package pgvector provides PostgreSQL+pgvector based vector storage for engram.
Package pgvector provides PostgreSQL+pgvector based vector storage for engram.
Package sqlitevec provides sqlite-vec based vector database integration for engram.
Package sqlitevec provides sqlite-vec based vector database integration for engram.

Jump to

Keyboard shortcuts

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