Spaces:
Building
Building
Update prompt_builder.py
Browse files- prompt_builder.py +82 -27
prompt_builder.py
CHANGED
@@ -7,15 +7,24 @@ from typing import List, Dict
|
|
7 |
from datetime import datetime, timedelta
|
8 |
from utils import log
|
9 |
from config_provider import ConfigProvider
|
|
|
10 |
|
11 |
-
# Date helper for
|
12 |
-
def _get_date_context() -> Dict[str, str]:
|
13 |
-
"""Generate date context
|
14 |
now = datetime.now()
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
# Calculate various dates
|
21 |
dates = {
|
@@ -29,12 +38,12 @@ def _get_date_context() -> Dict[str, str]:
|
|
29 |
"today_weekday": today_weekday,
|
30 |
"today_day": now.day,
|
31 |
"today_month": now.month,
|
32 |
-
"today_year": now.year
|
|
|
33 |
}
|
34 |
|
35 |
return dates
|
36 |
|
37 |
-
|
38 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
39 |
# INTENT PROMPT
|
40 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
@@ -180,12 +189,18 @@ _FMT = """#PARAMETERS:{"extracted":[{"name":"<param>","value":"<val>"},...],"mis
|
|
180 |
def build_parameter_prompt(intent_cfg,
|
181 |
missing_params: List[str],
|
182 |
user_input: str,
|
183 |
-
conversation: List[Dict[str, str]]
|
184 |
-
|
|
|
|
|
|
|
|
|
185 |
date_ctx = _get_date_context()
|
|
|
|
|
186 |
|
187 |
parts: List[str] = [
|
188 |
-
"You are extracting parameters from user messages in
|
189 |
f"Today is {date_ctx['today']} ({date_ctx['today_weekday']}). Tomorrow is {date_ctx['tomorrow']}.",
|
190 |
"Extract ONLY the parameters listed below from the conversation.",
|
191 |
"Look at BOTH the current message AND previous messages to find parameter values.",
|
@@ -206,22 +221,8 @@ def build_parameter_prompt(intent_cfg,
|
|
206 |
if p.name in missing_params:
|
207 |
# Special handling for date type parameters
|
208 |
if p.type == "date":
|
209 |
-
date_prompt = (
|
210 |
-
|
211 |
-
f" IMPORTANT DATE RULES:\n"
|
212 |
-
f" - 'bugΓΌn' = {date_ctx['today']}\n"
|
213 |
-
f" - 'yarΔ±n' = {date_ctx['tomorrow']}\n"
|
214 |
-
f" - 'ΓΆbΓΌr gΓΌn' = {date_ctx['day_after_tomorrow']}\n"
|
215 |
-
f" - 'bu hafta sonu' = {date_ctx['this_weekend_saturday']} or {date_ctx['this_weekend_sunday']}\n"
|
216 |
-
f" - 'bu cumartesi' = {date_ctx['this_weekend_saturday']}\n"
|
217 |
-
f" - 'bu pazar' = {date_ctx['this_weekend_sunday']}\n"
|
218 |
-
f" - 'haftaya' or 'gelecek hafta' = add 7 days to current date\n"
|
219 |
-
f" - 'haftaya bugΓΌn' = {date_ctx['next_week_same_day']}\n"
|
220 |
-
f" - 'iki hafta sonra' = {date_ctx['two_weeks_later']}\n"
|
221 |
-
f" - '15 gΓΌn sonra' = add 15 days to today\n"
|
222 |
-
f" - '10 Temmuz' = {date_ctx['today_year']}-07-10\n"
|
223 |
-
f" - Turkish months: Ocak=01, Εubat=02, Mart=03, Nisan=04, MayΔ±s=05, Haziran=06, "
|
224 |
-
f"Temmuz=07, AΔustos=08, EylΓΌl=09, Ekim=10, KasΔ±m=11, AralΔ±k=12"
|
225 |
)
|
226 |
parts.append(date_prompt)
|
227 |
else:
|
@@ -247,6 +248,60 @@ def build_parameter_prompt(intent_cfg,
|
|
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
|
|
|
7 |
from datetime import datetime, timedelta
|
8 |
from utils import log
|
9 |
from config_provider import ConfigProvider
|
10 |
+
from locale_manager import LocaleManager
|
11 |
|
12 |
+
# Date helper for date expressions
|
13 |
+
def _get_date_context(locale_code: str = "tr-TR") -> Dict[str, str]:
|
14 |
+
"""Generate date context based on locale"""
|
15 |
now = datetime.now()
|
16 |
|
17 |
+
# Locale verilerini yΓΌkle
|
18 |
+
locale_data = LocaleManager.get_locale(locale_code)
|
19 |
+
|
20 |
+
# Weekday names from locale
|
21 |
+
days = locale_data.get("days", {})
|
22 |
+
weekdays = [days.get(str(i), f"Day{i}") for i in range(7)]
|
23 |
+
|
24 |
+
# Monday is 0 in Python, Sunday is 6, but in locale Sunday is 0
|
25 |
+
python_weekday = now.weekday() # 0=Monday, 6=Sunday
|
26 |
+
locale_weekday = (python_weekday + 1) % 7 # Convert to locale format where 0=Sunday
|
27 |
+
today_weekday = days.get(str(locale_weekday), "")
|
28 |
|
29 |
# Calculate various dates
|
30 |
dates = {
|
|
|
38 |
"today_weekday": today_weekday,
|
39 |
"today_day": now.day,
|
40 |
"today_month": now.month,
|
41 |
+
"today_year": now.year,
|
42 |
+
"locale_code": locale_code
|
43 |
}
|
44 |
|
45 |
return dates
|
46 |
|
|
|
47 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
48 |
# INTENT PROMPT
|
49 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
189 |
def build_parameter_prompt(intent_cfg,
|
190 |
missing_params: List[str],
|
191 |
user_input: str,
|
192 |
+
conversation: List[Dict[str, str]],
|
193 |
+
locale_code: str = None) -> str:
|
194 |
+
# Intent'in locale'ini kullan, yoksa default
|
195 |
+
if not locale_code:
|
196 |
+
locale_code = getattr(intent_cfg, 'locale', 'tr-TR')
|
197 |
+
|
198 |
date_ctx = _get_date_context()
|
199 |
+
|
200 |
+
locale_data = LocaleManager.get_locale(locale_code)
|
201 |
|
202 |
parts: List[str] = [
|
203 |
+
f"You are extracting parameters from user messages in {locale_data.get('name', 'the target language')}.",
|
204 |
f"Today is {date_ctx['today']} ({date_ctx['today_weekday']}). Tomorrow is {date_ctx['tomorrow']}.",
|
205 |
"Extract ONLY the parameters listed below from the conversation.",
|
206 |
"Look at BOTH the current message AND previous messages to find parameter values.",
|
|
|
221 |
if p.name in missing_params:
|
222 |
# Special handling for date type parameters
|
223 |
if p.type == "date":
|
224 |
+
date_prompt = _build_locale_aware_date_prompt(
|
225 |
+
p, date_ctx, locale_data, locale_code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
)
|
227 |
parts.append(date_prompt)
|
228 |
else:
|
|
|
248 |
log(f"π Parameter prompt built for missing: {missing_params}")
|
249 |
return prompt
|
250 |
|
251 |
+
def _build_locale_aware_date_prompt(param, date_ctx: Dict, locale_data: Dict, locale_code: str) -> str:
|
252 |
+
"""Build date extraction prompt based on locale"""
|
253 |
+
|
254 |
+
# Locale'e ΓΆzel date expressions
|
255 |
+
date_expressions = locale_data.get("date_expressions", {})
|
256 |
+
months = locale_data.get("months", {})
|
257 |
+
|
258 |
+
prompt_parts = [
|
259 |
+
f"β’ {param.name}: {param.extraction_prompt}",
|
260 |
+
" IMPORTANT DATE RULES:"
|
261 |
+
]
|
262 |
+
|
263 |
+
# Common date expressions
|
264 |
+
if locale_code.startswith("tr"):
|
265 |
+
# Turkish specific
|
266 |
+
prompt_parts.extend([
|
267 |
+
f" - 'bugΓΌn' = {date_ctx['today']}",
|
268 |
+
f" - 'yarΔ±n' = {date_ctx['tomorrow']}",
|
269 |
+
f" - 'ΓΆbΓΌr gΓΌn' or 'ertesi gΓΌn' = {date_ctx['day_after_tomorrow']}",
|
270 |
+
f" - 'bu hafta sonu' = {date_ctx['this_weekend_saturday']} or {date_ctx['this_weekend_sunday']}",
|
271 |
+
f" - 'bu cumartesi' = {date_ctx['this_weekend_saturday']}",
|
272 |
+
f" - 'bu pazar' = {date_ctx['this_weekend_sunday']}",
|
273 |
+
f" - 'haftaya' or 'gelecek hafta' = add 7 days to current date",
|
274 |
+
f" - 'haftaya bugΓΌn' = {date_ctx['next_week_same_day']}",
|
275 |
+
f" - 'iki hafta sonra' = {date_ctx['two_weeks_later']}",
|
276 |
+
f" - 'X gΓΌn sonra' = add X days to today"
|
277 |
+
])
|
278 |
+
elif locale_code.startswith("en"):
|
279 |
+
# English specific
|
280 |
+
prompt_parts.extend([
|
281 |
+
f" - 'today' = {date_ctx['today']}",
|
282 |
+
f" - 'tomorrow' = {date_ctx['tomorrow']}",
|
283 |
+
f" - 'day after tomorrow' = {date_ctx['day_after_tomorrow']}",
|
284 |
+
f" - 'this weekend' = {date_ctx['this_weekend_saturday']} or {date_ctx['this_weekend_sunday']}",
|
285 |
+
f" - 'this Saturday' = {date_ctx['this_weekend_saturday']}",
|
286 |
+
f" - 'this Sunday' = {date_ctx['this_weekend_sunday']}",
|
287 |
+
f" - 'next week' = add 7 days to current date",
|
288 |
+
f" - 'in X days' = add X days to today"
|
289 |
+
])
|
290 |
+
# DiΔer diller iΓ§in de eklenebilir
|
291 |
+
|
292 |
+
# Month names
|
293 |
+
if months:
|
294 |
+
month_list = ", ".join([
|
295 |
+
f"{name}={num}" for num, name in sorted(months.items())
|
296 |
+
])
|
297 |
+
prompt_parts.append(f" - Month names: {month_list}")
|
298 |
+
|
299 |
+
# Date format hint
|
300 |
+
date_format = locale_data.get("date_format", "YYYY-MM-DD")
|
301 |
+
prompt_parts.append(f" - Expected date format: YYYY-MM-DD (convert from {date_format})")
|
302 |
+
|
303 |
+
return "\n".join(prompt_parts)
|
304 |
+
|
305 |
def build_smart_parameter_question_prompt(
|
306 |
collection_config, # ParameterCollectionConfig
|
307 |
intent_config, # IntentConfig
|