feat(shipping-notice): 增强订单抽取功能,添加进度事件和诊断运行管理,优化数据模型和访问控制
This commit is contained in:
1 parent
ce26d6d0c4
commit
c4095b08da
175 files changed
+13064
-1814
No files matched your search
@@ -12,11 +12,9 @@ import re
|
||||
from typing import Any
|
||||
|
||||
from app.services.shipping_notice.po_number import enrich_notice_with_po_number
|
||||
from app.services.shipping_notice.reservation_id import find_reservation_id_in_document
|
||||
from app.services.shipping_notice.notice_lines import (
|
||||
enrich_notice_with_document_currency,
|
||||
enrich_notice_with_document_required_date,
|
||||
enrich_notice_with_document_reservation,
|
||||
adjudicate_notice_lines,
|
||||
normalize_notice_lines,
|
||||
parse_notice_table_text,
|
||||
@@ -29,17 +27,32 @@ def _finalize_notice_parse(
|
||||
*,
|
||||
base_confidence: float,
|
||||
raw_text: str = "",
|
||||
schema_definition: dict[str, Any] | None = None,
|
||||
) -> tuple[dict[str, Any], float, list[str]]:
|
||||
enabled_fields = {
|
||||
str(item.get("field_code") or "").strip()
|
||||
for item in (schema_definition or {}).get("fields", [])
|
||||
if isinstance(item, dict) and item.get("enabled", True)
|
||||
}
|
||||
field_enabled = lambda code: not schema_definition or code in enabled_fields
|
||||
parse_source = parsed.get("parse_source")
|
||||
lines = normalize_notice_lines(parsed.get("lines") if isinstance(parsed.get("lines"), list) else [])
|
||||
parsed = enrich_notice_with_document_reservation({**parsed, "lines": lines}, raw_text=raw_text)
|
||||
parsed = enrich_notice_with_document_required_date(parsed)
|
||||
parsed = enrich_notice_with_document_currency(parsed)
|
||||
parsed = enrich_notice_with_po_number(parsed, raw_text=raw_text)
|
||||
lines = normalize_notice_lines(
|
||||
parsed.get("lines") if isinstance(parsed.get("lines"), list) else [],
|
||||
schema_definition=schema_definition,
|
||||
)
|
||||
parsed = {**parsed, "lines": lines}
|
||||
if field_enabled("required_date"):
|
||||
parsed = enrich_notice_with_document_required_date(parsed)
|
||||
if field_enabled("currency"):
|
||||
parsed = enrich_notice_with_document_currency(parsed)
|
||||
if field_enabled("po_number"):
|
||||
parsed = enrich_notice_with_po_number(parsed, raw_text=raw_text)
|
||||
if parse_source:
|
||||
parsed["parse_source"] = parse_source
|
||||
lines = parsed.get("lines") if isinstance(parsed.get("lines"), list) else []
|
||||
lines, parse_issues, validation_status = adjudicate_notice_lines(lines, raw_text=raw_text)
|
||||
lines, parse_issues, validation_status = adjudicate_notice_lines(
|
||||
lines, raw_text=raw_text, schema_definition=schema_definition,
|
||||
)
|
||||
parsed["lines"] = lines
|
||||
parsed["parse_issues"] = parse_issues
|
||||
parsed["validation_status"] = validation_status
|
||||
@@ -57,6 +70,7 @@ def extract_notice_fields(
|
||||
source_type: str = "manual",
|
||||
structured: dict[str, Any] | None = None,
|
||||
notice_files: list[dict[str, Any]] | None = None,
|
||||
schema_definition: dict[str, Any] | None = None,
|
||||
) -> tuple[dict[str, Any], float, list[str]]:
|
||||
"""
|
||||
抽取发货通知单头与订单行,并计算置信度与 warnings。
|
||||
@@ -71,6 +85,7 @@ def extract_notice_fields(
|
||||
source_type: 来源类型(manual/pdf/email 等)。
|
||||
structured: Dify 编排产出的结构化解析结果(最高优先级)。
|
||||
notice_files: 附件元数据列表(预留)。
|
||||
schema_definition: 预处理明确选择的完整订单 Schema;缺失时不执行确定性表格解析。
|
||||
|
||||
Returns:
|
||||
tuple: (parsed 字典, confidence, warnings 列表)。
|
||||
@@ -83,20 +98,27 @@ def extract_notice_fields(
|
||||
payload,
|
||||
base_confidence=float(structured.get("confidence") or 0.95),
|
||||
raw_text=str(structured.get("raw_text") or raw_text),
|
||||
schema_definition=schema_definition,
|
||||
)
|
||||
warnings.extend(line_warnings)
|
||||
return parsed, confidence, warnings
|
||||
|
||||
warnings.append("parse_fallback_heuristic")
|
||||
heuristic, confidence, heuristic_warnings = _heuristic_extract(raw_text)
|
||||
heuristic, confidence, heuristic_warnings = _heuristic_extract(
|
||||
raw_text, schema_definition=schema_definition,
|
||||
)
|
||||
warnings.extend(heuristic_warnings)
|
||||
heuristic["parse_source"] = "heuristic"
|
||||
parsed, confidence, line_warnings = _finalize_notice_parse(heuristic, base_confidence=confidence, raw_text=raw_text)
|
||||
parsed, confidence, line_warnings = _finalize_notice_parse(
|
||||
heuristic, base_confidence=confidence, raw_text=raw_text, schema_definition=schema_definition,
|
||||
)
|
||||
warnings.extend(line_warnings)
|
||||
return parsed, confidence, warnings
|
||||
|
||||
|
||||
def _heuristic_extract(raw_text: str) -> tuple[dict[str, Any], float, list[str]]:
|
||||
def _heuristic_extract(
|
||||
raw_text: str, *, schema_definition: dict[str, Any] | None = None,
|
||||
) -> tuple[dict[str, Any], float, list[str]]:
|
||||
text = str(raw_text or "").strip()
|
||||
warnings: list[str] = []
|
||||
customer_name = ""
|
||||
@@ -104,13 +126,13 @@ def _heuristic_extract(raw_text: str) -> tuple[dict[str, Any], float, list[str]]
|
||||
if m:
|
||||
customer_name = m.group(1)
|
||||
|
||||
lines = parse_notice_table_text(text)
|
||||
if schema_definition:
|
||||
from app.services.shipping_notice.order_schema.table_contract import compile_table_header_contract
|
||||
|
||||
lines = parse_notice_table_text(text, contract=compile_table_header_contract(schema_definition))
|
||||
else:
|
||||
lines = []
|
||||
if lines:
|
||||
doc_rid = find_reservation_id_in_document(text)
|
||||
if doc_rid:
|
||||
for line in lines:
|
||||
if not line.get("reservation_id"):
|
||||
line["reservation_id"] = doc_rid
|
||||
return (
|
||||
{
|
||||
"customer_name": customer_name,
|
||||
@@ -126,7 +148,7 @@ def _heuristic_extract(raw_text: str) -> tuple[dict[str, Any], float, list[str]]
|
||||
|
||||
lines = []
|
||||
for m in re.finditer(r"([A-Za-z0-9\-_]{3,})\s*[xX×*]\s*(\d+(?:\.\d+)?)", text):
|
||||
lines.append({"part_number": m.group(1), "qty": float(m.group(2)), "uom": "EA"})
|
||||
lines.append({"part_number": m.group(1), "qty": float(m.group(2))})
|
||||
|
||||
# 尝试纯数字 Reservation ID + SKU + Qty 的自由文本模式(如 1000140351 10-40594-001 2)
|
||||
if not lines:
|
||||
@@ -139,7 +161,6 @@ def _heuristic_extract(raw_text: str) -> tuple[dict[str, Any], float, list[str]]
|
||||
"reservation_id": m.group("resv"),
|
||||
"part_number": m.group(2),
|
||||
"qty": float(m.group(3)),
|
||||
"uom": "EA",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user