langchain 当重用图时,Neo4jVector与HuggingFaceEmbeddings的兼容性不佳,

uxhixvfz  于 3个月前  发布在  其他
关注(0)|答案(1)|浏览(41)

检查其他资源

  • 我为这个问题添加了一个非常描述性的标题。
  • 我在集成搜索中搜索了LangChain文档。
  • 我使用GitHub搜索找到了一个类似的问题,但没有找到。
  • 我确信这是LangChain中的一个bug,而不是我的代码。
  • 通过更新到LangChain的最新稳定版本(或特定集成包)无法解决这个bug。

示例代码

from langchain_community.vectorstores import Neo4jVector
from langchain_huggingface import HuggingFaceEmbeddings

embeddings = HuggingFaceEmbeddings(
    model_name="sentence-transformers/all-mpnet-base-v2"
)
self.existing_graph_parts = Neo4jVector.from_existing_graph(
    embedding=embeddings,
    url=uri,
    username=username,
    password=password,
    node_label="part",
    text_node_properties=["name"],
    embedding_node_property="embedding",
)

错误信息和堆栈跟踪(如果适用)

Traceback (most recent call last):
  File "D:\graph_rag.py", line 133, in <module>
    graph_rag = GraphRag()
                ^^^^^^^^^^
  File "D:\graph_rag.py", line 44, in __init__
    self.existing_graph_parts = Neo4jVector.from_existing_graph(
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\syh\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_community\vectorstores\neo4j_vector.py", line 1431, in from_existing_graph
    text_embeddings = embedding.embed_documents([el["text"] for el in data])
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\syh\AppData\Local\Programs\Python\Python312\Lib\site-packages\langchain_huggingface\embeddings\huggingface.py", line 87, in embed_documents
    embeddings = self.client.encode(
                 ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\syh\AppData\Local\Programs\Python\Python312\Lib\site-packages\sentence_transformers\SentenceTransformer.py", line 565, in encode
    if all_embeddings[0].dtype == torch.bfloat16:
       ~~~~~~~~~~~~~~^^^
IndexError: list index out of range

描述

抱歉我的英语很差!
当我第一次运行代码时,它运行得很好。
但是当我再次运行代码时,它出现了上面的错误。
我认为错误是因为所有节点都已经有嵌入了,所以在下面的库中运行代码时:
文件: langchain_community\vectorstores\neo4j_vector.py
从第1行开始

while True:
            fetch_query = (
                f"MATCH (n:`{node_label}`) "
                f"WHERE n.{embedding_node_property} IS null "
                "AND any(k in $props WHERE n[k] IS NOT null) "
                f"RETURN elementId(n) AS id, reduce(str='',"
                "k IN $props | str + '\\n' + k + ':' + coalesce(n[k], '')) AS text "
                "LIMIT 1000"
            )
            data = store.query(fetch_query, params={"props": text_node_properties})
            text_embeddings = embedding.embed_documents([el["text"] for el in data])

这段代码将获取一些没有embedding_node_property的节点。因为我的neo4j中的所有节点都已经有嵌入了,所以数据是一个空列表。
然后在下面的代码中,0是从一个空列表的索引中取出的。
文件: sentence_transformers\SentenceTransformer.py
从第563行开始

elif convert_to_numpy:
            if not isinstance(all_embeddings, np.ndarray):
                if all_embeddings[0].dtype == torch.bfloat16:
                    all_embeddings = np.asarray([emb.float().numpy() for emb in all_embeddings])
                else:
                    all_embeddings = np.asarray([emb.numpy() for emb in all_embeddings])

错误就发生在这里。
我已经从机器人那里得到了答案,但我仍然认为这是一个需要修复的bug!
谢谢!

系统信息

langchain==0.2.6
langchain-community==0.2.6
langchain-core==0.2.10
langchain-huggingface==0.0.3
langchain-openai==0.1.10
langchain-text-splitters==0.2.2
windows 11
python3.12

zpjtge22

zpjtge221#

当我使用OpenAIEmbeddings时,这个bug就不会发生了。

相关问题