csc525_retrieval_based_chatbot / run_chatbot_chat.py
JoeArmani
implement chat features
a763857
raw
history blame
2.83 kB
import os
import json
from chatbot_model import ChatbotConfig, RetrievalChatbot
from response_quality_checker import ResponseQualityChecker
from environment_setup import EnvironmentSetup
from logger_config import config_logger
logger = config_logger(__name__)
def run_chatbot_chat():
env = EnvironmentSetup()
env.initialize()
MODEL_DIR = "models"
FAISS_INDICES_DIR = os.path.join(MODEL_DIR, "faiss_indices")
FAISS_INDEX_PRODUCTION_PATH = os.path.join(FAISS_INDICES_DIR, "faiss_index_production.index")
FAISS_INDEX_TEST_PATH = os.path.join(FAISS_INDICES_DIR, "faiss_index_test.index")
# Toggle 'production' or 'test' env
ENVIRONMENT = "production"
if ENVIRONMENT == "test":
FAISS_INDEX_PATH = FAISS_INDEX_TEST_PATH
RESPONSE_POOL_PATH = FAISS_INDEX_TEST_PATH.replace(".index", "_responses.json")
else:
FAISS_INDEX_PATH = FAISS_INDEX_PRODUCTION_PATH
RESPONSE_POOL_PATH = FAISS_INDEX_PRODUCTION_PATH.replace(".index", "_responses.json")
# Load the config
config_path = os.path.join(MODEL_DIR, "config.json")
if os.path.exists(config_path):
with open(config_path, "r", encoding="utf-8") as f:
config_dict = json.load(f)
config = ChatbotConfig.from_dict(config_dict)
logger.info(f"Loaded ChatbotConfig from {config_path}")
else:
config = ChatbotConfig()
logger.warning("No config.json found. Using default ChatbotConfig.")
# Load RetrievalChatbot in 'inference' mode
try:
chatbot = RetrievalChatbot.load_model(load_dir=MODEL_DIR, mode="inference")
except Exception as e:
logger.error(f"Failed to load RetrievalChatbot: {e}")
return
# Confirm FAISS index & response pool exist
if not os.path.exists(FAISS_INDEX_PATH) or not os.path.exists(RESPONSE_POOL_PATH):
logger.error("FAISS index or response pool file is missing.")
return
# Load FAISS index and response pool
try:
chatbot.data_pipeline.load_faiss_index(FAISS_INDEX_PATH)
with open(RESPONSE_POOL_PATH, "r", encoding="utf-8") as f:
chatbot.data_pipeline.response_pool = json.load(f)
logger.info(f"FAISS index loaded from {FAISS_INDEX_PATH}.")
# Validate dimension consistency
chatbot.data_pipeline.validate_faiss_index()
except Exception as e:
logger.error(f"Failed to load or validate FAISS index: {e}")
return
# Init QualityChecker and Validator
quality_checker = ResponseQualityChecker(data_pipeline=chatbot.data_pipeline)
# Run interactive chat loop
logger.info("\nStarting interactive chat session...")
chatbot.run_interactive_chat(quality_checker)
if __name__ == "__main__":
run_chatbot_chat()