Spaces:
Building
Building
Update prompt_builder.py
Browse files- prompt_builder.py +71 -18
prompt_builder.py
CHANGED
@@ -1,25 +1,78 @@
|
|
1 |
"""
|
2 |
-
Flare – Prompt Builder
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
6 |
"""
|
7 |
|
8 |
-
from
|
9 |
-
from
|
10 |
-
from config_provider import IntentConfig, ParameterConfig
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
|
16 |
-
def build_param_extract_prompt(general: str, intent: IntentConfig, missing: List[ParameterConfig]) -> str:
|
17 |
-
names = ", ".join(p.name for p in missing)
|
18 |
-
return f"{general}\nIntent: {intent.name}\nEksik parametreleri çıkar: {names}"
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
def build_api_humanize_prompt(general: str, response_prompt: str, api_json: str) -> str:
|
25 |
-
return f"{general}\n{response_prompt}\nJSON:\n{api_json}"
|
|
|
1 |
"""
|
2 |
+
Flare – Prompt Builder (v2)
|
3 |
+
============================
|
4 |
+
• build_intent_prompt
|
5 |
+
• build_parameter_prompt
|
6 |
+
Spark (LLM) çağrılarına gidecek ‘system’ prompt’ları üretir.
|
7 |
"""
|
8 |
|
9 |
+
from typing import List, Dict
|
10 |
+
from datetime import datetime
|
|
|
11 |
|
12 |
+
# 🕒 zamanlı log
|
13 |
+
def log(msg: str) -> None:
|
14 |
+
print(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}", flush=True)
|
15 |
|
|
|
|
|
|
|
16 |
|
17 |
+
# ---------------------------------------------------------------------------
|
18 |
+
# INTENT DETECTION PROMPT
|
19 |
+
# ---------------------------------------------------------------------------
|
20 |
+
def build_intent_prompt(general_prompt: str,
|
21 |
+
conversation: List[Dict[str, str]],
|
22 |
+
user_input: str) -> str:
|
23 |
+
"""
|
24 |
+
Returns system-prompt string for LLM to detect intent.
|
25 |
+
"""
|
26 |
+
history = "\n".join(
|
27 |
+
f"{m['role'].upper()}: {m['content']}" for m in conversation[-10:]
|
28 |
+
)
|
29 |
+
prompt = (
|
30 |
+
f"{general_prompt}\n\n"
|
31 |
+
f"Conversation so far:\n{history}\n\n"
|
32 |
+
f"USER: {user_input.strip()}"
|
33 |
+
)
|
34 |
+
log("✅ Intent prompt built")
|
35 |
+
return prompt
|
36 |
+
|
37 |
+
|
38 |
+
# ---------------------------------------------------------------------------
|
39 |
+
# PARAMETER EXTRACTION PROMPT
|
40 |
+
# ---------------------------------------------------------------------------
|
41 |
+
_RESULT_SPEC = (
|
42 |
+
"Return exactly ONE line in the format:\n"
|
43 |
+
"#PARAMETERS:{\"extracted\":[{\"name\":\"<param>\",\"value\":\"<val>\"},...],"
|
44 |
+
"\"missing\":[\"<param>\",...]}"
|
45 |
+
)
|
46 |
+
|
47 |
+
def build_parameter_prompt(intent_cfg: Dict,
|
48 |
+
missing_params: List[str],
|
49 |
+
user_input: str,
|
50 |
+
conversation: List[Dict[str, str]]) -> str:
|
51 |
+
"""
|
52 |
+
intent_cfg : intent section from service_config
|
53 |
+
missing_params : list of param names still required
|
54 |
+
"""
|
55 |
+
lines = [
|
56 |
+
"You will extract ONLY the parameters listed below.",
|
57 |
+
"If a parameter cannot be found OR fails validation, keep it in the "
|
58 |
+
"\"missing\" list. Never guess values."
|
59 |
+
]
|
60 |
+
|
61 |
+
for p in intent_cfg["parameters"]:
|
62 |
+
if p["name"] in missing_params:
|
63 |
+
lines.append(f"* {p['name']}: {p['extraction_prompt']}")
|
64 |
+
|
65 |
+
lines.append(_RESULT_SPEC)
|
66 |
+
|
67 |
+
history = "\n".join(
|
68 |
+
f"{m['role'].upper()}: {m['content']}" for m in conversation[-10:]
|
69 |
+
)
|
70 |
+
|
71 |
+
prompt = (
|
72 |
+
"\n".join(lines) +
|
73 |
+
"\n\nConversation so far:\n" + history +
|
74 |
+
"\n\nUSER: " + user_input.strip()
|
75 |
+
)
|
76 |
+
log("✅ Parameter-prompt built")
|
77 |
+
return prompt
|
78 |
|
|
|
|