pgvector

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package pgvector provides a interfaces.Memory implementation backed by PostgreSQL with pgvector.

Expected table schema (embedding dimension must match EmbedFunc output):

CREATE TABLE agent_memories (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  text TEXT NOT NULL,
  kind TEXT NOT NULL DEFAULT '',
  user_id TEXT,
  tenant_id TEXT,
  agent_id TEXT,
  scope_tags TEXT[] NOT NULL DEFAULT '{}',
  metadata JSONB NOT NULL DEFAULT '{}',
  expires_at TIMESTAMPTZ,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  embedding vector(1536)
);

Callers provide EmbedFunc to vectorize memory text on store and recall queries on load.

Index

Constants

View Source
const (
	DefaultTable        = "agent_memories"
	DefaultTextCol      = "text"
	DefaultEmbeddingCol = "embedding"

	ColID        = "id"
	ColKind      = "kind"
	ColMetadata  = "metadata"
	ColScopeTags = "scope_tags"
	ColExpiresAt = "expires_at"
	ColCreatedAt = "created_at"
	ColUpdatedAt = "updated_at"
	ColUserID    = memory.ScopeKeyUserID
	ColTenantID  = memory.ScopeKeyTenantID
	ColAgentID   = memory.ScopeKeyAgentID
)

Default table and column names for Memory.

View Source
const DefaultLoadLimit = 10

DefaultLoadLimit is the maximum memories returned when interfaces.WithLoadLimit is zero or negative.

View Source
const DefaultMinScore float32 = 0.35

DefaultMinScore is the default cosine similarity when interfaces.WithMinScore is zero.

Variables

This section is empty.

Functions

This section is empty.

Types

type EmbedFunc

type EmbedFunc func(ctx context.Context, text string) ([]float32, error)

EmbedFunc converts plain text into a vector embedding.

type Memory

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

Memory stores and recalls agent memories in PostgreSQL with pgvector.

func NewMemory

func NewMemory(embed EmbedFunc, opts ...Option) (*Memory, error)

NewMemory builds a pgvector-backed interfaces.Memory. embed is required. When WithPool is omitted, WithDSN must be provided.

func (*Memory) Clear

func (m *Memory) Clear(ctx context.Context, scope interfaces.MemoryScope) error

Clear removes all memories matching scope.

func (*Memory) Load

Load recalls memories within scope. Non-empty query uses vector similarity; empty query lists by updated_at.

func (*Memory) Store

Store persists a memory in scope and returns its ID.

type Option

type Option func(*Memory)

Option configures Memory.

func WithDSN

func WithDSN(dsn string) Option

WithDSN sets the PostgreSQL connection string used to create a pool when WithPool is omitted.

func WithDefaultLimit

func WithDefaultLimit(limit int) Option

WithDefaultLimit sets the load limit when callers omit interfaces.WithLoadLimit.

func WithDefaultMinScore

func WithDefaultMinScore(minScore float32) Option

WithDefaultMinScore sets the cosine similarity floor when callers omit interfaces.WithMinScore.

func WithEmbeddingCol

func WithEmbeddingCol(col string) Option

WithEmbeddingCol sets the column that holds the pgvector embedding. Defaults to DefaultEmbeddingCol.

func WithLogLevel

func WithLogLevel(level string) Option

WithLogLevel sets the log level when no logger is provided.

func WithLogger

func WithLogger(l logger.Logger) Option

WithLogger sets the logger.

func WithPool

func WithPool(pool *pgxpool.Pool) Option

WithPool sets an existing pgxpool.Pool. When provided, WithDSN is ignored.

func WithTable

func WithTable(table string) Option

WithTable sets the PostgreSQL table. Defaults to DefaultTable.

func WithTextCol

func WithTextCol(col string) Option

WithTextCol sets the column that holds memory text. Defaults to DefaultTextCol.

Jump to

Keyboard shortcuts

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