Spaces:
Build error
Build error
Update utils/database.py
Browse files- utils/database.py +36 -0
utils/database.py
CHANGED
|
@@ -175,6 +175,42 @@ def create_tables(conn: sqlite3.Connection) -> None:
|
|
| 175 |
except Exception as e:
|
| 176 |
st.error(f"Unexpected error in create_tables: {e}")
|
| 177 |
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
def search_documents_in_collection(conn: sqlite3.Connection, collection_id: int, query: str) -> List[Dict]:
|
| 179 |
"""Search for documents within a collection."""
|
| 180 |
try:
|
|
|
|
| 175 |
except Exception as e:
|
| 176 |
st.error(f"Unexpected error in create_tables: {e}")
|
| 177 |
raise
|
| 178 |
+
|
| 179 |
+
def create_chat_tables(conn):
|
| 180 |
+
"""Create necessary tables for chat management."""
|
| 181 |
+
try:
|
| 182 |
+
cursor = conn.cursor()
|
| 183 |
+
|
| 184 |
+
# Create chats table
|
| 185 |
+
cursor.execute('''
|
| 186 |
+
CREATE TABLE IF NOT EXISTS chats (
|
| 187 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 188 |
+
title TEXT NOT NULL,
|
| 189 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 190 |
+
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 191 |
+
collection_id INTEGER,
|
| 192 |
+
FOREIGN KEY (collection_id) REFERENCES collections (id) ON DELETE SET NULL
|
| 193 |
+
)
|
| 194 |
+
''')
|
| 195 |
+
|
| 196 |
+
# Create chat messages table
|
| 197 |
+
cursor.execute('''
|
| 198 |
+
CREATE TABLE IF NOT EXISTS chat_messages (
|
| 199 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 200 |
+
chat_id INTEGER NOT NULL,
|
| 201 |
+
role TEXT NOT NULL,
|
| 202 |
+
content TEXT NOT NULL,
|
| 203 |
+
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 204 |
+
metadata TEXT, -- Store metadata as JSON string
|
| 205 |
+
FOREIGN KEY (chat_id) REFERENCES chats (id) ON DELETE CASCADE
|
| 206 |
+
)
|
| 207 |
+
''')
|
| 208 |
+
|
| 209 |
+
conn.commit()
|
| 210 |
+
|
| 211 |
+
except sqlite3.Error as e:
|
| 212 |
+
st.error(f"Error creating chat tables: {e}")
|
| 213 |
+
raise
|
| 214 |
def search_documents_in_collection(conn: sqlite3.Connection, collection_id: int, query: str) -> List[Dict]:
|
| 215 |
"""Search for documents within a collection."""
|
| 216 |
try:
|