langchain LlamaCppEmbeddings在第114行出现TypeError,提示错误:float()参数必须是字符串或实数,而不是'list',

3phpmpom  于 2个月前  发布在  Mac
关注(0)|答案(7)|浏览(29)

检查其他资源

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

示例代码

from langchain_community.embeddings import LlamaCppEmbeddings

#Initiate a vectorstore
llama_embed = LlamaCppEmbeddings(model_path="./models/codellama-7b-instruct.Q3_K_M.gguf", n_gpu_layers=10)
texts = ["text"]
embeddings = llama_embed.embed_documents(texts)
print(embeddings)

我正在使用的CodeLlama模型可以从huggingface下载在这里:https://huggingface.co/TheBloke/CodeLlama-7B-Instruct-GGUF/resolve/main/codellama-7b-instruct.Q3_K_M.gguf?download=true

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

Traceback (most recent call last):
文件 "D:\Projects\GenAI_CodeDocs\01-Code\03_embed.py",第6行,在
embeddings = llama_embed.embed_documents(texts)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
文件 "D:\Projects\GenAI_CodeDocs\00-VENV\code_doc\Lib\site-packages\langchain_community\embeddings\llamacpp.py",第114行,在
return [list(map(float, e)) for e in embeddings]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
文件 "D:\Projects\GenAI_CodeDocs\00-VENV\code_doc\Lib\site-packages\langchain_community\embeddings\llamacpp.py",第114行,在
return [list(map(float, e)) for e in embeddings]
TypeError: float() argument must be a string or a real number, not 'list'

描述

在以下行产生的嵌入:
langchain/libs/community/langchain_community/embeddings/llamacpp.py
第113行 58192d6
| | embeddings= [self.client.embed(text) for text in texts] |
给我一个列表的列表,即下面有3个列表,嵌入在下面的第三个列表上。

[  #List 1
    [ -> #List 2
        [-0.3025621473789215, -0.5258509516716003, ...] -> #List 3
        [-0.10983365029096603, 0.02027948945760727, ...]
    ]
]

But the following line 114

langchain/libs/community/langchain_community/embeddings/llamacpp.py
第114行 58192d6
| | return [list(map(float, e)) for e in embeddings] |
评估位于2个列表下方的列表
[
list(map(float, e (List2) )) for e (List2) in embeddings (List1)
]
由于List2的元素是一个列表,我们得到了错误。
TypeError: float() argument must be a string or a real number, not 'list'
将第114行更改为修复错误,但我不知道它会对系统的其他部分产生什么影响。
感谢您查看此问题。

ccgok5k5

ccgok5k51#

另一个可能的修复方法是将位于第三层的嵌入扁平化为第二层的一个单一列表。
修改第114行
langchain/libs/community/langchain_community/embeddings/llamacpp.py
第114行 in 58192d6
| | return [list(map(float, e)) foreinembeddings] |

return [list(map(float, sublist)) for e in embeddings for sublist in e]

正如在工单中所述,我不知道将嵌入扁平化会如何影响搜索和检索的性能或质量,所以将其留给Maven来决定:)
感谢您的支持。

z31licg0

z31licg02#

遇到了相同的错误,所以每个文本都通过嵌入转换成了列表的列表?希望得到官方的解决方案!

dnph8jn4

dnph8jn43#

确认,看起来llama-cpp-python返回的向量列表(每个词一个)而不是只有一个向量。
更新:找到了原因和解决方案abetlen/llama-cpp-python#1288(评论)
还请查看llama-cpp-python中关于嵌入的文档。
在Transformer风格的模型中,有两种主要的嵌入概念:词级和序列级。序列级嵌入是通过将词级嵌入“池化”在一起产生的,通常是通过求平均值或使用第一个词来实现的。

c3frrgcw

c3frrgcw4#

同时还出现了这个错误。

r1wp621o

r1wp621o5#

这个案例有任何更新吗?也得到了这个错误。

y53ybaqx

y53ybaqx6#

对于遇到这个错误信息的人,我找到了一个解决方法。TLDR:

pip install 'llama-cpp-python==0.2.47'

只需将llama-cpp-python降级到0.2.47版本即可。对我来说是有效的。😊

m4pnthwp

m4pnthwp7#

我的解决方案是:重写从LlamaCppEmbeddings继承的类中的方法,包括embed_documents()和embed_query();然后使用[0]对self.client.embed(text)的返回值进行压缩。

相关问题