memory

package
v1.16.69 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: GPL-3.0 Imports: 18 Imported by: 0

README

Karma Memory

Karma Memory is a sophisticated, long-term memory system for AI agents. It bridges the gap between stateless LLM calls and persistent, context-aware interactions by storing, retrieving, and managing memories using vector databases and intelligent caching.

Features

  • Long-term Persistence: Stores conversations, facts, rules, and skills in vector databases (Pinecone, Upstash).
  • Intelligent Retrieval:
    • Auto Mode: Fast, category-based retrieval for general context.
    • Conscious Mode: LLM-driven dynamic queries that filter by category, lifespan, importance, and status.
  • High-Performance Caching:
    • Multi-Level Caching: In-memory (local) and Redis (shared) support.
    • Smart Invalidation: Caches rules, facts, skills, and context separately with configurable TTLs.
    • Parallel Fetching: Optimizes latency by fetching from cache and vector stores concurrently.
  • Automatic Ingestion: Asynchronously processes conversation history to extract and store new memories.

Installation

go get github.com/MelloB1989/karma/ai/memory

Configuration

Ensure the following environment variables are set:

  • OPENAI_KEY: Required for embeddings and memory processing.
  • PINECONE_API_KEY / UPSTASH_VECTOR_REST_URL: Depending on your chosen vector service.
  • REDIS_URL: (Optional) For shared caching.

Initialization

Initialize the memory system with a KarmaAI client, a user ID, and an optional scope (e.g., project ID, session ID).

import (
    "github.com/MelloB1989/karma/ai"
    "github.com/MelloB1989/karma/ai/memory"
)

func main() {
    // 1. Initialize KarmaAI
    kai := ai.NewKarmaAI(ai.GPT4o, ai.OpenAI)

    // 2. Initialize Memory
    // NewKarmaMemory(client, userID, scope)
    mem := memory.NewKarmaMemory(kai, "user_123", "project_alpha")
    
    // 3. Configure (Optional)
    mem.UseRetrievalMode(memory.RetrievalModeConscious) // Use smarter, dynamic retrieval
    mem.EnableMemoryCache(memory.CacheConfig{Enabled: true}) // Enable local caching
}

Usage

1. Simple Usage (Managed Flow)

The easiest way to use Karma Memory is via the built-in ChatCompletion methods. These handle context retrieval, prompt augmentation, and history updates automatically.

response, err := mem.ChatCompletion("My name is John and I love Go.")
if err != nil {
    log.Fatal(err)
}

fmt.Println("AI:", response.AIResponse)
// The system has now learned that the user's name is John and they love Go.

// Next query will automatically retrieve this context
response2, err := mem.ChatCompletion("What language do I prefer?")
fmt.Println("AI:", response2.AIResponse) 
// Output: "You prefer Go."

Streaming Support:

_, err := mem.ChatCompletionStream("Tell me a story about my projects", func(chunk models.StreamedResponse) error {
    fmt.Print(chunk.Content)
    return nil
})
2. Advanced Usage (Manual Control)

For integration with existing chat loops or custom LLM calls, you can manually retrieve context and update history.

Step 1: Retrieve Context

Before sending a prompt to your LLM, fetch relevant context.

userPrompt := "How do I fix this bug in my React app?"

// GetContext returns a formatted string of relevant memories (facts, rules, previous messages)
contextStr, err := mem.GetContext(userPrompt)
if err != nil {
    log.Println("Error retrieving context:", err)
}

// Combine context with your prompt
fullPrompt := fmt.Sprintf("Context:\n%s\n\nUser: %s", contextStr, userPrompt)

Step 2: Update History

After generating a response, feed the interaction back into the memory system. This triggers the asynchronous ingestion process where the AI analyzes the conversation to store new facts or update existing ones.

// Create message objects
messages := []models.AIMessage{
    {
        Role:      models.User,
        Message:   userPrompt,
        Timestamp: time.Now(),
        UniqueId:  "msg_1",
    },
    {
        Role:      models.Assistant,
        Message:   aiResponseString,
        Timestamp: time.Now(),
        UniqueId:  "msg_2",
    },
}

