落地私聊图片询价:识图当口播复用三线文字流程,图上航线三字码保持原样,避免被改写成中文地名。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-18 15:51:55 +08:00
co-authored by Cursor
parent 982b3a2317
commit 8219dce84b
30 changed files with 2153 additions and 45 deletions
+38 -4
View File
@@ -24,13 +24,18 @@ _LABEL_LINE = re.compile(
r"^(起运地|起运港|目的地|目的港|品名|件数|毛重|体积|重量|重量\(KG\)|体积\(CBM\)|"
r"包装方式|包装类型|货量|货物数量|整柜或拼柜|箱型箱量|报价日期|车型/数量|车型数量|"
r"贸易条款|货好时间|商品海关编码|海关编码|HS编码|HS|是否含油|是否含电|是否含磁|"
r"客户名称|货值|是否为危险品)\s*[::]\s*(.+)$"
r"客户名称|货值|是否为危险品|通关口岸(非必填)|通关口岸)\s*[::]\s*(.+)$"
)
_LABEL_INLINE = re.compile(
r"(起运地|起运港|目的地|目的港|品名|件数|毛重|体积|重量|重量\(KG\)|体积\(CBM\)|"
r"包装方式|包装类型|货量|货物数量|整柜或拼柜|箱型箱量|报价日期|车型/数量|车型数量|"
r"贸易条款|货好时间|商品海关编码|海关编码|HS编码|HS|是否含油|是否含电|是否含磁|"
r"客户名称|货值|是否为危险品)\s*[::]\s*(\S+)"
r"客户名称|货值|是否为危险品|通关口岸(非必填)|通关口岸)\s*[::]\s*(\S+)"
)
# 陆运核对卡非必填:销售可能写「通关口岸皇岗」或照抄「通关口岸(非必填):皇岗」。长词优先。
_OPTIONAL_INQUIRY_LABELS = (
"通关口岸(非必填)",
"通关口岸",
)
# 群里协同标签,冒号可省略:「客户名称张三」「货值 10万」。长词优先。
_COLLAB_LABELS = (
@@ -122,6 +127,7 @@ def extract_inquiry_snapshot(
mode = (injected_mode or detect_transport_mode(text) or "").upper()
facts = harvest_oral_measures(text, normalize_facts(dict(injected_facts)))
facts = _merge_spoken_collab(text, facts)
facts = _merge_optional_inquiry_labels(text, facts)
subtype = str(facts.get("运输类型") or "").strip()
return {
"business_line": mode,
@@ -147,6 +153,7 @@ def extract_inquiry_snapshot(
source = "deepseek_b"
facts = harvest_oral_measures(text, normalize_facts(facts))
facts = _merge_spoken_collab(text, facts)
facts = _merge_optional_inquiry_labels(text, facts)
land_subtype = str((b_payload or {}).get("land_subtype") or facts.get("运输类型") or "").strip()
if land_subtype and not str(facts.get("运输类型") or "").strip():
facts["运输类型"] = land_subtype
@@ -168,9 +175,36 @@ def harvest_collab_labels(text: str) -> dict[str, str]:
「客户名称张三 包装方式编织袋 货值 10万」切成三项。
只说「纸箱」没有标签则不收。值取到下一个标签之前。
"""
return _harvest_labels(text, _COLLAB_LABELS)
def harvest_inquiry_optional_labels(text: str) -> dict[str, str]:
"""
陆运询价非必填标签:通关口岸。
核对卡写「通关口岸(非必填):」,销售可能照抄或只写「通关口岸皇岗」。
有无冒号都收。不猜口岸名。
"""
return _harvest_labels(text, _OPTIONAL_INQUIRY_LABELS)
def _merge_optional_inquiry_labels(text: str, facts: dict[str, str]) -> dict[str, str]:
"""把原话里的通关口岸写进询价事实。有值才覆盖。"""
from agent.schema.field_validate import normalize_facts
merged = dict(facts or {})
extra = normalize_facts(harvest_inquiry_optional_labels(text))
for key, val in extra.items():
if val:
merged[key] = val
return merged
def _harvest_labels(text: str, labels: tuple[str, ...]) -> dict[str, str]:
"""按标签切原话。同一起点只留最长标签。"""
raw = text or ""
hits: list[tuple[int, str]] = []
for label in _COLLAB_LABELS:
for label in labels:
start = 0
while True:
pos = raw.find(label, start)
@@ -181,7 +215,7 @@ def harvest_collab_labels(text: str) -> dict[str, str]:
if not hits:
return {}
hits.sort()
# 同一起点只留最长标签,避免 HS 抢 HS编码。
# 同一起点只留最长标签,避免 HS 抢 HS编码、通关口岸抢「通关口岸(非必填)」。
kept: list[tuple[int, str]] = []
for pos, label in hits:
if kept and kept[-1][0] == pos and len(label) <= len(kept[-1][1]):
+41 -2
View File
@@ -19,6 +19,42 @@ from agent.llm.modes import LlmMode
logger = logging.getLogger(__name__)
def message_plain_text(msg: dict[str, Any]) -> str:
"""
把 assistant message 收成纯文本。
千问视觉的 content 经常是 [{text: ...}] 数组,不是字符串;
思考模式打开时正文可能只在 reasoning_content。两者都漏会让识图变「没看清」。
"""
if not isinstance(msg, dict):
return ""
text = _content_to_text(msg.get("content"))
if text:
return text
reason = msg.get("reasoning_content")
if isinstance(reason, str) and reason.strip():
return reason.strip()
return ""
def _content_to_text(content: Any) -> str:
"""字符串或千问多模态数组 → 纯文本。"""
if isinstance(content, str):
return content.strip()
if isinstance(content, list):
parts: list[str] = []
for item in content:
if isinstance(item, str) and item.strip():
parts.append(item.strip())
elif isinstance(item, dict):
piece = item.get("text") if "text" in item else item.get("content")
got = _content_to_text(piece)
if got:
parts.append(got)
return "\n".join(parts).strip()
return ""
@dataclass
class LlmHttpResult:
ok: bool
@@ -72,7 +108,7 @@ class LlmHttpClient:
mode_val = mode.value if isinstance(mode, LlmMode) else str(mode)
if provider == "qwen":
if mode_val == LlmMode.VISION.value:
return self.settings.qwen_vl_model or "qwen-vl-plus"
return self.settings.qwen_vl_model or "qwen3-vl-plus"
return self.settings.qwen_text_model or "qwen-plus"
return self.settings.deepseek_model or "deepseek-chat"
@@ -115,6 +151,9 @@ class LlmHttpClient:
"messages": messages,
"temperature": temperature,
}
if provider == "qwen":
# 识图只要可见原文。思考开着时 content 常为空,销售侧就会「认不到」。
body["enable_thinking"] = False
if tools:
body["tools"] = tools
if tool_choice is not None:
@@ -147,7 +186,7 @@ class LlmHttpClient:
tool_calls: list[dict[str, Any]] = []
if choices:
msg = (choices[0].get("message") or {})
content = str(msg.get("content") or "")
content = message_plain_text(msg if isinstance(msg, dict) else {})
raw_calls = msg.get("tool_calls") or []
if isinstance(raw_calls, list):
tool_calls = [c for c in raw_calls if isinstance(c, dict)]
@@ -33,6 +33,7 @@ _FIELD_KEYS = (
"箱型箱量",
"车型/数量",
"车型数量",
"通关口岸",
)
@@ -127,7 +128,10 @@ def build_extract_messages(text: str) -> list[dict[str, str]]:
"10件/3pcs→空运件数;海运同样数字+件也要写入货量;100KGS/180kg/1.5KG/100公斤→毛重;2CBM/4.2cbm/2.5立方/2方→体积;"
"托盘/散货/卡板/纸箱/木箱→包装方式(销售说什么记什么,散货也要抽)。"
"9月15日/9月15号/2026-09-15→报价日期,规范成 yyyy-MM-dd,未写年用今年。"
"陆运「通关口岸:皇岗」「通关口岸皇岗」「过皇岗」有说就写入通关口岸,没说留空,禁止编造口岸。"
"「从南京飞吉隆坡」起运港=南京、目的港=吉隆坡;「北京飞吉隆坡」起运港=北京。"
"原话是机场三字码或航线就原样记,禁止翻译成城市名:"
"SZX-CGK / SZX/CGK / SZX到CGK → 起运港=SZX、目的港=CGK,不要写成深圳、雅加达。"
"换城市必须覆盖旧值,不要沿用上一句的港口。"
"运输方式仅在原话能判断时填 AIR/SEA/LAND,否则 UNKNOWN。"
),
+140 -4
View File
@@ -1,30 +1,166 @@
"""
千问视觉:读图 mode 壳。
千问视觉:只读图上可见原文。
本文件职责:把图片交给 qwen3-vl-plus,抄出看得见的文字。
属于 llm 包。依赖:LlmHttpClient(生产);单测注入 chat_fn。
禁止:填询价字段 JSON、算最终缺项、发明港口/重量/日期;禁止在回调线程长等(由 Worker 槽调用)。
"""
from __future__ import annotations
from typing import Any
import logging
from typing import Any, Optional
from agent.llm.modes import LlmMode
logger = logging.getLogger(__name__)
MODE = LlmMode.VISION
VISION_SYSTEM = (
"只读出图片上能看见的文字和数字,原样抄写,一行不漏。"
"英文、机场三字码、航线(如 SZX-CGK、SZX/CGK)必须保持原样,禁止翻译成中文城市名。"
"看不清的字不要猜。不要补询价缺项,不要发明港口、重量或日期。"
"只有整张图完全没有文字时才输出空;只要看得见字就必须抄出来。"
)
def sniff_image_mime(raw: bytes) -> str:
"""
按文件头认图片类型。企微截图经常是 PNG,不能一律当 JPEG 送给千问。
"""
blob = raw or b""
if len(blob) < 12:
return ""
if blob.startswith(b"\x89PNG\r\n\x1a\n"):
return "image/png"
if blob.startswith(b"\xff\xd8\xff"):
return "image/jpeg"
if blob[:4] == b"RIFF" and blob[8:12] == b"WEBP":
return "image/webp"
if blob.startswith(b"GIF87a") or blob.startswith(b"GIF89a"):
return "image/gif"
if blob[4:8] == b"ftyp":
return "image/heic"
if blob.startswith(b"BM"):
return "image/bmp"
return ""
def mode_meta() -> dict[str, Any]:
return {
"mode": MODE.value,
"provider": "qwen",
"purpose": "vision",
"implemented": False,
"implemented": True,
}
def _pack_text(text: str, *, stub: bool = False) -> dict[str, Any]:
body = (text or "").strip()
return {
"ok": True,
"stub": stub,
"mode": MODE.value,
"text": body,
"empty": not bool(body),
"error": "",
}
def invoke_shell(*, payload: dict[str, Any]) -> dict[str, Any]:
"""
无网/单测入口:assistant_text 当作图上可见原文。
没有该键时保持失败码,避免假装已经读图。
"""
if "assistant_text" in (payload or {}):
packed = _pack_text(str(payload.get("assistant_text") or ""), stub=True)
return packed
return {
"ok": False,
"stub": True,
"mode": MODE.value,
"text": "",
"empty": True,
"error": "vision_shell_not_implemented",
"echo_keys": sorted(payload.keys()),
"echo_keys": sorted((payload or {}).keys()),
}
def _vision_messages(*, image_b64: str, mime: str, image_url: str) -> list[dict[str, Any]]:
"""拼兼容 OpenAI 的图文消息。优先 data URL,其次 http 图链。"""
url = (image_url or "").strip()
if not url and image_b64:
kind = (mime or "image/jpeg").strip() or "image/jpeg"
url = f"data:{kind};base64,{image_b64}"
user_content: list[dict[str, Any]] = []
if url:
user_content.append({"type": "image_url", "image_url": {"url": url}})
user_content.append({"type": "text", "text": "请只抄写图上可见文字。"})
return [
{"role": "system", "content": VISION_SYSTEM},
{"role": "user", "content": user_content},
]
def invoke_vision(
*,
payload: dict[str, Any],
chat_fn: Optional[Any] = None,
allow_network: bool = False,
) -> dict[str, Any]:
"""
读一张图,返回可见原文。
chat_fn:单测注入,签名同 LlmHttpClient.chat。
allow_network=False 且无 chat_fn:走 invoke_shell,不打真网。
"""
data = dict(payload or {})
image_b64 = str(data.get("image_b64") or "")
image_url = str(data.get("image_url") or "")
mime = str(data.get("mime") or "").strip() or "image/jpeg"
if chat_fn is None and not allow_network:
return invoke_shell(payload=data)
if not image_b64 and not image_url:
logger.info("vision 拒绝:没有图")
return {
"ok": False,
"stub": False,
"mode": MODE.value,
"text": "",
"empty": True,
"error": "no_image",
}
if chat_fn is None:
from agent.llm.http_client import LlmHttpClient
client = LlmHttpClient.from_settings()
chat_fn = client.chat
messages = _vision_messages(
image_b64=image_b64,
mime=mime,
image_url=image_url,
)
result = chat_fn(mode=MODE, messages=messages)
if not getattr(result, "ok", False):
err = str(getattr(result, "error", "") or "vision_failed")
logger.info("vision 失败 err=%s", err)
return {
"ok": False,
"stub": False,
"mode": MODE.value,
"text": "",
"empty": True,
"error": err,
}
packed = _pack_text(str(getattr(result, "content", "") or ""))
packed["stub"] = False
logger.info(
"vision 读完 ok=%s empty=%s text_len=%s mime=%s",
True,
packed.get("empty"),
len(str(packed.get("text") or "")),
mime,
)
return packed