一、起因

最近在做一个 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 知识库召回图片的方法!​" :

Leave a Reply

Your email address will not be published. Required fields are marked *

本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。

最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。 若排除这种情况,可在对应资源底部留言,或联络我们。

对于会员专享、整站源码、程序插件、网站模板、网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。

如果您已经成功付款但是网站没有弹出成功提示,请联系站长提供付款信息为您处理

源码素材属于虚拟商品,具有可复制性,可传播性,一旦授予,不接受任何形式的退款、换货要求。请您在购买获取之前确认好 是您所需要的资源