Skip to content

Commit f1c7593

Browse files
committed
feat: enhance document handling in hit test results to include source file IDs and update related tests
1 parent 541064b commit f1c7593

3 files changed

Lines changed: 27 additions & 13 deletions

File tree

‎apps/knowledge/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,5 @@ the external vision model or tool runs in the worker.
9191
召回测试 `/hit_test` 的每条分段结果包含 `document_resource_type`,取自所属文档的 `resource_type`:
9292
`image` 表示图片文档,`document` 表示普通文档。工作空间、系统资源管理和系统共享资源的召回测试
9393
使用相同字段;此字段与命中向量类型无关,普通文档中的插图命中仍返回 `document`。
94+
图片文档的结果额外包含 `document_image_id`,取自文档的 `meta.source_file_id`,用于通过现有文件接口显示原图。
95+
该字段不依赖命中的是文本还是图片向量;普通文档以及缺少原文件引用的图片文档返回 `null`。

‎apps/knowledge/serializers/knowledge.py‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,18 +1448,20 @@ def hit_test(self):
14481448
str(paragraph.get("id")): paragraph
14491449
for paragraph in list_paragraph([hit.get("paragraph_id") for hit in hit_list])
14501450
}
1451-
document_resource_type_map = {
1452-
str(document_id): resource_type
1453-
for document_id, resource_type in QuerySet(Document)
1451+
document_map = {
1452+
str(document_id): {"resource_type": resource_type, "source_file_id": source_file_id}
1453+
for document_id, resource_type, source_file_id in QuerySet(Document)
14541454
.filter(id__in={paragraph.get("document_id") for paragraph in paragraph_dict.values()})
1455-
.values_list("id", "resource_type")
1455+
.values_list("id", "resource_type", "meta__source_file_id")
14561456
}
14571457
hit_asset_map = get_hit_asset_map(hit_list)
14581458
result_list = []
14591459
for hit in hit_list:
14601460
paragraph = paragraph_dict.get(str(hit.get("paragraph_id")))
14611461
if paragraph is None:
14621462
continue
1463+
document = document_map.get(str(paragraph.get("document_id")), {})
1464+
document_resource_type = document.get("resource_type", DocumentResourceType.DOCUMENT)
14631465
current_hit = hit
14641466
source_id = current_hit.get("source_id")
14651467
source_type = current_hit.get("source_type")
@@ -1468,9 +1470,10 @@ def hit_test(self):
14681470
result_list.append(
14691471
{
14701472
**paragraph,
1471-
"document_resource_type": document_resource_type_map.get(
1472-
str(paragraph.get("document_id")), DocumentResourceType.DOCUMENT
1473-
),
1473+
"document_resource_type": document_resource_type,
1474+
"document_image_id": document.get("source_file_id")
1475+
if document_resource_type == DocumentResourceType.IMAGE
1476+
else None,
14741477
"similarity": current_hit.get("similarity"),
14751478
"comprehensive_score": current_hit.get("comprehensive_score"),
14761479
"source_id": source_id,

‎apps/knowledge/tests/retrieval/test_vector_metadata.py‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def test_hit_test_decodes_sql_metadata_and_preserves_image_description_hit_type(
112112
{
113113
**paragraph,
114114
"document_resource_type": DocumentResourceType.DOCUMENT,
115+
"document_image_id": None,
115116
"similarity": 0.9,
116117
"comprehensive_score": 0.95,
117118
"source_id": asset_id,
@@ -137,15 +138,18 @@ def test_hit_test_reports_document_origin_independently_of_vector_type(
137138
knowledge_query.filter.return_value.exists.return_value = True
138139
document_query = MagicMock()
139140
query_set.side_effect = {Knowledge: knowledge_query, Document: document_query}.get
141+
image_file_id = "00000000-0000-0000-0000-000000000010"
140142
document_query.filter.return_value.values_list.return_value = [
141-
("image-document", DocumentResourceType.IMAGE),
142-
("ordinary-document", DocumentResourceType.DOCUMENT),
143+
("image-document", DocumentResourceType.IMAGE, image_file_id),
144+
("ordinary-document", DocumentResourceType.DOCUMENT, "00000000-0000-0000-0000-000000000011"),
145+
("image-without-file", DocumentResourceType.IMAGE, None),
143146
]
144147
paragraphs = [
145148
{"id": "image-text", "document_id": "image-document"},
146149
{"id": "image-vector", "document_id": "image-document"},
147150
{"id": "ordinary-text", "document_id": "ordinary-document"},
148151
{"id": "ordinary-image", "document_id": "ordinary-document"},
152+
{"id": "image-without-file", "document_id": "image-without-file"},
149153
]
150154
list_paragraph.return_value = paragraphs
151155
get_vector.return_value.hit_test.return_value = [
@@ -156,8 +160,8 @@ def test_hit_test_reports_document_origin_independently_of_vector_type(
156160
}
157161
for paragraph, source_type, unit_type in zip(
158162
paragraphs,
159-
[SourceType.PARAGRAPH, SourceType.IMAGE, SourceType.PARAGRAPH, SourceType.IMAGE],
160-
["text", "image", "text", "image"],
163+
[SourceType.PARAGRAPH, SourceType.IMAGE, SourceType.PARAGRAPH, SourceType.IMAGE, SourceType.PARAGRAPH],
164+
["text", "image", "text", "image", "text"],
161165
)
162166
]
163167

@@ -179,7 +183,12 @@ def test_hit_test_reports_document_origin_independently_of_vector_type(
179183
DocumentResourceType.IMAGE,
180184
DocumentResourceType.DOCUMENT,
181185
DocumentResourceType.DOCUMENT,
186+
DocumentResourceType.IMAGE,
182187
],
183188
)
184-
self.assertEqual([row["hit_unit_type"] for row in result], ["text", "image", "text", "image"])
185-
document_query.filter.return_value.values_list.assert_called_once_with("id", "resource_type")
189+
self.assertEqual([row["hit_unit_type"] for row in result], ["text", "image", "text", "image", "text"])
190+
self.assertEqual([row["document_image_id"] for row in result], [image_file_id, image_file_id, None, None, None])
191+
self.assertTrue(all("source_file_id" not in row and "image_id" not in row for row in result))
192+
document_query.filter.return_value.values_list.assert_called_once_with(
193+
"id", "resource_type", "meta__source_file_id"
194+
)

0 commit comments

Comments
 (0)