Spaces:
Running
Running
Update prompt_builder.py
Browse files- prompt_builder.py +142 -1
prompt_builder.py
CHANGED
@@ -171,6 +171,7 @@ def extract_value_from_json_path(data: Dict, path: str):
|
|
171 |
return value
|
172 |
except:
|
173 |
return None
|
|
|
174 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
175 |
# PARAMETER PROMPT
|
176 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
@@ -244,4 +245,144 @@ def build_parameter_prompt(intent_cfg,
|
|
244 |
)
|
245 |
|
246 |
log(f"π Parameter prompt built for missing: {missing_params}")
|
247 |
-
return prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
return value
|
172 |
except:
|
173 |
return None
|
174 |
+
|
175 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
176 |
# PARAMETER PROMPT
|
177 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
245 |
)
|
246 |
|
247 |
log(f"π Parameter prompt built for missing: {missing_params}")
|
248 |
+
return prompt
|
249 |
+
|
250 |
+
def build_smart_parameter_question_prompt(
|
251 |
+
collection_config, # ParameterCollectionConfig
|
252 |
+
intent_config, # IntentConfig
|
253 |
+
missing_params: List[str],
|
254 |
+
session, # Session object
|
255 |
+
project_language: str = "Turkish"
|
256 |
+
) -> str:
|
257 |
+
"""AkΔ±llΔ± parametre sorusu ΓΌretmek iΓ§in prompt oluΕtur"""
|
258 |
+
|
259 |
+
# Config'den template'i al
|
260 |
+
template = collection_config.collection_prompt
|
261 |
+
|
262 |
+
# Conversation history'yi formatla
|
263 |
+
conversation_history = _format_conversation_history(session.chat_history)
|
264 |
+
|
265 |
+
# Toplanan parametreleri formatla
|
266 |
+
collected_params = _format_collected_params(session.variables, intent_config)
|
267 |
+
|
268 |
+
# Eksik parametreleri formatla
|
269 |
+
missing_params_str = _format_missing_params(missing_params, intent_config)
|
270 |
+
|
271 |
+
# Cevaplanmayan parametreleri formatla
|
272 |
+
unanswered_params_str = _format_unanswered_params(
|
273 |
+
session.unanswered_parameters,
|
274 |
+
intent_config,
|
275 |
+
session.asked_parameters
|
276 |
+
)
|
277 |
+
|
278 |
+
# KaΓ§ parametre sorulacaΔΔ±nΔ± belirle
|
279 |
+
params_to_ask_count = min(
|
280 |
+
len(missing_params),
|
281 |
+
collection_config.max_params_per_question
|
282 |
+
)
|
283 |
+
|
284 |
+
# Template'i doldur
|
285 |
+
prompt = template.replace("{{conversation_history}}", conversation_history)
|
286 |
+
prompt = prompt.replace("{{intent_name}}", intent_config.name)
|
287 |
+
prompt = prompt.replace("{{intent_caption}}", intent_config.caption)
|
288 |
+
prompt = prompt.replace("{{collected_params}}", collected_params)
|
289 |
+
prompt = prompt.replace("{{missing_params}}", missing_params_str)
|
290 |
+
prompt = prompt.replace("{{unanswered_params}}", unanswered_params_str)
|
291 |
+
prompt = prompt.replace("{{max_params}}", str(params_to_ask_count))
|
292 |
+
prompt = prompt.replace("{{project_language}}", project_language)
|
293 |
+
|
294 |
+
log(f"π Smart parameter question prompt built for {params_to_ask_count} params")
|
295 |
+
return prompt
|
296 |
+
|
297 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
298 |
+
# HELPER FUNCTIONS FOR SMART PARAMETER COLLECTION
|
299 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
300 |
+
def _format_conversation_history(chat_history: List[Dict[str, str]]) -> str:
|
301 |
+
"""Format conversation history for prompt"""
|
302 |
+
# Son 5 mesajΔ± al
|
303 |
+
recent_history = chat_history[-5:] if len(chat_history) > 5 else chat_history
|
304 |
+
|
305 |
+
formatted = []
|
306 |
+
for msg in recent_history:
|
307 |
+
role = "User" if msg["role"] == "user" else "Assistant"
|
308 |
+
formatted.append(f"{role}: {msg['content']}")
|
309 |
+
|
310 |
+
return "\n".join(formatted) if formatted else "No previous conversation"
|
311 |
+
|
312 |
+
|
313 |
+
def _format_collected_params(variables: Dict[str, str], intent_config) -> str:
|
314 |
+
"""Format collected parameters"""
|
315 |
+
collected = []
|
316 |
+
|
317 |
+
for param in intent_config.parameters:
|
318 |
+
if param.variable_name in variables:
|
319 |
+
value = variables[param.variable_name]
|
320 |
+
collected.append(f"- {param.caption}: {value}")
|
321 |
+
|
322 |
+
return "\n".join(collected) if collected else "None collected yet"
|
323 |
+
|
324 |
+
|
325 |
+
def _format_missing_params(missing_params: List[str], intent_config) -> str:
|
326 |
+
"""Format missing parameters with their captions"""
|
327 |
+
missing = []
|
328 |
+
|
329 |
+
for param_name in missing_params:
|
330 |
+
param = next((p for p in intent_config.parameters if p.name == param_name), None)
|
331 |
+
if param:
|
332 |
+
missing.append(f"- {param.caption} ({param.name})")
|
333 |
+
|
334 |
+
return "\n".join(missing) if missing else "None"
|
335 |
+
|
336 |
+
|
337 |
+
def _format_unanswered_params(unanswered_params: List[str], intent_config, asked_params: Dict[str, int]) -> str:
|
338 |
+
"""Format unanswered parameters with ask count"""
|
339 |
+
unanswered = []
|
340 |
+
|
341 |
+
for param_name in unanswered_params:
|
342 |
+
param = next((p for p in intent_config.parameters if p.name == param_name), None)
|
343 |
+
if param:
|
344 |
+
ask_count = asked_params.get(param_name, 0)
|
345 |
+
unanswered.append(f"- {param.caption} (asked {ask_count} time{'s' if ask_count > 1 else ''})")
|
346 |
+
|
347 |
+
return "\n".join(unanswered) if unanswered else "None"
|
348 |
+
|
349 |
+
|
350 |
+
def extract_params_from_question(question: str, all_params: List[str], intent_config) -> List[str]:
|
351 |
+
"""
|
352 |
+
Sorulan sorudan hangi parametrelerin sorulduΔunu tahmin et
|
353 |
+
(LLM'in hangi parametreleri sorduΔunu anlamak iΓ§in basit bir yaklaΕΔ±m)
|
354 |
+
"""
|
355 |
+
found_params = []
|
356 |
+
question_lower = question.lower()
|
357 |
+
|
358 |
+
for param_name in all_params:
|
359 |
+
param = next((p for p in intent_config.parameters if p.name == param_name), None)
|
360 |
+
if param:
|
361 |
+
# Parametre caption'Δ±nΔ±n veya keywords'ΓΌnΓΌn soruda geΓ§ip geΓ§mediΔini kontrol et
|
362 |
+
caption_words = param.caption.lower().split()
|
363 |
+
|
364 |
+
# Caption kelimelerinden herhangi biri soruda geΓ§iyorsa
|
365 |
+
if any(word in question_lower for word in caption_words if len(word) > 3):
|
366 |
+
found_params.append(param_name)
|
367 |
+
continue
|
368 |
+
|
369 |
+
# Parametre tipine gΓΆre ΓΆzel kontroller
|
370 |
+
if param.type == "date":
|
371 |
+
date_keywords = ["tarih", "zaman", "gΓΌn", "hafta", "ay", "yΔ±l", "ne zaman", "hangi gΓΌn"]
|
372 |
+
if any(keyword in question_lower for keyword in date_keywords):
|
373 |
+
found_params.append(param_name)
|
374 |
+
elif param.type == "city":
|
375 |
+
city_keywords = ["Εehir", "nereden", "nereye", "hangi il", "il", "yer"]
|
376 |
+
if any(keyword in question_lower for keyword in city_keywords):
|
377 |
+
found_params.append(param_name)
|
378 |
+
elif param.type == "number":
|
379 |
+
number_keywords = ["kaΓ§", "sayΔ±", "miktar", "adet", "kiΕi", "yolcu"]
|
380 |
+
if any(keyword in question_lower for keyword in number_keywords):
|
381 |
+
found_params.append(param_name)
|
382 |
+
|
383 |
+
# EΔer hiΓ§bir parametre bulunamadΔ±ysa, en az ΓΆncelikli olanΔ± dΓΆndΓΌr
|
384 |
+
if not found_params and all_params:
|
385 |
+
found_params = [all_params[0]]
|
386 |
+
|
387 |
+
log(f"π Extracted params from question: {found_params}")
|
388 |
+
return found_params
|