memory

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BlobToVector

func BlobToVector(buf []byte) []float32

BlobToVector converts a binary blob back to float32 slice.

func CosineSimilarity

func CosineSimilarity(a, b []float32) float32

CosineSimilarity computes the cosine similarity between two vectors. Returns a value between -1 and 1, where 1 means identical direction.

func EuclideanDistance

func EuclideanDistance(a, b []float32) float32

EuclideanDistance computes the Euclidean distance between two vectors.

func VectorToBlob

func VectorToBlob(v []float32) []byte

ToBlob converts a float32 slice to binary blob for SQLite storage.

Types

type BySimilarity

type BySimilarity []VectorMatch

BySimiliarity implements sort.Interface for sorting by similarity (descending).

func (BySimilarity) Len

func (a BySimilarity) Len() int

func (BySimilarity) Less

func (a BySimilarity) Less(i, j int) bool

func (BySimilarity) Swap

func (a BySimilarity) Swap(i, j int)

type EmbeddingGenerator

type EmbeddingGenerator interface {
	GenerateEmbedding(ctx context.Context, text string) ([]float32, error)
}

EmbeddingGenerator generates vector embeddings for text content. This interface allows the memory store to request embeddings without depending directly on the AI service.

type Memory

type Memory struct {
	ID           string    `json:"id"`
	Content      string    `json:"content"`
	Importance   float64   `json:"importance"`
	Tags         []string  `json:"tags"`
	CreatedAt    time.Time `json:"created_at"`
	LastAccessed time.Time `json:"last_accessed"`
	AccessCount  int       `json:"access_count"`
}

Memory represents a stored memory

type Store

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

Store handles memory persistence

func NewStore

func NewStore(db *sql.DB) *Store

NewStore creates a new memory store

func (*Store) Create

func (s *Store) Create(ctx context.Context, content string, importance float64, tags []string) (*Memory, error)

Create stores a new memory with optional embedding generation

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, id string) error

Delete removes a memory

func (*Store) Get

func (s *Store) Get(ctx context.Context, id string) (*Memory, error)

Get retrieves a memory by ID

func (*Store) GetTopImportant

func (s *Store) GetTopImportant(ctx context.Context, limit int) ([]*Memory, error)

GetTopImportant returns the most important memories

func (*Store) GetWithoutEmbedding

func (s *Store) GetWithoutEmbedding(ctx context.Context, limit int) ([]*Memory, error)

GetWithoutEmbedding returns memories that don't have embeddings yet. Used for backfilling embeddings.

func (*Store) List

func (s *Store) List(ctx context.Context, limit int) ([]*Memory, error)

List returns the most recent memories

func (*Store) SearchByVector

func (s *Store) SearchByVector(ctx context.Context, embedding []float32, limit int) ([]*Memory, error)

SearchByVector searches using vector similarity (in-memory cosine similarity)

func (*Store) SearchRelevant

func (s *Store) SearchRelevant(ctx context.Context, query string, limit int) ([]string, error)

SearchRelevant searches for relevant memories (simple keyword search)

func (*Store) SetEmbedder

func (s *Store) SetEmbedder(e EmbeddingGenerator)

SetEmbedder sets the embedding generator for the store. When set, embeddings will be generated automatically when creating memories.

func (*Store) UpdateEmbedding

func (s *Store) UpdateEmbedding(ctx context.Context, id string, embedding []float32) error

UpdateEmbedding sets the embedding for an existing memory. Used for backfilling embeddings on memories that were created before embedding support.

type Vector

type Vector []float32

Vector represents a vector embedding for SQLite storage. Stored as binary BLOB (little-endian float32 array) for efficiency.

func (*Vector) Scan

func (v *Vector) Scan(src interface{}) error

Scan implements sql.Scanner for reading vectors from the database.

func (Vector) Value

func (v Vector) Value() (driver.Value, error)

Value implements driver.Valuer for database insertion. Converts the vector to a binary blob.

type VectorMatch

type VectorMatch struct {
	ID         string
	Similarity float32
}

VectorMatch represents a vector search result with similarity score.

Jump to

Keyboard shortcuts

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