vector

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 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 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 ExtractRowID added in v0.4.0

func ExtractRowID(metadata map[string]any) int64

ExtractRowID safely extracts a database row ID from vector result metadata. The metadata key is "sqlite_id" for historical reasons (pgvector inherited the name).

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 WhereFilter) ([]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)

	// 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

	// GetMetrics returns real query instrumentation metrics (count, latency percentiles, doc count).
	GetMetrics(ctx context.Context) VectorMetricsSnapshot

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

Client defines the interface for vector storage operations. pgvector.Client is the production implementation.

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 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
	SQLiteID int64
}

StaleVectorInfo contains information about a vector that needs rebuilding.

type VectorMetricsSnapshot added in v1.1.0

type VectorMetricsSnapshot struct {
	QueryCount   int64   `json:"query_count"`
	AvgLatencyMs float64 `json:"avg_latency_ms"`
	P50LatencyMs float64 `json:"p50_latency_ms"`
	P95LatencyMs float64 `json:"p95_latency_ms"`
	P99LatencyMs float64 `json:"p99_latency_ms"`
	TotalDocs    int64   `json:"total_documents"`
}

VectorMetricsSnapshot contains real query instrumentation data.

type WhereClause added in v0.4.0

type WhereClause struct {
	OrGroup []WhereClause
	Column  string
	Value   any
}

WhereClause is a single filter condition (equality or OR group).

type WhereFilter added in v0.4.0

type WhereFilter struct {
	Clauses []WhereClause
}

WhereFilter represents a filter for vector queries, supporting OR conditions.

func BuildWhereFilter

func BuildWhereFilter(docType DocType, project string, includeGlobal bool) WhereFilter

BuildWhereFilter creates a filter for vector queries. When includeGlobal is true and project is non-empty, results include both project-specific AND global-scoped observations.

Directories

Path Synopsis
Package pgvector provides PostgreSQL+pgvector based vector storage for engram.
Package pgvector provides PostgreSQL+pgvector based vector storage for engram.

Jump to

Keyboard shortcuts

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