Spaces:
Runtime error
Runtime error
Download memory_db.py from AumCoreAI/AumCore-AI: direct link, hf CLI and curl.
- Browser
- Download file 1.53 kB
-
https://huggingface.co/spaces/AumCoreAI/AumCore-AI/resolve/refs%2Fpr%2F1/memory_db.py
- Command line
-
hf download hf://spaces/AumCoreAI/AumCore-AI@refs/pr/1/memory_db.py
-
curl -L -o memory_db.py https://huggingface.co/spaces/AumCoreAI/AumCore-AI/resolve/refs%2Fpr%2F1/memory_db.py
1.53 kB
| # memory_db.py - TiDB Database Operations | |
| import pymysql | |
| from config import settings | |
| class MemoryDB: | |
| def __init__(self): | |
| self.connection = pymysql.connect( | |
| host=settings.TIDB_HOST, | |
| port=settings.TIDB_PORT, | |
| user=settings.TIDB_USER, | |
| password=settings.TIDB_PASSWORD, | |
| database=settings.TIDB_DATABASE, | |
| ssl={'ssl': {'ca': ''}} # TiDB SSL | |
| ) | |
| self.create_tables() | |
| def create_tables(self): | |
| with self.connection.cursor() as cursor: | |
| cursor.execute(""" | |
| CREATE TABLE IF NOT EXISTS chat_history ( | |
| id INT AUTO_INCREMENT PRIMARY KEY, | |
| user_input TEXT, | |
| ai_response MEDIUMTEXT, | |
| created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP | |
| ) | |
| """) | |
| self.connection.commit() | |
| def save_conversation(self, user_input, ai_response): | |
| with self.connection.cursor() as cursor: | |
| cursor.execute( | |
| "INSERT INTO chat_history (user_input, ai_response) VALUES (%s, %s)", | |
| (user_input, ai_response) | |
| ) | |
| self.connection.commit() | |
| def get_recent_conversations(self, limit=10): | |
| with self.connection.cursor() as cursor: | |
| cursor.execute( | |
| "SELECT user_input, ai_response FROM chat_history ORDER BY created_at DESC LIMIT %s", | |
| (limit,) | |
| ) | |
| return cursor.fetchall() | |