Skip to main content

Chapter 7.1 - SQLite Chat History

[!info] Tracking chat history persistently.

A RAG architecture is useless if it cannot remember what the user just said. We use a local SQLite database to store user sessions.


import sqlite3

def init_db():
conn = sqlite3.connect('rag_history.db')
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
role TEXT,
content TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
''')
conn.commit()

When generating an answer, we pull the last N messages from SQLite and prepend them to the LLM prompt, giving it full conversational context!