一、起因
最近在做一个 POC 项目,用了 Dify 框架做了个知识库召回的案例。
其中文字召回没问题,但是客户希望能召回图片,因为客户的文档里是包含图片的。
二、源码分析
一)Dify 知识库能召回图片吗?
Dify 知识库能召回图片吗?所谓“遇事不决翻源码”。带着这个问题我翻找了 Dify (版本为 1.2)的源码,终于找到了。
二)Dify 是如何 RAG 文档的?
在 api/core/rag/extractor/word_extractor.py 中,其中的关键方法是:_extract_images_from_docx,其源码如下:
代码块 def _extract_images_from_docx(self, doc, image_folder): # 创建要保存的文件夹 os.makedirs(image_folder, exist_ok=True) image_count = 0 image_map = {} for rel in doc.part.rels.values(): if "image" in rel.target_ref: image_count += 1 if rel.is_external: url = rel.target_ref response = ssrf_proxy.get(url) if response.status_code == 200: image_ext = mimetypes.guess_extension(response.headers["Content-Type"]) if image_ext is None: continue file_uuid = str(uuid.uuid4()) file_key = "image_files/" + self.tenant_id + "/" + file_uuid + "." + image_ext mime_type, _ = mimetypes.guess_type(file_key) storage.save(file_key, response.content) else: continue else: image_ext = rel.target_ref.split(".")[-1] if image_ext is None: continue # user uuid as file name file_uuid = str(uuid.uuid4()) file_key = "image_files/" + self.tenant_id + "/" + file_uuid + "." + image_ext mime_type, _ = mimetypes.guess_type(file_key) storage.save(file_key, rel.target_part.blob) # save file to db upload_file = UploadFile( tenant_id=self.tenant_id, storage_type=dify_config.STORAGE_TYPE, key=file_key, name=file_key, size=0, extension=str(image_ext), mime_type=mime_type or "", created_by=self.user_id,
为了更好的阅读体验,来飞书看吧:
https://owe5h2o40p7.feishu.cn/docx/IK6TdMuDQowhjrxUmoXckA2ln3c
Comments on "99% 的人都不知道的 Dify 知识库召回图片的方法!" :