import importlib from abc import ABC, abstractmethod from typing import Dict, Any, List from flask import current_app from common.models.interaction import SpecialistRetriever from common.utils.execution_progress import ExecutionProgressTracker from config.logging_config import TuningLogger from eveai_chat_workers.retrievers.base import BaseRetriever from eveai_chat_workers.retrievers.registry import RetrieverRegistry from eveai_chat_workers.specialists.specialist_typing import SpecialistArguments, SpecialistResult class BaseSpecialistExecutor(ABC): """Base class for all specialists""" def __init__(self, tenant_id: int, specialist_id: int, session_id: str, task_id: str): self.tenant_id = tenant_id self.specialist_id = specialist_id self.session_id = session_id self.task_id = task_id self.tuning = False self.tuning_logger = None self._setup_tuning_logger() self.ept = ExecutionProgressTracker() @property @abstractmethod def type(self) -> str: """The type of the specialist""" pass @property @abstractmethod def type_version(self) -> str: """The type version of the specialist""" pass def _initialize_retrievers(self) -> List[BaseRetriever]: """Initialize all retrievers associated with this specialist""" retrievers = [] # Get retriever associations from database specialist_retrievers = ( SpecialistRetriever.query .filter_by(specialist_id=self.specialist_id) .all() ) self.log_tuning("_initialize_retrievers", {"Nr of retrievers": len(specialist_retrievers)}) for spec_retriever in specialist_retrievers: # Get retriever configuration from database retriever = spec_retriever.retriever retriever_class = RetrieverRegistry.get_retriever_class(retriever.type) self.log_tuning("_initialize_retrievers", { "Retriever id": spec_retriever.retriever_id, "Retriever Type": retriever.type, "Retriever Class": str(retriever_class), }) # Initialize retriever with its configuration retrievers.append( retriever_class( tenant_id=self.tenant_id, retriever_id=retriever.id, ) ) return retrievers def _setup_tuning_logger(self): try: self.tuning_logger = TuningLogger( 'tuning', tenant_id=self.tenant_id, specialist_id=self.specialist_id, ) # Verify logger is working with a test message if self.tuning: self.tuning_logger.log_tuning('specialist', "Tuning logger initialized") except Exception as e: current_app.logger.error(f"Failed to setup tuning logger: {str(e)}") raise def log_tuning(self, message: str, data: Dict[str, Any] = None) -> None: if self.tuning and self.tuning_logger: try: self.tuning_logger.log_tuning('specialist', message, data) except Exception as e: current_app.logger.error(f"Processor: Error in tuning logging: {e}") def update_progress(self, processing_type, data) -> None: self.ept.send_update(self.task_id, processing_type, data) @abstractmethod def execute(self, arguments: SpecialistArguments) -> SpecialistResult: """Execute the specialist's logic""" pass def get_specialist_class(specialist_type: str, type_version: str): major_minor = '_'.join(type_version.split('.')[:2]) module_path = f"eveai_chat_workers.specialists.{specialist_type}.{major_minor}" module = importlib.import_module(module_path) return module.SpecialistExecutor