Files
inquiry_robot/inquiry-agent/tests/test_quote_recognition.py
T
linmianxun 04e00f0cfc checkpoint: 提交当前工作区代码(同义词配置初版 + 既有流程改动)
- 新增 inq_synonym 表、62组同义词种子与智能体只读接口
- Python 同义词加载/归一化与 1板/2箱 机读
- 同步当前分支既有协作、报价识别、陆运等改动
2026-11-24 16:26:49 +08:00

236 lines
10 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
报价识别分级:完整 / 部分 / 全无(产品 2026-11-23)。
不连企微、主账、TMS,只测识别判断和回执话术。
"""
from __future__ import annotations
import os
import sys
import unittest
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
if ROOT not in sys.path:
sys.path.insert(0, ROOT)
os.environ.setdefault("YTD_ENV", "test")
os.environ.setdefault("REDIS_BACKEND", "memory")
os.environ.setdefault("CHECKPOINT_BACKEND", "memory")
os.environ.setdefault("MESSAGE_STORE_BACKEND", "memory")
os.environ.setdefault("LLM_DATA_USAGE_CONFIRMED", "false")
os.environ.setdefault("LLM_ALLOW_NETWORK", "false")
os.environ.setdefault("LEDGER_BACKEND", "memory")
from agent.llm.extract_text import extract_product_quote
from agent.policy import inquiry_copy as copy
from agent.policy.quote_confirm import can_confirm_quote, is_confirm_text
from agent.schema.quote_recognition import inspect_quote_recognition
class ConfirmKeywordTests(unittest.TestCase):
"""确认关键词:确认/确定/正确及相近说法;销售本人或产品/航线可确认。"""
def test_confirm_keywords(self) -> None:
for text in ["确认", "确定", "正确", "对的", "没问题", "好的", "OK", "确认无误"]:
self.assertTrue(is_confirm_text(text), text)
# 群里显示名比配置名长时,@称呼必须整体剥掉,不能留下「测试」。
self.assertTrue(is_confirm_text("@智价通AIQ测试 正确"))
self.assertTrue(is_confirm_text("@智价通 AIQ 确认"))
for text in ["这个价格再谈谈", "确认报价单", "USD11/kg"]:
self.assertFalse(is_confirm_text(text), text)
def test_ticket_sales_can_confirm_quote(self) -> None:
ticket = {"sales_wecom_id": "u_sales", "business_line": "SEA"}
self.assertTrue(can_confirm_quote(None, ticket, "u_sales", business_line="SEA"))
self.assertFalse(can_confirm_quote(None, ticket, "u_other", business_line="SEA"))
class AirRecognitionTests(unittest.TestCase):
"""空运:标准话术完整;缺线路算部分;闲聊算全无。"""
def _inspect(self, text: str, facts: dict | None = None) -> dict:
quote = extract_product_quote(text, business_line="AIR", transport_mode="空运")
return inspect_quote_recognition(
text, quote, business_line="AIR", transport_mode="空运", facts=facts or {}
)
def test_standard_format_is_full(self) -> None:
text = "工单号,USD11/kg、TH、NKG-KUL、11月2日"
got = self._inspect(
text, {"起运港": "NKG", "目的港": "KUL", "报价日期": "2026-11-02"}
)
self.assertEqual(got["status"], "full")
self.assertEqual(got["missing"], [])
self.assertIn("线路:NKG-KUL", got["recognized"])
def test_receipt_lists_ratio_weight_break_and_services(self) -> None:
"""比例、重量等级、卡转/MUST GO 也是识别内容,回执不能漏。"""
text = "WO202611240005 1:300,+500,USD11/kg,卡转/MUST GO、TH、NKG-KUL、11月24日"
got = self._inspect(
text, {"起运港": "NKG", "目的港": "KUL", "报价日期": "2026-11-24"}
)
self.assertEqual(got["status"], "full")
self.assertIn("比例:1:300", got["recognized"])
self.assertIn("重量等级:+500", got["recognized"])
self.assertIn("增值服务:卡转/MUST GO", got["recognized"])
body = copy.quote_partial_recognized(
got["recognized"],
got["missing"],
business_line="AIR",
work_order_no="WO202611240005",
)
self.assertIn("比例:1:300", body)
self.assertIn("重量等级:+500", body)
self.assertIn("增值服务:卡转/MUST GO", body)
def test_missing_route_is_partial_and_uses_ticket_route(self) -> None:
# 真机 WO202611230013:话术只有价、航司、日期,线路在工单里。
text = "WO202611230013 USD11/kg,\nTH、11月23日"
got = self._inspect(
text, {"起运港": "香港", "目的港": "DEL", "报价日期": "2026-11-23"}
)
self.assertEqual(got["status"], "partial")
self.assertIn("空运费:USD11/kg", got["recognized"])
self.assertIn("航司:TH", got["recognized"])
self.assertIn("线路:香港-DEL", got["recognized"])
body = copy.quote_partial_recognized(
got["recognized"], got["missing"], business_line="AIR"
)
self.assertIn("我识别内容为:", body)
self.assertIn("请确认是否正确。", body)
# 部分识别不再贴格式样例;格式只留给「全无」提示。
self.assertNotIn("工单号,USD11/kg、TH、NKG-KUL、11月2日", body)
with_no = copy.quote_partial_recognized(
got["recognized"], got["missing"], business_line="AIR",
work_order_no="WO202611240001",
)
self.assertTrue(with_no.startswith("WO202611240001 我识别内容为:"))
def test_price_only_is_partial(self) -> None:
got = self._inspect(
"USD11/kg", {"起运港": "香港", "目的港": "DEL", "报价日期": "2026-11-23"}
)
self.assertEqual(got["status"], "partial")
self.assertIn("航司", got["missing"])
def test_chatter_is_none_and_prompt_has_format(self) -> None:
got = self._inspect(
"这个价格我再问下航线,晚点回你",
{"起运港": "香港", "目的港": "DEL", "报价日期": "2026-11-23"},
)
self.assertEqual(got["status"], "none")
body = copy.quote_not_recognized(business_line="AIR")
self.assertIn("我无法识别您的内容", body)
self.assertIn("工单号,USD11/kg、TH、NKG-KUL、11月2日", body)
# 留档说明不再出现在群里(产品 2026-11-23 晚确认),原话仍写后台。
self.assertNotIn("原始报价参考", body)
self.assertNotIn("询价中", body)
class GroupFieldCorrectionTests(unittest.TestCase):
"""陆运/海运字段纠错不能被当成改价(数字不能变运费)。"""
def test_field_corrections_are_not_prices(self) -> None:
from agent.policy.sea_group_ops import _is_group_field_correction
for text in (
"车型改为45HQ",
"税率改为6%",
"目的港改为春疆",
"包装改为木箱",
"贸易条款改为FOB",
):
self.assertTrue(_is_group_field_correction(text, "LAND"), text)
for text in ("运费改为1800", "报关费改为300", "海运费 1000"):
self.assertFalse(_is_group_field_correction(text, "LAND"), text)
class LandRecognitionTests(unittest.TestCase):
"""陆运:线路/车型/价格都在话术里才算完整;缺价格算部分。"""
def _inspect(self, text: str, facts: dict | None = None) -> dict:
quote = extract_product_quote(text, business_line="LAND", transport_mode="陆运")
return inspect_quote_recognition(
text, quote, business_line="LAND", transport_mode="陆运", facts=facts or {}
)
def test_standard_format_is_full(self) -> None:
text = "东莞-春疆,危险品,45HQ,CNY18500;都是含0点税"
got = self._inspect(
text, {"起运港": "东莞", "目的港": "春疆", "车型/数量": "45HQ"}
)
self.assertEqual(got["status"], "full")
self.assertEqual(got["missing"], [])
def test_no_price_is_partial(self) -> None:
text = "东莞-春疆,危险品,45HQ"
got = self._inspect(
text, {"起运港": "东莞", "目的港": "春疆", "车型/数量": "45HQ"}
)
self.assertEqual(got["status"], "partial")
self.assertIn("价格", got["missing"])
self.assertIn("起运地:东莞", got["recognized"])
self.assertIn("目的地:春疆", got["recognized"])
self.assertIn("车型/数量:45HQ", got["recognized"])
def test_chatter_is_none_and_prompt_has_land_format(self) -> None:
got = self._inspect("我晚点问下车队", {})
self.assertEqual(got["status"], "none")
body = copy.quote_not_recognized(business_line="LAND")
self.assertIn("我无法识别您的内容", body)
self.assertIn("东莞-春疆,危险品,45HQ,CNY18500;都是含0点税", body)
class LandZeroTaxTest(unittest.TestCase):
"""口语「0税」要算税率 0;「1700车」没斜杠也要认 /车。"""
def test_bare_zero_tax_and_car_unit(self) -> None:
from agent.policy.sea_group_ops import _attach_land_vehicle_rates_segment
text = "运费45HQ1700车0税"
quote = extract_product_quote(text, business_line="LAND", transport_mode="陆运")
self.assertEqual(str(quote.get("tax_rate") or ""), "0")
self.assertEqual(dict(quote.get("vehicleRates") or {}), {"45HQ": "1700"})
vrows = quote.get("vehicle_fee_rows") or []
self.assertTrue(vrows, quote)
self.assertEqual(str((vrows[0] or {}).get("unit") or "").strip(), "车")
got = inspect_quote_recognition(
text,
quote,
business_line="LAND",
transport_mode="陆运",
facts={"起运港": "深圳", "目的港": "香港"},
)
self.assertIn("税率:0", got["recognized"])
out = _attach_land_vehicle_rates_segment(
quote, {"facts": {"起运港": "深圳", "目的港": "香港"}}
)
lines = copy._manual_quote_lines(out, business_line="LAND", layout="manual")
self.assertIn("税率:0", lines)
class ImagePromptTest(unittest.TestCase):
"""图片/非 Excel 文件:回「无法识别,请按文字格式重新报价」。"""
def test_text_format_prompt(self) -> None:
body = copy.quote_not_recognized(
business_line="LAND", at_name="林绵勋", text_format=True
)
self.assertTrue(body.startswith("@林绵勋"))
self.assertIn("我无法识别您的内容,请按文字格式重新报价", body)
self.assertIn("东莞-春疆", body)
class SeaRecognitionTests(unittest.TestCase):
"""海运暂无标准话术:失败提示用「费用名+金额+币种」兜底。"""
def test_sea_failure_uses_generic_hint(self) -> None:
body = copy.quote_not_recognized(business_line="SEA")
self.assertIn("我无法识别您的内容", body)
self.assertIn("费用名+金额+币种", body)
if __name__ == "__main__":
unittest.main()