feat(shipping-notice): 实现货代发货通知的库存全流程与对话交互
This commit is contained in:
1 parent
a5cc48b6aa
commit
2607f4a9f6
26 files changed
+5013
-256
No files matched your search
@@ -6,6 +6,11 @@ from typing import Any
|
||||
|
||||
from app.services.dify import DifyService
|
||||
from app.services.shipping_notice.agent_resolver import resolve_extract_agent
|
||||
from app.services.shipping_notice.notice_lines import (
|
||||
normalize_notice_lines,
|
||||
parse_notice_table_text,
|
||||
validate_notice_lines,
|
||||
)
|
||||
|
||||
|
||||
def _parse_workflow_outputs(result: dict[str, Any]) -> dict[str, Any] | None:
|
||||
@@ -30,6 +35,17 @@ def _parse_workflow_outputs(result: dict[str, Any]) -> dict[str, Any] | None:
|
||||
return outputs if outputs else None
|
||||
|
||||
|
||||
def _finalize_notice_parse(parsed: dict[str, Any], *, base_confidence: float) -> tuple[dict[str, Any], float, list[str]]:
|
||||
lines = normalize_notice_lines(parsed.get("lines") if isinstance(parsed.get("lines"), list) else [])
|
||||
parsed = {**parsed, "lines": lines}
|
||||
warnings = validate_notice_lines(lines)
|
||||
from app.services.shipping_notice.notice_lines import notice_parse_completeness
|
||||
|
||||
completeness = notice_parse_completeness(lines)
|
||||
confidence = min(base_confidence, 0.95) * (0.5 + 0.5 * completeness) if lines else base_confidence * 0.5
|
||||
return parsed, round(confidence, 4), warnings
|
||||
|
||||
|
||||
def extract_notice_fields(
|
||||
*,
|
||||
raw_text: str,
|
||||
@@ -39,7 +55,7 @@ def extract_notice_fields(
|
||||
warnings: list[str] = []
|
||||
|
||||
if structured and isinstance(structured, dict):
|
||||
return structured, float(structured.get("confidence") or 0.95), warnings
|
||||
return _finalize_notice_parse(structured, base_confidence=float(structured.get("confidence") or 0.95))
|
||||
|
||||
agent = resolve_extract_agent()
|
||||
if agent and raw_text.strip():
|
||||
@@ -51,31 +67,76 @@ def extract_notice_fields(
|
||||
)
|
||||
parsed = _parse_workflow_outputs(result)
|
||||
if parsed:
|
||||
confidence = float(parsed.get("confidence") or 0.85)
|
||||
return parsed, confidence, warnings
|
||||
finalized, confidence, line_warnings = _finalize_notice_parse(
|
||||
parsed,
|
||||
base_confidence=float(parsed.get("confidence") or 0.85),
|
||||
)
|
||||
warnings.extend(line_warnings)
|
||||
return finalized, confidence, warnings
|
||||
warnings.append("workflow_returned_no_structured_output")
|
||||
except Exception as exc:
|
||||
warnings.append(f"workflow_error:{exc}")
|
||||
|
||||
return _heuristic_extract(raw_text), 0.5, warnings
|
||||
heuristic, confidence, heuristic_warnings = _heuristic_extract(raw_text)
|
||||
warnings.extend(heuristic_warnings)
|
||||
return _finalize_notice_parse(heuristic, base_confidence=confidence)
|
||||
|
||||
|
||||
def _heuristic_extract(raw_text: str) -> dict[str, Any]:
|
||||
def _heuristic_extract(raw_text: str) -> tuple[dict[str, Any], float, list[str]]:
|
||||
text = str(raw_text or "").strip()
|
||||
warnings: list[str] = []
|
||||
customer_name = ""
|
||||
m = re.search(r"客户[::]\s*(\S+)", text)
|
||||
if m:
|
||||
customer_name = m.group(1)
|
||||
|
||||
lines: list[dict[str, Any]] = []
|
||||
lines = parse_notice_table_text(text)
|
||||
if lines:
|
||||
return (
|
||||
{
|
||||
"customer_name": customer_name,
|
||||
"customer_code": "",
|
||||
"ship_to": "",
|
||||
"required_date": "",
|
||||
"lines": lines,
|
||||
"confidence": 0.72,
|
||||
},
|
||||
0.72,
|
||||
warnings,
|
||||
)
|
||||
|
||||
lines = []
|
||||
for m in re.finditer(r"([A-Za-z0-9\-_]{3,})\s*[xX×*]\s*(\d+(?:\.\d+)?)", text):
|
||||
lines.append({"sku": m.group(1), "qty": float(m.group(2)), "uom": "EA"})
|
||||
|
||||
return {
|
||||
"customer_name": customer_name,
|
||||
"customer_code": "",
|
||||
"ship_to": "",
|
||||
"required_date": "",
|
||||
"lines": lines,
|
||||
"confidence": 0.5 if lines else 0.3,
|
||||
}
|
||||
# 尝试纯数字 Reservation ID + SKU + Qty 的自由文本模式(如 1000140351 10-40594-001 2)
|
||||
if not lines:
|
||||
for m in re.finditer(
|
||||
r"(?P<resv>\d{8,12})\s+([A-Za-z0-9\-_]{3,})\s+(\d+(?:\.\d+)?)",
|
||||
text,
|
||||
):
|
||||
lines.append(
|
||||
{
|
||||
"reservation_id": m.group("resv"),
|
||||
"sku": m.group(2),
|
||||
"qty": float(m.group(3)),
|
||||
"uom": "EA",
|
||||
}
|
||||
)
|
||||
|
||||
if not lines:
|
||||
warnings.append("heuristic_no_lines_detected")
|
||||
|
||||
confidence = 0.5 if lines else 0.3
|
||||
return (
|
||||
{
|
||||
"customer_name": customer_name,
|
||||
"customer_code": "",
|
||||
"ship_to": "",
|
||||
"required_date": "",
|
||||
"lines": lines,
|
||||
"confidence": confidence,
|
||||
},
|
||||
confidence,
|
||||
warnings,
|
||||
)
|
||||
Reference in new issue
Block a user