python-3.x - ValueError : Expected IDs to be a non-empty list, 在 Chroma 中得到 []

标签 python-3.x openai-api langchain large-language-model chromadb

**ValueError:** Expected IDs to be a non-empty list, got []

**Traceback:**
File "C:\Users\scite\Desktop\HAMBOTAI\HAMBotAI\HAMBotAI\homehambotai.py", line 96, in app
    db = Chroma.from_documents(texts, embeddings)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\scite\AppData\Roaming\Python\Python311\site-packages\langchain_community\vectorstores\chroma.py", line 771, in from_documents
    return cls.from_texts(
           ^^^^^^^^^^^^^^^
File "C:\Users\scite\AppData\Roaming\Python\Python311\site-packages\langchain_community\vectorstores\chroma.py", line 729, in from_texts
    chroma_collection.add_texts(
File "C:\Users\scite\AppData\Roaming\Python\Python311\site-packages\langchain_community\vectorstores\chroma.py", line 324, in add_texts
    self._collection.upsert(
File "C:\Users\scite\AppData\Roaming\Python\Python311\site-packages\chromadb\api\models\Collection.py", line 449, in upsert
    ) = self._validate_embedding_set(
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\scite\AppData\Roaming\Python\Python311\site-packages\chromadb\api\models\Collection.py", line 512, in _validate_embedding_set
    valid_ids = validate_ids(maybe_cast_one_to_many_ids(ids))
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\scite\AppData\Roaming\Python\Python311\site-packages\chromadb\api\types.py", line 228, in validate_ids
    raise ValueError(f"Expected IDs to be a non-empty list, got {ids}")

代码片段:

if 'processed' in query_params:
    # Create a temporary text file
    with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as temp_file:
        temp_file.write(text)
        temp_file_path = temp_file.name

    # load document
    loader = TextLoader(temp_file_path)
    documents = loader.load()
    # split the documents into chunks
    text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
    texts = text_splitter.split_documents(documents)
    # select which embeddings we want to use
    embeddings = OpenAIEmbeddings()
    # ids =[str(i) for i in range(1, len(texts) + 1)]
    # create the vectorestore to use as the index
    db = Chroma.from_documents(texts, embeddings)
    # expose this index in a retriever interface
    retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 2})
    # create a chain to answer questions
    qa = ConversationalRetrievalChain.from_llm(OpenAI(), retriever)
    chat_history = []
    # query = "What's the Name of patient and doctor as mentioned in the data?"
    # result = qa({"question": query, "chat_history": chat_history})
    # st.write("Patient and Doctor name:", result['answer'])
    #
    # chat_history = [(query, result["answer"])]
    query = "Provide summary of medical and health related info from this data in points, every point should be in new line (Formatted in HTML)?"
    result = qa({"question": query, "chat_history": chat_history})
    toshow = result['answer']
    # chat_history = [(query, result["answer"])]
    # chat_history.append((query, result["answer"]))
    # print(chat_history)

    st.title("Data Fetched From Your Health & Medical Reports")
    components.html(
        f"""
        {toshow}
        """,
        height=250,
        scrolling=True,
    )

    if st.button('Continue to Questionarrie'):
        st.write('Loading')
    st.text("(OR)")
    if st.button('Chat with BotAI'):
        st.title("Chat with BotAI")

我成功地从 llm 获得了我的问题的答案,但是一旦我单击下面的任何按钮“继续调查问卷”/“与 BotAI 聊天”,它就会给出如上所示的错误,但它不应该出现。我想确定主要原因是什么以及如何消除此错误。

最佳答案

错误消息 ValueError: Expected IDs to be a non-empty list, got [] 有点令人困惑,因为实际问题是 documents 是空列表, ids是根据文档here创建的:

# texts created based on documents in Chroma.from_documents
texts = [doc.page_content for doc in documents]

# ids created based on texts in Chroma.add_texts
if ids is None:
   ids = [str(uuid.uuid1()) for _ in texts]

您可以使用以下代码重现错误:

from langchain.vectorstores import Chroma

vectordb = Chroma.from_documents(documents=[])

就您而言,我假设 text 是一个空字符串 "",在拆分 文档时会导致空列表 texts 使用 CharacterTextSplitter

为避免这种情况,请添加检查以确保 text 不为空:

if text and 'processed' in query_params:
   # your code

关于python-3.x - ValueError : Expected IDs to be a non-empty list, 在 Chroma 中得到 [],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77665660/

相关文章:

google-apps-script - 在 Google 电子表格上集成 OpenAI

next.js - 在 Next.js 中使用 Langchain 文档加载器时如何修复 'fs module not found' 错误?

azure - 阻止 AI 在单一响应中继续对话

python自变量从类到装饰器

python - 即使 Pygame 窗口未处于事件状态,也可以继续使用 Pygame 跟踪鼠标

android - 如何在没有人窃取 token 的情况下使用我的移动应用程序中的 API

javascript - LangChain 中的 Vector Stores 存储

python - 使用 zipfile 在跳过列表中的文件时归档目录内容

python - 如何根据类型对列表中的内容进行多次更改?

python - Ren'Py : ModuleNotFoundError: No module named 'netrc'