-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclarifier.py
More file actions
68 lines (58 loc) · 2.4 KB
/
Copy pathclarifier.py
File metadata and controls
68 lines (58 loc) · 2.4 KB
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
import json
from openai import OpenAI
from models import SQL_INVESTIGATION
CLARIFIER_SYSTEM_PROMPT = """
You decide whether clarification is required BEFORE analysis.
Inputs:
- user_question
- detected_intents
- dataset_schema (may be null)
Rules:
- If SQL_INVESTIGATION is present and dataset_schema is null, MUST require clarification.
- If time windows are vague ("last quarter", "recent", "this month"), ask for exact definition.
- If key metrics are undefined ("churn", "engagement", "conversion"), ask how they are defined.
Output STRICT JSON only:
{
"needs_clarification": true/false,
"questions": ["...", "..."]
}
No markdown. No extra text.
""".strip()
def get_clarification(client: OpenAI, question: str, intents: list[str], schema):
payload = {
"user_question": question,
"detected_intents": intents,
"dataset_schema": schema,
}
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": CLARIFIER_SYSTEM_PROMPT},
{"role": "user", "content": json.dumps(payload)},
],
temperature=0,
)
raw = (resp.choices[0].message.content or "").strip()
try:
result = json.loads(raw)
needs = bool(result.get("needs_clarification", False))
questions = result.get("questions", [])
if not isinstance(questions, list):
questions = []
# enforce server-side rule too
if (SQL_INVESTIGATION in intents) and (schema is None):
needs = True
if not questions:
questions = [
"You asked for SQL—please upload a CSV dataset (or provide the schema: tables + columns) so I can generate grounded SQL."
]
return {"needs_clarification": needs, "questions": questions[:3]}
except Exception:
fallback = []
if (SQL_INVESTIGATION in intents) and (schema is None):
fallback.append("You asked for SQL—please upload a CSV dataset (or provide tables + columns) so I can generate grounded SQL.")
fallback.extend([
"How is the key metric defined in your context (e.g., churn/engagement/conversion)—which column or rule should be used?",
"What exact time window should I use (exact start/end dates or calendar vs fiscal definition)?",
])
return {"needs_clarification": True, "questions": fallback[:3]}