flare / prompt_builder.py
ciyidogan's picture
Update prompt_builder.py
ec80e4b verified
raw
history blame
3.49 kB
"""
Flare – Prompt Builder (v4-fix Β· detection_prompt + examples)
==============================================================
"""
from typing import List, Dict
from datetime import datetime
def log(msg: str) -> None:
print(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}", flush=True)
# ─────────────────────────────────────────────────────────────────────────────
# INTENT PROMPT
# ─────────────────────────────────────────────────────────────────────────────
def build_intent_prompt(general_prompt: str,
conversation: List[Dict[str, str]],
user_input: str,
intents: List[Dict]) -> str:
# === INTENT INDEX ===
lines = ["### INTENT INDEX ###"]
for it in intents:
det = it.get("detection_prompt", "").strip()
det_part = f" β€’ detection_prompt β†’ β€œ{det}”" if det else ""
exs = " | ".join(it.get("examples", []))
ex_part = f" β€’ examples β†’ {exs}" if exs else ""
newline_between = "\n" if det_part and ex_part else ""
lines.append(f"{it['name']}:{det_part}{newline_between}{ex_part}")
intent_index = "\n".join(lines)
# === HISTORY ===
history_block = "\n".join(
f"{m['role'].upper()}: {m['content']}" for m in conversation[-10:]
)
prompt = (
f"{general_prompt}\n\n"
f"{intent_index}\n\n"
f"Conversation so far:\n{history_block}\n\n"
f"USER: {user_input.strip()}"
)
log("βœ… Intent prompt built (index with detection_prompt)")
return prompt
# ─────────────────────────────────────────────────────────────────────────────
# PARAMETER PROMPT (değişmedi)
# ─────────────────────────────────────────────────────────────────────────────
_FMT = (
"Return exactly ONE line in the format:\n"
"#PARAMETERS:{"
"\"extracted\":[{\"name\":\"<param>\",\"value\":\"<val>\"},...],"
"\"missing\":[\"<param>\",...]}"
)
def build_parameter_prompt(intent_cfg: Dict,
missing_params: List[str],
user_input: str,
conversation: List[Dict[str, str]]) -> str:
parts: List[str] = [
"You will extract ONLY the parameters listed below.",
"If a parameter cannot be found OR fails validation, keep it in the "
"\"missing\" list. Never guess values."
]
for p in intent_cfg["parameters"]:
if p["name"] in missing_params:
parts.append(f"* {p['name']}: {p['extraction_prompt']}")
parts.append(_FMT)
history_block = "\n".join(
f"{m['role'].upper()}: {m['content']}" for m in conversation[-10:]
)
prompt = (
"\n".join(parts) +
"\n\nConversation so far:\n" + history_block +
"\n\nUSER: " + user_input.strip()
)
log("βœ… Parameter prompt built")
return prompt