// Update history (triggers ingestion in background)
mem.UpdateMessageHistory(messages)

Caching

Caching significantly reduces latency and vector database costs.

In-Memory Cache

Best for single-instance deployments.

mem.EnableMemoryCache(memory.CacheConfig{
    Enabled:    true,
    RulesTTL:   30 * time.Minute,
    FactsTTL:   20 * time.Minute,
    ContextTTL: 10 * time.Minute,
})
Redis Cache

Best for distributed deployments where multiple instances share the same memory state.

// Requires REDIS_URL env var or config
mem.EnableRedisCache(memory.CacheConfig{
    Enabled: true,
    // ... custom TTLs
})

Retrieval Modes

You can switch between retrieval modes based on your application's needs:

  • RetrievalModeAuto (Default):

    • Fast and cost-effective.
    • Always retrieves active rules, facts, skills, and context.
    • Uses the raw user prompt for vector search.
  • RetrievalModeConscious:

    • Smarter but slightly higher latency.
    • Uses an LLM to analyze the user's prompt and generate a dynamic search query.
    • Filters memories by specific categories, lifespans, or importance levels relevant to the current query.
mem.UseRetrievalMode(memory.RetrievalModeConscious)

Documentation

Overview

* This file adds extenders to KarmaAI package, adding managed memory functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheConfig added in v1.16.43

type CacheConfig struct {
	Backend          CacheMethod
	RulesTTL         time.Duration
	FactsTTL         time.Duration
	SkillsTTL        time.Duration
	ContextTTL       time.Duration
	AllMemoriesTTL   time.Duration
	LocalCacheMaxAge time.Duration
	Enabled          bool
}

type CacheMethod added in v1.16.43

type CacheMethod string
const (
	CacheMethodMemory CacheMethod = "memory"
	CacheMethodRedis  CacheMethod = "redis"
)

type CachedMemories added in v1.16.43

type CachedMemories struct {
	Memories  []Memory  `json:"memories"`
	CachedAt  time.Time `json:"cached_at"`
	ExpiresAt time.Time `json:"expires_at"`
}

type EntityRelationship

type EntityRelationship struct {
	EntityType   string `json:"entity_type"`
	RelationType string `json:"relation_type"`
	EntityValue  string `json:"entity_value"`
}

type KarmaMemory

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

func NewKarmaMemory

func NewKarmaMemory(kai *ai.KarmaAI, userId string, sc ...string) *KarmaMemory

func (*KarmaMemory) ChatCompletion

func (km *KarmaMemory) ChatCompletion(prompt string) (*models.AIChatResponse, error)

func (*KarmaMemory) ChatCompletionStream

func (km *KarmaMemory) ChatCompletionStream(prompt string, callback func(chunk models.StreamedResponse) error) (*models.AIChatResponse, error)

func (*KarmaMemory) ClearHistory

func (k *KarmaMemory) ClearHistory()

func (*KarmaMemory) DisableCache added in v1.16.43

func (k *KarmaMemory) DisableCache()

func (*KarmaMemory) EnableCache added in v1.16.43

func (k *KarmaMemory) EnableCache(cfg CacheConfig)

func (*KarmaMemory) EnableMemoryCache added in v1.16.43

func (k *KarmaMemory) EnableMemoryCache(cfg ...CacheConfig)

func (*KarmaMemory) EnableRedisCache added in v1.16.43

func (k *KarmaMemory) EnableRedisCache(cfg ...CacheConfig)

func (*KarmaMemory) GetCacheMethod added in v1.16.43

func (k *KarmaMemory) GetCacheMethod() CacheMethod

func (*KarmaMemory) GetCacheStats added in v1.16.43

func (k *KarmaMemory) GetCacheStats() (map[string]any, error)

func (*KarmaMemory) GetContext

func (k *KarmaMemory) GetContext(userPrompt string) (string, error)

