0
0
Agentic AIml~20 mins

Memory persistence and storage in Agentic AI - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Memory persistence and storage
Problem:You have built an AI agent that learns from conversations but loses all memory after each session. This means it cannot remember past interactions, limiting its usefulness.
Current Metrics:Memory retention: 0% after session ends; agent responses lack context from previous sessions.
Issue:The AI agent does not persist memory between sessions, causing it to forget all learned information and user preferences.
Your Task
Implement memory persistence so the AI agent retains learned information and user context across sessions, improving continuity and relevance of responses.
You must use a simple file-based or database storage solution for memory persistence.
Do not change the core AI model architecture.
Ensure memory loading and saving does not significantly slow down the agent's response time.
Hint 1
Hint 2
Hint 3
Solution
Agentic AI
import json
import os

class MemoryAgent:
    def __init__(self, memory_file='memory.json'):
        self.memory_file = memory_file
        self.memory = self.load_memory()

    def load_memory(self):
        if os.path.exists(self.memory_file):
            with open(self.memory_file, 'r') as f:
                return json.load(f)
        else:
            return {}

    def save_memory(self):
        with open(self.memory_file, 'w') as f:
            json.dump(self.memory, f)

    def remember(self, key, value):
        self.memory[key] = value
        self.save_memory()

    def recall(self, key):
        return self.memory.get(key, None)

    def respond(self, user_input):
        # Simple example: remember user's name if mentioned
        if 'my name is' in user_input.lower():
            name = user_input.lower().split('my name is')[-1].strip()
            self.remember('user_name', name)
            return f'Nice to meet you, {name}!'
        elif 'what is my name' in user_input.lower():
            name = self.recall('user_name')
            if name:
                return f'Your name is {name}.'
            else:
                return 'I do not know your name yet.'
        else:
            return 'Tell me your name by saying "My name is ..."'

# Example usage
agent = MemoryAgent()
print(agent.respond('My name is Alice'))  # Stores name
print(agent.respond('What is my name?'))  # Recalls name

# After restarting the program, the agent will still remember Alice
Added a MemoryAgent class to handle loading and saving memory to a JSON file.
Implemented remember and recall methods to store and retrieve information.
Modified respond method to use persistent memory for user name.
Ensured memory is saved after each update to persist across sessions.
Results Interpretation

Before: Memory retention was 0%. The agent forgot all information after each session, resulting in generic responses.

After: Memory retention is 100% for stored data. The agent remembers user names and can recall them in later sessions, improving interaction quality.

Persisting memory outside the AI model allows agents to maintain context and user information across sessions, making interactions more natural and useful.
Bonus Experiment
Try implementing memory persistence using a lightweight database like SQLite instead of JSON files.
💡 Hint
Use Python's sqlite3 module to create a simple table for key-value pairs and update the load and save methods accordingly.