feat(shipping-notice): 支持发货通知解析延后至 Dify 画布,优化任务创建流程
This commit is contained in:
1 parent
d46d61bfa1
commit
70e8f700ff
38 files changed
+3215
-714
No files matched your search
@@ -3,7 +3,7 @@ services/shipping_notice/extract.py
|
||||
|
||||
用途:抽取发货通知中的单头字段与订单行,并在收尾阶段补充文档级字段和告警。
|
||||
依赖:po_number、reservation_id、notice_lines 中的标准化与回填工具。
|
||||
逻辑:由 task_workflow.parse_notice 调用本文件完成原始文本解析,再把结果交给 parse_summary 和状态机链路。
|
||||
逻辑:由 task_workflow.parse_notice 调用;优先 Dify 传入的 structured,无则走后端启发式规则兜底。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -29,11 +29,14 @@ def _finalize_notice_parse(
|
||||
base_confidence: float,
|
||||
raw_text: str = "",
|
||||
) -> tuple[dict[str, Any], float, list[str]]:
|
||||
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)
|
||||
if parse_source:
|
||||
parsed["parse_source"] = parse_source
|
||||
lines = parsed.get("lines") if isinstance(parsed.get("lines"), list) else []
|
||||
warnings = validate_notice_lines(lines)
|
||||
from app.services.shipping_notice.notice_lines import notice_parse_completeness
|
||||
@@ -52,12 +55,16 @@ def extract_notice_fields(
|
||||
) -> tuple[dict[str, Any], float, list[str]]:
|
||||
"""
|
||||
抽取发货通知单头与订单行,并计算置信度与 warnings。
|
||||
【AI 触发时机】:由 parse_notice 内部调用;Agent 应调 parse_notice 而非直接调用本函数。
|
||||
【AI 触发时机】:由 parse_notice Tool 调用;Dify 画布/Agent 应先用 LLM 产出 structured 再传入,勿依赖本函数做 LLM 抽取。
|
||||
|
||||
解析优先级:
|
||||
1. structured:Dify LLM/画布已抽取并传入 parse_notice
|
||||
2. 后端启发式规则(_heuristic_extract,仅作 Dify 无法实现时的兜底)
|
||||
|
||||
Args:
|
||||
raw_text: 发货通知正文。
|
||||
source_type: 来源类型(manual/pdf/email 等)。
|
||||
structured: Dify/上游已结构化的解析结果(优先使用)。
|
||||
structured: Dify 编排产出的结构化解析结果(最高优先级)。
|
||||
notice_files: 附件元数据列表(预留)。
|
||||
|
||||
Returns:
|
||||
@@ -66,15 +73,22 @@ def extract_notice_fields(
|
||||
warnings: list[str] = []
|
||||
|
||||
if structured and isinstance(structured, dict):
|
||||
return _finalize_notice_parse(
|
||||
structured,
|
||||
payload = {**structured, "parse_source": structured.get("parse_source") or "dify"}
|
||||
parsed, confidence, line_warnings = _finalize_notice_parse(
|
||||
payload,
|
||||
base_confidence=float(structured.get("confidence") or 0.95),
|
||||
raw_text=str(structured.get("raw_text") or ""),
|
||||
raw_text=str(structured.get("raw_text") or raw_text),
|
||||
)
|
||||
warnings.extend(line_warnings)
|
||||
return parsed, confidence, warnings
|
||||
|
||||
warnings.append("parse_fallback_heuristic")
|
||||
heuristic, confidence, heuristic_warnings = _heuristic_extract(raw_text)
|
||||
warnings.extend(heuristic_warnings)
|
||||
return _finalize_notice_parse(heuristic, base_confidence=confidence, raw_text=raw_text)
|
||||
heuristic["parse_source"] = "heuristic"
|
||||
parsed, confidence, line_warnings = _finalize_notice_parse(heuristic, base_confidence=confidence, raw_text=raw_text)
|
||||
warnings.extend(line_warnings)
|
||||
return parsed, confidence, warnings
|
||||
|
||||
|
||||
def _heuristic_extract(raw_text: str) -> tuple[dict[str, Any], float, list[str]]:
|
||||
|
||||
Reference in new issue
Block a user