func (*KarmaMemory) GetHistory

func (k *KarmaMemory) GetHistory() models.AIChatHistory

func (*KarmaMemory) InvalidateCache added in v1.16.43

func (k *KarmaMemory) InvalidateCache() error

func (*KarmaMemory) IsCacheEnabled added in v1.16.43

func (k *KarmaMemory) IsCacheEnabled() bool

func (*KarmaMemory) NumberOfMessages

func (k *KarmaMemory) NumberOfMessages() int

func (*KarmaMemory) UpdateMessageHistory

func (k *KarmaMemory) UpdateMessageHistory(messages []models.AIMessage)

Advanced implementations require custom logic to manage message history, in such cases below function can be used to update the message history

func (*KarmaMemory) UseEmbeddingLLM

func (k *KarmaMemory) UseEmbeddingLLM(llm ai.BaseModel, provider ai.Provider)

func (*KarmaMemory) UseLogger added in v1.16.42

func (k *KarmaMemory) UseLogger(logger *zap.Logger)

func (*KarmaMemory) UseMemoryLLM

func (k *KarmaMemory) UseMemoryLLM(llm ai.BaseModel, provider ai.Provider, extraConfig ...MemoryLlmConfig)

func (*KarmaMemory) UseRetrievalMode

func (k *KarmaMemory) UseRetrievalMode(mode RetrievalMode)

func (*KarmaMemory) UseScope

func (k *KarmaMemory) UseScope(scope string) bool

func (*KarmaMemory) UseService added in v1.16.42

func (k *KarmaMemory) UseService(service VectorServices) error

func (*KarmaMemory) UseUser

func (k *KarmaMemory) UseUser(userId string) bool

func (*KarmaMemory) WarmupCache added in v1.16.43

func (k *KarmaMemory) WarmupCache() error

type Memory

type Memory struct {
	Id                      string               `json:"id"` // Vector Id
	SubjectKey              string               `json:"subject_key"`
	Namespace               string               `json:"namespace"`
	Category                MemoryCategory       `json:"category"`
	Summary                 string               `json:"summary"`
	RawText                 string               `json:"raw_text"`
	Importance              int                  `json:"importance"`
	Mutability              MemoryMutability     `json:"mutability"`
	Lifespan                MemoryLifespan       `json:"lifespan"`
	ForgetScore             float64              `json:"forget_score"`
	Status                  MemoryStatus         `json:"status"`
	SupersedesCanonicalKeys []string             `json:"supersedes_canonical_keys"`
	Metadata                json.RawMessage      `json:"metadata"`
	CreatedAt               time.Time            `json:"created_at"`
	UpdatedAt               time.Time            `json:"updated_at"`
	ExpiresAt               *time.Time           `json:"expires_at,omitempty"`
	EntityRelationships     []EntityRelationship `json:"entity_relationships"`
}

func (*Memory) ToMap added in v1.16.42

func (m *Memory) ToMap() (map[string]any, error)

type MemoryCache added in v1.16.43

type MemoryCache interface {
	IsEnabled() bool
	GetBackend() CacheMethod
	CacheMemoriesByCategory(ctx context.Context, userID, scope string, category MemoryCategory, memories []Memory) error
	GetCachedMemoriesByCategory(ctx context.Context, userID, scope string, category MemoryCategory) ([]Memory, bool)
	CacheRules(ctx context.Context, userID, scope string, rules []Memory) error
	GetCachedRules(ctx context.Context, userID, scope string) ([]Memory, bool)
	CacheFacts(ctx context.Context, userID, scope string, facts []Memory) error
	GetCachedFacts(ctx context.Context, userID, scope string) ([]Memory, bool)
	CacheSkills(ctx context.Context, userID, scope string, skills []Memory) error
	GetCachedSkills(ctx context.Context, userID, scope string) ([]Memory, bool)
	CacheContext(ctx context.Context, userID, scope string, contextMemories []Memory) error
	GetCachedContext(ctx context.Context, userID, scope string) ([]Memory, bool)
	// Dynamic filtering for conscious mode
	CacheAllMemories(ctx context.Context, userID, scope string, memories []Memory) error
	GetCachedMemoriesWithFilter(ctx context.Context, userID, scope string, filter MemoryFilter) ([]Memory, bool)
	InvalidateCategoryCache(ctx context.Context, userID, scope string, category MemoryCategory) error
	InvalidateUserCache(ctx context.Context, userID string) error
	GetCacheStats(ctx context.Context, userID string) (map[string]any, error)
	WarmupCache(ctx context.Context, userID, scope string, vectorClient vectorService) error
	Close() error
}

