Neo4j

本节将指导您完成设置Neo4jVectorStore来存储文档嵌入并执行相似性搜索。spring-doc.cadn.net.cn

Neo4j 是一个开源的 NoSQL 图形数据库。 它是一个完全事务性的数据库 (ACID),将数据存储为由节点组成的图形,通过关系连接。 受现实世界结构的启发,它允许对复杂数据进行高查询性能,同时为开发人员保持直观和简单。spring-doc.cadn.net.cn

Neo4j 的 Vector Search 允许用户从大型数据集中查询向量嵌入。 嵌入是数据对象(如文本、图像、音频或文档)的数字表示形式。 嵌入可以存储在 Node 属性上,并且可以使用db.index.vector.queryNodes()功能。 这些索引由 Lucene 提供支持,使用分层可导航小世界图 (HNSW) 对向量字段执行 k 个近似最近邻 (k-ANN) 查询。spring-doc.cadn.net.cn

先决条件

依赖

将 Neo4j Vector Store 依赖项添加到您的项目中:spring-doc.cadn.net.cn

<dependency>
	<groupId>org.springframework.ai</groupId>
	<artifactId>spring-ai-neo4j-store</artifactId>
</dependency>

或发送到您的 Gradlebuild.gradlebuild 文件。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-neo4j-store'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。

矢量存储实现可以为您初始化必要的架构,但您必须通过指定initializeSchemaboolean 或设置…​initialize-schema=trueapplication.properties文件。spring-doc.cadn.net.cn

这是一个突破性的变化!在早期版本的 Spring AI 中,默认情况下会进行此架构初始化。

配置

要连接到 Neo4j 并使用Neo4jVectorStore,您需要提供实例的访问详细信息。 可以通过 Spring Boot 的 application.properties 提供简单的配置,spring-doc.cadn.net.cn

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>

环境变量 /spring-doc.cadn.net.cn

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 文件中。spring-doc.cadn.net.cn

如果您选择创建 shell 脚本以方便将来的工作,请确保在启动应用程序之前通过“源”文件运行它,即source <your_script_name>.sh.
除了application.properties和环境变量之外, Spring Boot 还提供了其他配置选项

Spring Boot 的 Neo4j 驱动程序自动配置功能将创建一个 bean 实例,该实例将由Neo4jVectorStore.spring-doc.cadn.net.cn

自动配置

Spring AI 为 Neo4j Vector Store 提供 Spring Boot 自动配置。 要启用它,请将以下依赖项添加到项目的 Maven 中pom.xml文件:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-neo4j-store-spring-boot-starter</artifactId>
</dependency>

或发送到您的 Gradlebuild.gradlebuild 文件。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-neo4j-store-spring-boot-starter'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。

请查看 vector store 的配置参数列表,了解默认值和配置选项。spring-doc.cadn.net.cn

请参阅 Repositories 部分,将 Milestone 和/或 Snapshot Repositories 添加到您的构建文件中。

此外,您还需要配置一个EmbeddingModel豆。请参阅 EmbeddingModel 部分以了解更多信息。spring-doc.cadn.net.cn

以下是所需 bean 的示例:spring-doc.cadn.net.cn

@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 的情况下Driverbean 不是你想要或需要的,你仍然可以定义自己的 bean。 请阅读 Neo4j Java 驱动程序参考 以获取有关自定义驱动程序配置的更深入信息。spring-doc.cadn.net.cn

@Bean
public Driver driver() {
    return GraphDatabase.driver("neo4j://<host>:<bolt-port>",
            AuthTokens.basic("<username>", "<password>"));
}

现在,您可以自动连接Neo4jVectorStore作为应用程序中的向量存储。spring-doc.cadn.net.cn

元数据筛选

您也可以在 Neo4j store 中使用通用的可移植元数据过滤器spring-doc.cadn.net.cn

例如,您可以使用文本表达式语言:spring-doc.cadn.net.cn

vectorStore.similaritySearch(
                    SearchRequest.defaults()
                            .withQuery("The World")
                            .withTopK(TOP_K)
                            .withSimilarityThreshold(SIMILARITY_THRESHOLD)
                            .withFilterExpression("author in ['john', 'jill'] && 'article_type' == 'blog'"));

或者以编程方式使用Filter.ExpressionDSL:spring-doc.cadn.net.cn

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 筛选表达式

例如,此可移植筛选条件表达式:spring-doc.cadn.net.cn

author in ['john', 'jill'] && 'article_type' == 'blog'

转换为专有的 Neo4j 过滤器格式:spring-doc.cadn.net.cn

node.`metadata.author` IN ["john","jill"] AND node.`metadata.'article_type'` = "blog"

Neo4jVectorStore 属性

您可以在 Spring Boot 配置中使用以下属性来自定义 Neo4j 矢量存储。spring-doc.cadn.net.cn

财产 默认值

spring.ai.vectorstore.neo4j.database-namespring-doc.cadn.net.cn

Neo4Jspring-doc.cadn.net.cn

spring.ai.vectorstore.neo4j.initialize-schemaspring-doc.cadn.net.cn

spring-doc.cadn.net.cn

spring.ai.vectorstore.neo4j.embedding-dimensionspring-doc.cadn.net.cn

1536spring-doc.cadn.net.cn

spring.ai.vectorstore.neo4j.distance-typespring-doc.cadn.net.cn

余弦spring-doc.cadn.net.cn

spring.ai.vectorstore.neo4j.labelspring-doc.cadn.net.cn

公文spring-doc.cadn.net.cn

spring.ai.vectorstore.neo4j.embedding-propertyspring-doc.cadn.net.cn

嵌入spring-doc.cadn.net.cn

spring.ai.vectorstore.neo4j.index-namespring-doc.cadn.net.cn

spring-ai-document-index (英文)spring-doc.cadn.net.cn