解决离线服务器无法加载HuggingFaceEmbeddings向量化模型的问题

由于服务器是离线的,因此我先在本地到huggingface官网下载模型text2vec,然后上传到服务器上运行,报错:

(MaxRetryError('HTTPSConnectionPool(host=\'huggingface.co\', port=443): Max retries exceeded with url: /api/models/sentence-transformers/text2vec-base-chinese/revision/main (Caused by NameResolutionError("<urllib3.connection.HTTPSConnection object at 0x7f12ebf56350>: Failed to resolve \'huggingface.co\' ([Errno -3] Temporary failure in name resolution)"))'), '(Request ID: d787098f-eabd-4f11-8150-623bd99a55e2)')

An error happened while trying to locate the files on the Hub and we cannot find the appropriate snapshot folder for the specified revision on the local disk. Please check your internet connection and try again.

huggingface_hub.utils._errors.LocalEntryNotFoundError: Cannot find an appropriate cached snapshot folder for the specified revision on the local disk and outgoing traffic has been disabled. To enable repo look-ups and downloads online, pass 'local_files_only=False' as input.

HuggingFaceEmbeddings它识别不了这个路径下的文件,因此我将cache_folder的值换成'shibing624/text2vec-base-chinese'依然报相同的错误。

然后我去官网看如何调用这个模型,选择了下面这种方式:

再次报错:

db = store_chroma(documents,embeddings)

AttributeError: 'BertModel' object has no attribute 'embed_documents'

也就是说加载完向量化模型后,我初始化了Chroma向量数据库,然后对传入的doc文档数据进行向量化,它会提示找不到embed_documents这个属性。本身BertModel是没有这个属性的,但是Chroma却需要调用这个属性来实现文档向量化(通过HuggingFaceEmbeddings来加载模型就不会报错,但是离线服务器上用不了)。因此,自己写一个embed_documents来实现就好啦,如下所示:

先在load_embedding_mode里面添加一个方法embedding_function,然后再建立一个类:

最后再调用这个类,就能正常对文档进行向量化和本地持久化了。

复制代码
embedding_function = EmbeddingFunction(load_embedding_mode())
db = Chroma(embedding_function=embedding_function, persist_directory='VectorStore')
相关推荐
chao_7891 天前
针对“仅某个地区出现Bug”的原因分析与解决方案
测试用例·bug
帅帅哥的兜兜1 天前
Bug问题
bug
养意1 天前
git提交代码和解决冲突修复bug
git·bug
东方不败之鸭梨的测试笔记2 天前
20250605车充安服务器受木马攻击导致服务不可用
bug
Htht1112 天前
【Qt】之【Get√】【Bug】通过值捕获(或 const 引用捕获)传进 lambda,会默认复制成 const
数据库·bug
前端发现2 天前
如何用 pnpm patch 给 element-plus 打补丁修复线上 bug(以 2.4.4 修复 PR#15197 为例)
bug
可乐鸡翅好好吃3 天前
通过BUG(prvIdleTask、pxTasksWaitingTerminatio不断跳转问题)了解空闲函数(prvIdleTask)和TCB
c语言·stm32·单片机·嵌入式硬件·bug·keil
神膘护体小月半3 天前
bug 记录 - 使用 el-dialog 的 before-close 的坑
前端·javascript·bug
顽强d石头3 天前
bug:undefined is not iterable (cannot read property Symbol(Symbol.iterator))
前端·bug
阿松のblog4 天前
opencv使用经典bug
人工智能·opencv·bug