Spaces:
Building
Building
File size: 16,186 Bytes
67cc066 7798bfb 67cc066 7798bfb ccdbef1 e1ef72d 7798bfb 67cc066 7798bfb e1ef72d 7798bfb a22ac01 7798bfb e1ef72d 7798bfb a22ac01 7798bfb 3bd94e0 7798bfb 3bd94e0 7798bfb a22ac01 3bd94e0 7798bfb 037f971 ec80e4b 037f971 2167be3 037f971 7285c41 7798bfb e1ef72d e47265d 7798bfb e47265d 847112a e1ef72d 847112a 7798bfb c22dfdb 7798bfb 7285c41 847112a e1ef72d 7285c41 e1ef72d 7285c41 ec80e4b 037f971 c22dfdb 724a384 e7addaf e1ef72d c22dfdb ec80e4b 724a384 ec80e4b 037f971 ec80e4b 037f971 2167be3 037f971 847112a 2167be3 847112a 037f971 2167be3 7798bfb 2167be3 add2298 037f971 c31edec 2167be3 724a384 2167be3 a22ac01 7798bfb c22dfdb 7798bfb a22ac01 3bd94e0 037f971 a22ac01 3bd94e0 c31edec 6506311 c31edec dbe0aa3 c31edec 2167be3 6506311 c22dfdb c31edec 724a384 7798bfb c22dfdb e1ef72d 3bd94e0 a22ac01 3bd94e0 7798bfb 6506311 7798bfb c31edec c22dfdb 6506311 7798bfb c22dfdb 6506311 c22dfdb 6506311 7798bfb c22dfdb 060c625 7798bfb e1ef72d 7798bfb e1ef72d 7798bfb e1ef72d 7798bfb a22ac01 7798bfb a22ac01 e1ef72d 7798bfb e1ef72d 7798bfb 060c625 7798bfb 060c625 7798bfb 060c625 7798bfb 060c625 7798bfb 060c625 e1ef72d 7798bfb 060c625 e1ef72d 7798bfb 060c625 7798bfb 060c625 7798bfb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
"""
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>", 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>", intent_captions_str)
# Project language
internal_prompt = internal_prompt.replace("<project language>", 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":"<param>","value":"<val>"},...],"missing":["<param>",...]}"""
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 |