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
| Perform a vector index search on your database either based by passing in a vector index or a search phrase.
116
+
114
117
|===
115
118
116
119
== Custom logic
@@ -137,20 +140,6 @@ of any required fields that is passed as arguments to the custom resolver.
137
140
138
141
|===
139
142
140
-
== Generative AI
141
-
142
-
[cols="2,5"]
143
-
|===
144
-
| Directive | Description
145
-
146
-
| xref::/directives/genai.adoc#_vector[`@vector`]
147
-
| Use an existing vector embedding on a node in the database to perform a nearest neighbor search.
148
-
149
-
| xref::/directives/genai.adoc#_genai[`@genAI`]
150
-
| Perform a query which utilizes the link::https://neo4j.com/docs/cypher-manual/current/genai-integrations/[Neo4j GenAI plugin] to create a vector embedding for a search phrase and then compares it to existing vector embeddings on nodes in the database.
:description: Directives related to generative AI in the Neo4j GraphQL Library.
253
+
254
+
255
+
== Vector index search
256
+
257
+
With the `@vector` GraphQL directive you can query your database to perform a vector index search.
258
+
Queries are performed by passing in either a vector index or a query phrase.
259
+
260
+
A query by vector index finds nodes with a vector embedding similar to that index, a nearest neighbor search.
261
+
262
+
In contrast, a query by phrase (a string of text) forwards the phrase to the link:https://neo4j.com/docs/cypher-manual/current/genai-integrations/[Neo4j GenAI plugin] and the plugin generates a vector embedding for it.
263
+
This embedding is then compared to the node vector embeddings in the database and the result set.
264
+
265
+
[NOTE]
266
+
.Prerequisites
267
+
====
268
+
* The database must be Neo4j version 5.15 or higher.
269
+
* The node vector embeddings already exist in the database.
270
+
* The embeddings must have been created using the same method.
271
+
* Queries by vector index cannot be performed across multiple labels.
272
+
* Queries by phrase require credentials for the Neo4j GenAI plugin.
273
+
====
274
+
275
+
276
+
=== Definition
277
+
278
+
[source, graphql]
279
+
----
280
+
"""Informs @neo4j/graphql that there should be a vector index in the database, allows users to search by the index in the generated schema."""
281
+
directive @vector(indexes: [VectorIndexInput]!) on OBJECT
282
+
----
283
+
284
+
`VectorIndexInput` is defined as follows:
285
+
286
+
[source, graphql]
287
+
----
288
+
input VectorIndexInput {
289
+
"""(Required) The name of the vector index."""
290
+
indexName: String!
291
+
"""(Required) The name of the embedding property on the node."""
292
+
property: String!
293
+
"""(Required) The name of the query."""
294
+
queryName: String
295
+
"""(Optional) The name of the provider."""
296
+
provider: String
297
+
"""(Optional) The name of the callback function."""
298
+
callback: String
299
+
}
300
+
----
301
+
302
+
If the optional field `provider` is set, the type can be used for a query by phrase, otherwise for a query by vector index.
303
+
304
+
=== Usage
305
+
306
+
==== Query by vector index
307
+
308
+
Perform a nearest neighbor search by passing a vector index to your database and find nodes with a vector embedding similar to that index.
309
+
310
+
.Type definition
311
+
[source, graphql]
312
+
----
313
+
type Product @vector(indexes: [{
314
+
indexName: "productDescriptionIndex",
315
+
property: "descriptionVector",
316
+
queryName: "searchByDescription"
317
+
}]) {
318
+
id: ID!
319
+
name: String!
320
+
description: String!
321
+
}
322
+
----
323
+
324
+
This defines the query to be performed on all `Product` nodes which have a vector index named `productDescriptionIndex` for the property `descriptionVector`, implying that a vector index has been created for the `description` property of each node.
325
+
326
+
.Example query
327
+
[source, graphql]
328
+
----
329
+
query FindSimilarProducts($vector: [Float]!) {
330
+
searchByDescription(vector: $vector) {
331
+
productsConnection {
332
+
edges {
333
+
cursor
334
+
score
335
+
node {
336
+
id
337
+
name
338
+
description
339
+
}
340
+
}
341
+
}
342
+
}
343
+
}
344
+
----
345
+
346
+
The query returns all `Product` nodes with a vector embedding on their `descriptionVector` property which is similar to the vector index argument `$vector`.
347
+
348
+
=== Query by phrase
349
+
350
+
Perform a query which utilizes the link:https://neo4j.com/docs/cypher-manual/current/genai-integrations/[Neo4j GenAI plugin] to create a vector embedding for a search phrase and then compares it to existing vector embeddings on nodes in the database.
351
+
352
+
[NOTE]
353
+
====
354
+
Requires credentials for the plugin.
355
+
====
356
+
357
+
.Type definition
358
+
[source, graphql]
359
+
----
360
+
type Product @vector(indexes: [{
361
+
indexName: "productDescriptionIndex",
362
+
property: "descriptionVector",
363
+
provider: OpenAI, # Assuming this is configured in the server
364
+
queryName: "searchByPhrase"
365
+
}]) {
366
+
id: ID!
367
+
name: String!
368
+
description: String!
369
+
}
370
+
----
371
+
372
+
This defines the query to be performed on all `Product` nodes which have a vector index named `productDescriptionIndex` for the property `descriptionVector`, implying that a vector index has been created for the `description` property of each node.
373
+
374
+
.Example query
375
+
[source, graphql]
376
+
----
377
+
query SearchProductsByPhrase($phrase: String!) {
378
+
searchByPhrase(phrase: $phrase) {
379
+
productsConnection {
380
+
edges {
381
+
cursor
382
+
score
383
+
node {
384
+
id
385
+
name
386
+
description
387
+
}
388
+
}
389
+
}
390
+
}
391
+
}
392
+
----
393
+
394
+
First, the query passes the query phrase argument `$phrase` to the GenAI plugin and lets it generate a vector embedding for the phrase.
395
+
Then it returns all `Product` nodes with a vector embedding on their `descriptionVector` property which is similar to the vector embedding generated by the plugin.
0 commit comments