Spaces:
Sleeping
Sleeping
Update modules/database/chat_mongo_db.py
Browse files
modules/database/chat_mongo_db.py
CHANGED
|
@@ -5,36 +5,63 @@ import logging
|
|
| 5 |
from .database_init import get_mongodb # Aseg煤rate de que esta importaci贸n est茅 al principio del archivo
|
| 6 |
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
-
|
| 9 |
COLLECTION_NAME = 'chat_history-v3'
|
| 10 |
|
| 11 |
-
def store_chat_history(username, messages, analysis_type):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
return list(cursor)
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
#def get_chat_history(username, analysis_type=None, limit=10):
|
| 39 |
# query = {"username": username}
|
| 40 |
# if analysis_type:
|
|
|
|
| 5 |
from .database_init import get_mongodb # Aseg煤rate de que esta importaci贸n est茅 al principio del archivo
|
| 6 |
|
| 7 |
logger = logging.getLogger(__name__)
|
|
|
|
| 8 |
COLLECTION_NAME = 'chat_history-v3'
|
| 9 |
|
| 10 |
+
def store_chat_history(username, messages, analysis_type='sidebar'):
|
| 11 |
+
"""
|
| 12 |
+
Guarda el historial del chat
|
| 13 |
+
Args:
|
| 14 |
+
username: nombre del usuario
|
| 15 |
+
messages: lista de mensajes
|
| 16 |
+
analysis_type: tipo de chat ('sidebar' por defecto)
|
| 17 |
+
"""
|
| 18 |
+
try:
|
| 19 |
+
chat_document = {
|
| 20 |
+
'username': username,
|
| 21 |
+
'timestamp': datetime.now(timezone.utc).isoformat(),
|
| 22 |
+
'messages': messages,
|
| 23 |
+
'analysis_type': analysis_type
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
result = insert_document(COLLECTION_NAME, chat_document)
|
| 27 |
+
if result:
|
| 28 |
+
logger.info(f"Historial de chat guardado con ID: {result} para el usuario: {username}")
|
| 29 |
+
return True
|
| 30 |
+
return False
|
| 31 |
+
except Exception as e:
|
| 32 |
+
logger.error(f"Error al guardar historial de chat: {str(e)}")
|
| 33 |
+
return False
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
def get_chat_history(username, analysis_type='sidebar', limit=None):
|
| 36 |
+
"""
|
| 37 |
+
Recupera el historial del chat
|
| 38 |
+
Args:
|
| 39 |
+
username: nombre del usuario
|
| 40 |
+
analysis_type: tipo de chat ('sidebar' por defecto)
|
| 41 |
+
limit: l铆mite de mensajes a recuperar
|
| 42 |
+
Returns:
|
| 43 |
+
list: Lista de conversaciones
|
| 44 |
+
"""
|
| 45 |
+
try:
|
| 46 |
+
query = {
|
| 47 |
+
"username": username,
|
| 48 |
+
"analysis_type": analysis_type
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
db = get_mongodb()
|
| 52 |
+
if not db:
|
| 53 |
+
raise Exception("No se pudo conectar a la base de datos")
|
| 54 |
+
|
| 55 |
+
collection = db[COLLECTION_NAME]
|
| 56 |
+
|
| 57 |
+
cursor = collection.find(query).sort("timestamp", -1)
|
| 58 |
+
if limit:
|
| 59 |
+
cursor = cursor.limit(limit)
|
| 60 |
+
|
| 61 |
+
return list(cursor)
|
| 62 |
+
except Exception as e:
|
| 63 |
+
logger.error(f"Error al recuperar historial de chat: {str(e)}")
|
| 64 |
+
return []
|
| 65 |
#def get_chat_history(username, analysis_type=None, limit=10):
|
| 66 |
# query = {"username": username}
|
| 67 |
# if analysis_type:
|