File size: 2,493 Bytes
67cc066
2167be3
 
 
 
 
67cc066
 
2167be3
 
67cc066
2167be3
 
 
67cc066
 
2167be3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67cc066
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
Flare – Prompt Builder  (v2)
============================
• build_intent_prompt
• build_parameter_prompt
Spark (LLM) çağrılarına gidecek ‘system’ prompt’ları üretir.
"""

from typing import List, Dict
from datetime import datetime

# 🕒 zamanlı log
def log(msg: str) -> None:
    print(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}", flush=True)


# ---------------------------------------------------------------------------
# INTENT DETECTION PROMPT
# ---------------------------------------------------------------------------
def build_intent_prompt(general_prompt: str,
                        conversation: List[Dict[str, str]],
                        user_input: str) -> str:
    """
    Returns system-prompt string for LLM to detect intent.
    """
    history = "\n".join(
        f"{m['role'].upper()}: {m['content']}" for m in conversation[-10:]
    )
    prompt = (
        f"{general_prompt}\n\n"
        f"Conversation so far:\n{history}\n\n"
        f"USER: {user_input.strip()}"
    )
    log("✅ Intent prompt built")
    return prompt


# ---------------------------------------------------------------------------
# PARAMETER EXTRACTION PROMPT
# ---------------------------------------------------------------------------
_RESULT_SPEC = (
    "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:
    """
    intent_cfg     : intent section from service_config
    missing_params : list of param names still required
    """
    lines = [
        "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:
            lines.append(f"* {p['name']}: {p['extraction_prompt']}")

    lines.append(_RESULT_SPEC)

    history = "\n".join(
        f"{m['role'].upper()}: {m['content']}" for m in conversation[-10:]
    )

    prompt = (
        "\n".join(lines) +
        "\n\nConversation so far:\n" + history +
        "\n\nUSER: " + user_input.strip()
    )
    log("✅ Parameter-prompt built")
    return prompt