Skip to navigation

Adding Fact Triples

How to add fact triples to your Graphiti graph

A “fact triple” consists of two nodes and an edge between them, where the edge typically contains some fact. You can manually add a fact triple of your choosing to the graph like this:

Use an application-authorized group_id for all three objects. Derive the namespace from authenticated application state. Do not accept a group_id from an untrusted request. For more information, read Graph namespacing.

from graphiti_core.nodes import EpisodeType, EntityNode
from graphiti_core.edges import EntityEdge
import uuid
from datetime import datetime
source_name = "Bob"
target_name = "bananas"
source_uuid = "some existing UUID" # This is an existing node, so we use the existing UUID obtained from Neo4j Desktop
target_uuid = str(uuid.uuid4()) # This is a new node, so we create a new UUID
edge_name = "LIKES"
edge_fact = "Bob likes bananas"
source_node = EntityNode(
uuid=source_uuid,
name=source_name,
group_id=""
)
target_node = EntityNode(
uuid=target_uuid,
name=target_name,
group_id=""
)
edge = EntityEdge(
group_id="",
source_node_uuid=source_uuid,
target_node_uuid=target_uuid,
created_at=datetime.now(),
name=edge_name,
fact=edge_fact
)
await graphiti.add_triplet(source_node, edge, target_node)

When you add a fact triple, Graphiti will attempt to deduplicate your passed in nodes and edge with the already existing nodes and edges in the graph. If there are no duplicates, it will add them as new nodes and edges.

You can also avoid constructing EntityEdge or EntityNode objects manually by using Graphiti search results. See Searching the Graph.