Spaces:
Paused
Paused
File size: 798 Bytes
b71436a b1dd335 b71436a b1dd335 b71436a b1dd335 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import sqlite3
conn = sqlite3.connect("memory.db", check_same_thread=False)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS conversations (
user_id TEXT,
role TEXT,
content TEXT
)
""")
conn.commit()
def save_message(user_id, role, content):
cursor.execute(
"INSERT INTO conversations VALUES (?, ?, ?)",
(user_id, role, content)
)
conn.commit()
def load_memory(user_id, limit=6):
cursor.execute("""
SELECT role, content FROM conversations
WHERE user_id=?
ORDER BY rowid DESC
LIMIT ?
""", (user_id, limit))
rows = cursor.fetchall()
# Convert into LLM message format
formatted = [
{"role": role, "content": content}
for role, content in reversed(rows)
]
return formatted
|