Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions llama-index-core/llama_index/core/graph_stores/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,6 @@ def get_triplets(self) -> List[Triplet]:
def add_triplet(self, triplet: Triplet) -> None:
"""Add a triplet."""
subj, rel, obj = triplet
if (subj.id, rel.id, obj.id) in self.triplets:
return

self.triplets.add((subj.id, rel.id, obj.id))
self.nodes[subj.id] = subj
self.nodes[obj.id] = obj
Expand Down
45 changes: 45 additions & 0 deletions llama-index-core/tests/graph_stores/test_simple_lpg_upserts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import pytest

from llama_index.core.graph_stores.simple_labelled import SimplePropertyGraphStore
from llama_index.core.graph_stores.types import EntityNode, Relation


@pytest.mark.asyncio
@pytest.mark.parametrize("use_async", [False, True])
async def test_relation_upsert_updates_existing_properties(use_async: bool) -> None:
store = SimplePropertyGraphStore()
alice = EntityNode(name="Alice", properties={"role": "author"})
bob = EntityNode(name="Bob", properties={"role": "reviewer"})
original = Relation(
label="KNOWS",
source_id=alice.id,
target_id=bob.id,
properties={"confidence": 0.1},
)
other = Relation(label="MENTIONS", source_id=alice.id, target_id=bob.id)
updated = Relation(
label="KNOWS",
source_id=alice.id,
target_id=bob.id,
properties={"confidence": 0.9},
)
store.upsert_nodes([alice, bob])
store.upsert_relations([original, other])

if use_async:
await store.aupsert_relations([updated])
else:
store.upsert_relations([updated])

assert store.get_triplets(relation_names=["KNOWS"]) == [(alice, updated, bob)]
assert store.get_triplets(relation_names=["MENTIONS"]) == [(alice, other, bob)]
assert len(store.graph.triplets) == 2
assert len(store.graph.relations) == 2
assert store.get(ids=[alice.id, bob.id]) == [alice, bob]

if use_async:
await store.aupsert_relations([updated])
else:
store.upsert_relations([updated])
assert len(store.graph.triplets) == 2
assert store.get_triplets(relation_names=["KNOWS"]) == [(alice, updated, bob)]