# -*- coding: utf-8 -*- """ 阶段 1 验证脚本:检查向量库中的 metadata 是否包含图片信息 运行方式: .\venv\Scripts\python.exe scripts/test_image_metadata.py """ import sys import os # 添加项目根目录到路径 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from knowledge.manager import get_kb_manager def test_image_metadata(): """检查向量库中的 metadata 是否包含图片信息""" print("=" * 60) print("阶段 1 验证:检查 metadata 中的图片信息") print("=" * 60) kb = get_kb_manager() coll = kb.get_collection('public_kb') if not coll: print("❌ public_kb 不存在") return # 获取所有切片 result = coll.get(include=['metadatas', 'documents']) if not result or not result.get('ids'): print("❌ 向量库为空,请先上传一个包含图片的 PDF") return total = len(result['ids']) print(f"\n📊 向量库总切片数: {total}") # 统计图片相关字段 has_images_json = 0 has_image_path = 0 image_chunks = 0 table_chunks = 0 print("\n📋 切片详情(前 20 个):") print("-" * 60) for i, (chunk_id, meta, doc) in enumerate(zip(result['ids'], result['metadatas'], result['documents'])): chunk_type = meta.get('chunk_type', 'text') images_json = meta.get('images_json') image_path = meta.get('image_path') if images_json: has_images_json += 1 if image_path: has_image_path += 1 if chunk_type in ('image', 'chart'): image_chunks += 1 if chunk_type == 'table': table_chunks += 1 # 打印前 20 个切片的详情 if i < 20: print(f"[{i+1}] chunk_type: {chunk_type}") if images_json: print(f" images_json: {images_json[:100]}...") if image_path: print(f" image_path: {image_path}") print() print("-" * 60) print("\n📈 统计结果:") print(f" - 包含 images_json 的切片: {has_images_json}") print(f" - 包含 image_path 的切片: {has_image_path}") print(f" - 图片类型切片 (image/chart): {image_chunks}") print(f" - 表格类型切片: {table_chunks}") # 验证结果 print("\n✅ 验证结果:") if has_images_json > 0: print(f" ✅ images_json 字段已正确写入 ({has_images_json} 个切片)") else: print(" ⚠️ images_json 字段未找到,可能需要重新上传 PDF") if has_image_path > 0: print(f" ✅ image_path 字段已正确写入 ({has_image_path} 个切片)") else: print(" ⚠️ image_path 字段未找到,可能没有独立图片切片") if __name__ == "__main__": if sys.platform == 'win32': sys.stdout.reconfigure(encoding='utf-8') test_image_metadata()