背景:逐图框审查固定「整体 + 2x2 象限」截图,大图幅(A0/A1)读不清细节。 改为按**实体分布**自适应决定截几张、截哪里。 - 新增 dwg_review/tiling.py(纯函数): SHEET_SIZES_MM / frame_world_size / frame_scale(sheet_scale>0 用之,否则由 bbox/纸张反算,否则 default)/ effective_px(与 downsample 同规则)/ world_per_px / is_text_type(标准名 + 自定义类名含 TEXT/LABEL/DIM/NOTE/TAG/ ANNO/TITLE)/ cluster_text_flags / tiles_for_box / subdivide / plan_regions (文字簇优先、空白区不出图、max_tiles 截断、窗口夹到图框范围) - 新增 verify_vlm_resolution_offline.py:PIL 合成类图纸图(精确像素字高 + 可控线间距/线宽/虚线)→ 问 VLM 强约束 JSON → 定 R_text / R_line。 实测:R_text=12px(黑底;白底 10px);R_line=6px。 另:实测该模型**无法可靠计数**(6 条线在 2~10px 间距下答 4/10/4/5/4/4), 故线条判据改用「1 条 vs 2 条」二选一。 - config 新增 TILING_ENABLED / TEXT_HEIGHT_MM / TEXT_READABLE_PX=12 / LINE_READABLE_PX=6 / TILE_MAX_COUNT / TILE_OVERLAP / HEATMAP_CELL_DIV / HEATMAP_CLUSTER_DILATE / HEATMAP_MAX_AREA_FRAC;EVIDENCE_DETAIL_TILES 与 EVIDENCE_MAX_IMAGES 保留作回退路径参数 - llm_client 新增 image_size_b64(测量绘图区实际像素,决定单张覆盖范围) - phases/evidence.capture_images 支持瓦片规划:先截整体图并测像素 → plan_regions 成功用规划窗口,否则退回 sub_windows(行为零变化); 末尾始终复位整体视口。collect_frame 传实体/文字 bbox;任一信封 total>returned(截断)→ 退回固定策略,避免误删有内容的瓦片 - 测试:tests/test_tiling.py(28 项)、tests/test_evidence.py 新增规划路径与 降级用例、test_config / test_llm_client 补新常量与 image_size_b64; 全量 283 项全绿
217 lines
7.1 KiB
Python
217 lines
7.1 KiB
Python
"""`dwg_review.llm_client` 单元测试(绝不真实调用 LLM)。"""
|
|
|
|
import base64
|
|
import io
|
|
|
|
import pytest
|
|
|
|
from dwg_review import config
|
|
from dwg_review import llm_client as llm_module
|
|
from dwg_review.llm_client import LlmClient, LlmError, downsample_image_b64
|
|
from tests.fakes import llm_text
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 假 OpenAI SDK
|
|
# ---------------------------------------------------------------------------
|
|
class _Completions:
|
|
def __init__(self, outer):
|
|
self.outer = outer
|
|
|
|
def create(self, **kwargs):
|
|
self.outer.kwargs.append(kwargs)
|
|
if self.outer.raises:
|
|
raise RuntimeError("upstream boom")
|
|
return self.outer.result
|
|
|
|
|
|
class _Chat:
|
|
def __init__(self, outer):
|
|
self.completions = _Completions(outer)
|
|
|
|
|
|
class _FakeOpenAI:
|
|
def __init__(self, **kwargs):
|
|
self.init_kwargs = kwargs
|
|
self.kwargs = []
|
|
self.raises = False
|
|
self.result = None
|
|
self.chat = _Chat(self)
|
|
|
|
|
|
@pytest.fixture
|
|
def fake_openai(monkeypatch):
|
|
monkeypatch.setattr(llm_module, "OpenAI", _FakeOpenAI)
|
|
monkeypatch.setattr(llm_module.time, "sleep", lambda *_: None)
|
|
return _FakeOpenAI
|
|
|
|
|
|
def build_client(fake_openai, **kw):
|
|
return LlmClient(api_key="sk-test", **kw)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 构造
|
|
# ---------------------------------------------------------------------------
|
|
def test_init_requires_api_key(monkeypatch):
|
|
monkeypatch.setattr(config, "API_KEY", "")
|
|
with pytest.raises(LlmError):
|
|
LlmClient()
|
|
|
|
|
|
def test_init_passes_key_and_base_url(fake_openai):
|
|
client = LlmClient(api_key="sk-abc", base_url="https://example.test")
|
|
assert client._client.init_kwargs["api_key"] == "sk-abc"
|
|
assert client._client.init_kwargs["base_url"] == "https://example.test"
|
|
|
|
|
|
def test_init_uses_config_defaults(fake_openai, monkeypatch):
|
|
monkeypatch.setattr(config, "MODEL_NAME", "test-model")
|
|
monkeypatch.setattr(config, "BASE_URL", "https://cfg.test")
|
|
client = LlmClient(api_key="sk-abc")
|
|
assert client.model == "test-model"
|
|
assert client._client.init_kwargs["base_url"] == "https://cfg.test"
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# chat / chat_text
|
|
# ---------------------------------------------------------------------------
|
|
def test_chat_returns_raw_response(fake_openai):
|
|
client = build_client(fake_openai)
|
|
client._client.result = llm_text("hi")
|
|
resp = client.chat([{"role": "user", "content": "x"}])
|
|
assert resp.choices[0].message.content == "hi"
|
|
|
|
|
|
def test_chat_includes_tools_only_when_present(fake_openai):
|
|
client = build_client(fake_openai)
|
|
client._client.result = llm_text("x")
|
|
client.chat([{"role": "user", "content": "x"}])
|
|
assert "tools" not in client._client.kwargs[0]
|
|
|
|
client.chat([{"role": "user", "content": "x"}], tools=[{"type": "function"}])
|
|
assert client._client.kwargs[1]["tools"] == [{"type": "function"}]
|
|
|
|
|
|
def test_chat_wraps_exception_into_llmerror(fake_openai):
|
|
client = build_client(fake_openai)
|
|
client._client.raises = True
|
|
with pytest.raises(LlmError):
|
|
client.chat([{"role": "user", "content": "x"}], retries=0)
|
|
|
|
|
|
def test_chat_retries_then_succeeds(fake_openai):
|
|
client = build_client(fake_openai)
|
|
|
|
class Flaky(_Completions):
|
|
def __init__(self, outer):
|
|
super().__init__(outer)
|
|
self.n = 0
|
|
|
|
def create(self, **kwargs):
|
|
self.n += 1
|
|
if self.n < 3:
|
|
raise RuntimeError("transient")
|
|
return llm_text("ok")
|
|
|
|
client._client.chat.completions = Flaky(client._client)
|
|
resp = client.chat([{"role": "user", "content": "x"}], retries=2)
|
|
assert resp.choices[0].message.content == "ok"
|
|
assert client._client.chat.completions.n == 3
|
|
|
|
|
|
def test_chat_text_returns_assistant_text(fake_openai):
|
|
client = build_client(fake_openai)
|
|
client._client.result = llm_text("报告正文")
|
|
assert client.chat_text("sys", "user") == "报告正文"
|
|
|
|
|
|
def test_image_content_format():
|
|
block = LlmClient.image_content("QUJD", "image/jpeg")
|
|
assert block == {
|
|
"type": "image_url",
|
|
"image_url": {"url": "data:image/jpeg;base64,QUJD"},
|
|
}
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 截图下采样
|
|
# ---------------------------------------------------------------------------
|
|
def test_downsample_passthrough_without_pil(monkeypatch):
|
|
monkeypatch.setattr(llm_module, "HAS_PIL", False)
|
|
assert downsample_image_b64("QUJD", "image/png", max_edge=10) == ("QUJD", "image/png")
|
|
|
|
|
|
def test_downsample_passthrough_when_max_edge_zero():
|
|
assert downsample_image_b64("QUJD", "image/png", max_edge=0) == ("QUJD", "image/png")
|
|
|
|
|
|
def test_downsample_passthrough_when_no_resize_needed():
|
|
pytest.importorskip("PIL")
|
|
from PIL import Image
|
|
|
|
buf = io.BytesIO()
|
|
Image.new("RGB", (100, 80), "white").save(buf, format="PNG")
|
|
b64 = base64.b64encode(buf.getvalue()).decode("ascii")
|
|
out, mime = downsample_image_b64(b64, "image/png", max_edge=1280)
|
|
assert out == b64 and mime == "image/png"
|
|
|
|
|
|
def test_downsample_resizes_large_image():
|
|
pytest.importorskip("PIL")
|
|
from PIL import Image
|
|
|
|
buf = io.BytesIO()
|
|
Image.new("RGB", (2000, 1000), "white").save(buf, format="PNG")
|
|
b64 = base64.b64encode(buf.getvalue()).decode("ascii")
|
|
out, mime = downsample_image_b64(b64, "image/png", max_edge=500)
|
|
assert mime == "image/png"
|
|
img = Image.open(io.BytesIO(base64.b64decode(out)))
|
|
assert max(img.size) <= 500
|
|
|
|
|
|
def test_downsample_converts_to_jpeg():
|
|
pytest.importorskip("PIL")
|
|
from PIL import Image
|
|
|
|
buf = io.BytesIO()
|
|
Image.new("RGB", (2000, 1000), "white").save(buf, format="PNG")
|
|
b64 = base64.b64encode(buf.getvalue()).decode("ascii")
|
|
out, mime = downsample_image_b64(b64, "image/png", max_edge=400, fmt="jpeg")
|
|
assert mime == "image/jpeg"
|
|
assert Image.open(io.BytesIO(base64.b64decode(out))).format == "JPEG"
|
|
|
|
|
|
def test_downsample_bad_base64_returns_original():
|
|
pytest.importorskip("PIL")
|
|
bad = base64.b64encode(b"this is not an image").decode("ascii")
|
|
assert downsample_image_b64(bad, "image/png", max_edge=10) == (bad, "image/png")
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 图片尺寸探测(瓦片规划用)
|
|
# ---------------------------------------------------------------------------
|
|
def test_image_size_returns_none_without_pil(monkeypatch):
|
|
monkeypatch.setattr(llm_module, "HAS_PIL", False)
|
|
assert llm_module.image_size_b64("QUJD") is None
|
|
|
|
|
|
def test_image_size_returns_none_on_empty():
|
|
assert llm_module.image_size_b64("") is None
|
|
|
|
|
|
def test_image_size_returns_none_on_bad_data():
|
|
pytest.importorskip("PIL")
|
|
bad = base64.b64encode(b"not an image").decode("ascii")
|
|
assert llm_module.image_size_b64(bad) is None
|
|
|
|
|
|
def test_image_size_reads_dimensions():
|
|
pytest.importorskip("PIL")
|
|
from PIL import Image
|
|
|
|
buf = io.BytesIO()
|
|
Image.new("RGB", (321, 123), "white").save(buf, format="PNG")
|
|
b64 = base64.b64encode(buf.getvalue()).decode("ascii")
|
|
assert llm_module.image_size_b64(b64) == (321, 123)
|