mirror of
https://github.com/langgenius/dify.git
synced 2026-08-28 21:06:51 +08:00
Merge 088a028e70 into 8031ea6d73
This commit is contained in:
commit
c19142202a
@ -280,17 +280,20 @@ class WeaviateVector(BaseVector):
|
||||
@override
|
||||
def _get_uuids(self, documents: list[Document]) -> list[str]:
|
||||
"""
|
||||
Generates deterministic UUIDs for documents based on their content.
|
||||
|
||||
Uses UUID5 with URL namespace to ensure consistent IDs for identical content.
|
||||
Use Dify's own doc_id (index_node_id, set in each Document's metadata by
|
||||
the caller) as the Weaviate object UUID instead of hashing the page
|
||||
content. This keeps write-time IDs consistent with what delete_by_ids()
|
||||
looks up, and stops segments with identical text across different
|
||||
documents from colliding on the same Weaviate object.
|
||||
Falls back to a fresh random UUID if a document has no usable doc_id.
|
||||
"""
|
||||
URL_NAMESPACE = _uuid.UUID("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
|
||||
|
||||
uuids = []
|
||||
for doc in documents:
|
||||
uuid_val = _uuid.uuid5(URL_NAMESPACE, doc.page_content)
|
||||
uuids.append(str(uuid_val))
|
||||
|
||||
doc_id = (doc.metadata or {}).get("doc_id")
|
||||
if doc_id and self._is_uuid(str(doc_id)):
|
||||
uuids.append(str(doc_id))
|
||||
else:
|
||||
uuids.append(str(_uuid.uuid4()))
|
||||
return uuids
|
||||
|
||||
@override
|
||||
|
||||
@ -759,6 +759,37 @@ class TestWeaviateVector(unittest.TestCase):
|
||||
assert wv._is_uuid("123e4567-e89b-12d3-a456-426614174000") is True
|
||||
assert wv._is_uuid("not-a-uuid") is False
|
||||
|
||||
def test_get_uuids_returns_doc_id_from_metadata(self):
|
||||
"""_get_uuids should reuse Dify's own doc_id instead of hashing content."""
|
||||
wv = WeaviateVector.__new__(WeaviateVector)
|
||||
doc_id = "123e4567-e89b-12d3-a456-426614174000"
|
||||
doc = Document(page_content="same content", metadata={"doc_id": doc_id})
|
||||
|
||||
assert wv._get_uuids([doc]) == [doc_id]
|
||||
|
||||
def test_get_uuids_does_not_collide_for_identical_content_across_documents(self):
|
||||
"""Two documents with identical page_content but different doc_id must not
|
||||
produce the same Weaviate UUID, otherwise one overwrites the other."""
|
||||
wv = WeaviateVector.__new__(WeaviateVector)
|
||||
doc_id_a = "123e4567-e89b-12d3-a456-426614174000"
|
||||
doc_id_b = "223e4567-e89b-12d3-a456-426614174000"
|
||||
doc_a = Document(page_content="duplicate segment text", metadata={"doc_id": doc_id_a})
|
||||
doc_b = Document(page_content="duplicate segment text", metadata={"doc_id": doc_id_b})
|
||||
|
||||
uuids = wv._get_uuids([doc_a, doc_b])
|
||||
|
||||
assert uuids == [doc_id_a, doc_id_b]
|
||||
assert uuids[0] != uuids[1]
|
||||
|
||||
def test_get_uuids_falls_back_to_random_uuid_when_doc_id_missing(self):
|
||||
wv = WeaviateVector.__new__(WeaviateVector)
|
||||
doc = Document(page_content="no doc id here", metadata={})
|
||||
|
||||
uuids = wv._get_uuids([doc])
|
||||
|
||||
assert len(uuids) == 1
|
||||
assert wv._is_uuid(uuids[0])
|
||||
|
||||
def test_delete_by_metadata_field_returns_when_collection_is_missing(self):
|
||||
wv = WeaviateVector.__new__(WeaviateVector)
|
||||
wv._collection_name = self.collection_name
|
||||
|
||||
Loading…
Reference in New Issue
Block a user