Spaces:
Paused
Paused
Update prompt_builder.py
Browse files- prompt_builder.py +10 -50
prompt_builder.py
CHANGED
|
@@ -1,64 +1,41 @@
|
|
| 1 |
"""
|
| 2 |
-
Flare β Prompt Builder (v4 Β· detection_prompt
|
| 3 |
-
|
| 4 |
-
β’ build_intent_prompt
|
| 5 |
-
β Genel kurallar + INTENT INDEX (detection_prompt + examples)
|
| 6 |
-
β’ build_parameter_prompt
|
| 7 |
"""
|
| 8 |
|
| 9 |
from typing import List, Dict
|
| 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
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 54 |
intent_index = "\n".join(lines)
|
| 55 |
|
| 56 |
-
# ===
|
| 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"
|
|
@@ -70,7 +47,7 @@ def build_intent_prompt(general_prompt: str,
|
|
| 70 |
|
| 71 |
|
| 72 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 73 |
-
# PARAMETER
|
| 74 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 75 |
_FMT = (
|
| 76 |
"Return exactly ONE line in the format:\n"
|
|
@@ -83,31 +60,14 @@ 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(
|
|
|
|
| 1 |
"""
|
| 2 |
+
Flare β Prompt Builder (v4-fix Β· detection_prompt + examples)
|
| 3 |
+
==============================================================
|
|
|
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
from typing import List, Dict
|
| 7 |
from datetime import datetime
|
| 8 |
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
def log(msg: str) -> None:
|
| 11 |
print(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}", flush=True)
|
| 12 |
|
| 13 |
|
| 14 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 15 |
+
# INTENT PROMPT
|
| 16 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 17 |
def build_intent_prompt(general_prompt: str,
|
| 18 |
conversation: List[Dict[str, str]],
|
| 19 |
user_input: str,
|
| 20 |
intents: List[Dict]) -> str:
|
| 21 |
+
# === INTENT INDEX ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
lines = ["### INTENT INDEX ###"]
|
| 23 |
for it in intents:
|
| 24 |
det = it.get("detection_prompt", "").strip()
|
| 25 |
det_part = f" β’ detection_prompt β β{det}β" if det else ""
|
| 26 |
exs = " | ".join(it.get("examples", []))
|
| 27 |
ex_part = f" β’ examples β {exs}" if exs else ""
|
| 28 |
+
|
| 29 |
+
newline_between = "\n" if det_part and ex_part else ""
|
| 30 |
+
lines.append(f"{it['name']}:{det_part}{newline_between}{ex_part}")
|
| 31 |
+
|
| 32 |
intent_index = "\n".join(lines)
|
| 33 |
|
| 34 |
+
# === HISTORY ===
|
| 35 |
history_block = "\n".join(
|
| 36 |
f"{m['role'].upper()}: {m['content']}" for m in conversation[-10:]
|
| 37 |
)
|
| 38 |
|
|
|
|
| 39 |
prompt = (
|
| 40 |
f"{general_prompt}\n\n"
|
| 41 |
f"{intent_index}\n\n"
|
|
|
|
| 47 |
|
| 48 |
|
| 49 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 50 |
+
# PARAMETER PROMPT (deΔiΕmedi)
|
| 51 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 52 |
_FMT = (
|
| 53 |
"Return exactly ONE line in the format:\n"
|
|
|
|
| 60 |
missing_params: List[str],
|
| 61 |
user_input: str,
|
| 62 |
conversation: List[Dict[str, str]]) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
parts: List[str] = [
|
| 64 |
"You will extract ONLY the parameters listed below.",
|
| 65 |
"If a parameter cannot be found OR fails validation, keep it in the "
|
| 66 |
"\"missing\" list. Never guess values."
|
| 67 |
]
|
|
|
|
|
|
|
| 68 |
for p in intent_cfg["parameters"]:
|
| 69 |
if p["name"] in missing_params:
|
| 70 |
parts.append(f"* {p['name']}: {p['extraction_prompt']}")
|
|
|
|
| 71 |
parts.append(_FMT)
|
| 72 |
|
| 73 |
history_block = "\n".join(
|