[GH-ISSUE #11884] Issue: feat: Evolve Ollama Client into a Cognitive Agent with Local Persistent Memory #54401

Closed
opened 2026-04-29 05:53:31 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @pmotadeee on GitHub (Aug 13, 2025).
Original GitHub issue: https://github.com/ollama/ollama/issues/11884

Description: Is just idea
Vision

Transform the standard Ollama client from a simple prompt executor into an intelligent agent that learns from every interaction, possesses persistent memory, and optimizes its responses based on past experiences, all while running locally.

  1. Proposed Architecture: Ollama Client with Cognitive Memory

We will integrate a CognitiveMemoryManager directly into the Go client. This manager will be adapted from previous implementations (Java/TypeScript) and will handle a local, bio-inspired memory ledger.

graph TD
    subgraph Go Client
        A[User Input] --> B{CognitiveMemoryManager};
        B --"Fetches Relevant Memories (O(log n))"--> C[Local SQLite];
        B --"Generates Compressed Context"--> D[Ollama Client];
        D --"Optimized Prompt"--> E[Ollama API (Local LLM)];
        E --"Response"--> F[Output to User];
        F --"Learns from Interaction"--> B;
    end

Workflow:

Capture: The main.go file captures the user's prompt.

Memory: Instead of sending the prompt directly to Ollama, it is first processed by the CognitiveMemoryManager.

Retrieval: The CognitiveMemoryManager performs an ultra-fast search on the local SQLite database to find relevant past interactions (memories).

Context Building: It constructs a "compressed context"—an optimized prompt that includes the current question along with snippets from the most important memories.

Inference: This optimized prompt is then sent to Ollama via client.Chat.

Learning: The LLM's response and the user's query are saved as a new memory in the SQLite database, strengthening the agent's knowledge base.
  1. Implementation Plan: Go Modules

The project will be divided into clear, manageable modules.

Module 1: memory/manager.go (The Agent's Brain)

This module implements the CognitiveMemoryManager.

Current State: Non-existent. Interactions are volatile and forgotten immediately.

Suggested Improvement (Go Code):

// memory/manager.go
package memory

import (
    "database/sql"
    _ "github.com/mattn/go-sqlite3"
    "time"
)

// MemoryRecord reflects the schema of our cognitive ledger.
type MemoryRecord struct {
    ID              string
    Timestamp       int64
    Content         string
    Valence         float64
    Flags           int
    ActivationScore float64
}

type CognitiveMemoryManager struct {
    db *sql.DB
}

func NewManager(dbPath string) (*CognitiveMemoryManager, error) {
    db, err := sql.Open("sqlite3", dbPath)
    if err != nil {
        return nil, err
    }
    // ... logic to create 'memories' table if it doesn't exist ...
    return &CognitiveMemoryManager{db: db}, nil
}

// addInteraction analyzes and saves a new memory.
func (m *CognitiveMemoryManager) addInteraction(content string) (MemoryRecord, error) {
    valence := m.calculateValence(content)
    flags := m.detectFlags(content)
    activationScore := m.calculateActivationScore(flags, valence)

    mem := MemoryRecord{
        ID:              /* Generate UUID */,
        Timestamp:       time.Now().Unix(),
        Content:         content,
        Valence:         valence,
        Flags:           flags,
        ActivationScore: activationScore,
    }

    // ... Logic for INSERT into SQLite ...
    return mem, nil
}

// semanticSearch finds relevant memories for a new prompt.
func (m *CognitiveMemoryManager) semanticSearch(query string, limit int) ([]MemoryRecord, error) {
    targetFlags := m.detectFlags(query)
    // ... Optimized SELECT logic based on flags and activation_score ...
    return nil, nil
}

// Helper functions: calculateValence, detectFlags, calculateActivationScore...

Module 2: main.go (The Interaction Point)

This is the main file that orchestrates the user interaction.

Current State: Directly calls client.Chat with a static, hardcoded message history.

Suggested Improvement:

// main.go (improved version)
import (
    "context"
    "fmt"
    "log"
    "your-project/memory" // Import our new memory module
    "github.com/ollama/ollama/api"
)

func main() {
    // 1. Initialize the memory manager
    memManager, err := memory.NewManager("./my_agent_brain.db")
    if err != nil {
        log.Fatal(err)
    }

    // 2. Simulate a new user prompt
    userPrompt := "which of these is the most dangerous?"

    // 3. Retrieve relevant memories
    relevantMemories, err := memManager.semanticSearch(userPrompt, 3)
    if err != nil {
        log.Fatal(err)
    }

    // 4. Dynamically build the message history
    messages := []api.Message{
        {Role: "system", Content: "You are a helpful assistant with a long-term memory. Use the provided context from past interactions to inform your answer."},
    }

    // Add context from memory
    for _, mem := range relevantMemories {
        messages = append(messages, api.Message{Role: "system", Content: fmt.Sprintf("[Past relevant interaction]: %s", mem.Content)})
    }

    // Add the current question
    messages = append(messages, api.Message{Role: "user", Content: userPrompt})

    // 5. Call Ollama with the optimized context
    client, _ := api.ClientFromEnvironment()
    req := &api.ChatRequest{
        Model:    "llama3.2",
        Messages: messages,
    }

    var fullResponse string
    respFunc := func(resp api.ChatResponse) error {
        fmt.Print(resp.Message.Content)
        fullResponse += resp.Message.Content
        return nil
    }

    err = client.Chat(context.Background(), req, respFunc)
    if err != nil {
        log.Fatal(err)
    }

    // 6. Learn from the interaction
    // Save the user's query and the full response as new memories
    memManager.addInteraction(userPrompt)
    memManager.addInteraction(fullResponse)
}
  1. Benefits of Brain-like Emulation

    Long-Term Memory: The agent remembers past interactions via my_agent_brain.db, becoming more helpful and personalized over time.

    Selective Attention: The semanticSearch function acts like the brain's focus, bringing only the most relevant information into the "working memory" (the LLM prompt) while ignoring noise.

    Extreme Efficiency:

     Token Cost: Instead of sending an ever-growing chat history, we send only a small, relevant context, drastically saving LLM resources.
    
     Speed: The SQLite search (O(log n)) is millions of times faster than the vector search (O(n)) used by traditional systems.
    

    Continuous Learning: Every interaction (query and response) is saved, refining the agent's knowledge base.

    Offline & Private Operation: Since Ollama and SQLite run locally, the agent's entire "brain" functions without an internet connection and with complete data privacy.

Originally created by @pmotadeee on GitHub (Aug 13, 2025). Original GitHub issue: https://github.com/ollama/ollama/issues/11884 Description: Is just idea Vision Transform the standard Ollama client from a simple prompt executor into an intelligent agent that learns from every interaction, possesses persistent memory, and optimizes its responses based on past experiences, all while running locally. 1. Proposed Architecture: Ollama Client with Cognitive Memory We will integrate a CognitiveMemoryManager directly into the Go client. This manager will be adapted from previous implementations (Java/TypeScript) and will handle a local, bio-inspired memory ledger. ``` graph TD subgraph Go Client A[User Input] --> B{CognitiveMemoryManager}; B --"Fetches Relevant Memories (O(log n))"--> C[Local SQLite]; B --"Generates Compressed Context"--> D[Ollama Client]; D --"Optimized Prompt"--> E[Ollama API (Local LLM)]; E --"Response"--> F[Output to User]; F --"Learns from Interaction"--> B; end ``` Workflow: Capture: The main.go file captures the user's prompt. Memory: Instead of sending the prompt directly to Ollama, it is first processed by the CognitiveMemoryManager. Retrieval: The CognitiveMemoryManager performs an ultra-fast search on the local SQLite database to find relevant past interactions (memories). Context Building: It constructs a "compressed context"—an optimized prompt that includes the current question along with snippets from the most important memories. Inference: This optimized prompt is then sent to Ollama via client.Chat. Learning: The LLM's response and the user's query are saved as a new memory in the SQLite database, strengthening the agent's knowledge base. 2. Implementation Plan: Go Modules The project will be divided into clear, manageable modules. Module 1: memory/manager.go (The Agent's Brain) This module implements the CognitiveMemoryManager. Current State: Non-existent. Interactions are volatile and forgotten immediately. Suggested Improvement (Go Code): // memory/manager.go package memory import ( "database/sql" _ "github.com/mattn/go-sqlite3" "time" ) // MemoryRecord reflects the schema of our cognitive ledger. type MemoryRecord struct { ID string Timestamp int64 Content string Valence float64 Flags int ActivationScore float64 } type CognitiveMemoryManager struct { db *sql.DB } func NewManager(dbPath string) (*CognitiveMemoryManager, error) { db, err := sql.Open("sqlite3", dbPath) if err != nil { return nil, err } // ... logic to create 'memories' table if it doesn't exist ... return &CognitiveMemoryManager{db: db}, nil } // addInteraction analyzes and saves a new memory. func (m *CognitiveMemoryManager) addInteraction(content string) (MemoryRecord, error) { valence := m.calculateValence(content) flags := m.detectFlags(content) activationScore := m.calculateActivationScore(flags, valence) mem := MemoryRecord{ ID: /* Generate UUID */, Timestamp: time.Now().Unix(), Content: content, Valence: valence, Flags: flags, ActivationScore: activationScore, } // ... Logic for INSERT into SQLite ... return mem, nil } // semanticSearch finds relevant memories for a new prompt. func (m *CognitiveMemoryManager) semanticSearch(query string, limit int) ([]MemoryRecord, error) { targetFlags := m.detectFlags(query) // ... Optimized SELECT logic based on flags and activation_score ... return nil, nil } // Helper functions: calculateValence, detectFlags, calculateActivationScore... Module 2: main.go (The Interaction Point) This is the main file that orchestrates the user interaction. Current State: Directly calls client.Chat with a static, hardcoded message history. Suggested Improvement: // main.go (improved version) import ( "context" "fmt" "log" "your-project/memory" // Import our new memory module "github.com/ollama/ollama/api" ) func main() { // 1. Initialize the memory manager memManager, err := memory.NewManager("./my_agent_brain.db") if err != nil { log.Fatal(err) } // 2. Simulate a new user prompt userPrompt := "which of these is the most dangerous?" // 3. Retrieve relevant memories relevantMemories, err := memManager.semanticSearch(userPrompt, 3) if err != nil { log.Fatal(err) } // 4. Dynamically build the message history messages := []api.Message{ {Role: "system", Content: "You are a helpful assistant with a long-term memory. Use the provided context from past interactions to inform your answer."}, } // Add context from memory for _, mem := range relevantMemories { messages = append(messages, api.Message{Role: "system", Content: fmt.Sprintf("[Past relevant interaction]: %s", mem.Content)}) } // Add the current question messages = append(messages, api.Message{Role: "user", Content: userPrompt}) // 5. Call Ollama with the optimized context client, _ := api.ClientFromEnvironment() req := &api.ChatRequest{ Model: "llama3.2", Messages: messages, } var fullResponse string respFunc := func(resp api.ChatResponse) error { fmt.Print(resp.Message.Content) fullResponse += resp.Message.Content return nil } err = client.Chat(context.Background(), req, respFunc) if err != nil { log.Fatal(err) } // 6. Learn from the interaction // Save the user's query and the full response as new memories memManager.addInteraction(userPrompt) memManager.addInteraction(fullResponse) } 3. Benefits of Brain-like Emulation Long-Term Memory: The agent remembers past interactions via my_agent_brain.db, becoming more helpful and personalized over time. Selective Attention: The semanticSearch function acts like the brain's focus, bringing only the most relevant information into the "working memory" (the LLM prompt) while ignoring noise. Extreme Efficiency: Token Cost: Instead of sending an ever-growing chat history, we send only a small, relevant context, drastically saving LLM resources. Speed: The SQLite search (O(log n)) is millions of times faster than the vector search (O(n)) used by traditional systems. Continuous Learning: Every interaction (query and response) is saved, refining the agent's knowledge base. Offline & Private Operation: Since Ollama and SQLite run locally, the agent's entire "brain" functions without an internet connection and with complete data privacy.
GiteaMirror added the feature request label 2026-04-29 05:53:31 -05:00
Author
Owner

@rick-github commented on GitHub (Aug 13, 2025):

This sounds like a project that could implement a new client that could be added to integrations.

<!-- gh-comment-id:3184202196 --> @rick-github commented on GitHub (Aug 13, 2025): This sounds like a project that could implement a new client that could be added to [integrations](https://github.com/ollama/ollama?tab=readme-ov-file#terminal).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#54401