Skip to content

Commit c25394f

Browse files
committed
Support extra properties for nodes in knowledge graph.
1 parent c4055c4 commit c25394f

File tree

3 files changed

+244
-108
lines changed

3 files changed

+244
-108
lines changed

examples/docs_to_kg/main.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
@dataclasses.dataclass
1010
class Relationship:
1111
"""Describe a relationship between two nodes."""
12-
source: str
13-
relationship_name: str
14-
target: str
12+
subject: str
13+
predicate: str
14+
object: str
1515

1616
@dataclasses.dataclass
1717
class Relationships:
@@ -52,23 +52,29 @@ def docs_to_kg_flow(flow_builder: cocoindex.FlowBuilder, data_scope: cocoindex.D
5252
instruction=(
5353
"Please extract relationships from CocoIndex documents. "
5454
"Focus on concepts and ingnore specific examples. "
55-
"Each relationship should be a tuple of (source, relationship, target).")))
55+
"Each relationship should be a tuple of (subject, predicate, object).")))
5656

5757
with chunk["relationships"]["relationships"].row() as relationship:
5858
relationships.collect(
5959
id=cocoindex.GeneratedField.UUID,
60-
source=relationship["source"],
61-
relationship_name=relationship["relationship_name"],
62-
target=relationship["target"],
60+
subject=relationship["subject"],
61+
predicate=relationship["predicate"],
62+
object=relationship["object"],
6363
)
6464

6565
relationships.export(
6666
"relationships",
6767
cocoindex.storages.Neo4jRelationship(
6868
connection=conn_spec,
69-
relationship="RELATIONSHIP",
70-
source=cocoindex.storages.Neo4jRelationshipEndSpec(field_name="source", label="Entity"),
71-
target=cocoindex.storages.Neo4jRelationshipEndSpec(field_name="target", label="Entity"),
69+
rel_type="RELATIONSHIP",
70+
source=cocoindex.storages.Neo4jRelationshipEndSpec(
71+
label="Entity",
72+
fields=[cocoindex.storages.Neo4jFieldMapping(field_name="subject", node_field_name="value")]
73+
),
74+
target=cocoindex.storages.Neo4jRelationshipEndSpec(
75+
label="Entity",
76+
fields=[cocoindex.storages.Neo4jFieldMapping(field_name="object", node_field_name="value")]
77+
),
7278
nodes={
7379
"Entity": cocoindex.storages.Neo4jRelationshipNodeSpec(key_field_name="value"),
7480
},

python/cocoindex/storages.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@ class Neo4jConnectionSpec:
1717
password: str
1818
db: str | None = None
1919

20+
@dataclass
21+
class Neo4jFieldMapping:
22+
"""Mapping for a Neo4j field."""
23+
field_name: str
24+
# Field name for the node in the Knowledge Graph.
25+
# If unspecified, it's the same as `field_name`.
26+
node_field_name: str | None = None
27+
2028
@dataclass
2129
class Neo4jRelationshipEndSpec:
2230
"""Spec for a Neo4j node type."""
23-
field_name: str
2431
label: str
32+
fields: list[Neo4jFieldMapping]
2533

2634
@dataclass
2735
class Neo4jRelationshipNodeSpec:
@@ -32,7 +40,7 @@ class Neo4jRelationship(op.StorageSpec):
3240
"""Graph storage powered by Neo4j."""
3341

3442
connection: AuthEntryReference
35-
relationship: str
43+
rel_type: str
3644
source: Neo4jRelationshipEndSpec
3745
target: Neo4jRelationshipEndSpec
3846
nodes: dict[str, Neo4jRelationshipNodeSpec]

0 commit comments

Comments
 (0)