Spaces:
Building
Building
Update prompt_builder.py
Browse files- prompt_builder.py +78 -20
prompt_builder.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
"""
|
2 |
-
Flare β Prompt Builder (
|
3 |
-
|
4 |
β’ build_intent_prompt
|
|
|
5 |
β’ build_parameter_prompt
|
6 |
"""
|
7 |
|
@@ -9,56 +10,113 @@ from typing import List, Dict
|
|
9 |
from datetime import datetime
|
10 |
|
11 |
|
|
|
|
|
|
|
12 |
def log(msg: str) -> None:
|
13 |
print(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}", flush=True)
|
14 |
|
15 |
|
16 |
-
#
|
17 |
# INTENT DETECTION PROMPT
|
18 |
-
#
|
19 |
def build_intent_prompt(general_prompt: str,
|
20 |
conversation: List[Dict[str, str]],
|
21 |
-
user_input: str
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
f"{m['role'].upper()}: {m['content']}" for m in conversation[-10:]
|
24 |
)
|
|
|
|
|
25 |
prompt = (
|
26 |
f"{general_prompt}\n\n"
|
27 |
-
f"
|
|
|
28 |
f"USER: {user_input.strip()}"
|
29 |
)
|
30 |
-
log("β
Intent prompt built")
|
31 |
return prompt
|
32 |
|
33 |
|
34 |
-
#
|
35 |
-
# PARAMETER EXTRACTION PROMPT
|
36 |
-
#
|
37 |
-
_FMT = (
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
40 |
|
41 |
def build_parameter_prompt(intent_cfg: Dict,
|
42 |
missing_params: List[str],
|
43 |
user_input: str,
|
44 |
conversation: List[Dict[str, str]]) -> str:
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
"You will extract ONLY the parameters listed below.",
|
47 |
"If a parameter cannot be found OR fails validation, keep it in the "
|
48 |
"\"missing\" list. Never guess values."
|
49 |
]
|
|
|
|
|
50 |
for p in intent_cfg["parameters"]:
|
51 |
if p["name"] in missing_params:
|
52 |
-
|
53 |
-
|
|
|
54 |
|
55 |
-
|
56 |
f"{m['role'].upper()}: {m['content']}" for m in conversation[-10:]
|
57 |
)
|
58 |
|
59 |
prompt = (
|
60 |
-
"\n".join(
|
61 |
-
"\n\nConversation so far:\n" +
|
62 |
"\n\nUSER: " + user_input.strip()
|
63 |
)
|
64 |
log("β
Parameter prompt built")
|
|
|
1 |
"""
|
2 |
+
Flare β Prompt Builder (v4 Β· detection_prompt destekli)
|
3 |
+
========================================================
|
4 |
β’ build_intent_prompt
|
5 |
+
β Genel kurallar + INTENT INDEX (detection_prompt + examples)
|
6 |
β’ build_parameter_prompt
|
7 |
"""
|
8 |
|
|
|
10 |
from datetime import datetime
|
11 |
|
12 |
|
13 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
14 |
+
# KΓΌΓ§ΓΌk, zaman damgalΔ± log
|
15 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
16 |
def log(msg: str) -> None:
|
17 |
print(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}", flush=True)
|
18 |
|
19 |
|
20 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
21 |
# INTENT DETECTION PROMPT
|
22 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
23 |
def build_intent_prompt(general_prompt: str,
|
24 |
conversation: List[Dict[str, str]],
|
25 |
+
user_input: str,
|
26 |
+
intents: List[Dict]) -> str:
|
27 |
+
"""
|
28 |
+
Creates the system prompt for Spark /generate.
|
29 |
+
|
30 |
+
Parameters
|
31 |
+
----------
|
32 |
+
general_prompt : str
|
33 |
+
The project-level general system prompt.
|
34 |
+
conversation : List[Dict[str,str]]
|
35 |
+
Last chat turns (role/user etc.).
|
36 |
+
user_input : str
|
37 |
+
Current user message.
|
38 |
+
intents : List[Dict]
|
39 |
+
The intents list from service_config (βintentsβ section).
|
40 |
+
|
41 |
+
Returns
|
42 |
+
-------
|
43 |
+
str
|
44 |
+
System prompt passed to Spark LLM.
|
45 |
+
"""
|
46 |
+
# === 1) INTENT INDEX with detection_prompt ===
|
47 |
+
lines = ["### INTENT INDEX ###"]
|
48 |
+
for it in intents:
|
49 |
+
det = it.get("detection_prompt", "").strip()
|
50 |
+
det_part = f" β’ detection_prompt β β{det}β" if det else ""
|
51 |
+
exs = " | ".join(it.get("examples", []))
|
52 |
+
ex_part = f" β’ examples β {exs}" if exs else ""
|
53 |
+
lines.append(f"{it['name']}:{det_part}{'\\n' if det_part and ex_part else ''}{ex_part}")
|
54 |
+
intent_index = "\n".join(lines)
|
55 |
+
|
56 |
+
# === 2) HISTORY (last 10 messages) ===
|
57 |
+
history_block = "\n".join(
|
58 |
f"{m['role'].upper()}: {m['content']}" for m in conversation[-10:]
|
59 |
)
|
60 |
+
|
61 |
+
# === 3) Compose prompt ===
|
62 |
prompt = (
|
63 |
f"{general_prompt}\n\n"
|
64 |
+
f"{intent_index}\n\n"
|
65 |
+
f"Conversation so far:\n{history_block}\n\n"
|
66 |
f"USER: {user_input.strip()}"
|
67 |
)
|
68 |
+
log("β
Intent prompt built (index with detection_prompt)")
|
69 |
return prompt
|
70 |
|
71 |
|
72 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
73 |
+
# PARAMETER EXTRACTION PROMPT (deΔiΕmedi)
|
74 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
75 |
+
_FMT = (
|
76 |
+
"Return exactly ONE line in the format:\n"
|
77 |
+
"#PARAMETERS:{"
|
78 |
+
"\"extracted\":[{\"name\":\"<param>\",\"value\":\"<val>\"},...],"
|
79 |
+
"\"missing\":[\"<param>\",...]}"
|
80 |
+
)
|
81 |
|
82 |
def build_parameter_prompt(intent_cfg: Dict,
|
83 |
missing_params: List[str],
|
84 |
user_input: str,
|
85 |
conversation: List[Dict[str, str]]) -> str:
|
86 |
+
"""
|
87 |
+
Builds the parameter-extraction prompt for Spark.
|
88 |
+
|
89 |
+
Parameters
|
90 |
+
----------
|
91 |
+
intent_cfg : Dict
|
92 |
+
Single intent config dictionary.
|
93 |
+
missing_params : List[str]
|
94 |
+
Names of parameters still required.
|
95 |
+
user_input : str
|
96 |
+
Current user message.
|
97 |
+
conversation : List[Dict]
|
98 |
+
Chat history.
|
99 |
+
"""
|
100 |
+
parts: List[str] = [
|
101 |
"You will extract ONLY the parameters listed below.",
|
102 |
"If a parameter cannot be found OR fails validation, keep it in the "
|
103 |
"\"missing\" list. Never guess values."
|
104 |
]
|
105 |
+
|
106 |
+
# Parametre bazlΔ± extraction_promptβlar
|
107 |
for p in intent_cfg["parameters"]:
|
108 |
if p["name"] in missing_params:
|
109 |
+
parts.append(f"* {p['name']}: {p['extraction_prompt']}")
|
110 |
+
|
111 |
+
parts.append(_FMT)
|
112 |
|
113 |
+
history_block = "\n".join(
|
114 |
f"{m['role'].upper()}: {m['content']}" for m in conversation[-10:]
|
115 |
)
|
116 |
|
117 |
prompt = (
|
118 |
+
"\n".join(parts) +
|
119 |
+
"\n\nConversation so far:\n" + history_block +
|
120 |
"\n\nUSER: " + user_input.strip()
|
121 |
)
|
122 |
log("β
Parameter prompt built")
|