ciyidogan commited on
Commit
ec80e4b
Β·
verified Β·
1 Parent(s): 037f971

Update prompt_builder.py

Browse files
Files changed (1) hide show
  1. prompt_builder.py +10 -50
prompt_builder.py CHANGED
@@ -1,64 +1,41 @@
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
 
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 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"
@@ -70,7 +47,7 @@ def build_intent_prompt(general_prompt: str,
70
 
71
 
72
  # ─────────────────────────────────────────────────────────────────────────────
73
- # PARAMETER EXTRACTION PROMPT (değişmedi)
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(