You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/asciidoc/modules/ROOT/pages/ml/genai.adoc
+145Lines changed: 145 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -91,6 +91,7 @@ RETURN m.title
91
91
| apiKey | OpenAI API key | in case `apoc.openai.key` is not defined
92
92
| model | The Open AI model | no, default `gpt-3.5-turbo`
93
93
| sample | The number of nodes to skip, e.g. a sample of 1000 will read every 1000th node. It's used as a parameter to `apoc.meta.data` procedure that computes the schema | no, default is a random number
94
+
| additionalPrompts | To specify other prompts to be passed to improve the request
94
95
|===
95
96
96
97
.Results
@@ -102,6 +103,107 @@ RETURN m.title
102
103
|===
103
104
104
105
106
+
We can use the `additionalPrompts` config to improve the request, e.g. adding the natural language description of the schema (like the output of the `apoc.ml.schema` for instance).
107
+
Since OpenAI is mainly trained to elaborate natural language questions asked in, rather than Cypher queries, by using this configuration it is possible to achieve better results.
108
+
For example, given the https://neo4j.com/docs/getting-started/appendix/tutorials/guide-import-relational-and-etl/[Northwind dataset] we can execute:
109
+
110
+
.Query call
111
+
[source,cypher]
112
+
----
113
+
CALL apoc.ml.schema({apiKey: $apiKey}) YIELD value
114
+
WITH value
115
+
CALL apoc.ml.query("Which 5 employees had sold the product 'Chocolade' and has the highest selling count of another product?
116
+
Please returns the employee identificator, the other product name and the count orders of another product",
117
+
{
118
+
retries: 8,
119
+
retryWithError: true,
120
+
apiKey: $apiKey,
121
+
additionalPrompts: [
122
+
{role: "system", content: "The human description of the schema is the following:\n" + value}
123
+
]
124
+
})
125
+
YIELD query, value RETURN query, value
126
+
----
127
+
128
+
with a result similar to the following.
129
+
130
+
NOTE: the results are not deterministic and will potentially change each time the query is re-executed
131
+
132
+
.Results
133
+
[%autowidth, opts=header]
134
+
|===
135
+
| query | value
136
+
| "cypher
137
+
MATCH (p:Product {productName: 'Chocolade'})<-[:CONTAINS]-(:Order)<-[:SOLD]-(e:Employee)
138
+
MATCH (e)-[:SOLD]->(o:Order)-[:CONTAINS]->(p2:Product)
139
+
WITH e, p2, COUNT(DISTINCT o) AS orderCount
140
+
ORDER BY orderCount DESC
141
+
RETURN e.employeeID AS employeeID, p2.productName AS otherProduct, orderCount
142
+
LIMIT 5
143
+
"
144
+
| {
145
+
"otherProduct": "Gnocchi di nonna Alice",
146
+
"employeeID": "4",
147
+
"orderCount": 14
148
+
}
149
+
| "cypher
150
+
MATCH (p:Product {productName: 'Chocolade'})<-[:CONTAINS]-(:Order)<-[:SOLD]-(e:Employee)
151
+
MATCH (e)-[:SOLD]->(o:Order)-[:CONTAINS]->(p2:Product)
152
+
WITH e, p2, COUNT(DISTINCT o) AS orderCount
153
+
ORDER BY orderCount DESC
154
+
RETURN e.employeeID AS employeeID, p2.productName AS otherProduct, orderCount
155
+
LIMIT 5
156
+
"
157
+
| {
158
+
"otherProduct": "Pâté chinois",
159
+
"employeeID": "4",
160
+
"orderCount": 12
161
+
}
162
+
| "cypher
163
+
MATCH (p:Product {productName: 'Chocolade'})<-[:CONTAINS]-(:Order)<-[:SOLD]-(e:Employee)
164
+
MATCH (e)-[:SOLD]->(o:Order)-[:CONTAINS]->(p2:Product)
165
+
WITH e, p2, COUNT(DISTINCT o) AS orderCount
166
+
ORDER BY orderCount DESC
167
+
RETURN e.employeeID AS employeeID, p2.productName AS otherProduct, orderCount
168
+
LIMIT 5
169
+
"
170
+
| {
171
+
"otherProduct": "Gumbär Gummibärchen",
172
+
"employeeID": "3",
173
+
"orderCount": 12
174
+
}
175
+
| "cypher
176
+
MATCH (p:Product {productName: 'Chocolade'})<-[:CONTAINS]-(:Order)<-[:SOLD]-(e:Employee)
177
+
MATCH (e)-[:SOLD]->(o:Order)-[:CONTAINS]->(p2:Product)
178
+
WITH e, p2, COUNT(DISTINCT o) AS orderCount
179
+
ORDER BY orderCount DESC
180
+
RETURN e.employeeID AS employeeID, p2.productName AS otherProduct, orderCount
181
+
LIMIT 5
182
+
"
183
+
| {
184
+
"otherProduct": "Flotemysost",
185
+
"employeeID": "1",
186
+
"orderCount": 12
187
+
}
188
+
| "cypher
189
+
MATCH (p:Product {productName: 'Chocolade'})<-[:CONTAINS]-(:Order)<-[:SOLD]-(e:Employee)
190
+
MATCH (e)-[:SOLD]->(o:Order)-[:CONTAINS]->(p2:Product)
191
+
WITH e, p2, COUNT(DISTINCT o) AS orderCount
192
+
ORDER BY orderCount DESC
193
+
RETURN e.employeeID AS employeeID, p2.productName AS otherProduct, orderCount
194
+
LIMIT 5
195
+
"
196
+
| {
197
+
"otherProduct": "Pavlova",
198
+
"employeeID": "1",
199
+
"orderCount": 11
200
+
}
201
+
|===
202
+
203
+
Respect to using the procedure without the natural language schema description, the output has fewer hallucinations,
204
+
like properties hold by different labels and relationships linked to other entities.
205
+
206
+
105
207
== Describe the graph model with natural language
106
208
107
209
This procedure `apoc.ml.schema` returns a description, in natural language, of the underlying dataset.
@@ -126,6 +228,7 @@ RETURN *
126
228
1 row
127
229
----
128
230
231
+
129
232
.Input Parameters
130
233
[%autowidth, opts=header]
131
234
|===
@@ -205,6 +308,7 @@ RETURN DISTINCT a.name
205
308
| apiKey | OpenAI API key | in case `apoc.openai.key` is not defined
206
309
| model | The Open AI model | no, default `gpt-3.5-turbo`
207
310
| sample | The number of nodes to skip, e.g. a sample of 1000 will read every 1000th node. It's used as a parameter to `apoc.meta.data` procedure that computes the schema | no, default is a random number
311
+
| additionalPrompts | To specify other prompts to be passed to improve the request
208
312
|===
209
313
210
314
.Results
@@ -214,6 +318,47 @@ RETURN DISTINCT a.name
214
318
| value | the description of the dataset
215
319
|===
216
320
321
+
322
+
We can use the `additionalPrompts` config to improve the request, e.g. adding the natural language description of the schema (like the output of the `apoc.ml.schema` for instance).
323
+
Since OpenAI is mainly trained to elaborate natural language questions asked in, rather than Cypher queries, by using this configuration it is possible to achieve better results.
324
+
For example, given the https://neo4j.com/docs/getting-started/appendix/tutorials/guide-import-relational-and-etl/[Northwind dataset] we can execute:
325
+
326
+
.Query call
327
+
[source,cypher]
328
+
----
329
+
CALL apoc.ml.schema({apiKey: $apiKey}) YIELD value
330
+
WITH value
331
+
CALL apoc.ml.cypher("Which 5 employees had sold the product 'Chocolade' and has the highest selling count of another product?
332
+
Please returns the employee identificator, the other product name and the count orders of another product",
333
+
{
334
+
count: 1,
335
+
apiKey: $apiKey,
336
+
additionalPrompts: [
337
+
{role: "system", content: "The human description of the schema is the following:\n" + value}
338
+
]
339
+
})
340
+
YIELD value RETURN value
341
+
----
342
+
343
+
with a result similar to the following.
344
+
345
+
NOTE: the results are not deterministic and will potentially change each time the query is re-executed
346
+
347
+
.Results
348
+
[%autowidth, opts=header]
349
+
|===
350
+
| value
351
+
| MATCH (p:Product {productName: 'Chocolade'})<-[:CONTAINS]-(o:Order)<-[:SOLD]-(e:Employee)
352
+
MATCH (e)-[:SOLD]->(o2:Order)-[:CONTAINS]->(p2:Product)
353
+
WITH e, p2, COUNT(DISTINCT o2) AS ordersCnt
354
+
ORDER BY ordersCnt DESC
355
+
RETURN e.employeeID AS employeeID, p2.productName AS otherProduct, ordersCnt
356
+
LIMIT 5
357
+
|===
358
+
359
+
Respect to using the procedure without the natural language schema description, the output has fewer hallucinations,
360
+
like properties hold by different labels and relationships linked to other entities.
361
+
217
362
== Create a natural language query explanation from a cypher query
218
363
219
364
This procedure `apoc.ml.fromCypher` takes a natural language question and transforms it into natural language query explanation.
0 commit comments