generated from kgod/ai-review-template
自内部仓库剥离深度优化与 RAG 知识库后的交付版本: - Builder 对话式简历生成(FSM + 意图路由 LLM 兜底增强) - 条目级轻度优化:事实覆盖门禁 + STAR/bullet 修复链,功能/简介/成果与技术栈同级保护 - 简历导入:DOCX/PDF 解析、结构归一、手机号脱敏 - PostgreSQL 运行时 + Alembic 迁移链 Co-Authored-By: Claude <noreply@anthropic.com>
77 lines
2.7 KiB
Python
77 lines
2.7 KiB
Python
"""Import parsing speed-ups: slim schema without model evidence (A) + sha256 parse cache (B)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
from typing import Any
|
|
|
|
from docx import Document
|
|
|
|
from app.import_parser import ImportParseOutput, OpenAIResumeImportParser
|
|
from app.resume_import_service import ResumeImportService
|
|
|
|
|
|
class FakeCompletion:
|
|
def __init__(self) -> None:
|
|
self.calls: list[dict[str, Any]] = []
|
|
|
|
def complete(self, **kwargs: Any) -> Any:
|
|
self.calls.append(kwargs)
|
|
return kwargs["schema"].model_validate(
|
|
{
|
|
"basics": {"name": "张三"},
|
|
"sections": [
|
|
{
|
|
"kind": "education",
|
|
"heading": "教育经历",
|
|
"items": [{"fields": {"school": "示例大学", "major": "软件工程"}}],
|
|
}
|
|
],
|
|
"skill_groups": [],
|
|
}
|
|
)
|
|
|
|
|
|
def _docx(text: str) -> bytes:
|
|
document = Document()
|
|
document.add_paragraph(text)
|
|
buffer = io.BytesIO()
|
|
document.save(buffer)
|
|
return buffer.getvalue()
|
|
|
|
|
|
def _service(tmp_path, completion: FakeCompletion) -> ResumeImportService:
|
|
return ResumeImportService(
|
|
storage_root=tmp_path / "imports",
|
|
parser=OpenAIResumeImportParser(completion=completion),
|
|
)
|
|
|
|
|
|
def test_service_uses_slim_schema_without_model_evidence(tmp_path) -> None:
|
|
"""模型不再逐条输出 evidence 引用(本地匹配已覆盖),输出 token 与时间同降。"""
|
|
from app.import_parser_fast import SlimImportParseOutput
|
|
|
|
completion = FakeCompletion()
|
|
prepared = _service(tmp_path, completion).prepare(
|
|
file_name="r.docx", declared_mime=None, content=_docx("张三 示例大学 软件工程")
|
|
)
|
|
|
|
assert completion.calls[0]["schema"] is SlimImportParseOutput
|
|
assert "evidence" not in completion.calls[0]["system_prompt"].casefold()
|
|
assert prepared["document"]["sections"][0]["items"][0]["school"] == "示例大学"
|
|
assert all(item["evidence"] for item in prepared["field_reviews"]) # 本地匹配仍然提供证据
|
|
|
|
|
|
def test_repeated_upload_of_same_file_skips_llm_parse(tmp_path) -> None:
|
|
"""同一文件跨会话重复上传命中 sha256 缓存,不再调 LLM 解析。"""
|
|
completion = FakeCompletion()
|
|
service = _service(tmp_path, completion)
|
|
content = _docx("张三 示例大学 软件工程")
|
|
|
|
first = service.prepare(file_name="a.docx", declared_mime=None, content=content)
|
|
second = service.prepare(file_name="b.docx", declared_mime=None, content=content)
|
|
|
|
assert len(completion.calls) == 1
|
|
assert second["document"] == first["document"]
|
|
assert second["sha256"] == first["sha256"]
|