Spaces:
Running
Running
Update prompt_builder.py
Browse files- prompt_builder.py +77 -75
prompt_builder.py
CHANGED
|
@@ -47,7 +47,6 @@ def build_intent_prompt(general_prompt: str,
|
|
| 47 |
conversation: List[Dict[str, str]],
|
| 48 |
user_input: str,
|
| 49 |
intents: List,
|
| 50 |
-
project_name: str = None,
|
| 51 |
project_locale: str = "tr") -> str:
|
| 52 |
|
| 53 |
# Get config when needed
|
|
@@ -62,9 +61,10 @@ def build_intent_prompt(general_prompt: str,
|
|
| 62 |
intent_names = [it.name for it in intents]
|
| 63 |
intent_captions = [it.caption or it.name for it in intents]
|
| 64 |
|
| 65 |
-
# Get
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
# Replace placeholders in internal prompt
|
| 70 |
if internal_prompt:
|
|
@@ -82,16 +82,14 @@ def build_intent_prompt(general_prompt: str,
|
|
| 82 |
# === INTENT INDEX ===
|
| 83 |
lines = ["### INTENT INDEX ###"]
|
| 84 |
for it in intents:
|
| 85 |
-
#
|
| 86 |
-
locale_examples = it.get_examples_for_locale(project_locale)
|
| 87 |
-
|
| 88 |
det = it.detection_prompt.strip() if it.detection_prompt else ""
|
| 89 |
det_part = f' β’ detection_prompt β "{det}"' if det else ""
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
|
| 96 |
newline_between = "\n" if det_part and ex_part else ""
|
| 97 |
lines.append(f"{it.name}:{det_part}{newline_between}{ex_part}")
|
|
@@ -124,15 +122,18 @@ def build_parameter_prompt(intent_cfg,
|
|
| 124 |
missing_params: List[str],
|
| 125 |
user_input: str,
|
| 126 |
conversation: List[Dict[str, str]],
|
| 127 |
-
locale_code: str = None
|
| 128 |
-
|
| 129 |
# Use project locale if not specified
|
| 130 |
if not locale_code:
|
| 131 |
-
locale_code =
|
| 132 |
-
|
| 133 |
-
|
|
|
|
| 134 |
locale_data = LocaleManager.get_locale(locale_code)
|
| 135 |
|
|
|
|
|
|
|
| 136 |
parts: List[str] = [
|
| 137 |
f"You are extracting parameters from user messages in {locale_data.get('name', 'the target language')}.",
|
| 138 |
f"Today is {date_ctx['today']} ({date_ctx['today_weekday']}). Tomorrow is {date_ctx['tomorrow']}.",
|
|
@@ -149,12 +150,12 @@ def build_parameter_prompt(intent_cfg,
|
|
| 149 |
""
|
| 150 |
]
|
| 151 |
|
| 152 |
-
# Add parameter descriptions
|
| 153 |
parts.append("Parameters to extract:")
|
| 154 |
for p in intent_cfg.parameters:
|
| 155 |
if p.name in missing_params:
|
| 156 |
-
# Get
|
| 157 |
-
caption = p.get_caption_for_locale(locale_code
|
| 158 |
|
| 159 |
# Special handling for date type parameters
|
| 160 |
if p.type == "date":
|
|
@@ -163,25 +164,27 @@ def build_parameter_prompt(intent_cfg,
|
|
| 163 |
)
|
| 164 |
parts.append(date_prompt)
|
| 165 |
else:
|
| 166 |
-
|
| 167 |
-
parts.append(f"
|
| 168 |
|
| 169 |
-
#
|
| 170 |
parts.append("")
|
| 171 |
parts.append("IMPORTANT: Your response must start with '#PARAMETERS:' followed by the JSON.")
|
| 172 |
-
parts.append(
|
| 173 |
-
parts.append(
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
for msg in conversation[-5:]:
|
| 179 |
-
parts.append(f"{msg['role'].upper()}: {msg['content']}")
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
|
|
|
|
| 185 |
|
| 186 |
def _build_locale_aware_date_prompt(param, date_ctx: Dict, locale_data: Dict, locale_code: str) -> str:
|
| 187 |
"""Build date extraction prompt with locale awareness"""
|
|
@@ -210,67 +213,66 @@ def _build_locale_aware_date_prompt(param, date_ctx: Dict, locale_data: Dict, lo
|
|
| 210 |
# SMART PARAMETER QUESTION
|
| 211 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 212 |
def build_smart_parameter_question_prompt(
|
|
|
|
| 213 |
intent_config,
|
| 214 |
missing_params: List[str],
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
unanswered_params: List[str] = None
|
| 219 |
) -> str:
|
| 220 |
"""Build prompt for smart parameter collection"""
|
| 221 |
|
| 222 |
-
cfg = ConfigProvider.get()
|
| 223 |
-
|
| 224 |
# Get parameter collection config from LLM provider settings
|
| 225 |
-
|
| 226 |
if cfg.global_config.llm_provider and cfg.global_config.llm_provider.settings:
|
| 227 |
-
collection_config = cfg.global_config.llm_provider.settings.get("parameter_collection_config",
|
| 228 |
|
| 229 |
-
#
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
Intent: {{intent_name}} - {{intent_caption}}
|
| 234 |
-
Still needed: {{missing_params}}
|
| 235 |
-
|
| 236 |
-
Ask for the missing parameters in a natural, conversational way in {{project_language}}.
|
| 237 |
-
Generate ONLY the question, nothing else.
|
| 238 |
-
""")
|
| 239 |
|
| 240 |
# Get locale info
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
for param_name in missing_params:
|
| 247 |
param = next((p for p in intent_config.parameters if p.name == param_name), None)
|
| 248 |
if param:
|
| 249 |
-
caption = param.get_caption_for_locale(
|
| 250 |
-
|
|
|
|
| 251 |
|
| 252 |
-
#
|
| 253 |
-
|
| 254 |
-
for param_name, value in collected_params.items():
|
| 255 |
-
param = next((p for p in intent_config.parameters if p.name == param_name), None)
|
| 256 |
-
if param:
|
| 257 |
-
caption = param.get_caption_for_locale(project_locale)
|
| 258 |
-
collected_descriptions.append(f"{param_name} ({caption}): {value}")
|
| 259 |
-
|
| 260 |
-
# Build conversation history
|
| 261 |
-
conv_history = "\n".join([f"{msg['role']}: {msg['content']}" for msg in conversation[-5:]])
|
| 262 |
-
|
| 263 |
-
# Replace placeholders
|
| 264 |
-
prompt = collection_prompt
|
| 265 |
-
prompt = prompt.replace("{{conversation_history}}", conv_history)
|
| 266 |
prompt = prompt.replace("{{intent_name}}", intent_config.name)
|
| 267 |
-
prompt = prompt.replace("{{intent_caption}}", intent_config.caption
|
| 268 |
-
prompt = prompt.replace("{{collected_params}}",
|
| 269 |
-
prompt = prompt.replace("{{missing_params}}",
|
| 270 |
-
prompt = prompt.replace("{{unanswered_params}}", ", ".join(unanswered_params) if unanswered_params else "None")
|
| 271 |
prompt = prompt.replace("{{max_params}}", str(collection_config.get("max_params_per_question", 2)))
|
| 272 |
prompt = prompt.replace("{{project_language}}", project_language)
|
| 273 |
|
|
|
|
| 274 |
return prompt
|
| 275 |
|
| 276 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 47 |
conversation: List[Dict[str, str]],
|
| 48 |
user_input: str,
|
| 49 |
intents: List,
|
|
|
|
| 50 |
project_locale: str = "tr") -> str:
|
| 51 |
|
| 52 |
# Get config when needed
|
|
|
|
| 61 |
intent_names = [it.name for it in intents]
|
| 62 |
intent_captions = [it.caption or it.name for it in intents]
|
| 63 |
|
| 64 |
+
# Get locale info
|
| 65 |
+
from locale_manager import LocaleManager
|
| 66 |
+
locale_data = LocaleManager.get_locale(project_locale)
|
| 67 |
+
project_language = locale_data.get("name", "Turkish") if locale_data else "Turkish"
|
| 68 |
|
| 69 |
# Replace placeholders in internal prompt
|
| 70 |
if internal_prompt:
|
|
|
|
| 82 |
# === INTENT INDEX ===
|
| 83 |
lines = ["### INTENT INDEX ###"]
|
| 84 |
for it in intents:
|
| 85 |
+
# IntentConfig object attribute access
|
|
|
|
|
|
|
| 86 |
det = it.detection_prompt.strip() if it.detection_prompt else ""
|
| 87 |
det_part = f' β’ detection_prompt β "{det}"' if det else ""
|
| 88 |
|
| 89 |
+
# Get examples for project locale
|
| 90 |
+
examples = it.get_examples_for_locale(project_locale)
|
| 91 |
+
exs = " | ".join(examples) if examples else ""
|
| 92 |
+
ex_part = f" β’ examples β {exs}" if exs else ""
|
| 93 |
|
| 94 |
newline_between = "\n" if det_part and ex_part else ""
|
| 95 |
lines.append(f"{it.name}:{det_part}{newline_between}{ex_part}")
|
|
|
|
| 122 |
missing_params: List[str],
|
| 123 |
user_input: str,
|
| 124 |
conversation: List[Dict[str, str]],
|
| 125 |
+
locale_code: str = None) -> str:
|
| 126 |
+
|
| 127 |
# Use project locale if not specified
|
| 128 |
if not locale_code:
|
| 129 |
+
locale_code = "tr" # Default
|
| 130 |
+
|
| 131 |
+
# Get locale info
|
| 132 |
+
from locale_manager import LocaleManager
|
| 133 |
locale_data = LocaleManager.get_locale(locale_code)
|
| 134 |
|
| 135 |
+
date_ctx = _get_date_context()
|
| 136 |
+
|
| 137 |
parts: List[str] = [
|
| 138 |
f"You are extracting parameters from user messages in {locale_data.get('name', 'the target language')}.",
|
| 139 |
f"Today is {date_ctx['today']} ({date_ctx['today_weekday']}). Tomorrow is {date_ctx['tomorrow']}.",
|
|
|
|
| 150 |
""
|
| 151 |
]
|
| 152 |
|
| 153 |
+
# Add parameter descriptions
|
| 154 |
parts.append("Parameters to extract:")
|
| 155 |
for p in intent_cfg.parameters:
|
| 156 |
if p.name in missing_params:
|
| 157 |
+
# Get caption for locale
|
| 158 |
+
caption = p.get_caption_for_locale(locale_code)
|
| 159 |
|
| 160 |
# Special handling for date type parameters
|
| 161 |
if p.type == "date":
|
|
|
|
| 164 |
)
|
| 165 |
parts.append(date_prompt)
|
| 166 |
else:
|
| 167 |
+
parts.append(f"β’ {p.name}: {p.extraction_prompt}")
|
| 168 |
+
parts.append(f" Caption: {caption}")
|
| 169 |
|
| 170 |
+
# Rest of the function remains the same...
|
| 171 |
parts.append("")
|
| 172 |
parts.append("IMPORTANT: Your response must start with '#PARAMETERS:' followed by the JSON.")
|
| 173 |
+
parts.append("Return ONLY this format with no extra text before or after:")
|
| 174 |
+
parts.append(_FMT)
|
| 175 |
|
| 176 |
+
history_block = "\n".join(
|
| 177 |
+
f"{m['role'].upper()}: {m['content']}" for m in conversation[-10:]
|
| 178 |
+
)
|
|
|
|
|
|
|
| 179 |
|
| 180 |
+
prompt = (
|
| 181 |
+
"\n".join(parts) +
|
| 182 |
+
"\n\nConversation so far:\n" + history_block +
|
| 183 |
+
"\n\nUSER: " + user_input.strip()
|
| 184 |
+
)
|
| 185 |
|
| 186 |
+
log(f"π Parameter prompt built for missing: {missing_params}")
|
| 187 |
+
return prompt
|
| 188 |
|
| 189 |
def _build_locale_aware_date_prompt(param, date_ctx: Dict, locale_data: Dict, locale_code: str) -> str:
|
| 190 |
"""Build date extraction prompt with locale awareness"""
|
|
|
|
| 213 |
# SMART PARAMETER QUESTION
|
| 214 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 215 |
def build_smart_parameter_question_prompt(
|
| 216 |
+
collection_config,
|
| 217 |
intent_config,
|
| 218 |
missing_params: List[str],
|
| 219 |
+
session,
|
| 220 |
+
project_language: str = "Turkish",
|
| 221 |
+
locale_code: str = None
|
|
|
|
| 222 |
) -> str:
|
| 223 |
"""Build prompt for smart parameter collection"""
|
| 224 |
|
|
|
|
|
|
|
| 225 |
# Get parameter collection config from LLM provider settings
|
| 226 |
+
cfg = ConfigProvider.get()
|
| 227 |
if cfg.global_config.llm_provider and cfg.global_config.llm_provider.settings:
|
| 228 |
+
collection_config = cfg.global_config.llm_provider.settings.get("parameter_collection_config", collection_config)
|
| 229 |
|
| 230 |
+
# Use locale if specified
|
| 231 |
+
if not locale_code:
|
| 232 |
+
locale_code = "tr"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
# Get locale info
|
| 235 |
+
from locale_manager import LocaleManager
|
| 236 |
+
locale_data = LocaleManager.get_locale(locale_code)
|
| 237 |
+
if locale_data:
|
| 238 |
+
project_language = locale_data.get("name", project_language)
|
| 239 |
+
|
| 240 |
+
# Rest of the function implementation...
|
| 241 |
+
template = collection_config.get("collection_prompt", "")
|
| 242 |
+
|
| 243 |
+
# Format conversation history
|
| 244 |
+
conversation_history = _format_conversation_history(session.chat_history)
|
| 245 |
+
|
| 246 |
+
# Format collected parameters with locale-aware captions
|
| 247 |
+
collected_params = ""
|
| 248 |
+
if session.variables:
|
| 249 |
+
params_list = []
|
| 250 |
+
for param_name, value in session.variables.items():
|
| 251 |
+
param = next((p for p in intent_config.parameters if p.name == param_name), None)
|
| 252 |
+
if param:
|
| 253 |
+
caption = param.get_caption_for_locale(locale_code)
|
| 254 |
+
params_list.append(f"- {caption}: {value}")
|
| 255 |
+
collected_params = "\n".join(params_list)
|
| 256 |
+
|
| 257 |
+
# Format missing parameters with locale-aware captions
|
| 258 |
+
missing_params_list = []
|
| 259 |
for param_name in missing_params:
|
| 260 |
param = next((p for p in intent_config.parameters if p.name == param_name), None)
|
| 261 |
if param:
|
| 262 |
+
caption = param.get_caption_for_locale(locale_code)
|
| 263 |
+
missing_params_list.append(f"- {caption} ({param.name})")
|
| 264 |
+
missing_params_str = "\n".join(missing_params_list)
|
| 265 |
|
| 266 |
+
# Rest of template replacement...
|
| 267 |
+
prompt = template.replace("{{conversation_history}}", conversation_history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
prompt = prompt.replace("{{intent_name}}", intent_config.name)
|
| 269 |
+
prompt = prompt.replace("{{intent_caption}}", intent_config.caption)
|
| 270 |
+
prompt = prompt.replace("{{collected_params}}", collected_params)
|
| 271 |
+
prompt = prompt.replace("{{missing_params}}", missing_params_str)
|
|
|
|
| 272 |
prompt = prompt.replace("{{max_params}}", str(collection_config.get("max_params_per_question", 2)))
|
| 273 |
prompt = prompt.replace("{{project_language}}", project_language)
|
| 274 |
|
| 275 |
+
log(f"π Smart parameter question prompt built for {len(missing_params)} params in {locale_code}")
|
| 276 |
return prompt
|
| 277 |
|
| 278 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|