Files
Cyrene_v2/backend/services/memory/model.go
T
Aska 8495a4b055 feat(memory): hybrid vector search via embedder
- repository: optional pgvector embedding column (migration with
  CREATE EXTENSION guarded by main), SearchByVector cosine search,
  vector-enabled insert path
- service: SetEmbedder — memories are embedded on Add; Search merges
  vector hits with n-gram keyword matches (vector first, deduped)
- embedder was already implemented (OpenAI-compatible API + n-gram FNV
  fallback) but never instantiated; main now wires it from
  EMBEDDING_API_URL/EMBEDDING_MODEL config
2026-08-20 20:57:13 +08:00

54 lines
1.7 KiB
Go

// Package memory implements the long-term memory system for Cyrene.
// It provides CRUD operations, semantic search via pgvector, deduplication, and decay.
package memory
import "time"
// Memory represents a stored memory entry.
type Memory struct {
ID string `json:"id"`
UserID string `json:"user_id"`
SourceID string `json:"source_id"` // Group/private chat source
Content string `json:"content"`
Summary string `json:"summary"` // LLM-generated summary
Keywords []string `json:"keywords"` // Extracted keywords
Category string `json:"category"` // "fact", "preference", "event", "plan"
Priority float64 `json:"priority"` // 0.0 - 1.0
Decay float64 `json:"decay"` // Decay factor, starts at 1.0
AccessCnt int `json:"access_count"`
LastAcc time.Time `json:"last_accessed_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// Embedding is the vector representation used for semantic search.
// Not serialized or scanned by default queries; pgvector-only.
Embedding []float64 `json:"-"`
}
// SearchQuery represents a memory search request.
type SearchQuery struct {
UserID string
SourceID string // Optional, filter by source
Query string
Limit int
Category string // Optional filter
}
// MemoryResult wraps a memory with its search relevance score.
type MemoryResult struct {
Memory Memory `json:"memory"`
Score float64 `json:"score"`
}
// Category constants
const (
CatFact = "fact"
CatPreference = "preference"
CatEvent = "event"
CatPlan = "plan"
CatForgotten = "forgotten"
)
// DecayThreshold is the decay value below which a memory is considered forgotten.
const DecayThreshold = 0.1