-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
306 lines (278 loc) · 11.2 KB
/
app.py
File metadata and controls
306 lines (278 loc) · 11.2 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import time
import json
import argparse
from src.agents.retriever import retrieve, load_policies
from src.agents.generator import generate_answer
from src.agents.verifier import verify
from src.agents.safety import audit_log, redact_pii
from src.agents.query_understanding import extract_entities
from src.agents.translation import detect_language, translate_text
from src.agents.country_detection import (
detect_supported_countries,
detect_unsupported_country_mentions,
)
def _extract_countries():
countries = set()
for policy in load_policies():
country = policy.get("country")
if country:
countries.add(country.lower())
return countries
def _print_block(title, lines):
print(f"\n== {title} ==")
if not lines:
print("None")
return
for line in lines:
print(line)
def run_query(query):
original_query = query
lang = detect_language(original_query)
query_for_processing = original_query
if lang != "en":
query_for_processing = translate_text(original_query, "en", source_lang=lang)
countries = _extract_countries()
detected = detect_supported_countries(query_for_processing, countries)
unsupported_mentions = detect_unsupported_country_mentions(original_query, countries)
if lang != "en":
unsupported_mentions.extend(
detect_unsupported_country_mentions(query_for_processing, countries)
)
unsupported_mentions = list(dict.fromkeys(unsupported_mentions))
if len(detected) == 0:
if unsupported_mentions:
unsupported_display = ", ".join(unsupported_mentions)
supported_display = ", ".join(sorted(c.title() for c in countries))
final_answer = (
f"Country context detected ({unsupported_display}), but policy coverage "
"for that country is not available in this dataset."
)
reason = "Country is provided, but no policy documents exist for it."
follow_up = (
f"Please add policy documents for {unsupported_display}, or ask about a "
f"supported country: {supported_display}."
)
event_name = "clarify_country_unsupported"
event_payload = {
"query": redact_pii(original_query),
"mentioned_countries": unsupported_mentions,
"lang": lang,
}
else:
final_answer = "Unable to answer without country context."
reason = "Country is missing."
follow_up = "Which country does this question apply to?"
event_name = "clarify_country_missing"
event_payload = {"query": redact_pii(original_query), "lang": lang}
if lang != "en":
final_answer = translate_text(final_answer, lang, source_lang="en")
reason = translate_text(reason, lang, source_lang="en")
follow_up = translate_text(follow_up, lang, source_lang="en")
_print_block("Final Answer", [final_answer])
_print_block("Citations", [])
_print_block("Confidence", ["Low"])
_print_block("Reason", [reason])
_print_block("Escalation", ["Ask for clarification"])
_print_block("Follow-up Questions", [follow_up])
audit_log(event_name, event_payload)
return
if len(detected) > 1:
final_answer = "Unable to answer without a single country context."
reason = "Multiple countries detected."
follow_up = f"Multiple countries detected ({', '.join(detected)}). Which one applies?"
if lang != "en":
final_answer = translate_text(final_answer, lang, source_lang="en")
reason = translate_text(reason, lang, source_lang="en")
follow_up = translate_text(follow_up, lang, source_lang="en")
_print_block("Final Answer", [final_answer])
_print_block("Citations", [])
_print_block("Confidence", ["Low"])
_print_block("Reason", [reason])
_print_block("Escalation", ["Ask for clarification"])
_print_block("Follow-up Questions", [follow_up])
audit_log(
"clarify_country_ambiguous",
{"query": redact_pii(original_query), "countries": detected, "lang": lang}
)
return
print("\n" + "=" * 60)
print(f"Query: {original_query}")
print(f"Language: {lang}")
entities = extract_entities(query_for_processing)
audit_log(
"query_entities",
{"query": redact_pii(original_query), "entities": entities, "lang": lang}
)
t0 = time.time()
evidence = retrieve(query_for_processing)
t_retrieval_ms = int((time.time() - t0) * 1000)
evidence_lines = [
f"{e['doc_id']} - {e['section']} ({e['timestamp']}) [score={e.get('score', 0):.3f}]"
for e in evidence
]
_print_block("Retrieved Evidence", evidence_lines)
if evidence:
audit_log(
"evidence_trail",
{
"query": redact_pii(original_query),
"evidence": [
{
"doc_id": e.get("doc_id"),
"section": e.get("section"),
"timestamp": e.get("timestamp"),
"version": e.get("version"),
}
for e in evidence
],
},
)
t1 = time.time()
draft_raw = generate_answer(query_for_processing, evidence)
if isinstance(draft_raw, dict):
draft = draft_raw
else:
try:
draft = json.loads(draft_raw)
except Exception:
draft = {
"final_answer": "Unable to determine the answer from available policy evidence.",
"citations": [],
"confidence": "Low",
"reason": "Generator returned invalid or empty JSON.",
"escalation": "Consult Legal",
"follow_up_questions": []
}
if evidence and not draft.get("citations"):
draft["citations"] = [
{
"doc_id": e.get("doc_id"),
"section": e.get("section"),
"timestamp": e.get("timestamp"),
}
for e in evidence
]
if not draft.get("reason"):
draft["reason"] = "Citations added from retrieved evidence."
if not draft.get("final_answer"):
draft["final_answer"] = "Unable to determine the answer from available policy evidence."
refusal_phrases = [
"unable to provide a grounded answer",
"unable to determine",
"insufficient",
"does not contain information",
"cannot determine",
]
if evidence and any(p in draft["final_answer"].lower() for p in refusal_phrases):
# Deterministic fallback: extract a concise answer from evidence.
evidence_summary = " ".join(
[f"{e['text']}" for e in evidence]
)
draft["final_answer"] = (
"Based on the policy evidence: " + evidence_summary
)
if not draft.get("reason"):
draft["reason"] = "Answer derived from retrieved evidence."
t_generation_ms = int((time.time() - t1) * 1000)
_print_block("Draft Answer", [draft.get("final_answer", "")])
draft_citations = []
for c in draft.get("citations", []):
score = None
for e in evidence:
if (
e.get("doc_id") == c.get("doc_id")
and e.get("section") == c.get("section")
and e.get("timestamp") == c.get("timestamp")
):
score = e.get("score")
break
if score is not None:
draft_citations.append(
f"{c['doc_id']} | {c['section']} | {c['timestamp']} | score={score:.3f}"
)
else:
draft_citations.append(
f"{c['doc_id']} | {c['section']} | {c['timestamp']}"
)
_print_block("Draft Citations", draft_citations)
verification = verify(query_for_processing, draft, evidence)
audit_log(
"verification",
{
"query": redact_pii(original_query),
"confidence": verification.get("confidence"),
"reason": verification.get("reason"),
"escalation": verification.get("escalation"),
"evidence_ids": [e.get("doc_id") for e in evidence],
"citations": draft.get("citations", []),
"lang": lang,
"latency_ms": {
"retrieval": t_retrieval_ms,
"generation": t_generation_ms,
},
},
)
_print_block("Verifier Feedback", [json.dumps(verification)])
# Final Answer output block
final_answer = draft.get("final_answer", "")
citations = draft.get("citations", [])
confidence = verification.get("confidence")
reason = verification.get("reason")
escalation = verification.get("escalation")
follow_up = verification.get("follow_up_questions") or draft.get("follow_up_questions", [])
if confidence != "Medium" or escalation != "None":
final_answer = "Unable to provide a grounded answer. Escalation required: " + escalation
citations = []
audit_log(
"escalation",
{
"query": redact_pii(query),
"reason": verification.get("reason"),
"escalation": verification.get("escalation"),
},
)
if lang != "en":
final_answer = translate_text(final_answer, lang, source_lang="en")
reason = translate_text(reason, lang, source_lang="en")
if escalation != "None":
escalation = translate_text(escalation, lang, source_lang="en")
follow_up = [translate_text(q, lang, source_lang="en") for q in follow_up]
_print_block("Final Answer", [final_answer])
final_citations = []
for c in citations:
score = None
for e in evidence:
if (
e.get("doc_id") == c.get("doc_id")
and e.get("section") == c.get("section")
and e.get("timestamp") == c.get("timestamp")
):
score = e.get("score")
break
if score is not None:
final_citations.append(
f"{c['doc_id']} | {c['section']} | {c['timestamp']} | score={score:.3f}"
)
else:
final_citations.append(
f"{c['doc_id']} | {c['section']} | {c['timestamp']}"
)
_print_block("Citations", final_citations)
_print_block("Confidence", [confidence])
_print_block("Reason", [reason])
_print_block("Escalation", [escalation])
_print_block("Follow-up Questions", follow_up)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="EOR Compliance Copilot")
parser.add_argument("--query", type=str, help="Run a single ad-hoc query")
args = parser.parse_args()
if args.query:
run_query(args.query)
else:
try:
with open("evaluation/test_queries.json") as f:
test_cases = json.load(f)
for case in test_cases:
run_query(case["query"])
except Exception:
run_query("Can we terminate during probation in Germany?")