Couchbase
本節將引導您設定 `CouchbaseSearchVectorStore` 以儲存文件嵌入並使用 Couchbase 執行相似性搜尋。
Couchbase 是一個分散式 JSON 文件資料庫,擁有關係型 DBMS 的所有所需功能。除其他功能外,它還允許使用者使用基於向量的儲存和檢索來查詢資訊。
先決條件
一個正在執行的 Couchbase 例項。以下選項可用:Couchbase * Docker * Capella - Couchbase 即服務 * 本地安裝 Couchbase * Couchbase Kubernetes 運算子
自動配置
|
Spring AI 自動配置、啟動模組的工件名稱發生了重大變化。請參閱 升級說明 以獲取更多資訊。 |
Spring AI 為 Couchbase Vector Store 提供了 Spring Boot 自動配置。要啟用它,請將以下依賴項新增到您專案的 Maven `pom.xml` 檔案中
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-couchbase</artifactId>
</dependency>
或新增到您的 Gradle build.gradle 構建檔案中。
dependencies {
implementation 'org.springframework.ai:spring-ai-couchbase-store-spring-boot-starter'
}
| Couchbase Vector 搜尋僅在版本 7.6 及更高版本和 Java SDK 版本 3.6.0" 中可用 |
| 請參閱 依賴項管理 部分,將 Spring AI BOM 新增到您的構建檔案中。 |
| 請參閱Artifact Repositories部分,將里程碑和/或快照儲存庫新增到您的構建檔案中。 |
向量儲存實現可以為您初始化配置的儲存桶、作用域、集合和搜尋索引,並使用預設選項,但您必須透過在相應的建構函式中指定 `initializeSchema` 布林值來選擇啟用。
| 這是一個重大更改!在早期版本的 Spring AI 中,此模式初始化是預設發生的。 |
請查看向量儲存的配置引數列表,以瞭解預設值和配置選項。
此外,您還需要一個配置的 `EmbeddingModel` bean。有關更多資訊,請參閱EmbeddingModel部分。
現在您可以在應用程式中將 `CouchbaseSearchVectorStore` 自動裝配為向量儲存。
@Autowired VectorStore vectorStore;
// ...
List <Document> documents = List.of(
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
new Document("The World is Big and Salvation Lurks Around the Corner"),
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));
// Add the documents to Qdrant
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));
配置屬性
要連線到 Couchbase 並使用 `CouchbaseSearchVectorStore`,您需要提供例項的訪問詳細資訊。配置可以透過 Spring Boot 的 `application.properties` 提供
spring.ai.openai.api-key=<key>
spring.couchbase.connection-string=<conn_string>
spring.couchbase.username=<username>
spring.couchbase.password=<password>
如果您更喜歡將環境變數用於密碼或 API 金鑰等敏感資訊,您有多種選擇
選項 1:使用 Spring 表示式語言 (SpEL)
您可以使用自定義環境變數名稱並在應用程式配置中使用 SpEL 引用它們
# In application.yml
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
couchbase:
connection-string: ${COUCHBASE_CONN_STRING}
username: ${COUCHBASE_USER}
password: ${COUCHBASE_PASSWORD}
# In your environment or .env file
export OPENAI_API_KEY=<api-key>
export COUCHBASE_CONN_STRING=<couchbase connection string like couchbase://>
export COUCHBASE_USER=<couchbase username>
export COUCHBASE_PASSWORD=<couchbase password>
選項 2:以程式設計方式訪問環境變數
或者,您可以在 Java 程式碼中訪問環境變數
String apiKey = System.getenv("OPENAI_API_KEY");
這種方法讓您在命名環境變數時具有靈活性,同時將敏感資訊排除在應用程式配置檔案之外。
| 如果您選擇建立 shell 指令碼以便將來工作,請務必在啟動應用程式之前透過“引用”檔案來執行它,即 `source |
Spring Boot 的 Couchbase 叢集自動配置功能將建立一個 bean 例項,該例項將由 `CouchbaseSearchVectorStore` 使用。
以 `spring.couchbase.*` 開頭的 Spring Boot 屬性用於配置 Couchbase 叢集例項
| 財產 | 描述 | 預設值 |
|---|---|---|
|
Couchbase 連線字串 |
|
|
用於與 Couchbase 身份驗證的密碼。 |
- |
|
用於與 Couchbase 身份驗證的使用者名稱。 |
- |
|
每個節點的最小套接字數。 |
1 |
|
每個節點的最大套接字數。 |
12 |
|
HTTP 連線在關閉並從池中移除之前可以保持空閒的時間長度。 |
1秒 |
|
是否啟用 SSL 支援。如果提供了“bundle”,則除非另有指定,否則會自動啟用。 |
- |
|
SSL 捆綁包名稱。 |
- |
|
儲存桶連線超時。 |
10秒 |
|
儲存桶斷開連線超時。 |
10秒 |
|
特定鍵值操作的超時。 |
2500ms |
|
具有永續性級別的特定鍵值操作的超時。 |
10秒 |
|
具有永續性級別的特定鍵值操作的超時。 |
10秒 |
|
SQL++ 查詢操作超時。 |
75s |
|
常規和地理空間檢視操作超時。 |
75s |
|
搜尋服務的超時。 |
75s |
|
分析服務的超時。 |
75s |
|
管理操作的超時。 |
75s |
以 `spring.ai.vectorstore.couchbase.*` 字首開頭的屬性用於配置 `CouchbaseSearchVectorStore`。
| 財產 | 描述 | 預設值 |
|---|---|---|
|
用於儲存向量的索引名稱。 |
spring-ai-document-index |
|
Couchbase 儲存桶的名稱,作用域的父級。 |
default |
|
Couchbase 作用域的名稱,集合的父級。搜尋查詢將在作用域上下文中執行。 |
default |
|
用於儲存文件的 Couchbase 集合的名稱。 |
default |
|
向量的維度數。 |
1536 |
|
要使用的相似性函式。 |
|
|
要使用的相似性函式。 |
|
|
是否初始化所需的模式 |
|
以下相似性函式可用
-
l2_norm
-
dot_product
以下索引最佳化可用
-
recall
-
latency
有關每個的更多詳細資訊,請參閱關於向量搜尋的Couchbase 文件。
元資料過濾
您可以將通用的、可移植的元資料過濾器與 Couchbase 儲存一起使用。
例如,您可以使用文字表示式語言
vectorStore.similaritySearch(
SearchRequest.defaults()
.query("The World")
.topK(TOP_K)
.filterExpression("author in ['john', 'jill'] && article_type == 'blog'"));
或使用 `Filter.Expression` DSL 以程式設計方式
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.defaults()
.query("The World")
.topK(TOP_K)
.filterExpression(b.and(
b.in("author","john", "jill"),
b.eq("article_type", "blog")).build()));
| 這些過濾器表示式被轉換為等效的 Couchbase SQL++ 過濾器。 |
手動配置
您可以手動配置 Couchbase 向量儲存,而不是使用 Spring Boot 自動配置。為此,您需要將 `spring-ai-couchbase-store` 新增到您的專案中
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-couchbase-store</artifactId>
</dependency>
或新增到您的 Gradle build.gradle 構建檔案中。
dependencies {
implementation 'org.springframework.ai:spring-ai-couchbase-store'
}
建立一個 Couchbase `Cluster` bean。閱讀Couchbase 文件以獲取有關自定義 Cluster 例項配置的更深入資訊。
@Bean
public Cluster cluster() {
return Cluster.connect("couchbase://", "username", "password");
}
然後使用構建器模式建立 `CouchbaseSearchVectorStore` bean
@Bean
public VectorStore couchbaseSearchVectorStore(Cluster cluster,
EmbeddingModel embeddingModel,
Boolean initializeSchema) {
return CouchbaseSearchVectorStore
.builder(cluster, embeddingModel)
.bucketName("test")
.scopeName("test")
.collectionName("test")
.initializeSchema(initializeSchema)
.build();
}
// This can be any EmbeddingModel implementation.
@Bean
public EmbeddingModel embeddingModel() {
return new OpenAiEmbeddingModel(OpenAiApi.builder().apiKey(this.openaiKey).build());
}