func NewCache added in v1.16.43

func NewCache(logger *zap.Logger, cfg ...CacheConfig) MemoryCache

func NewMemoryCache added in v1.16.43

func NewMemoryCache(logger *zap.Logger, cfg ...CacheConfig) MemoryCache

func NewRedisCache added in v1.16.43

func NewRedisCache(logger *zap.Logger, cfg ...CacheConfig) MemoryCache

type MemoryCategory

type MemoryCategory string
const (
	CategoryFact       MemoryCategory = "fact"
	CategoryPreference MemoryCategory = "preference"
	CategorySkill      MemoryCategory = "skill"
	CategoryContext    MemoryCategory = "context"
	CategoryRule       MemoryCategory = "rule"
	CategoryEntity     MemoryCategory = "entity"
	CategoryEpisodic   MemoryCategory = "episodic"
)

type MemoryFilter added in v1.16.43

type MemoryFilter struct {
	Categories    []MemoryCategory // Filter by categories (e.g., fact, rule, skill)
	Lifespans     []MemoryLifespan // Filter by lifespans (e.g., short_term, lifelong)
	Status        *MemoryStatus    // Filter by status (e.g., active)
	MinImportance *int             // Filter by minimum importance
	NotExpired    bool             // Only include non-expired memories
}

MemoryFilter represents dynamic filter criteria for conscious mode retrieval

type MemoryLifespan

type MemoryLifespan string
const (
	LifespanShortTerm MemoryLifespan = "short_term"
	LifespanMidTerm   MemoryLifespan = "mid_term"
	LifespanLongTerm  MemoryLifespan = "long_term"
	LifespanLifelong  MemoryLifespan = "lifelong"
)

type MemoryLlmConfig added in v1.16.43

type MemoryLlmConfig struct {
	MaxTokens   *int
	Temperature *float32
}

type MemoryMutability

type MemoryMutability string
const (
	MutabilityMutable   MemoryMutability = "mutable"
	MutabilityImmutable MemoryMutability = "immutable"
)

type MemoryStatus

type MemoryStatus string
const (
	StatusActive     MemoryStatus = "active"
	StatusSuperseded MemoryStatus = "superseded"
	StatusDeleted    MemoryStatus = "deleted"
)

type RetrievalMode

type RetrievalMode string

RetrievalMode defines how memory context is retrieved

const (
	// RetrievalModeConscious uses AI to generate dynamic search queries based on user prompt.
	// It analyzes the prompt to determine relevant categories, lifespan, and search terms.
	// Best for: Complex queries where context matters, conversational AI.
	// Tradeoff: Higher latency due to LLM call, more tokens used.
	RetrievalModeConscious RetrievalMode = "conscious"

	// RetrievalModeAuto uses a fixed query strategy with the user prompt as the search text.
	// Always includes non-expired facts, preferences, rules, entities, and context.
	// Best for: Fast retrieval, predictable behavior, lower cost.
	// Tradeoff: Less intelligent filtering, may retrieve less relevant memories.
	RetrievalModeAuto RetrievalMode = "auto"
)

type VectorServices added in v1.16.42

type VectorServices string
const (
	VectorServiceUpstash  VectorServices = "upstash"
	VectorServicePinecone VectorServices = "pinecone"
)

Jump to

Keyboard shortcuts

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