Transform your chatbot into an agent that remembers everything—from past conversations to user preferences.

Why Add Memory?

An AI Agent without memory is like a digital Goldfish: it forgets everything after each interaction. With memory, you can:

  • Personalize responses based on user history.

  • Maintain context in long conversations (e.g., multi-step technical support).

  • Scale applications for multiple users simultaneously.

How to Save and Retrieve Conversations

We will explore two approaches: simple (JSON) and advanced (SQLite).

Method 1: Using JSON (For Simple Projects)

Store conversation history in a JSON file for basic persistence.

Step 1: Save Conversations

Modify your chatbot.py to include:

import json

def save_conversation(history, file="history.json"):  
    with open(file, "w") as f:  
        json.dump(history, f, ensure_ascii=False, indent=2)  

# After each AI response, add:  
save_conversation(history)

Step 2: Retrieve Conversations

Add this at the beginning of your code:

def load_conversation(file="history.json"):  
    try:  
        with open(file, "r") as f:  
            return json.load(f)  
    except FileNotFoundError:  
        return []

history = load_conversation()

Advantages:

  • Ideal for quick prototypes.

  • No database required.

Limitations:

  • Inefficient for large datasets.

  • Does not support complex queries.

Method 2: Using SQLite (For Professional Applications)

SQLite is a lightweight, built-in database for Python.

Step 1: Create a Database

Create a file database.py:

import sqlite3

conn = sqlite3.connect("chatbot.db")  
cursor = conn.cursor()  

# Create a table to store conversations  
cursor.execute('''  
    CREATE TABLE IF NOT EXISTS conversations (  
        id INTEGER PRIMARY KEY AUTOINCREMENT,  
        user TEXT,  
        role TEXT,  
        content TEXT,  
        timestamp DATETIME DEFAULT CURRENT_TIMESTAMP  
    )  
''')  

conn.commit()  
conn.close()

Step 2: Integrate with the Chatbot

Modify chatbot.py:

import sqlite3

def save_message(user, role, content):  
    conn = sqlite3.connect("chatbot.db")  
    cursor = conn.cursor()  
    cursor.execute('''  
        INSERT INTO conversations (user, role, content)  
        VALUES (?, ?, ?)  
    ''', (user, role, content))  
    conn.commit()  
    conn.close()  

# After each user or AI message:  
save_message("John", "user", question)  
save_message("John", "assistant", ai_response)

Step 3: Retrieve History

def load_history(user):  
    conn = sqlite3.connect("chatbot.db")  
    cursor = conn.cursor()  
    cursor.execute('''  
        SELECT role, content FROM conversations  
        WHERE user = ?  
        ORDER BY timestamp  
    ''', (user,))  
    history = [{"role": row[0], "content": row[1]} for row in cursor.fetchall()]  
    conn.close()  
    return history

history = load_history("John")

Advantages:

  • Scalable for thousands of users.

  • Allows complex queries (e.g., "Show all questions about Python").

🤖 Creating a More Interactive AI

Use memory to make the agent smarter:

Example 1: Remembering Preferences

# In system prompt:  
history = [{  
    "role": "system",  
    "content": "You are an assistant that calls the user by their name. User's name: John."  
}]

Output:

"John, do you prefer coffee or tea? I remember last time you chose coffee."

Example 2: Long-Term Context

# Before sending the question to the API, add:  
history.append({  
    "role": "system",  
    "content": f"Summary of history: {history_summary}"  
})

💡 Tip: Use GPT itself to summarize long conversations!

🚨 Common Errors (and Solutions)

Corrupted JSON: json.decoder.JSONDecodeError

Solution: Use try/except blocks and validate the file before loading.

SQLite Locking Issue: sqlite3.OperationalError: database is locked

Solution: Close connections after each operation using conn.close().

Memory Leak: History too long, exceeding token limit.

Solution: Implement a summarization or pagination system for history.

🚀 Practical Project: Personal Assistant with Memory

Create an AI Agent that:

  • Remembers user tasks (e.g., "John, your meeting is at 3 PM").

  • Recommends content based on past conversations.

Ready to make your AI Agent incredibly useful? In the next chapter, it will access the internet!