Advertisement
Ra7eN

AI Memry

Apr 17th, 2025 (edited)
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | Source Code | 0 0
  1. import sqlite3
  2. import os
  3.  
  4. # Path to the SQLite database
  5. DB_PATH = os.path.join(os.path.dirname(__file__), "memory.db")
  6.  
  7. # Connect to the database (or create it)
  8. def connect_db():
  9.     conn = sqlite3.connect(DB_PATH)
  10.     conn.row_factory = sqlite3.Row
  11.     return conn
  12.  
  13. # Initialize database and tables
  14. def init_db():
  15.     conn = connect_db()
  16.     cursor = conn.cursor()
  17.  
  18.     # Table to store people (names, facts)
  19.     cursor.execute('''
  20.        CREATE TABLE IF NOT EXISTS people (
  21.            id INTEGER PRIMARY KEY AUTOINCREMENT,
  22.            name TEXT NOT NULL,
  23.            info TEXT
  24.        )
  25.    ''')
  26.  
  27.     # Table to store conversations
  28.     cursor.execute('''
  29.            CREATE TABLE IF NOT EXISTS conversations (
  30.                id INTEGER PRIMARY KEY AUTOINCREMENT,
  31.                timestamp TEXT NOT NULL,
  32.                user_input TEXT NOT NULL,
  33.                ai_response TEXT NOT NULL
  34.            )
  35.        ''')
  36.  
  37.     # Table to store memory facts
  38.     cursor.execute('''
  39.        CREATE TABLE IF NOT EXISTS memory_facts (
  40.            id INTEGER PRIMARY KEY AUTOINCREMENT,
  41.            subject TEXT,
  42.            fact TEXT
  43.        )
  44.    ''')
  45.  
  46.     conn.commit()
  47.     conn.close()
  48.    
  49. from datetime import datetime
  50.  
  51. def get_recent_conversations(limit=10):
  52.     with sqlite3.connect(ai_memory.db_path) as conn:
  53.         cursor = conn.execute('''
  54.            SELECT timestamp, user_input, ai_response
  55.            FROM conversations
  56.            ORDER BY id DESC
  57.            LIMIT ?
  58.        ''', (limit,))
  59.         return cursor.fetchall()
  60.  
  61.  
  62. def log_conversation(user_input, ai_response):
  63.     timestamp = datetime.now().isoformat()
  64.     with sqlite3.connect(ai_memory.db_path) as conn:
  65.         conn.execute('''
  66.            INSERT INTO conversations (timestamp, user_input, ai_response)
  67.            VALUES (?, ?, ?)
  68.        ''', (timestamp, user_input, ai_response))
  69.    
  70.  
  71. # Save a conversation turn
  72. def save_conversation(user_input, ai_response):
  73.     conn = connect_db()
  74.     cursor = conn.cursor()
  75.     cursor.execute('''
  76.        INSERT INTO conversations (user_input, ai_response)
  77.        VALUES (?, ?)
  78.    ''', (user_input, ai_response))
  79.     conn.commit()
  80.     conn.close()
  81.  
  82. # Save or update a memory fact
  83. def save_fact(subject, fact):
  84.     conn = connect_db()
  85.     cursor = conn.cursor()
  86.     cursor.execute('DELETE FROM memory_facts WHERE subject = ?', (subject,))
  87.     cursor.execute('INSERT INTO memory_facts (subject, fact) VALUES (?, ?)', (subject, fact))
  88.     conn.commit()
  89.     conn.close()
  90.  
  91. # Get all memory facts
  92. def get_facts():
  93.     conn = connect_db()
  94.     cursor = conn.cursor()
  95.     cursor.execute('SELECT subject, fact FROM memory_facts')
  96.     rows = cursor.fetchall()
  97.     conn.close()
  98.     return {row['subject']: row['fact'] for row in rows}
  99.  
  100. # Save a person of importance
  101. def save_person(name, info):
  102.     conn = connect_db()
  103.     cursor = conn.cursor()
  104.     cursor.execute('INSERT INTO people (name, info) VALUES (?, ?)', (name, info))
  105.     conn.commit()
  106.     conn.close()
  107.  
  108. # Retrieve all saved people
  109. def get_people():
  110.     conn = connect_db()
  111.     cursor = conn.cursor()
  112.     cursor.execute('SELECT name, info FROM people')
  113.     rows = cursor.fetchall()
  114.     conn.close()
  115.     return {row['name']: row['info'] for row in rows}
  116.  
  117. # Call this once at startup
  118. def initialize():
  119.     init_db()
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement