需求未定稿时可先跑通追问/判定/出方案;槽位与判定树外置 YAML,后续可接 Ragflow。 Co-authored-by: haijd <haijd@163.com>
38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
|
|
def _match_condition(cond: dict[str, Any], slots: dict[str, str]) -> bool:
|
|
ctype = cond.get("type")
|
|
if ctype == "always":
|
|
return True
|
|
slot = str(cond.get("slot") or "")
|
|
value = str(cond.get("value") or "")
|
|
actual = (slots.get(slot) or "").strip()
|
|
if ctype == "slot_equals":
|
|
return actual == value
|
|
if ctype == "slot_contains":
|
|
return value.lower() in actual.lower()
|
|
return False
|
|
|
|
|
|
def evaluate_tree(rules: list[dict[str, Any]], slots: dict[str, str]) -> dict[str, Any]:
|
|
for rule in rules:
|
|
conditions = rule.get("when_all") or []
|
|
if all(_match_condition(c, slots) for c in conditions):
|
|
then = dict(rule.get("then") or {})
|
|
return {
|
|
"rule_id": rule.get("id"),
|
|
"description": rule.get("description"),
|
|
**then,
|
|
}
|
|
return {
|
|
"rule_id": "empty",
|
|
"product_bundle": ["待专家确认"],
|
|
"tech_line": "待判定",
|
|
"summary": "未匹配到规则",
|
|
"confidence": 0.0,
|
|
"handoff": True,
|
|
}
|