""" Multi-language Prompt Loader Loads system prompts for different agents in different languages. """ import yaml from pathlib import Path from typing import Optional from utils.logger import get_logger logger = get_logger(__name__) # Base directory for prompt templates PROMPTS_DIR = Path(__file__).parent # Supported locales SUPPORTED_LOCALES = ["en", "nl", "de", "es", "fr", "it", "tr"] # Default locale DEFAULT_LOCALE = "en" class PromptLoader: """Load and cache prompt templates for different languages""" def __init__(self): self._cache = {} def get_prompt(self, agent_type: str, locale: str) -> str: """Get system prompt for agent type and locale Args: agent_type: Type of agent (customer_service, aftersale, order, product, router) locale: Language locale (en, nl, de, etc.) Returns: System prompt string """ # Validate locale if locale not in SUPPORTED_LOCALES: logger.warning( "Unsupported locale, using default", requested_locale=locale, default_locale=DEFAULT_LOCALE ) locale = DEFAULT_LOCALE # Check cache cache_key = f"{agent_type}:{locale}" if cache_key in self._cache: return self._cache[cache_key] # Load prompt file prompt_file = PROMPTS_DIR / agent_type / f"{locale}.yaml" if not prompt_file.exists(): logger.warning( "Prompt file not found, using default", agent_type=agent_type, locale=locale, file=str(prompt_file) ) # Fallback to English prompt_file = PROMPTS_DIR / agent_type / f"{DEFAULT_LOCALE}.yaml" if not prompt_file.exists(): # Fallback to hardcoded English prompt logger.error("No prompt file found, using fallback", agent_type=agent_type) return self._get_fallback_prompt(agent_type) # Load and parse YAML try: with open(prompt_file, 'r', encoding='utf-8') as f: data = yaml.safe_load(f) prompt = data.get('system_prompt', '') self._cache[cache_key] = prompt return prompt except Exception as e: logger.error("Failed to load prompt file", file=str(prompt_file), error=str(e)) return self._get_fallback_prompt(agent_type) def _get_fallback_prompt(self, agent_type: str) -> str: """Get fallback prompt if file loading fails""" fallbacks = { "customer_service": """You are a professional B2B customer service assistant. Help users with their questions.""", "aftersale": """You are a professional B2B aftersale service assistant. Help users with returns and exchanges.""", "order": """You are a professional B2B order assistant. Help users with order inquiries.""", "product": """You are a professional B2B product assistant. Help users find products.""", "router": """You are an AI assistant that routes user messages to appropriate agents.""" } return fallbacks.get(agent_type, "You are a helpful AI assistant.") # Global loader instance _loader = PromptLoader() def get_prompt(agent_type: str, locale: str) -> str: """Get system prompt for agent type and locale Args: agent_type: Type of agent (customer_service, aftersale, order, product, router) locale: Language locale (en, nl, de, etc.) Returns: System prompt string """ return _loader.get_prompt(agent_type, locale)