""" Flare – Prompt Builder (Refactored with Multi-language Support) ============================================================== """ from typing import List, Dict, Any from datetime import datetime, timedelta from config_provider import ConfigProvider from utils import log from locale_manager import LocaleManager # Date helper for locale-aware date expressions def _get_date_context(locale_code: str = "tr") -> Dict[str, str]: """Generate date context for date expressions""" now = datetime.now() # Get locale data locale_data = LocaleManager.get_locale(locale_code) weekdays = locale_data.get("weekdays", ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]) today_weekday = weekdays[now.weekday()] # Calculate various dates dates = { "today": now.strftime("%Y-%m-%d"), "tomorrow": (now + timedelta(days=1)).strftime("%Y-%m-%d"), "day_after_tomorrow": (now + timedelta(days=2)).strftime("%Y-%m-%d"), "this_weekend_saturday": (now + timedelta(days=(5-now.weekday())%7)).strftime("%Y-%m-%d"), "this_weekend_sunday": (now + timedelta(days=(6-now.weekday())%7)).strftime("%Y-%m-%d"), "next_week_same_day": (now + timedelta(days=7)).strftime("%Y-%m-%d"), "two_weeks_later": (now + timedelta(days=14)).strftime("%Y-%m-%d"), "today_weekday": today_weekday, "today_day": now.day, "today_month": now.month, "today_year": now.year, "locale_code": locale_code } return dates def _build_locale_aware_date_prompt(param, date_ctx: Dict, locale_data: Dict, locale_code: str) -> str: """Build locale-aware date extraction prompt""" # Get locale-specific date patterns date_patterns = locale_data.get("date_patterns", {}) months = locale_data.get("months", []) prompt_parts = [f"• {param.name}: {param.extraction_prompt}"] prompt_parts.append(f" IMPORTANT DATE RULES for {locale_data.get('name', 'this language')}:") # Add locale-specific patterns if locale_code == "tr": prompt_parts.extend([ f" - 'bugün' = {date_ctx['today']}", f" - 'yarın' = {date_ctx['tomorrow']}", f" - 'öbür gün' = {date_ctx['day_after_tomorrow']}", f" - 'bu hafta sonu' = {date_ctx['this_weekend_saturday']} or {date_ctx['this_weekend_sunday']}", f" - 'bu cumartesi' = {date_ctx['this_weekend_saturday']}", f" - 'bu pazar' = {date_ctx['this_weekend_sunday']}", f" - 'haftaya' or 'gelecek hafta' = add 7 days to current date", f" - 'haftaya bugün' = {date_ctx['next_week_same_day']}", f" - 'iki hafta sonra' = {date_ctx['two_weeks_later']}", f" - '15 gün sonra' = add 15 days to today", f" - '10 Temmuz' = {date_ctx['today_year']}-07-10", f" - Turkish months: Ocak=01, Şubat=02, Mart=03, Nisan=04, Mayıs=05, Haziran=06, " f"Temmuz=07, Ağustos=08, Eylül=09, Ekim=10, Kasım=11, Aralık=12" ]) elif locale_code == "en": prompt_parts.extend([ f" - 'today' = {date_ctx['today']}", f" - 'tomorrow' = {date_ctx['tomorrow']}", f" - 'day after tomorrow' = {date_ctx['day_after_tomorrow']}", f" - 'this weekend' = {date_ctx['this_weekend_saturday']} or {date_ctx['this_weekend_sunday']}", f" - 'this Saturday' = {date_ctx['this_weekend_saturday']}", f" - 'this Sunday' = {date_ctx['this_weekend_sunday']}", f" - 'next week' = add 7 days to current date", f" - 'next {date_ctx['today_weekday']}' = {date_ctx['next_week_same_day']}", f" - 'in two weeks' = {date_ctx['two_weeks_later']}", f" - 'July 10' or '10th of July' = {date_ctx['today_year']}-07-10", f" - English months: January=01, February=02, March=03, April=04, May=05, June=06, " f"July=07, August=08, September=09, October=10, November=11, December=12" ]) # Add month mappings if available if months: month_mapping = [f"{month}={i+1:02d}" for i, month in enumerate(months)] prompt_parts.append(f" - Month names: {', '.join(month_mapping)}") return "\n".join(prompt_parts) # ───────────────────────────────────────────────────────────────────────────── # INTENT PROMPT # ───────────────────────────────────────────────────────────────────────────── def build_intent_prompt(general_prompt: str, conversation: List[Dict[str, str]], user_input: str, intents: List, project_name: str = None, project_locale: str = "tr") -> str: # Get config cfg = ConfigProvider.get() # Get internal prompt from LLM provider settings internal_prompt = "" if cfg.global_config.llm_provider and cfg.global_config.llm_provider.settings: internal_prompt = cfg.global_config.llm_provider.settings.get("internal_prompt", "") # Extract intent names and captions intent_names = [it.name for it in intents] intent_captions = [it.caption or it.name for it in intents] # Get project language name locale_data = LocaleManager.get_locale(project_locale) project_language = locale_data.get("name", "Turkish") # Replace placeholders in internal prompt if internal_prompt: # Intent names - quoted and comma-separated intent_names_str = ', '.join([f'"{name}"' for name in intent_names]) internal_prompt = internal_prompt.replace("", intent_names_str) # Intent captions - quoted and comma-separated intent_captions_str = ', '.join([f'"{caption}"' for caption in intent_captions]) internal_prompt = internal_prompt.replace("", intent_captions_str) # Project language internal_prompt = internal_prompt.replace("", project_language) # === INTENT INDEX === lines = ["### INTENT INDEX ###"] for it in intents: # IntentConfig object attribute access det = it.detection_prompt.strip() if it.detection_prompt else "" det_part = f' • detection_prompt → "{det}"' if det else "" # Get examples for project locale examples = it.get_examples_for_locale(project_locale) exs = " | ".join(examples) if examples else "" ex_part = f" • examples → {exs}" if exs else "" newline_between = "\n" if det_part and ex_part else "" lines.append(f"{it.name}:{det_part}{newline_between}{ex_part}") intent_index = "\n".join(lines) # === HISTORY === history_block = "\n".join( f"{m['role'].upper()}: {m['content']}" for m in conversation[-10:] ) # Combine prompts combined_prompt = internal_prompt + "\n\n" + general_prompt if internal_prompt else general_prompt prompt = ( f"{combined_prompt}\n\n" f"{intent_index}\n\n" f"Conversation so far:\n{history_block}\n\n" f"USER: {user_input.strip()}" ) log("✅ Intent prompt built (with internal prompt and locale support)") return prompt # ───────────────────────────────────────────────────────────────────────────── # PARAMETER PROMPT # ───────────────────────────────────────────────────────────────────────────── _FMT = """#PARAMETERS:{"extracted":[{"name":"","value":""},...],"missing":["",...]}""" def build_parameter_prompt(intent_cfg, missing_params: List[str], user_input: str, conversation: List[Dict[str, str]], locale_code: str = "tr") -> str: date_ctx = _get_date_context(locale_code) locale_data = LocaleManager.get_locale(locale_code) parts: List[str] = [ f"You are extracting parameters from user messages in {locale_data.get('name', 'the target language')}.", f"Today is {date_ctx['today']} ({date_ctx['today_weekday']}). Tomorrow is {date_ctx['tomorrow']}.", "Extract ONLY the parameters listed below from the conversation.", "Look at BOTH the current message AND previous messages to find parameter values.", "If a parameter cannot be found, is invalid, or wasn't provided, keep it in the \"missing\" list.", "Never guess or make up values. Only extract values explicitly given by the user.", "", "IMPORTANT: If the user is NOT providing the requested parameter but instead:", "- Asking for recommendations or advice (e.g. 'nereye gitsem?', 'önerin var mı?')", "- Expressing uncertainty (e.g. 'tam net değil', 'emin değilim', 'bilmiyorum')", "- Changing the subject or asking something else", "Then DO NOT extract any value for that parameter. Keep it in the 'missing' list.", "" ] # Add parameter descriptions parts.append("Parameters to extract:") for p in intent_cfg.parameters: if p.name in missing_params: # Get localized caption caption = p.get_caption_for_locale(locale_code) # Special handling for date type parameters if p.type == "date": date_prompt = _build_locale_aware_date_prompt( p, date_ctx, locale_data, locale_code ) parts.append(date_prompt) else: parts.append(f"• {p.name}: {p.extraction_prompt} (Display: {caption})") # Add format instruction parts.append("") parts.append("IMPORTANT: Your response must start with '#PARAMETERS:' followed by the JSON.") parts.append("Return ONLY this format with no extra text before or after:") parts.append(_FMT) # Add conversation history history_block = "\n".join( f"{m['role'].upper()}: {m['content']}" for m in conversation[-10:] ) prompt = ( "\n".join(parts) + "\n\nConversation so far:\n" + history_block + "\n\nUSER: " + user_input.strip() ) log(f"📝 Parameter prompt built for missing: {missing_params} in locale: {locale_code}") return prompt # ───────────────────────────────────────────────────────────────────────────── # SMART PARAMETER COLLECTION PROMPT # ───────────────────────────────────────────────────────────────────────────── def build_smart_parameter_question_prompt(intent_cfg, missing_params: List[str], conversation: List[Dict[str, str]], collection_config: Dict[str, Any], project_language: str) -> str: """Build prompt for smart parameter collection""" # Get the collection prompt template template = collection_config.get("collection_prompt", "Ask for the missing parameters.") # Replace placeholders prompt = template.replace("{{max_params}}", str(collection_config.get("max_params_per_question", 2))) prompt = prompt.replace("{{project_language}}", project_language) # Add parameter information param_info = [] for param_name in missing_params[:collection_config.get("max_params_per_question", 2)]: param = next((p for p in intent_cfg.parameters if p.name == param_name), None) if param: # Get caption for default locale (first supported locale) caption = param.get_caption_for_locale("tr") # Default to Turkish param_info.append(f"- {param_name}: {caption}") # Add context parts = [ prompt, "", "Missing parameters:", "\n".join(param_info), "", "Recent conversation:" ] # Add recent conversation for msg in conversation[-5:]: parts.append(f"{msg['role'].upper()}: {msg['content']}") return "\n".join(parts) # ───────────────────────────────────────────────────────────────────────────── # PARAMETER EXTRACTION FROM QUESTION # ───────────────────────────────────────────────────────────────────────────── def extract_params_from_question(question: str, param_names: List[str]) -> Dict[str, str]: """Extract parameters that might be embedded in the question itself""" extracted = {} # Simple pattern matching for common cases # This is a basic implementation - can be enhanced with NLP # Example: "İstanbul'dan Ankara'ya ne zaman gitmek istiyorsunuz?" # Could extract origin=İstanbul, destination=Ankara # For now, return empty dict # This function can be enhanced based on specific use cases return extracted # ───────────────────────────────────────────────────────────────────────────── # API RESPONSE PROMPT # ───────────────────────────────────────────────────────────────────────────── def build_api_response_prompt(api_config, api_response: Dict) -> str: """Build prompt for API response with mappings""" response_prompt = api_config.response_prompt if not response_prompt: return "İşlem başarıyla tamamlandı." # Apply response mappings if available mapped_data = {} if hasattr(api_config, 'response_mappings'): for mapping in api_config.response_mappings: field_path = mapping.get('field_path', '') display_name = mapping.get('display_name', field_path) # Extract value from response value = _extract_value_from_path(api_response, field_path) if value is not None: mapped_data[display_name] = value # Replace placeholders in response prompt for key, value in mapped_data.items(): response_prompt = response_prompt.replace(f"{{{key}}}", str(value)) # Also try direct field replacement for key, value in api_response.items(): response_prompt = response_prompt.replace(f"{{{key}}}", str(value)) return response_prompt def _extract_value_from_path(data: Dict, path: str) -> Any: """Extract value from nested dict using dot notation""" try: parts = path.split('.') value = data for part in parts: if isinstance(value, dict): value = value.get(part) elif isinstance(value, list) and part.isdigit(): value = value[int(part)] else: return None return value except: return None