feat(срез 4.3): контракт EstimateDraft→движок(strict)→verify→результат
estimate_contract.py: LLM выдаёт EstimateDraft (код+объём), движок считает числа (strict, НР/СП из resolve_nr_sp), estimate_verify проверяет. ok=False → не отдавать. Golden known-answer до копейки (итог 4380.0) + metamorphic + fail-loud. 4 теста. xlsx-выгрузка (generate_excel) — в 4.4 при подключении в чат. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
"""estimate_contract — контракт «LLM структурирует, код считает» (срез 4.3).
|
||||
|
||||
LLM выдаёт EstimateDraft (список позиций: код+объём) → детерминированный движок
|
||||
(strict) считает числа → estimate_verify проверяет → результат. LLM НИКОГДА не
|
||||
называет числа: они из движка. Не прошёл strict/verify → ok=False (не отдавать как
|
||||
готовую смету). НР/СП — из reserve_nr_sp (реальные), не хардкод.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
# smeta_railway на пути (как в main.py/claude_bridge_glm)
|
||||
_SR = Path(__file__).resolve().parent.parent / "smeta-railway" / "src"
|
||||
if _SR.exists() and str(_SR) not in sys.path:
|
||||
sys.path.insert(0, str(_SR))
|
||||
|
||||
import estimate_verify # noqa: E402
|
||||
|
||||
|
||||
class DraftPosition(BaseModel):
|
||||
code: str
|
||||
qty: float
|
||||
unit: str = ""
|
||||
name: str = ""
|
||||
|
||||
|
||||
class DraftMeta(BaseModel):
|
||||
region: str = ""
|
||||
price_level: str = ""
|
||||
norm_base: str = "ФСНБ-2022"
|
||||
vat_pct: float = 22.0
|
||||
|
||||
|
||||
class EstimateDraft(BaseModel):
|
||||
meta: DraftMeta = Field(default_factory=DraftMeta)
|
||||
positions: list[DraftPosition] = Field(default_factory=list)
|
||||
|
||||
|
||||
def build_and_calc(draft: EstimateDraft, resources: dict, fgis) -> dict:
|
||||
"""EstimateDraft → расчёт движком (strict) → verify. Возвращает
|
||||
{ok, grand_total, grand_total_with_limits, missing, verify, positions_count}.
|
||||
ok=False → в смете нерассчитанные позиции или несходимость: НЕ отдавать файл."""
|
||||
from smeta_railway.calc.estimate import EstimateCalculator
|
||||
from smeta_railway.config import Section, Subsection, WorkItem
|
||||
|
||||
items = [WorkItem(code=p.code, name=p.name or p.code, unit=p.unit, qty=p.qty)
|
||||
for p in draft.positions]
|
||||
section = Section(name="Смета", subsections=[Subsection(name="", items=items)])
|
||||
calc = EstimateCalculator(fgis=fgis, resources=resources, strict=True)
|
||||
result = calc.calculate([section])
|
||||
|
||||
vp = draft.meta.vat_pct
|
||||
gt = result.grand_total
|
||||
vat = round(gt * vp / 100, 2)
|
||||
norm = {
|
||||
"positions": [{"code": p.gesn_code, "name": p.name, "unit": p.unit,
|
||||
"qty": p.qty, "total": p.total, "norm_base": draft.meta.norm_base}
|
||||
for p in result.positions if not p.unresolved],
|
||||
"totals": {"grand_total": gt, "vat_pct": vp, "vat": vat,
|
||||
"total_with_vat": round(gt + vat, 2)},
|
||||
}
|
||||
v = estimate_verify.verify(norm)
|
||||
return {
|
||||
"ok": result.ok and v["ok"],
|
||||
"grand_total": gt,
|
||||
"grand_total_with_limits": result.grand_total_with_limits,
|
||||
"missing": result.missing,
|
||||
"verify": v,
|
||||
"positions_count": len(result.positions),
|
||||
}
|
||||
Reference in New Issue
Block a user