Spaces:
Sleeping
Sleeping
fix: letter lost the duration sentence when AI paraphrasing dropped the 'ภายใน X วัน' signal — (1) build_letter falls back to the raw TOR clause when cleaning loses it, (2) generate_body rejects AI bodies missing the supplied day-number
Browse files- ai_clean.py +8 -3
- letter_builder.py +6 -0
- tests/test_ai_clean.py +30 -0
- tests/test_letter_builder.py +32 -0
ai_clean.py
CHANGED
|
@@ -240,9 +240,14 @@ def generate_body(fields, retries=2):
|
|
| 240 |
f"VENDOR={(fields.get('vendor') or '').strip()}\nWORK={(fields.get('work') or '').strip()}\n"
|
| 241 |
f"DURATION={duration}\nSTART={(fields.get('start_date') or '').strip()}"
|
| 242 |
)
|
| 243 |
-
# accept only if on-format
|
| 244 |
-
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
return (out, True) if out else (det, False)
|
| 247 |
|
| 248 |
|
|
|
|
| 240 |
f"VENDOR={(fields.get('vendor') or '').strip()}\nWORK={(fields.get('work') or '').strip()}\n"
|
| 241 |
f"DURATION={duration}\nSTART={(fields.get('start_date') or '').strip()}"
|
| 242 |
)
|
| 243 |
+
# accept only if on-format: both anchor phrases present AND, when a duration was
|
| 244 |
+
# supplied, its day-number survived (models occasionally drop the DURATION value)
|
| 245 |
+
_dur_m = re.search(r"ภายใน\s*(\d+)", duration)
|
| 246 |
+
_dur_num = _dur_m.group(1) if _dur_m else None
|
| 247 |
+
out = _complete(user,
|
| 248 |
+
lambda o: "ตามที่" in o and "จึงขอแจ้งให้" in o
|
| 249 |
+
and (not _dur_num or _dur_num in o),
|
| 250 |
+
retries, system=_BODY_SYSTEM)
|
| 251 |
return (out, True) if out else (det, False)
|
| 252 |
|
| 253 |
|
letter_builder.py
CHANGED
|
@@ -55,6 +55,12 @@ def build_letter(*, selected_form, doc_number, po_number, doc_type, signer,
|
|
| 55 |
# first in the TOR. Never raises — deterministic fallback on any AI failure.
|
| 56 |
delivery_clean, used_ai_delivery = ai_clean.clean_delivery(
|
| 57 |
delivery_raw, (selected_nguad or {}).get("label"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# AI-generate the 2-paragraph body in the exact format (deterministic
|
| 60 |
# fallback keeps it on-format if the AI Builder flow fails)
|
|
|
|
| 55 |
# first in the TOR. Never raises — deterministic fallback on any AI failure.
|
| 56 |
delivery_clean, used_ai_delivery = ai_clean.clean_delivery(
|
| 57 |
delivery_raw, (selected_nguad or {}).get("label"))
|
| 58 |
+
# Guard: the AI paraphrase must not lose the "ภายใน X วัน" duration signal that
|
| 59 |
+
# the raw TOR clause carries (LLM rewording like "ภายในระยะเวลา 120 วัน" breaks
|
| 60 |
+
# the strict extractor). If it did, keep the raw clause for the body.
|
| 61 |
+
if ai_clean._duration_from_tor(delivery_raw, None) and \
|
| 62 |
+
not ai_clean._duration_from_tor(delivery_clean, None):
|
| 63 |
+
delivery_clean = delivery_raw
|
| 64 |
|
| 65 |
# AI-generate the 2-paragraph body in the exact format (deterministic
|
| 66 |
# fallback keeps it on-format if the AI Builder flow fails)
|
tests/test_ai_clean.py
CHANGED
|
@@ -91,6 +91,36 @@ def test_thai_date():
|
|
| 91 |
assert ai_clean.thai_date(None) == ""
|
| 92 |
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
def test_raw_fallback_drops_heading_and_filters_nguad():
|
| 95 |
clause = ("การส่งมอบงาน\n"
|
| 96 |
"แบ่งการส่งมอบเป็น 2 งวด\n"
|
|
|
|
| 91 |
assert ai_clean.thai_date(None) == ""
|
| 92 |
|
| 93 |
|
| 94 |
+
def test_generate_body_rejects_ai_output_missing_duration(monkeypatch):
|
| 95 |
+
"""If a duration was supplied, an AI body that drops its day-number must be
|
| 96 |
+
rejected in favour of the deterministic composer (seen live 2026-07-16)."""
|
| 97 |
+
monkeypatch.setenv("AI_BACKEND", "powerautomate")
|
| 98 |
+
monkeypatch.setenv("AIBUILDER_FLOW_URL", "https://example.invalid/flow")
|
| 99 |
+
|
| 100 |
+
def fake_call(url, prompt, timeout=90):
|
| 101 |
+
# anchors present, but the 90-day duration silently dropped
|
| 102 |
+
return "ตามที่ ... ให้ดำเนินการ งาน ... จึงขอแจ้งให้ ... เป็นต้นไป"
|
| 103 |
+
monkeypatch.setattr(ai_clean, "_call_powerautomate", fake_call)
|
| 104 |
+
|
| 105 |
+
text, used_ai = ai_clean.generate_body(FIELDS, retries=1)
|
| 106 |
+
assert used_ai is False # off-format → deterministic fallback
|
| 107 |
+
assert text == ai_clean.compose_body(FIELDS)
|
| 108 |
+
assert "ภายใน 90 (เก้าสิบ) วัน" in text
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def test_generate_body_accepts_ai_output_with_duration(monkeypatch):
|
| 112 |
+
monkeypatch.setenv("AI_BACKEND", "powerautomate")
|
| 113 |
+
monkeypatch.setenv("AIBUILDER_FLOW_URL", "https://example.invalid/flow")
|
| 114 |
+
|
| 115 |
+
good = ("ตามที่ บริษัท ปตท. จำกัด (มหาชน) ได้มีข้อตกลง ให้ดำเนินการ งาน "
|
| 116 |
+
"โดยกำหนดให้แล้วเสร็จภายใน 90 วัน\n\nจึงขอแจ้งให้ เริ่มเข้าดำเนินการ")
|
| 117 |
+
monkeypatch.setattr(ai_clean, "_call_powerautomate",
|
| 118 |
+
lambda url, prompt, timeout=90: good)
|
| 119 |
+
|
| 120 |
+
text, used_ai = ai_clean.generate_body(FIELDS, retries=1)
|
| 121 |
+
assert used_ai is True and text == good
|
| 122 |
+
|
| 123 |
+
|
| 124 |
def test_raw_fallback_drops_heading_and_filters_nguad():
|
| 125 |
clause = ("การส่งมอบงาน\n"
|
| 126 |
"แบ่งการส่งมอบเป็น 2 งวด\n"
|
tests/test_letter_builder.py
CHANGED
|
@@ -89,3 +89,35 @@ def test_build_letter_uses_selected_nguad_deadline():
|
|
| 89 |
all_text = "\n".join(p.text for p in doc.paragraphs)
|
| 90 |
assert "ภายใน 120 วัน" in all_text # the chosen งวด's deadline
|
| 91 |
assert "ภายใน 15 วัน" not in all_text # not งวดที่ 1's
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
all_text = "\n".join(p.text for p in doc.paragraphs)
|
| 90 |
assert "ภายใน 120 วัน" in all_text # the chosen งวด's deadline
|
| 91 |
assert "ภายใน 15 วัน" not in all_text # not งวดที่ 1's
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def test_build_letter_keeps_raw_clause_when_cleaning_loses_duration(monkeypatch):
|
| 95 |
+
"""Seen live 2026-07-16: the AI paraphrase reworded 'ภายใน 120 วัน' so the strict
|
| 96 |
+
extractor found nothing and the letter lost its deadline sentence. The guard must
|
| 97 |
+
fall back to the raw TOR clause when that happens."""
|
| 98 |
+
import ai_clean
|
| 99 |
+
|
| 100 |
+
raw = ("ผู้รับจ้างต้องส่งมอบงานภายใน 120 วัน (ไม่เว้นวันหยุด) "
|
| 101 |
+
"นับถัดจากผู้รับจ้างได้รับใบสั่งจ้างจาก ปตท.")
|
| 102 |
+
paraphrase_without_signal = ("ผู้รับจ้างต้องส่งมอบงานภายในระยะเวลา 120 วัน "
|
| 103 |
+
"นับถัดจากได้รับใบสั่งจ้าง") # 'ภายใน' not followed by digits
|
| 104 |
+
monkeypatch.setattr(ai_clean, "clean_delivery",
|
| 105 |
+
lambda clause, label=None, retries=2:
|
| 106 |
+
(paraphrase_without_signal, True))
|
| 107 |
+
|
| 108 |
+
docx_bytes, _fn, _log, _used_ai = letter_builder.build_letter(
|
| 109 |
+
selected_form=FORM_TYPES[0],
|
| 110 |
+
doc_number="TEST-002/2569",
|
| 111 |
+
po_number="4500987654",
|
| 112 |
+
doc_type=DOC_TYPE_OPTIONS[0],
|
| 113 |
+
signer="นายทดสอบ ระบบดี",
|
| 114 |
+
signer_position="",
|
| 115 |
+
lookup_data={**LOOKUP, "daySLA": None},
|
| 116 |
+
start_date=date(2026, 7, 20),
|
| 117 |
+
delivery_raw=raw,
|
| 118 |
+
selected_nguad={"label": "งวดเดียว", "number": None, "manual": False},
|
| 119 |
+
now=datetime(2026, 7, 16, 12, 0, 0),
|
| 120 |
+
)
|
| 121 |
+
doc = Document(BytesIO(docx_bytes))
|
| 122 |
+
all_text = "\n".join(p.text for p in doc.paragraphs)
|
| 123 |
+
assert "โดยกำหนดให้แล้วเสร็จภายใน 120 วัน" in all_text # raw clause won
|