Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- import os
- # Path to the SQLite database
- DB_PATH = os.path.join(os.path.dirname(__file__), "memory.db")
- # Connect to the database (or create it)
- def connect_db():
- conn = sqlite3.connect(DB_PATH)
- conn.row_factory = sqlite3.Row
- return conn
- # Initialize database and tables
- def init_db():
- conn = connect_db()
- cursor = conn.cursor()
- # Table to store people (names, facts)
- cursor.execute('''
- CREATE TABLE IF NOT EXISTS people (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- name TEXT NOT NULL,
- info TEXT
- )
- ''')
- # Table to store conversations
- cursor.execute('''
- CREATE TABLE IF NOT EXISTS conversations (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- timestamp TEXT NOT NULL,
- user_input TEXT NOT NULL,
- ai_response TEXT NOT NULL
- )
- ''')
- # Table to store memory facts
- cursor.execute('''
- CREATE TABLE IF NOT EXISTS memory_facts (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- subject TEXT,
- fact TEXT
- )
- ''')
- conn.commit()
- conn.close()
- from datetime import datetime
- def get_recent_conversations(limit=10):
- with sqlite3.connect(ai_memory.db_path) as conn:
- cursor = conn.execute('''
- SELECT timestamp, user_input, ai_response
- FROM conversations
- ORDER BY id DESC
- LIMIT ?
- ''', (limit,))
- return cursor.fetchall()
- def log_conversation(user_input, ai_response):
- timestamp = datetime.now().isoformat()
- with sqlite3.connect(ai_memory.db_path) as conn:
- conn.execute('''
- INSERT INTO conversations (timestamp, user_input, ai_response)
- VALUES (?, ?, ?)
- ''', (timestamp, user_input, ai_response))
- # Save a conversation turn
- def save_conversation(user_input, ai_response):
- conn = connect_db()
- cursor = conn.cursor()
- cursor.execute('''
- INSERT INTO conversations (user_input, ai_response)
- VALUES (?, ?)
- ''', (user_input, ai_response))
- conn.commit()
- conn.close()
- # Save or update a memory fact
- def save_fact(subject, fact):
- conn = connect_db()
- cursor = conn.cursor()
- cursor.execute('DELETE FROM memory_facts WHERE subject = ?', (subject,))
- cursor.execute('INSERT INTO memory_facts (subject, fact) VALUES (?, ?)', (subject, fact))
- conn.commit()
- conn.close()
- # Get all memory facts
- def get_facts():
- conn = connect_db()
- cursor = conn.cursor()
- cursor.execute('SELECT subject, fact FROM memory_facts')
- rows = cursor.fetchall()
- conn.close()
- return {row['subject']: row['fact'] for row in rows}
- # Save a person of importance
- def save_person(name, info):
- conn = connect_db()
- cursor = conn.cursor()
- cursor.execute('INSERT INTO people (name, info) VALUES (?, ?)', (name, info))
- conn.commit()
- conn.close()
- # Retrieve all saved people
- def get_people():
- conn = connect_db()
- cursor = conn.cursor()
- cursor.execute('SELECT name, info FROM people')
- rows = cursor.fetchall()
- conn.close()
- return {row['name']: row['info'] for row in rows}
- # Call this once at startup
- def initialize():
- init_db()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement