AumCoreAI commited on
Commit
3f47213
·
verified ·
1 Parent(s): 1156b22

Update memory_db.py

Browse files
Files changed (1) hide show
  1. memory_db.py +85 -26
memory_db.py CHANGED
@@ -1,43 +1,102 @@
1
- # memory_db.py - TiDB Database Operations
2
  import pymysql
3
- from config import settings
 
4
 
5
- class MemoryDB:
6
  def __init__(self):
7
- self.connection = pymysql.connect(
8
- host=settings.TIDB_HOST,
9
- port=settings.TIDB_PORT,
10
- user=settings.TIDB_USER,
11
- password=settings.TIDB_PASSWORD,
12
- database=settings.TIDB_DATABASE,
13
- ssl={'ssl': {'ca': ''}} # TiDB SSL
14
- )
15
- self.create_tables()
 
 
 
 
 
 
 
 
 
16
 
17
  def create_tables(self):
 
 
 
18
  with self.connection.cursor() as cursor:
19
  cursor.execute("""
20
  CREATE TABLE IF NOT EXISTS chat_history (
21
  id INT AUTO_INCREMENT PRIMARY KEY,
22
  user_input TEXT,
23
  ai_response MEDIUMTEXT,
 
 
 
 
 
 
 
 
 
 
 
24
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
25
  )
26
  """)
27
  self.connection.commit()
28
 
29
- def save_conversation(self, user_input, ai_response):
30
- with self.connection.cursor() as cursor:
31
- cursor.execute(
32
- "INSERT INTO chat_history (user_input, ai_response) VALUES (%s, %s)",
33
- (user_input, ai_response)
34
- )
35
- self.connection.commit()
 
 
 
 
 
 
 
 
36
 
37
- def get_recent_conversations(self, limit=10):
38
- with self.connection.cursor() as cursor:
39
- cursor.execute(
40
- "SELECT user_input, ai_response FROM chat_history ORDER BY created_at DESC LIMIT %s",
41
- (limit,)
42
- )
43
- return cursor.fetchall()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # memory_db.py - TiDB Integration
2
  import pymysql
3
+ import os
4
+ from datetime import datetime
5
 
6
+ class TiDBMemory:
7
  def __init__(self):
8
+ self.connection = None
9
+ self.connect()
10
+
11
+ def connect(self):
12
+ try:
13
+ self.connection = pymysql.connect(
14
+ host=os.getenv("TIDB_HOST", "gateway01.ap-southeast-1.prod.aws.tidbcloud.com"),
15
+ port=int(os.getenv("TIDB_PORT", 4000)),
16
+ user=os.getenv("TIDB_USER", "2Rg6kfo2rNEB3PN.root"),
17
+ password=os.getenv("TIDB_PASSWORD", "9JJabiRfo0WpH9FP"),
18
+ database=os.getenv("TIDB_DATABASE", "test"),
19
+ ssl={'ssl': {'ca': ''}}
20
+ )
21
+ self.create_tables()
22
+ print("✅ TiDB connected successfully")
23
+ except Exception as e:
24
+ print(f"❌ TiDB connection failed: {e}")
25
+ self.connection = None
26
 
27
  def create_tables(self):
28
+ if not self.connection:
29
+ return
30
+
31
  with self.connection.cursor() as cursor:
32
  cursor.execute("""
33
  CREATE TABLE IF NOT EXISTS chat_history (
34
  id INT AUTO_INCREMENT PRIMARY KEY,
35
  user_input TEXT,
36
  ai_response MEDIUMTEXT,
37
+ language_mode VARCHAR(20),
38
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
39
+ )
40
+ """)
41
+ cursor.execute("""
42
+ CREATE TABLE IF NOT EXISTS code_snippets (
43
+ id INT AUTO_INCREMENT PRIMARY KEY,
44
+ code_type VARCHAR(100),
45
+ code_content LONGTEXT,
46
+ description TEXT,
47
+ usage_count INT DEFAULT 0,
48
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
49
  )
50
  """)
51
  self.connection.commit()
52
 
53
+ def save_chat(self, user_input, ai_response, language_mode):
54
+ if not self.connection:
55
+ return False
56
+
57
+ try:
58
+ with self.connection.cursor() as cursor:
59
+ cursor.execute(
60
+ "INSERT INTO chat_history (user_input, ai_response, language_mode) VALUES (%s, %s, %s)",
61
+ (user_input, ai_response, language_mode)
62
+ )
63
+ self.connection.commit()
64
+ return True
65
+ except Exception as e:
66
+ print(f"❌ Save chat error: {e}")
67
+ return False
68
 
69
+ def get_recent_chats(self, limit=10):
70
+ if not self.connection:
71
+ return []
72
+
73
+ try:
74
+ with self.connection.cursor() as cursor:
75
+ cursor.execute(
76
+ "SELECT user_input, ai_response, language_mode FROM chat_history ORDER BY created_at DESC LIMIT %s",
77
+ (limit,)
78
+ )
79
+ return cursor.fetchall()
80
+ except Exception as e:
81
+ print(f"❌ Get chats error: {e}")
82
+ return []
83
+
84
+ def save_code_snippet(self, code_type, code_content, description):
85
+ if not self.connection:
86
+ return False
87
+
88
+ try:
89
+ with self.connection.cursor() as cursor:
90
+ cursor.execute(
91
+ """INSERT INTO code_snippets (code_type, code_content, description)
92
+ VALUES (%s, %s, %s)""",
93
+ (code_type, code_content, description)
94
+ )
95
+ self.connection.commit()
96
+ return True
97
+ except Exception as e:
98
+ print(f"❌ Save code error: {e}")
99
+ return False
100
+
101
+ # Global instance
102
+ tidb_memory = TiDBMemory()