File size: 2,437 Bytes
aa3daf7 9a5bed3 aa3daf7 e64914d b520edc e64914d b520edc e64914d b520edc e64914d b520edc e64914d b520edc aa3daf7 b520edc e64914d aa3daf7 c58df45 |
1 2 3 4 5 6 7 8 9 10 11 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
#/modules/database/chat_mongo_db.py
from .mongo_db import insert_document, find_documents, get_collection
from datetime import datetime, timezone
import logging
from .database_init import get_mongodb # Aseg煤rate de que esta importaci贸n est茅 al principio del archivo
logger = logging.getLogger(__name__)
COLLECTION_NAME = 'chat_history-v3'
def get_chat_history(username, analysis_type='sidebar', limit=None):
"""
Recupera el historial del chat
"""
try:
query = {
"username": username,
"analysis_type": analysis_type
}
collection = get_collection(COLLECTION_NAME) # Usar get_collection en lugar de get_mongodb
if collection is None:
logger.error("No se pudo obtener la colecci贸n de chat")
return []
cursor = collection.find(query).sort("timestamp", -1)
if limit:
cursor = cursor.limit(limit)
return list(cursor)
except Exception as e:
logger.error(f"Error al recuperar historial de chat: {str(e)}")
return []
def store_chat_history(username, messages, analysis_type='sidebar'):
"""
Guarda el historial del chat
"""
try:
collection = get_collection(COLLECTION_NAME)
if collection is None:
logger.error("No se pudo obtener la colecci贸n de chat")
return False
chat_document = {
'username': username,
'timestamp': datetime.now(timezone.utc).isoformat(),
'messages': messages,
'analysis_type': analysis_type
}
result = collection.insert_one(chat_document)
if result.inserted_id:
logger.info(f"Historial de chat guardado con ID: {result.inserted_id} para el usuario: {username}")
return True
logger.error("No se pudo insertar el documento")
return False
except Exception as e:
logger.error(f"Error al guardar historial de chat: {str(e)}")
return False
#def get_chat_history(username, analysis_type=None, limit=10):
# query = {"username": username}
# if analysis_type:
# query["analysis_type"] = analysis_type
# return find_documents(COLLECTION_NAME, query, sort=[("timestamp", -1)], limit=limit)
# Agregar funciones para actualizar y eliminar chat si es necesario |