langchain使用jina-embeddings构建Chroma向量库,解决加载模型初始化失败

摘要

使用 {"trust_remote_code":True} 传递给 langchain_community.embeddings 的 SentenceTransformerEmbeddings ,逐步解析 jinaai/jina-embeddings-v2-base-en 编码模型初始化加载异常的问题。

背景

首先先说一下,最近的研究方向,想构建一个向量数据库,做一些RAG和相似文本筛选方面的实验。

接下来,我们来看看 LangChain 官方给的示例代码。

https://python.langchain.com/v0.2/docs/integrations/vectorstores/chroma/

python 复制代码
# import
from langchain_chroma import Chroma
from langchain_community.document_loaders import TextLoader
from langchain_community.embeddings.sentence_transformer import (
    SentenceTransformerEmbeddings,
)
from langchain_text_splitters import CharacterTextSplitter

# load the document and split it into chunks
loader = TextLoader("../../how_to/state_of_the_union.txt")
documents = loader.load()

# split it into chunks
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)

# create the open-source embedding function
embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")

# load it into Chroma
db = Chroma.from_documents(docs, embedding_function)

# query it
query = "What did the president say about Ketanji Brown Jackson"
docs = db.similarity_search(query)

# print results
print(docs[0].page_content)

官方使用的是 all-MiniLM-L6-v2 编码模型。笔者并不想用这个模型,在https://huggingface.co/models?sort=trending&search=embedding上搜索"embedding"关键词。

jina-embeddings-v2-base-en 排在第一位,有182k的下载量,故笔者想使用这个模型作为向量编码模型。

加载模型:

python 复制代码
# create the open-source embedding function
embedding_function = SentenceTransformerEmbeddings(model_name="jinaai/jina-embeddings-v2-base-en")

Output:

python 复制代码
No sentence-transformers model found with name jinaai/jina-embeddings-v2-base-en. Creating a new one with mean pooling.
Some weights of BertModel were not initialized from the model checkpoint at jinaai/jina-embeddings-v2-base-en and are newly initialized: ['embeddings.position_embeddings.weight', 'encoder.layer.0.intermediate.dense.bias', 'encoder.layer.0.intermediate.dense.weight', 'encoder.layer.0.output.LayerNorm.bias', 'encoder.layer.0.output.LayerNorm.weight', 'encoder.layer.0.output.dense.bias', 'encoder.layer.0.output.dense.weight', 'encoder.layer.1.intermediate.dense.bias', 'encoder.layer.1.intermediate.dense.weight',       
                                             ......                                         'encoder.layer.8.output.dense.bias', 'encoder.layer.8.output.dense.weight', 'encoder.layer.9.intermediate.dense.bias', 'encoder.layer.9.intermediate.dense.weight', 'encoder.layer.9.output.LayerNorm.bias', 'encoder.layer.9.output.LayerNorm.weight', 'encoder.layer.9.output.dense.bias', 'encoder.layer.9.output.dense.weight']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.

在加载jinaai/jina-embeddings-v2-base-en模型的时候,提醒我们很多层的权重,重新初始化了,必须要引起重视。如果你忽视了这个报错,你会得到的错误的结果。

Tips:

其实一开始我也没有重视,忽视了这个报错,因为我自认为参考LangChain官方的代码,可以不用理会这个报错。

后果就是,模型每次重新初始化,权重都会变化,导致得到的编码向量不一致,这才让我引起重视,不得不解决这个问题。

那么笔者是如何解决这个问题的呢?

通过浏览 https://huggingface.co/jinaai/jina-embeddings-v2-base-en 的说明文档,发现在文档中早有说明了。

通过浏览上述的说明,解决方法就是在加载模型时,传入 trust_remote_code=True

可以看到 LangChain 的 SentenceTransformerEmbeddings 接收一个 model_kwargs 字典参数。我都不用看源码,就能想到 model_kwargs 会传递给模型进行初始化。

python 复制代码
# create the open-source embedding function
embedding_function = SentenceTransformerEmbeddings(
    model_name="jinaai/jina-embeddings-v2-base-en",
    model_kwargs={"trust_remote_code":True}
    )

运行上述代码,传入{"trust_remote_code":True} 就不会报警告了。

接下来的部分,大家就可以浏览 LangChain 上的教程,进行进一步的学习了。

附录

若无法连接 huggingface,可尝试使用 proxy:

只需运行下述代码,与huggingface的连接,就会走proxy

python 复制代码
import os
os.environ['HTTP_PROXY'] = 'http://127.0.0.1:7890'
os.environ['HTTPS_PROXY'] = 'http://127.0.0.1:7890'

参考资料

相关推荐
用户84913717547165 小时前
🚀5 分钟实现 Markdown 智能摘要生成器:LangChain + OpenAI 实战教程
langchain·openai
金汐脉动8 小时前
实践指南:从零开始搭建RAG驱动的智能问答系统
langchain
MrGaoGang1 天前
AI应用开发:LangGraph+MCP
前端·人工智能·langchain
西部荒野子1 天前
LangChain.js 中的 Runnable 系统
langchain
大尾巴青年1 天前
06 一分钟搞懂langchain的Agent是如何工作的
langchain·llm
敲键盘的小夜猫2 天前
LangChain核心之Runnable接口底层实现
langchain
疯狂的小强呀2 天前
基于langchain的简单RAG的实现
python·langchain·rag检索增强
用户711283928472 天前
LangChain(三) LCEL
人工智能·langchain
啾啾大学习2 天前
LangChain快速筑基(带代码)P3-连续对话Memory
langchain
啾啾大学习2 天前
LangChain快速筑基(带代码)P0-DeepSeek对话与联网搜索
langchain