IndexError:列表索引超出 Streamlit 中的范围

问题描述 投票:0回答:1

因此,我正在尝试构建一个 Streamlit RAG 应用程序,该应用程序从 url 中提取信息并从中学习,然后用户可以向模型询问与 url 中的文章相关的问题,模型将提供合适的答案。

我在我的笔记本上做了这个,它工作得很好,只是在我的streamlit应用程序中遇到了

IndexError: list index out of range
错误,我使用GoogleGenerativeAIEmbeddings和FAISS。

这是代码块

embeddings = GoogleGenerativeAIEmbeddings(model = 'models/embedding-001')
vector_index = FAISS.from_documents(docs,embeddings)

这是来自 stresmlit 应用程序的回溯

IndexError: list index out of range
Traceback:
File "C:\Python312\Lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 584, in _run_script
    exec(code, module.__dict__)
File "C:\Users\owner\Desktop\Projects\nlp\main.py", line 84, in <module>
    vectorstore_openai = FAISS.from_documents(docs, embeddings)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\langchain_core\vectorstores.py", line 550, in from_documents
    return cls.from_texts(texts, embedding, metadatas=metadatas, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\langchain_community\vectorstores\faiss.py", line 931, in from_texts
    return cls.__from(
           ^^^^^^^^^^^
File "C:\Python312\Lib\site-packages\langchain_community\vectorstores\faiss.py", line 888, in __from
    index = faiss.IndexFlatL2(len(embeddings[0]))
                                  ~~~~~~~~~~^^^

就像我上面说的,这在我的笔记本上完美运行,我很困惑为什么会发生这种情况

python machine-learning streamlit langchain faiss
1个回答
0
投票

问题在于从

GoogleGenerativeAIEmbeddings
加载嵌入模型,因此您传递给 FAISS 的
embeddings
列表为空。您可以在传递到 FAISS 之前打印
embeddings
长度来轻松检查这一点。

但是,通常认为更好的做法是在本地下载嵌入模型并提供模型的本地路径,而不是每次都从互联网加载。以下是将模型保存到本地路径的方法:

from langchain.embeddings import GoogleGenerativeAIEmbeddings

embeddings = GoogleGenerativeAIEmbeddings(model='models/embedding-001')
output_dir = 'path/to/save/model'
embeddings.model.save_pretrained(output_dir)

要从本地路径加载嵌入模型,您可以使用以下命令:

embeddings = GoogleGenerativeAIEmbeddings.from_pretrained(model_path)
© www.soinside.com 2019 - 2024. All rights reserved.