Neo4j
本节将指导您完成设置Neo4jVectorStore
来存储文档嵌入并执行相似性搜索。
Neo4j 是一个开源的 NoSQL 图形数据库。 它是一个完全事务性的数据库 (ACID),将数据存储为由节点组成的图形,通过关系连接。 受现实世界结构的启发,它允许对复杂数据进行高查询性能,同时为开发人员保持直观和简单。
Neo4j 的 Vector Search 允许用户从大型数据集中查询向量嵌入。
嵌入是数据对象(如文本、图像、音频或文档)的数字表示形式。
嵌入可以存储在 Node 属性上,并且可以使用db.index.vector.queryNodes()
功能。
这些索引由 Lucene 提供支持,使用分层可导航小世界图 (HNSW) 对向量字段执行 k 个近似最近邻 (k-ANN) 查询。
先决条件
-
一个正在运行的 Neo4j (5.15+) 实例。以下选项可用:
-
如果需要,EmbeddingModel 的 API 密钥,用于生成由
Neo4jVectorStore
.
依赖
将 Neo4j Vector Store 依赖项添加到您的项目中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-neo4j-store</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-neo4j-store'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
矢量存储实现可以为您初始化必要的架构,但您必须通过指定initializeSchema
boolean 或设置…initialize-schema=true
在application.properties
文件。
这是一个突破性的变化!在早期版本的 Spring AI 中,默认情况下会进行此架构初始化。 |
配置
要连接到 Neo4j 并使用Neo4jVectorStore
,您需要提供实例的访问详细信息。
可以通过 Spring Boot 的 application.properties 提供简单的配置,
spring.neo4j.uri=<uri_for_your_neo4j_instance>
spring.neo4j.authentication.username=<your_username>
spring.neo4j.authentication.password=<your_password>
# API key if needed, e.g. OpenAI
spring.ai.openai.api.key=<api-key>
环境变量 /
export SPRING_NEO4J_URI=<uri_for_your_neo4j_instance>
export SPRING_NEO4J_AUTHENTICATION_USERNAME=<your_username>
export SPRING_NEO4J_AUTHENTICATION_PASSWORD=<your_password>
# API key if needed, e.g. OpenAI
export SPRING_AI_OPENAI_API_KEY=<api-key>
也可以是这些的混合。 例如,如果您想将 API 密钥存储为环境变量,但将其余部分保留在普通的 application.properties 文件中。
如果您选择创建 shell 脚本以方便将来的工作,请确保在启动应用程序之前通过“源”文件运行它,即source <your_script_name>.sh . |
除了application.properties和环境变量之外, Spring Boot 还提供了其他配置选项。 |
Spring Boot 的 Neo4j 驱动程序自动配置功能将创建一个 bean 实例,该实例将由Neo4jVectorStore
.
自动配置
Spring AI 为 Neo4j Vector Store 提供 Spring Boot 自动配置。
要启用它,请将以下依赖项添加到项目的 Maven 中pom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-neo4j-store-spring-boot-starter</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-neo4j-store-spring-boot-starter'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
请查看 vector store 的配置参数列表,了解默认值和配置选项。
请参阅 Repositories 部分,将 Milestone 和/或 Snapshot Repositories 添加到您的构建文件中。 |
此外,您还需要配置一个EmbeddingModel
豆。请参阅 EmbeddingModel 部分以了解更多信息。
以下是所需 bean 的示例:
@Bean
public EmbeddingModel embeddingModel() {
// Can be any other Embeddingmodel implementation.
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("SPRING_AI_OPENAI_API_KEY")));
}
在 Spring Boot 自动配置 Neo4j 的情况下Driver
bean 不是你想要或需要的,你仍然可以定义自己的 bean。
请阅读 Neo4j Java 驱动程序参考 以获取有关自定义驱动程序配置的更深入信息。
@Bean
public Driver driver() {
return GraphDatabase.driver("neo4j://<host>:<bolt-port>",
AuthTokens.basic("<username>", "<password>"));
}
现在,您可以自动连接Neo4jVectorStore
作为应用程序中的向量存储。
元数据筛选
您也可以在 Neo4j store 中使用通用的可移植元数据过滤器。
例如,您可以使用文本表达式语言:
vectorStore.similaritySearch(
SearchRequest.defaults()
.withQuery("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression("author in ['john', 'jill'] && 'article_type' == 'blog'"));
或者以编程方式使用Filter.Expression
DSL:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.defaults()
.withQuery("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression(b.and(
b.in("author", "john", "jill"),
b.eq("article_type", "blog")).build()));
这些(可移植的)过滤器表达式会自动转换为专有的 Neo4jWHERE 筛选表达式。 |
例如,此可移植筛选条件表达式:
author in ['john', 'jill'] && 'article_type' == 'blog'
转换为专有的 Neo4j 过滤器格式:
node.`metadata.author` IN ["john","jill"] AND node.`metadata.'article_type'` = "blog"
Neo4jVectorStore 属性
您可以在 Spring Boot 配置中使用以下属性来自定义 Neo4j 矢量存储。
财产 | 默认值 |
---|---|
|
Neo4J |
|
假 |
|
1536 |
|
余弦 |
|
公文 |
|
嵌入 |
|
spring-ai-document-index (英文) |