Neo4jQueryWriter
Neo4jQueryWriter ¤
A component for writing arbitrary data to Neo4j database using plain Cypher query.
This component gives flexible way to write data to Neo4j by running arbitrary Cypher query with
parameters. Query parameters can be supplied in a pipeline from other components (or pipeline data).
You could use such queries to write Documents with additional graph nodes for a more complex RAG scenarios.
The difference between DocumentWriter and Neo4jQueryWriter
is that the latter can write any data to Neo4j - not just Documents.
Note
Please consider data types mappings in Cypher query when working with query parameters. Neo4j Python Driver handles type conversions/mappings. Specifically you can figure out in the documentation of the driver how to work with temporal types.
from neo4j_haystack.client.neo4j_client import Neo4jClientConfig
from neo4j_haystack.components.neo4j_query_writer import Neo4jQueryWriter
client_config = Neo4jClientConfig("bolt://localhost:7687", database="neo4j", username="neo4j", password="passw0rd")
doc_meta = {"year": 2020, "source_url": "https://www.deepset.ai/blog"}
writer = Neo4jQueryWriter(client_config=client_config, verify_connectivity=True, runtime_parameters=["doc_meta"])
result = writer.run(
query=(
"MERGE (doc:`Document` {id: $doc_id})"
"SET doc += {id: $doc_id, content: $content, year: $doc_meta.year, source_url: $doc_meta.source_url}"
),
parameters={"doc_id": "123", "content": "beautiful graph"},
doc_meta=doc_meta
)
Output
>>> {'query_status': 'success'}
In case query execution results in error and raise_on_failure=False
the output will contain the error, e.g.:
Output
>>> {'query_status': 'error', 'error_message': 'Invalid cypher syntax', error: <Exception>}
In RAG pipeline runtime parameters could be connected from other components. Make sure during component creation to
specify which runtime_parameters
are expected to become as input slots for the component. In the example above
doc_meta
can be connected , e.g. pipeline.connect("other_component.output", "writer.doc_meta")
.
Important
At the moment parameters support simple data types, dictionaries (see doc_meta
in the example above) and
python dataclasses (which can be converted to dict
). For example haystack.Document
or haystack.ChatMessage
instances are valid query parameter inputs. However, currently Neo4j Python Driver does not convert dataclasses
to dictionaries automatically for us. By default Neo4jQueryParametersMarshaller
is used to handle such conversions. You can change this logic by creating your own marshaller (see the
query_parameters_marshaller
attribute)
Source code in src/neo4j_haystack/components/neo4j_query_writer.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
|
__init__ ¤
__init__(
client_config: Neo4jClientConfig,
query: Optional[str] = None,
runtime_parameters: Optional[List[str]] = None,
verify_connectivity: Optional[bool] = False,
raise_on_failure: bool = True,
query_parameters_marshaller: Optional[QueryParametersMarshaller] = None,
)
Parameters:
-
client_config
(Neo4jClientConfig
) –Neo4j client configuration to connect to database (e.g. credentials and connection settings).
-
query
(Optional[str]
, default:None
) –Optional Cypher query for document retrieval. If
None
should be provided as component input. -
runtime_parameters
(Optional[List[str]]
, default:None
) –list of input parameters/slots for connecting components in a pipeline.
-
verify_connectivity
(Optional[bool]
, default:False
) –If
True
will verify connectivity with Neo4j database configured byclient_config
. -
raise_on_failure
(bool
, default:True
) –If
True
raises an exception if it fails to execute given Cypher query. -
query_parameters_marshaller
(Optional[QueryParametersMarshaller]
, default:None
) –Marshaller responsible for converting query parameters which can be used in Cypher query, e.g. python dataclasses to be converted to dictionary.
Neo4jQueryParametersMarshaller
is the default marshaller implementation.
Source code in src/neo4j_haystack/components/neo4j_query_writer.py
to_dict ¤
Serialize this component to a dictionary.
Source code in src/neo4j_haystack/components/neo4j_query_writer.py
from_dict
classmethod
¤
Deserialize this component from a dictionary.
Source code in src/neo4j_haystack/components/neo4j_query_writer.py
run ¤
run(
query: Optional[str] = None,
parameters: Optional[Dict[str, Any]] = None,
**kwargs
) -> QueryResult
Runs the arbitrary Cypher query
with parameters
to write data to Neo4j.
Once data is written to Neo4j the component returns back some execution stats.
Parameters:
-
query
(Optional[str]
, default:None
) –Cypher query to run.
-
parameters
(Optional[Dict[str, Any]]
, default:None
) –Cypher query parameters which can be used as placeholders in the
query
. -
kwargs
–Runtime parameters from connected components in a pipeline, e.g.
pipeline.connect("year_provider.year_start", "writer.year_start")
, whereyear_start
will be part ofkwargs
.
Returns:
-
Output
(QueryResult
) –Query execution stats.
Example:
{'query_status': 'success'}
Source code in src/neo4j_haystack/components/neo4j_query_writer.py
_serialize_parameters ¤
Serializes parameters
into a data structure which can be accepted by Neo4j Python Driver (and a Cypher query
respectively). See Neo4jQueryParametersMarshaller
for more details.