text2cypher 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- text2cypher-0.1.0/LICENSE +202 -0
- text2cypher-0.1.0/PKG-INFO +124 -0
- text2cypher-0.1.0/README.md +92 -0
- text2cypher-0.1.0/pyproject.toml +149 -0
- text2cypher-0.1.0/pyproject.toml.orig +117 -0
- text2cypher-0.1.0/src/text2cypher/__init__.py +47 -0
- text2cypher-0.1.0/src/text2cypher/_config.py +51 -0
- text2cypher-0.1.0/src/text2cypher/_errors.py +5 -0
- text2cypher-0.1.0/src/text2cypher/_execution.py +25 -0
- text2cypher-0.1.0/src/text2cypher/_generation/__init__.py +8 -0
- text2cypher-0.1.0/src/text2cypher/_generation/extract.py +22 -0
- text2cypher-0.1.0/src/text2cypher/_generation/generate.py +51 -0
- text2cypher-0.1.0/src/text2cypher/_generation/problems.py +193 -0
- text2cypher-0.1.0/src/text2cypher/_generation/prompts.py +141 -0
- text2cypher-0.1.0/src/text2cypher/_pipeline.py +86 -0
- text2cypher-0.1.0/src/text2cypher/_results.py +91 -0
- text2cypher-0.1.0/src/text2cypher/_schema_source.py +60 -0
- text2cypher-0.1.0/src/text2cypher/_validation/__init__.py +15 -0
- text2cypher-0.1.0/src/text2cypher/_validation/issues.py +100 -0
- text2cypher-0.1.0/src/text2cypher/_validation/validator.py +106 -0
- text2cypher-0.1.0/src/text2cypher/_workflow.py +99 -0
- text2cypher-0.1.0/src/text2cypher/llm/__init__.py +5 -0
- text2cypher-0.1.0/src/text2cypher/llm/_base.py +43 -0
- text2cypher-0.1.0/src/text2cypher/llm/_fake.py +47 -0
- text2cypher-0.1.0/src/text2cypher/llm/_openai.py +59 -0
- text2cypher-0.1.0/src/text2cypher/py.typed +0 -0
- text2cypher-0.1.0/src/text2cypher/schema/__init__.py +14 -0
- text2cypher-0.1.0/src/text2cypher/schema/_filter.py +89 -0
- text2cypher-0.1.0/src/text2cypher/schema/_from_snapshot.py +84 -0
- text2cypher-0.1.0/src/text2cypher/schema/_model.py +81 -0
- text2cypher-0.1.0/src/text2cypher/schema/_render.py +55 -0
- text2cypher-0.1.0/src/text2cypher/schema/_stopwords.py +81 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Killeroid
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: text2cypher
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: text2cypher: Generate validated Cypher queries from natural language text with LLMs
|
|
5
|
+
Keywords: cypher,text2cypher,falkordb,llm,knowledge-graph,openCypher,graphdb
|
|
6
|
+
Author: Kwaku Yeboah-Antwi
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Framework :: AsyncIO
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Topic :: Database
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Dist: cypher-guardrail>=1.3.0
|
|
21
|
+
Requires-Dist: falkordb>=1.8.0
|
|
22
|
+
Requires-Dist: openai>=3.31.0
|
|
23
|
+
Requires-Dist: redis>=8.1.0
|
|
24
|
+
Requires-Dist: snowballstemmer>=3.1.1
|
|
25
|
+
Requires-Python: >=3.12
|
|
26
|
+
Project-URL: Homepage, https://github.com/Killeroid/text2cypher
|
|
27
|
+
Project-URL: Documentation, https://text2cypher.readthedocs.io/en/latest/
|
|
28
|
+
Project-URL: Changelog, https://github.com/Killeroid/text2cypher/blob/main/CHANGELOG.md
|
|
29
|
+
Project-URL: Issues, https://github.com/Killeroid/text2cypher/issues
|
|
30
|
+
Project-URL: Repository, https://github.com/Killeroid/text2cypher
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# text2cypher
|
|
34
|
+
|
|
35
|
+
**Generate validated Cypher queries from natural language text with LLMs**
|
|
36
|
+
*Ask a question in plain language and get back a read-only Cypher query that has been checked against your [FalkorDB](https://www.falkordb.com/) graph.*
|
|
37
|
+
|
|
38
|
+
[](https://github.com/Killeroid/text2cypher/actions/workflows/ci.yml)
|
|
39
|
+
[](https://text2cypher.readthedocs.io/en/latest/)
|
|
40
|
+
[](https://www.python.org/)
|
|
41
|
+
[](https://github.com/Killeroid/text2cypher/blob/main/CHANGELOG.md)
|
|
42
|
+
[](https://github.com/Killeroid/text2cypher/blob/main/LICENSE)
|
|
43
|
+
|
|
44
|
+
text2cypher asks an LLM to write the query, checks it against the real graph with [cypher-guardrail](https://github.com/Killeroid/cypher-guardrail), and sends what is wrong back to the LLM until the query passes or the attempts run out. A query that fails the checks is never handed back as if it were fine.
|
|
45
|
+
|
|
46
|
+
* **Grounded in your graph**: the prompt carries the schema read from the graph itself (labels, relationship types, properties with their value types, the paths that exist, and the indexes), cut down to what the question is about.
|
|
47
|
+
* **Validated before it is returned**: syntax, schema (labels, relationship types, paths and their direction) and property checks. A query that could write to the graph is never valid.
|
|
48
|
+
* **Self-correcting**: the problems found are written back to the LLM. If the cut-down schema does not get there, the loop moves to the full one.
|
|
49
|
+
* **Honest outcomes**: `valid`, `unanswerable` (the LLM declined) or `failed` (the corrections ran out), with every attempt, its issues, the schema it used and its token usage.
|
|
50
|
+
* **Optional execution**: run the final query read-only, with a timeout, and get the rows.
|
|
51
|
+
* **Bring your own LLM**: a one-method protocol, with an OpenAI client included. Async throughout.
|
|
52
|
+
|
|
53
|
+
## Installation
|
|
54
|
+
|
|
55
|
+
Python 3.12 or later.
|
|
56
|
+
|
|
57
|
+
```sh
|
|
58
|
+
uv add text2cypher # or: pip install text2cypher
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
You need a FalkorDB graph that has data, reachable with the async client (`falkordb.asyncio`), and a model. The included client uses OpenAI's API and reads `OPENAI_API_KEY`.
|
|
62
|
+
|
|
63
|
+
## Quickstart
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
import asyncio
|
|
67
|
+
|
|
68
|
+
from falkordb.asyncio import FalkorDB
|
|
69
|
+
from text2cypher import OpenAIClient, Text2Cypher
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
async def main() -> None:
|
|
73
|
+
client = FalkorDB(host="localhost", port=6379) # one client per event loop
|
|
74
|
+
agent = Text2Cypher(client, OpenAIClient("your-model"))
|
|
75
|
+
|
|
76
|
+
result = await agent.run("Which actors appeared in The Matrix?", graph="movies")
|
|
77
|
+
print(result.status) # "valid", "unanswerable" or "failed"
|
|
78
|
+
print(
|
|
79
|
+
result.query
|
|
80
|
+
) # MATCH (p:Person)-[:ACTED_IN]->(m:Movie {title: 'The Matrix'}) RETURN p.name
|
|
81
|
+
|
|
82
|
+
await client.aclose()
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
asyncio.run(main())
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Always look at `result.status`. `valid` means the query passed the checks, not that it means what you meant (see below).
|
|
89
|
+
|
|
90
|
+
There is also [a quickstart that needs no model and no server](https://github.com/Killeroid/text2cypher/blob/main/examples/quickstart.py): it starts an embedded FalkorDB and answers with a scripted LLM, including one correction.
|
|
91
|
+
|
|
92
|
+
## What is and is not checked
|
|
93
|
+
|
|
94
|
+
* **Checked**: the query is valid Cypher for FalkorDB, it cannot write, its parameters are provided (the prompt asks for values written in the query, so a `$name` is a problem), the labels, relationship types and paths exist in the direction used, and every property read exists on the label or relationship type it is read from.
|
|
95
|
+
* **Not checked**: that the query means what the question meant. A query can be valid and still answer another question, for example by using a property with a similar name. Look at the query, or at the rows, before you rely on them.
|
|
96
|
+
* **Early days**: the prompts and the loop have been tried with small models and small graphs, and the filter has not been measured on a real large schema. Expect them to need work.
|
|
97
|
+
|
|
98
|
+
## Documentation
|
|
99
|
+
|
|
100
|
+
The full documentation is at **[text2cypher.readthedocs.io](https://text2cypher.readthedocs.io/en/latest/)**:
|
|
101
|
+
|
|
102
|
+
* [Using text2cypher](https://text2cypher.readthedocs.io/en/latest/usage.html): the result, the configuration, running the query, errors
|
|
103
|
+
* [How it works](https://text2cypher.readthedocs.io/en/latest/how-it-works.html): the loop, the escalation and what it costs
|
|
104
|
+
* [The schema and its filter](https://text2cypher.readthedocs.io/en/latest/schema.html): what the LLM is shown, your own filter, keeping the schema between runs
|
|
105
|
+
* [LLM clients](https://text2cypher.readthedocs.io/en/latest/llm.html): your own client, other OpenAI-compatible models, `FakeLLM` for tests
|
|
106
|
+
* [What is and is not checked](https://text2cypher.readthedocs.io/en/latest/checks.html) and the [API reference](https://text2cypher.readthedocs.io/en/latest/reference.html)
|
|
107
|
+
|
|
108
|
+
For contributors: [how it is built, and why](https://github.com/Killeroid/text2cypher/blob/main/docs/architecture.md), and [a harness](https://github.com/Killeroid/text2cypher/blob/main/evals/README.md) that replays questions through the real pipeline with replies written by a model or a person.
|
|
109
|
+
|
|
110
|
+
## Development
|
|
111
|
+
|
|
112
|
+
```sh
|
|
113
|
+
uv sync # create the environment (Python >= 3.12 also installs FalkorDBLite for the tests)
|
|
114
|
+
uv run pytest # the tests, on an embedded FalkorDB
|
|
115
|
+
uv run pytest -m "not db" # only the tests that need no database (under ten seconds)
|
|
116
|
+
uv run ruff check . && uv run ruff format --check . && uv run ty check && uv run lint-imports
|
|
117
|
+
uv run coverage run -m pytest && uv run coverage report
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Contributions are welcome: please open an issue or a pull request.
|
|
121
|
+
|
|
122
|
+
## License
|
|
123
|
+
|
|
124
|
+
Apache License 2.0: see [LICENSE](https://github.com/Killeroid/text2cypher/blob/main/LICENSE).
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# text2cypher
|
|
2
|
+
|
|
3
|
+
**Generate validated Cypher queries from natural language text with LLMs**
|
|
4
|
+
*Ask a question in plain language and get back a read-only Cypher query that has been checked against your [FalkorDB](https://www.falkordb.com/) graph.*
|
|
5
|
+
|
|
6
|
+
[](https://github.com/Killeroid/text2cypher/actions/workflows/ci.yml)
|
|
7
|
+
[](https://text2cypher.readthedocs.io/en/latest/)
|
|
8
|
+
[](https://www.python.org/)
|
|
9
|
+
[](https://github.com/Killeroid/text2cypher/blob/main/CHANGELOG.md)
|
|
10
|
+
[](https://github.com/Killeroid/text2cypher/blob/main/LICENSE)
|
|
11
|
+
|
|
12
|
+
text2cypher asks an LLM to write the query, checks it against the real graph with [cypher-guardrail](https://github.com/Killeroid/cypher-guardrail), and sends what is wrong back to the LLM until the query passes or the attempts run out. A query that fails the checks is never handed back as if it were fine.
|
|
13
|
+
|
|
14
|
+
* **Grounded in your graph**: the prompt carries the schema read from the graph itself (labels, relationship types, properties with their value types, the paths that exist, and the indexes), cut down to what the question is about.
|
|
15
|
+
* **Validated before it is returned**: syntax, schema (labels, relationship types, paths and their direction) and property checks. A query that could write to the graph is never valid.
|
|
16
|
+
* **Self-correcting**: the problems found are written back to the LLM. If the cut-down schema does not get there, the loop moves to the full one.
|
|
17
|
+
* **Honest outcomes**: `valid`, `unanswerable` (the LLM declined) or `failed` (the corrections ran out), with every attempt, its issues, the schema it used and its token usage.
|
|
18
|
+
* **Optional execution**: run the final query read-only, with a timeout, and get the rows.
|
|
19
|
+
* **Bring your own LLM**: a one-method protocol, with an OpenAI client included. Async throughout.
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
Python 3.12 or later.
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
uv add text2cypher # or: pip install text2cypher
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
You need a FalkorDB graph that has data, reachable with the async client (`falkordb.asyncio`), and a model. The included client uses OpenAI's API and reads `OPENAI_API_KEY`.
|
|
30
|
+
|
|
31
|
+
## Quickstart
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
import asyncio
|
|
35
|
+
|
|
36
|
+
from falkordb.asyncio import FalkorDB
|
|
37
|
+
from text2cypher import OpenAIClient, Text2Cypher
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
async def main() -> None:
|
|
41
|
+
client = FalkorDB(host="localhost", port=6379) # one client per event loop
|
|
42
|
+
agent = Text2Cypher(client, OpenAIClient("your-model"))
|
|
43
|
+
|
|
44
|
+
result = await agent.run("Which actors appeared in The Matrix?", graph="movies")
|
|
45
|
+
print(result.status) # "valid", "unanswerable" or "failed"
|
|
46
|
+
print(
|
|
47
|
+
result.query
|
|
48
|
+
) # MATCH (p:Person)-[:ACTED_IN]->(m:Movie {title: 'The Matrix'}) RETURN p.name
|
|
49
|
+
|
|
50
|
+
await client.aclose()
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
asyncio.run(main())
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Always look at `result.status`. `valid` means the query passed the checks, not that it means what you meant (see below).
|
|
57
|
+
|
|
58
|
+
There is also [a quickstart that needs no model and no server](https://github.com/Killeroid/text2cypher/blob/main/examples/quickstart.py): it starts an embedded FalkorDB and answers with a scripted LLM, including one correction.
|
|
59
|
+
|
|
60
|
+
## What is and is not checked
|
|
61
|
+
|
|
62
|
+
* **Checked**: the query is valid Cypher for FalkorDB, it cannot write, its parameters are provided (the prompt asks for values written in the query, so a `$name` is a problem), the labels, relationship types and paths exist in the direction used, and every property read exists on the label or relationship type it is read from.
|
|
63
|
+
* **Not checked**: that the query means what the question meant. A query can be valid and still answer another question, for example by using a property with a similar name. Look at the query, or at the rows, before you rely on them.
|
|
64
|
+
* **Early days**: the prompts and the loop have been tried with small models and small graphs, and the filter has not been measured on a real large schema. Expect them to need work.
|
|
65
|
+
|
|
66
|
+
## Documentation
|
|
67
|
+
|
|
68
|
+
The full documentation is at **[text2cypher.readthedocs.io](https://text2cypher.readthedocs.io/en/latest/)**:
|
|
69
|
+
|
|
70
|
+
* [Using text2cypher](https://text2cypher.readthedocs.io/en/latest/usage.html): the result, the configuration, running the query, errors
|
|
71
|
+
* [How it works](https://text2cypher.readthedocs.io/en/latest/how-it-works.html): the loop, the escalation and what it costs
|
|
72
|
+
* [The schema and its filter](https://text2cypher.readthedocs.io/en/latest/schema.html): what the LLM is shown, your own filter, keeping the schema between runs
|
|
73
|
+
* [LLM clients](https://text2cypher.readthedocs.io/en/latest/llm.html): your own client, other OpenAI-compatible models, `FakeLLM` for tests
|
|
74
|
+
* [What is and is not checked](https://text2cypher.readthedocs.io/en/latest/checks.html) and the [API reference](https://text2cypher.readthedocs.io/en/latest/reference.html)
|
|
75
|
+
|
|
76
|
+
For contributors: [how it is built, and why](https://github.com/Killeroid/text2cypher/blob/main/docs/architecture.md), and [a harness](https://github.com/Killeroid/text2cypher/blob/main/evals/README.md) that replays questions through the real pipeline with replies written by a model or a person.
|
|
77
|
+
|
|
78
|
+
## Development
|
|
79
|
+
|
|
80
|
+
```sh
|
|
81
|
+
uv sync # create the environment (Python >= 3.12 also installs FalkorDBLite for the tests)
|
|
82
|
+
uv run pytest # the tests, on an embedded FalkorDB
|
|
83
|
+
uv run pytest -m "not db" # only the tests that need no database (under ten seconds)
|
|
84
|
+
uv run ruff check . && uv run ruff format --check . && uv run ty check && uv run lint-imports
|
|
85
|
+
uv run coverage run -m pytest && uv run coverage report
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Contributions are welcome: please open an issue or a pull request.
|
|
89
|
+
|
|
90
|
+
## License
|
|
91
|
+
|
|
92
|
+
Apache License 2.0: see [LICENSE](https://github.com/Killeroid/text2cypher/blob/main/LICENSE).
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "text2cypher"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "text2cypher: Generate validated Cypher queries from natural language text with LLMs"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
dependencies = [
|
|
8
|
+
"cypher-guardrail>=1.3.0",
|
|
9
|
+
"falkordb>=1.8.0",
|
|
10
|
+
"openai>=3.31.0",
|
|
11
|
+
"redis>=8.1.0",
|
|
12
|
+
"snowballstemmer>=3.1.1",
|
|
13
|
+
]
|
|
14
|
+
keywords = [
|
|
15
|
+
"cypher",
|
|
16
|
+
"text2cypher",
|
|
17
|
+
"falkordb",
|
|
18
|
+
"llm",
|
|
19
|
+
"knowledge-graph",
|
|
20
|
+
"openCypher",
|
|
21
|
+
"graphdb",
|
|
22
|
+
]
|
|
23
|
+
classifiers = [
|
|
24
|
+
"Development Status :: 3 - Alpha",
|
|
25
|
+
"Framework :: AsyncIO",
|
|
26
|
+
"Intended Audience :: Developers",
|
|
27
|
+
"Operating System :: OS Independent",
|
|
28
|
+
"Programming Language :: Python :: 3",
|
|
29
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
30
|
+
"Programming Language :: Python :: 3.12",
|
|
31
|
+
"Programming Language :: Python :: 3.13",
|
|
32
|
+
"Programming Language :: Python :: 3.14",
|
|
33
|
+
"Topic :: Database",
|
|
34
|
+
"Typing :: Typed",
|
|
35
|
+
]
|
|
36
|
+
license = "Apache-2.0"
|
|
37
|
+
license-files = ["LICENSE"]
|
|
38
|
+
|
|
39
|
+
[[project.authors]]
|
|
40
|
+
name = "Kwaku Yeboah-Antwi"
|
|
41
|
+
|
|
42
|
+
[project.urls]
|
|
43
|
+
Homepage = "https://github.com/Killeroid/text2cypher"
|
|
44
|
+
Documentation = "https://text2cypher.readthedocs.io/en/latest/"
|
|
45
|
+
Changelog = "https://github.com/Killeroid/text2cypher/blob/main/CHANGELOG.md"
|
|
46
|
+
Issues = "https://github.com/Killeroid/text2cypher/issues"
|
|
47
|
+
Repository = "https://github.com/Killeroid/text2cypher"
|
|
48
|
+
|
|
49
|
+
[build-system]
|
|
50
|
+
requires = ["uv_build>=0.13.1,<0.14.0"]
|
|
51
|
+
build-backend = "uv_build"
|
|
52
|
+
|
|
53
|
+
[tool.uv.build-backend]
|
|
54
|
+
module-name = "text2cypher"
|
|
55
|
+
|
|
56
|
+
[tool.pytest.ini_options]
|
|
57
|
+
testpaths = ["tests"]
|
|
58
|
+
markers = ['db: needs the embedded FalkorDB server (skip with -m "not db")']
|
|
59
|
+
addopts = [
|
|
60
|
+
"--import-mode=importlib",
|
|
61
|
+
"--strict-markers",
|
|
62
|
+
]
|
|
63
|
+
pythonpath = [
|
|
64
|
+
".",
|
|
65
|
+
"tests",
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
[tool.coverage.run]
|
|
69
|
+
branch = true
|
|
70
|
+
source = [
|
|
71
|
+
"text2cypher",
|
|
72
|
+
"evals",
|
|
73
|
+
]
|
|
74
|
+
|
|
75
|
+
[tool.coverage.report]
|
|
76
|
+
show_missing = true
|
|
77
|
+
skip_empty = true
|
|
78
|
+
|
|
79
|
+
[tool.ruff]
|
|
80
|
+
src = [
|
|
81
|
+
"src",
|
|
82
|
+
".",
|
|
83
|
+
]
|
|
84
|
+
|
|
85
|
+
[tool.ruff.lint]
|
|
86
|
+
extend-select = [
|
|
87
|
+
"B",
|
|
88
|
+
"I",
|
|
89
|
+
"UP",
|
|
90
|
+
"SIM",
|
|
91
|
+
"C4",
|
|
92
|
+
"C90",
|
|
93
|
+
"PTH",
|
|
94
|
+
"RET",
|
|
95
|
+
"RUF",
|
|
96
|
+
"ASYNC",
|
|
97
|
+
"PT",
|
|
98
|
+
"TID",
|
|
99
|
+
"TC",
|
|
100
|
+
"FURB",
|
|
101
|
+
]
|
|
102
|
+
|
|
103
|
+
[tool.ruff.lint.mccabe]
|
|
104
|
+
max-complexity = 10
|
|
105
|
+
|
|
106
|
+
[tool.importlinter]
|
|
107
|
+
root_packages = [
|
|
108
|
+
"text2cypher",
|
|
109
|
+
"evals",
|
|
110
|
+
]
|
|
111
|
+
|
|
112
|
+
[[tool.importlinter.contracts]]
|
|
113
|
+
name = "The layers of the package"
|
|
114
|
+
type = "layers"
|
|
115
|
+
layers = [
|
|
116
|
+
"text2cypher._pipeline",
|
|
117
|
+
"text2cypher._workflow",
|
|
118
|
+
"text2cypher._generation | text2cypher._execution | text2cypher._schema_source",
|
|
119
|
+
"text2cypher._results",
|
|
120
|
+
"text2cypher._validation",
|
|
121
|
+
"text2cypher.llm | text2cypher.schema | text2cypher._config | text2cypher._errors",
|
|
122
|
+
]
|
|
123
|
+
|
|
124
|
+
[[tool.importlinter.contracts]]
|
|
125
|
+
name = "The package does not use the trial harness"
|
|
126
|
+
type = "forbidden"
|
|
127
|
+
source_modules = ["text2cypher"]
|
|
128
|
+
forbidden_modules = ["evals"]
|
|
129
|
+
|
|
130
|
+
[tool.ty.environment]
|
|
131
|
+
python = ".venv"
|
|
132
|
+
|
|
133
|
+
[dependency-groups]
|
|
134
|
+
dev = [
|
|
135
|
+
"anyio>=4.15.1",
|
|
136
|
+
"coverage>=7.16.2",
|
|
137
|
+
"falkordblite; python_version >= '3.12'",
|
|
138
|
+
"httpx2>=2.13.1",
|
|
139
|
+
"import-linter>=2.15",
|
|
140
|
+
"pytest>=9.1.1",
|
|
141
|
+
"ruff>=0.17.0",
|
|
142
|
+
"ty>=0.0.87",
|
|
143
|
+
]
|
|
144
|
+
docs = [
|
|
145
|
+
"furo>=2025.12.19",
|
|
146
|
+
"myst-parser>=5.1.0",
|
|
147
|
+
"sphinx>=9.1.1",
|
|
148
|
+
"sphinx-copybutton>=0.5.2",
|
|
149
|
+
]
|