Use scheduler to save chat history to disk every 5 minutes

- The previous mechanism to trigger saving on shutdown event did not work
- Use scheduler to persist chat sessions to disk at a 5 minute interval
  - This improve time granularity, fixed interval of saving chat logs
  - It may lose ~5 minutes of chat history until mechanism to also
    write on shutdown found/resolved
- Create conversation directory if it doesn't exist before attempting write
- Reset chat_session after writing it to disk
This commit is contained in:
Debanjum Singh Solanky 2023-01-13 21:29:04 -03:00
parent 5294693e97
commit d8ee0f0e9a

View file

@ -4,13 +4,14 @@ import logging
from typing import Optional from typing import Optional
# External Packages # External Packages
import schedule
from fastapi import APIRouter from fastapi import APIRouter
# Internal Packages # Internal Packages
from src.routers.api import search from src.routers.api import search
from src.processor.conversation.gpt import converse, extract_search_type, message_to_log, message_to_prompt, understand, summarize from src.processor.conversation.gpt import converse, extract_search_type, message_to_log, message_to_prompt, understand, summarize
from src.utils.config import SearchType from src.utils.config import SearchType
from src.utils.helpers import get_absolute_path, get_from_dict from src.utils.helpers import get_from_dict, resolve_absolute_path
from src.utils import state from src.utils import state
@ -100,12 +101,11 @@ def chat(q: str):
return {'status': status, 'response': gpt_response} return {'status': status, 'response': gpt_response}
@api_beta.on_event('shutdown') @schedule.repeat(schedule.every(5).minutes)
def shutdown_event(): def save_chat_session():
# No need to create empty log file # No need to create empty log file
if not (state.processor_config and state.processor_config.conversation and state.processor_config.conversation.meta_log): if not (state.processor_config and state.processor_config.conversation and state.processor_config.conversation.meta_log and state.processor_config.conversation.chat_session):
return return
logger.debug('INFO:\tSaving conversation logs to disk...')
# Summarize Conversation Logs for this Session # Summarize Conversation Logs for this Session
chat_session = state.processor_config.conversation.chat_session chat_session = state.processor_config.conversation.chat_session
@ -121,10 +121,13 @@ def shutdown_event():
conversation_log['session'].append(session) conversation_log['session'].append(session)
else: else:
conversation_log['session'] = [session] conversation_log['session'] = [session]
logger.info('Added new chat session to conversation logs')
# Save Conversation Metadata Logs to Disk # Save Conversation Metadata Logs to Disk
conversation_logfile = get_absolute_path(state.processor_config.conversation.conversation_logfile) conversation_logfile = resolve_absolute_path(state.processor_config.conversation.conversation_logfile)
conversation_logfile.parent.mkdir(parents=True, exist_ok=True) # create conversation directory if doesn't exist
with open(conversation_logfile, "w+", encoding='utf-8') as logfile: with open(conversation_logfile, "w+", encoding='utf-8') as logfile:
json.dump(conversation_log, logfile) json.dump(conversation_log, logfile)
logger.info('INFO:\tConversation logs saved to disk.') state.processor_config.conversation.chat_session = None
logger.info('Saved updated conversation logs to disk.')