Spaces:
Build error
Build error
Update utils/database.py
Browse files- utils/database.py +94 -30
utils/database.py
CHANGED
|
@@ -176,41 +176,105 @@ def create_tables(conn: sqlite3.Connection) -> None:
|
|
| 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 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 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:
|
|
|
|
| 176 |
st.error(f"Unexpected error in create_tables: {e}")
|
| 177 |
raise
|
| 178 |
|
| 179 |
+
def create_chat_tables(conn: sqlite3.Connection) -> None:
|
| 180 |
"""Create necessary tables for chat management."""
|
| 181 |
try:
|
| 182 |
+
with conn_lock:
|
| 183 |
+
cursor = conn.cursor()
|
| 184 |
+
|
| 185 |
+
# Create tags table first
|
| 186 |
+
cursor.execute('''
|
| 187 |
+
CREATE TABLE IF NOT EXISTS document_tags (
|
| 188 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 189 |
+
document_id INTEGER NOT NULL,
|
| 190 |
+
tag TEXT NOT NULL,
|
| 191 |
+
confidence FLOAT,
|
| 192 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 193 |
+
FOREIGN KEY (document_id) REFERENCES documents (id) ON DELETE CASCADE,
|
| 194 |
+
UNIQUE(document_id, tag)
|
| 195 |
+
)
|
| 196 |
+
''')
|
| 197 |
+
|
| 198 |
+
# Create chats table with collection_id
|
| 199 |
+
cursor.execute('''
|
| 200 |
+
CREATE TABLE IF NOT EXISTS chats (
|
| 201 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 202 |
+
title TEXT NOT NULL,
|
| 203 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 204 |
+
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 205 |
+
collection_id INTEGER,
|
| 206 |
+
FOREIGN KEY (collection_id) REFERENCES collections (id) ON DELETE SET NULL
|
| 207 |
+
)
|
| 208 |
+
''')
|
| 209 |
+
|
| 210 |
+
# Create chat messages table
|
| 211 |
+
cursor.execute('''
|
| 212 |
+
CREATE TABLE IF NOT EXISTS chat_messages (
|
| 213 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 214 |
+
chat_id INTEGER NOT NULL,
|
| 215 |
+
role TEXT NOT NULL,
|
| 216 |
+
content TEXT NOT NULL,
|
| 217 |
+
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 218 |
+
metadata TEXT,
|
| 219 |
+
FOREIGN KEY (chat_id) REFERENCES chats (id) ON DELETE CASCADE
|
| 220 |
+
)
|
| 221 |
+
''')
|
| 222 |
+
|
| 223 |
+
conn.commit()
|
| 224 |
+
|
| 225 |
except sqlite3.Error as e:
|
| 226 |
st.error(f"Error creating chat tables: {e}")
|
| 227 |
raise
|
| 228 |
+
|
| 229 |
+
async def generate_document_tags(content: str) -> List[str]:
|
| 230 |
+
"""Generate tags for a document using AI."""
|
| 231 |
+
try:
|
| 232 |
+
llm = ChatOpenAI(temperature=0.2, model="gpt-3.5-turbo")
|
| 233 |
+
|
| 234 |
+
prompt = """Analyze the following document content and generate relevant tags/keywords.
|
| 235 |
+
Focus on key themes, topics, and important terminology.
|
| 236 |
+
Return only the tags as a comma-separated list.
|
| 237 |
+
Content: {content}"""
|
| 238 |
+
|
| 239 |
+
response = await llm.ainvoke(prompt.format(content=content[:2000])) # Use first 2000 chars
|
| 240 |
+
tags = [tag.strip() for tag in response.split(',')]
|
| 241 |
+
return tags
|
| 242 |
+
except Exception as e:
|
| 243 |
+
st.error(f"Error generating tags: {e}")
|
| 244 |
+
return []
|
| 245 |
+
|
| 246 |
+
def add_document_tags(conn: sqlite3.Connection, document_id: int, tags: List[str]) -> bool:
|
| 247 |
+
"""Add tags to a document."""
|
| 248 |
+
try:
|
| 249 |
+
with conn_lock:
|
| 250 |
+
cursor = conn.cursor()
|
| 251 |
+
for tag in tags:
|
| 252 |
+
cursor.execute('''
|
| 253 |
+
INSERT OR IGNORE INTO document_tags (document_id, tag)
|
| 254 |
+
VALUES (?, ?)
|
| 255 |
+
''', (document_id, tag))
|
| 256 |
+
conn.commit()
|
| 257 |
+
return True
|
| 258 |
+
except sqlite3.Error as e:
|
| 259 |
+
st.error(f"Error adding tags: {e}")
|
| 260 |
+
return False
|
| 261 |
+
|
| 262 |
+
def get_document_tags(conn: sqlite3.Connection, document_id: int) -> List[str]:
|
| 263 |
+
"""Get all tags for a document."""
|
| 264 |
+
try:
|
| 265 |
+
with conn_lock:
|
| 266 |
+
cursor = conn.cursor()
|
| 267 |
+
cursor.execute('''
|
| 268 |
+
SELECT tag FROM document_tags
|
| 269 |
+
WHERE document_id = ?
|
| 270 |
+
ORDER BY tag
|
| 271 |
+
''', (document_id,))
|
| 272 |
+
return [row[0] for row in cursor.fetchall()]
|
| 273 |
+
except sqlite3.Error as e:
|
| 274 |
+
st.error(f"Error retrieving tags: {e}")
|
| 275 |
+
return []
|
| 276 |
+
|
| 277 |
+
|
| 278 |
def search_documents_in_collection(conn: sqlite3.Connection, collection_id: int, query: str) -> List[Dict]:
|
| 279 |
"""Search for documents within a collection."""
|
| 280 |
try:
|