**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/