Skip to content

Commit 214a2f7

Browse files
authored
feat(neo4j)!: add ReferencedNode as declaration (#374)
1 parent 3b3efa3 commit 214a2f7

File tree

6 files changed

+213
-166
lines changed

6 files changed

+213
-166
lines changed

examples/docs_to_knowledge_graph/main.py

+18-11
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,31 @@ def docs_to_kg_flow(flow_builder: cocoindex.FlowBuilder, data_scope: cocoindex.D
9292
id=cocoindex.GeneratedField.UUID, entity=relationship["object"],
9393
filename=doc["filename"], location=chunk["location"],
9494
)
95+
9596
document_node.export(
9697
"document_node",
9798
cocoindex.storages.Neo4j(
9899
connection=conn_spec,
99100
mapping=cocoindex.storages.NodeMapping(label="Document")),
100101
primary_key_fields=["filename"],
101102
)
103+
flow_builder.declare(
104+
cocoindex.storages.Neo4jDeclarations(
105+
connection=conn_spec,
106+
referenced_nodes=[
107+
cocoindex.storages.ReferencedNode(
108+
label="Entity",
109+
primary_key_fields=["value"],
110+
vector_indexes=[
111+
cocoindex.VectorIndexDef(
112+
field_name="embedding",
113+
metric=cocoindex.VectorSimilarityMetric.COSINE_SIMILARITY,
114+
),
115+
],
116+
)
117+
]
118+
)
119+
)
102120
entity_relationship.export(
103121
"entity_relationship",
104122
cocoindex.storages.Neo4j(
@@ -123,17 +141,6 @@ def docs_to_kg_flow(flow_builder: cocoindex.FlowBuilder, data_scope: cocoindex.D
123141
source="object_embedding", target="embedding"),
124142
]
125143
),
126-
nodes_storage_spec={
127-
"Entity": cocoindex.storages.NodeStorageSpec(
128-
primary_key_fields=["value"],
129-
vector_indexes=[
130-
cocoindex.VectorIndexDef(
131-
field_name="embedding",
132-
metric=cocoindex.VectorSimilarityMetric.COSINE_SIMILARITY,
133-
),
134-
],
135-
),
136-
},
137144
),
138145
),
139146
primary_key_fields=["id"],

python/cocoindex/flow.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,6 @@ def export(self, name: str, target_spec: op.StorageSpec, /, *,
290290
name, _spec_kind(target_spec), dump_engine_object(target_spec),
291291
dump_engine_object(index_options), self._engine_data_collector, setup_by_user)
292292

293-
def declare(self, spec: op.DeclarationSpec):
294-
"""
295-
Add a declaration to the flow.
296-
"""
297-
self._flow_builder_state.engine_flow_builder.declare(dump_engine_object(spec))
298-
299293

300294
_flow_name_builder = _NameBuilder()
301295

@@ -361,6 +355,12 @@ def add_source(self, spec: op.SourceSpec, /, *,
361355
name
362356
)
363357

358+
def declare(self, spec: op.DeclarationSpec):
359+
"""
360+
Add a declaration to the flow.
361+
"""
362+
self._state.engine_flow_builder.declare(dump_engine_object(spec))
363+
364364
@dataclass
365365
class FlowLiveUpdaterOptions:
366366
"""

python/cocoindex/op.py

-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
from typing import get_type_hints, Protocol, Any, Callable, Awaitable, dataclass_transform
99
from enum import Enum
10-
from functools import partial
1110

1211
from .typing import encode_enriched_type
1312
from .convert import to_engine_value, make_engine_value_converter
@@ -43,8 +42,6 @@ class StorageSpec(metaclass=SpecMeta, category=OpCategory.STORAGE): # pylint: di
4342

4443
class DeclarationSpec(metaclass=SpecMeta, category=OpCategory.DECLARATION): # pylint: disable=too-few-public-methods
4544
"""A declaration spec. All its subclass can be instantiated similar to a dataclass, i.e. ClassName(field1=value1, field2=value2, ...)"""
46-
kind: str
47-
4845
class Executor(Protocol):
4946
"""An executor for an operation."""
5047
op_category: OpCategory

python/cocoindex/storages.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ class NodeReferenceMapping:
4343
fields: list[TargetFieldMapping]
4444

4545
@dataclass
46-
class NodeStorageSpec:
46+
class ReferencedNode:
4747
"""Storage spec for a graph node."""
48+
label: str
4849
primary_key_fields: Sequence[str]
4950
vector_indexes: Sequence[index.VectorIndexDef] = ()
5051

@@ -63,10 +64,16 @@ class RelationshipMapping:
6364
rel_type: str
6465
source: NodeReferenceMapping
6566
target: NodeReferenceMapping
66-
nodes_storage_spec: dict[str, NodeStorageSpec] | None = None
6767

6868
class Neo4j(op.StorageSpec):
6969
"""Graph storage powered by Neo4j."""
7070

7171
connection: AuthEntryReference
7272
mapping: NodeMapping | RelationshipMapping
73+
74+
class Neo4jDeclarations(op.DeclarationSpec):
75+
"""Declarations for Neo4j."""
76+
77+
kind = "Neo4j"
78+
connection: AuthEntryReference
79+
referenced_nodes: Sequence[ReferencedNode] = ()

0 commit comments

Comments
 (0)