KIE: bóc 2 lượt theo trang, mặc định Qwen3.5-4B
- Lượt 1: document_type, document_number, issued_date, issuing_authority, title chỉ trên 3 trang đầu. - Lượt 2: signer chỉ trên trang DLA có Signature/Stamp; ưu tiên trang có Stamp trong thân trang (bỏ dấu giáp lai ở dải 10% mép), tối đa IDP_KIE_MAX_PAGES trang cuối; không có trang nào thì không gọi model. - Viết lại description các trường theo yêu cầu nghiệp vụ. - normalize_answer nhận signer trả về dạng mảng. - IDP_KIE_URL/IDP_KIE_MODEL mặc định trỏ Qwen3.5-4B cổng 9224, không còn dùng chung model OCR (Chandra DLA sinh JSON lan man khi làm KIE). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -208,7 +208,8 @@ def model_server(kie_content):
|
||||
answer = {"choices": [{"finish_reason": "stop", "message": {"content": kie_content}}]}
|
||||
else:
|
||||
answer = {"choices": [{"finish_reason": "stop", "message": {"content":
|
||||
'<div data-label="Paragraph" data-bbox="1 1 500 500">Test</div>'}}]}
|
||||
'<div data-label="Paragraph" data-bbox="1 1 500 500">Test</div>'
|
||||
'<div data-label="Signature" data-bbox="600 800 900 900"></div>'}}]}
|
||||
else:
|
||||
callbacks.append(body)
|
||||
answer = {"ok": True}
|
||||
@@ -244,9 +245,11 @@ def test_extract_fixed_schema_end_to_end_and_callback(tmp_path):
|
||||
|
||||
drain(Worker(settings), settings)
|
||||
|
||||
# Một lượt KIE cho cả tài liệu, gửi kèm ảnh mọi trang.
|
||||
assert len(kie_requests) == 1
|
||||
assert len(kie_requests[0]["messages"][0]["content"]) == 3
|
||||
# Lượt 1 hỏi trường đầu văn bản trên 3 trang đầu, lượt 2 hỏi người ký trên trang có chữ ký.
|
||||
assert len(kie_requests) == 2
|
||||
header, signer = (request["messages"][0]["content"] for request in kie_requests)
|
||||
assert len(header) == 3 and '"document_type"' in header[-1]["text"] and '"signer"' not in header[-1]["text"]
|
||||
assert len(signer) == 3 and '"signer"' in signer[-1]["text"] and '"title"' not in signer[-1]["text"]
|
||||
assert kie_requests[0]["model"] == settings.kie_model
|
||||
assert kie_requests[0]["max_tokens"] == 4096
|
||||
|
||||
@@ -307,7 +310,7 @@ def test_extract_retries_bad_answer_then_gives_up(tmp_path):
|
||||
def test_kie_prompt_schema_and_answer_shape():
|
||||
prompt = kie.render_prompt(3)
|
||||
assert prompt.startswith("Trích xuất thông tin từ ảnh tài liệu (3 trang, ảnh theo thứ tự trang)")
|
||||
assert '- "signer" (object): Người ký ban hành ở cuối văn bản\n - "name"' in prompt
|
||||
assert '- "signer" (object): Người có thẩm quyền ký' in prompt and '\n - "name"' in prompt
|
||||
assert [line for line in prompt.splitlines() if line.startswith('- "')][:5] == [
|
||||
line for line in kie.render_fields(kie.SCHEMA) if line.startswith('- "')][:5]
|
||||
assert kie.render_prompt(1).startswith("Trích xuất thông tin từ ảnh tài liệu và")
|
||||
@@ -319,10 +322,21 @@ def test_kie_prompt_schema_and_answer_shape():
|
||||
"issuing_authority": None, "title": None,
|
||||
"signer": {"name": "Nguyễn Văn A", "position": None}}
|
||||
assert list(answer) == [field["key"] for field in kie.SCHEMA]
|
||||
assert kie.normalize_answer({"signer": [{"name": "Lương Cường", "position": "CHỦ TỊCH"}, {"name": "B"}]},
|
||||
kie.SIGNER_FIELDS) == {"signer": {"name": "Lương Cường", "position": "CHỦ TỊCH"}}
|
||||
|
||||
# Tài liệu dài chỉ gửi các trang đầu và trang cuối (nơi có người ký).
|
||||
assert kie.select_pages(3, 5) == [1, 2, 3]
|
||||
assert kie.select_pages(20, 5) == [1, 2, 3, 4, 20]
|
||||
# Trường đầu văn bản lấy 3 trang đầu; người ký lấy trang DLA có chữ ký/con dấu, tối đa N trang cuối.
|
||||
assert kie.header_pages(1) == [1]
|
||||
assert kie.header_pages(10) == [1, 2, 3]
|
||||
text = [{"label": "Paragraph", "bbox": [0, 0, 1000, 500]}]
|
||||
edge_stamp = [{"label": "Stamp", "bbox": [950, 100, 1000, 200]}]
|
||||
initials = [{"label": "Signature", "bbox": [400, 850, 600, 930]}]
|
||||
seal = [{"label": "Stamp", "bbox": [480, 400, 650, 530]}]
|
||||
assert kie.signer_pages({1: text, 2: text}, 5) == []
|
||||
assert kie.signer_pages({1: edge_stamp, 2: text, 3: initials}, 5) == [1, 3]
|
||||
assert kie.signer_pages({no: edge_stamp for no in range(1, 11)}, 3) == [8, 9, 10]
|
||||
# Có dấu trong thân trang thì chỉ lấy trang đó, bỏ trang chữ ký nháy và dấu giáp lai.
|
||||
assert kie.signer_pages({1: initials + edge_stamp, 2: initials, 3: seal}, 5) == [3]
|
||||
|
||||
|
||||
def test_keeps_empty_visual_blocks_but_rejects_empty_text_blocks():
|
||||
@@ -547,3 +561,4 @@ def test_voffice_decryption_rejects_wrong_key(tmp_path):
|
||||
pass
|
||||
else:
|
||||
raise AssertionError("Invalid encrypted document was accepted")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user