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
The Neo4j vector store implementation has been enhanced with a builder
pattern to be more intuitive than using ctors and follows spring ai builder
conventions. Current constructors have been deprecated to maintain
backward compatibility for one releaes cycle.
The change includes:
* Move classes to dedicated neo4j package for better organization
* Add comprehensive builder pattern implementation with validation
* Improve documentation with detailed usage examples
* Deprecate but maintain old configuration approach for compatibility
* Update integration tests to demonstrate new builder pattern
* Enhance code readability and maintainability
* If required, an API key for the xref:api/embeddings.adoc#available-implementations[EmbeddingModel] to generate the embeddings stored by the `Neo4jVectorStore`.
22
22
23
-
== Dependencies
23
+
== Auto-configuration
24
24
25
-
Add the Neo4j Vector Store dependency to your project:
25
+
Spring AI provides Spring Boot auto-configuration for the Neo4j Vector Store.
26
+
To enable it, add the following dependency to your project's Maven `pom.xml` file:
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
47
46
47
+
Please have a look at the list of xref:#_neo4jvectorstore_properties[configuration parameters] for the vector store to learn about the default values and configuration options.
48
+
49
+
TIP: Refer to the xref:getting-started.adoc#repositories[Repositories] section to add Milestone and/or Snapshot Repositories to your build file.
48
50
49
51
The vector store implementation can initialize the requisite schema for you, but you must opt-in by specifying the `initializeSchema` boolean in the appropriate constructor or by setting `...initialize-schema=true` in the `application.properties` file.
50
52
51
53
NOTE: this is a breaking change! In earlier versions of Spring AI, this schema initialization happened by default.
52
54
55
+
Additionally, you will need a configured `EmbeddingModel` bean. Refer to the xref:api/embeddings.adoc#available-implementations[EmbeddingModel] section for more information.
53
56
54
-
== Configuration
55
-
56
-
To connect to Neo4j and use the `Neo4jVectorStore`, you need to provide access details for your instance.
57
-
A simple configuration can either be provided via Spring Boot's _application.properties_,
57
+
Now you can auto-wire the `Neo4jVectorStore` as a vector store in your application.
For example, if you want to store your API key as an environment variable but keep the rest in the plain _application.properties_ file.
77
+
[[neo4jvector-properties]]
78
+
=== Configuration Properties
79
+
80
+
To connect to Neo4j and use the `Neo4jVectorStore`, you need to provide access details for your instance.
81
+
A simple configuration can be provided via Spring Boot's `application.yml`:
82
+
83
+
[source,yaml]
84
+
----
85
+
spring:
86
+
neo4j:
87
+
uri: <neo4j instance URI>
88
+
authentication:
89
+
username: <neo4j username>
90
+
password: <neo4j password>
91
+
ai:
92
+
vectorstore:
93
+
neo4j:
94
+
initialize-schema: true
95
+
database-name: neo4j
96
+
index-name: custom-index
97
+
dimensions: 1536
98
+
distance-type: cosine
99
+
batching-strategy: TOKEN_COUNT # Optional: Controls how documents are batched for embedding
100
+
----
101
+
102
+
The Spring Boot properties starting with `spring.neo4j.*` are used to configure the Neo4j client:
103
+
104
+
[cols="2,5,1",stripes=even]
105
+
|===
106
+
|Property | Description | Default Value
107
+
108
+
| `spring.neo4j.uri` | URI for connecting to the Neo4j instance | `neo4j://localhost:7687`
109
+
| `spring.neo4j.authentication.username` | Username for authentication with Neo4j | `neo4j`
110
+
| `spring.neo4j.authentication.password` | Password for authentication with Neo4j | -
111
+
|===
112
+
113
+
Properties starting with `spring.ai.vectorstore.neo4j.*` are used to configure the `Neo4jVectorStore`:
81
114
82
-
NOTE: If you choose to create a shell script for ease in future work, be sure to run it prior to starting your application by "sourcing" the file, i.e. `source <your_script_name>.sh`.
115
+
[cols="2,5,1",stripes=even]
116
+
|===
117
+
|Property | Description | Default Value
118
+
119
+
|`spring.ai.vectorstore.neo4j.initialize-schema`| Whether to initialize the required schema | `false`
120
+
|`spring.ai.vectorstore.neo4j.database-name` | The name of the Neo4j database to use | `neo4j`
121
+
|`spring.ai.vectorstore.neo4j.index-name` | The name of the index to store the vectors | `spring-ai-document-index`
122
+
|`spring.ai.vectorstore.neo4j.dimensions` | The number of dimensions in the vector | `1536`
123
+
|`spring.ai.vectorstore.neo4j.distance-type` | The distance function to use | `cosine`
124
+
|`spring.ai.vectorstore.neo4j.label` | The label used for document nodes | `Document`
125
+
|`spring.ai.vectorstore.neo4j.embedding-property` | The property name used to store embeddings | `embedding`
126
+
|`spring.ai.vectorstore.neo4j.batching-strategy` | Strategy for batching documents when calculating embeddings. Options are `TOKEN_COUNT` or `FIXED_SIZE` | `TOKEN_COUNT`
127
+
|===
83
128
84
-
NOTE: Besides _application.properties_ and environment variables, Spring Boot offers https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config[additional configuration options].
129
+
The following distance functions are available:
85
130
86
-
Spring Boot's auto-configuration feature for the Neo4j Driver will create a bean instance that will be used by the `Neo4jVectorStore`.
131
+
* `cosine` - Default, suitable for most use cases. Measures cosine similarity between vectors.
Spring AI provides Spring Boot auto-configuration for the Neo4j Vector Store.
91
-
To enable it, add the following dependency to your project's Maven `pom.xml` file:
136
+
Instead of using the Spring Boot auto-configuration, you can manually configure the Neo4j vector store. For this you need to add the `spring-ai-neo4j-store` to your project:
TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
111
156
112
-
Please have a look at the list of xref:#_neo4jvectorstore_properties[configuration parameters] for the vector store to learn about the default values and configuration options.
113
-
114
-
TIP: Refer to the xref:getting-started.adoc#repositories[Repositories] section to add Milestone and/or Snapshot Repositories to your build file.
115
-
116
-
Additionally, you will need a configured `EmbeddingModel` bean. Refer to the xref:api/embeddings.adoc#available-implementations[EmbeddingModel] section for more information.
117
-
118
-
Here is an example of the needed bean:
157
+
Create a Neo4j `Driver` bean.
158
+
Read the link:https://neo4j.com/docs/java-manual/current/client-applications/[Neo4j Documentation] for more in-depth information about the configuration of a custom driver.
119
159
120
160
[source,java]
121
161
----
122
162
@Bean
123
-
public EmbeddingModel embeddingModel() {
124
-
// Can be any other Embeddingmodel implementation.
125
-
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("SPRING_AI_OPENAI_API_KEY")));
In cases where the Spring Boot auto-configured Neo4j `Driver` bean is not what you want or need, you can still define your own bean.
130
-
Please read the https://neo4j.com/docs/java-manual/current/client-applications/[Neo4j Java Driver reference] for more in-depth information about the configuration of a custom driver.
169
+
Then create the `Neo4jVectorStore` bean using the builder pattern:
NOTE: Those (portable) filter expressions get automatically converted into the proprietary Neo4j `WHERE` link:https://neo4j.com/developer/cypher/filtering-query-results/[filter expressions].
175
228
176
229
For example, this portable filter expression:
177
230
178
-
```sql
231
+
[source,sql]
232
+
----
179
233
author in ['john', 'jill'] && 'article_type' == 'blog'
180
-
```
234
+
----
181
235
182
236
is converted into the proprietary Neo4j filter format:
183
237
184
-
```
238
+
[source,text]
239
+
----
185
240
node.`metadata.author` IN ["john","jill"] AND node.`metadata.'article_type'` = "blog"
186
-
```
187
-
188
-
== Neo4jVectorStore properties
189
-
190
-
You can use the following properties in your Spring Boot configuration to customize the Neo4j vector store.
Copy file name to clipboardExpand all lines: spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/vectorstore/neo4j/Neo4jVectorStoreAutoConfiguration.java
Copy file name to clipboardExpand all lines: spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/vectorstore/neo4j/Neo4jVectorStoreProperties.java
0 commit comments