Neo4j
本節將引導您設定 Neo4jVectorStore
以儲存文件嵌入 (document embedding) 並執行相似度搜尋。
Neo4j 是一個開放原始碼的 NoSQL 圖形資料庫。它是一個完全交易式的資料庫 (ACID),以圖形結構儲存資料,圖形由節點組成,並由關係連接。受到真實世界結構的啟發,它允許在複雜資料上實現高效能查詢,同時對開發人員保持直觀和簡單。
Neo4j 的向量搜尋 允許使用者從大型資料集中查詢向量嵌入。嵌入是資料物件 (例如文字、影像、音訊或文件) 的數值表示。嵌入可以儲存在節點屬性上,並可以使用 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>
或新增至您的 Gradle build.gradle
建置檔案。
dependencies {
implementation 'org.springframework.ai:spring-ai-neo4j-store'
}
請參閱相依性管理章節,將 Spring AI BOM 新增至您的建置檔案。 |
向量儲存實作可以為您初始化必要的結構描述,但您必須透過在適當的建構函式中指定 initializeSchema
布林值,或在 application.properties
檔案中設定 …initialize-schema=true
來選擇加入。
這是一個重大變更!在 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」該檔案,即 source <your_script_name>.sh 。 |
除了 application.properties 和環境變數之外,Spring Boot 還提供其他組態選項。 |
Spring Boot Neo4j Driver 的自動組態功能將建立一個 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>
或新增至您的 Gradle build.gradle
建置檔案。
dependencies {
implementation 'org.springframework.ai:spring-ai-neo4j-store-spring-boot-starter'
}
請參閱相依性管理章節,將 Spring AI BOM 新增至您的建置檔案。 |
請查看 組態參數 列表,以了解向量儲存的預設值和組態選項。
請參閱儲存庫章節,將 Milestone 和/或 Snapshot 儲存庫新增至您的建置檔案。 |
此外,您還需要一個已組態的 EmbeddingModel
bean。請參閱 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 Driver 參考文件 以取得有關自訂驅動程式組態的更深入資訊。
@Bean
public Driver driver() {
return GraphDatabase.driver("neo4j://<host>:<bolt-port>",
AuthTokens.basic("<username>", "<password>"));
}
現在您可以將 Neo4jVectorStore
自動注入 (auto-wire) 為應用程式中的向量儲存。
Metadata 過濾
您也可以將通用的、可攜式的 metadata 過濾器 與 Neo4j 儲存一起使用。
例如,您可以使用文字表達式語言
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()));
這些(可攜式)過濾器表達式會自動轉換為專有的 Neo4j WHERE 過濾器表達式。 |
例如,這個可攜式過濾器表達式
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 |
|
false |
|
1536 |
|
cosine |
|
Document |
|
embedding |
|
spring-ai-document-index |