OpenSearch
本節將引導您設定 OpenSearch VectorStore
,以儲存文件嵌入並執行相似性搜尋。
OpenSearch 是一個開源搜尋和分析引擎,最初從 Elasticsearch 分叉而來,並在 Apache License 2.0 下發布。它通過簡化 AI 生成資產的整合和管理,增強了 AI 應用程式的開發。 OpenSearch 支持向量、詞彙和混合搜尋功能,利用先進的向量資料庫功能來促進低延遲查詢和相似性搜尋,詳情請參閱向量資料庫頁面。此平台非常適合構建可擴展的 AI 驅動應用程式,並提供用於資料管理、容錯和資源訪問控制的強大工具。
先決條件
-
一個正在運行的 OpenSearch 實例。以下選項可用
-
EmbeddingModel
實例來計算文件嵌入。有多種選項可用-
如果需要,用於 EmbeddingModel 的 API 金鑰,以生成
OpenSearchVectorStore
儲存的嵌入。
-
相依性
將 OpenSearch Vector Store 相依性添加到您的專案
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-opensearch-store</artifactId>
</dependency>
dependencies {
implementation 'org.springframework.ai:spring-ai-opensearch-store'
}
請參閱相依性管理章節,將 Spring AI BOM 添加到您的構建檔案。 |
組態
要連接到 OpenSearch 並使用 OpenSearchVectorStore
,您需要提供實例的訪問詳細資訊。可以通過 Spring Boot 的 application.yml
提供一個簡單的組態,
spring:
ai:
vectorstore:
opensearch:
uris: <opensearch instance URIs>
username: <opensearch username>
password: <opensearch password>
indexName: <opensearch index name>
mappingJson: <JSON mapping for opensearch index>
aws:
host: <aws opensearch host>
serviceName: <aws service name>
accessKey: <aws access key>
secretKey: <aws secret key>
region: <aws region>
# API key if needed, e.g. OpenAI
openai:
apiKey: <api-key>
查看組態參數列表,以了解預設值和組態選項。 |
自動組態
自託管 OpenSearch
Spring AI 為 OpenSearch Vector Store 提供 Spring Boot 自動組態。要啟用它,請將以下相依性添加到您的專案的 Maven pom.xml
或 Gradle build.gradle
構建檔案中
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-opensearch-store-spring-boot-starter</artifactId>
</dependency>
dependencies {
implementation 'org.springframework.ai:spring-ai-opensearch-store-spring-boot-starter'
}
然後使用 spring.ai.vectorstore.opensearch.*
屬性來組態與自託管 OpenSearch 實例的連線。
Amazon OpenSearch Service
要啟用 Amazon OpenSearch Service,請將以下相依性添加到您的專案的 Maven pom.xml
或 Gradle build.gradle
構建檔案中
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-aws-opensearch-store-spring-boot-starter</artifactId>
</dependency>
dependencies {
implementation 'org.springframework.ai:spring-ai-aws-opensearch-store-spring-boot-starter'
}
然後使用 spring.ai.vectorstore.opensearch.aws.*
屬性來組態與 Amazon OpenSearch Service 的連線。
請參閱相依性管理章節,將 Spring AI BOM 添加到您的構建檔案。 |
以下是所需 bean 的範例
@Bean
public EmbeddingModel embeddingModel() {
// Can be any other EmbeddingModel implementation
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("SPRING_AI_OPENAI_API_KEY")));
}
現在您可以在您的應用程式中自動裝配 OpenSearchVectorStore
作為向量儲存。
@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 OpenSearch
vectorStore.add(List.of(document));
// Retrieve documents similar to a query
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));
組態屬性
您可以在 Spring Boot 組態中使用以下屬性來自訂 OpenSearch 向量儲存。
屬性 | 描述 | 預設值 |
---|---|---|
|
OpenSearch 叢集端點的 URI。 |
- |
|
用於訪問 OpenSearch 叢集的使用者名稱。 |
- |
|
指定使用者名稱的密碼。 |
- |
|
要在 OpenSearch 叢集中使用的預設索引名稱。 |
|
|
JSON 字串,用於定義索引的映射;指定如何儲存和索引文件及其欄位。 |
{ "properties":{ "embedding":{ "type":"knn_vector", "dimension":1536 } } } |
|
OpenSearch 實例的主機名稱。 |
- |
|
OpenSearch 實例的 AWS 服務名稱。 |
- |
|
OpenSearch 實例的 AWS 訪問金鑰。 |
- |
|
OpenSearch 實例的 AWS 密鑰。 |
- |
|
OpenSearch 實例的 AWS 區域。 |
- |
自訂 OpenSearch Client 組態
如果 Spring Boot 自動組態的 OpenSearchClient 與 Apache HttpClient 5 Transport
bean 不是您想要的或需要的,您仍然可以定義自己的 bean。請閱讀 OpenSearch Java Client 文件
元數據篩選
您也可以將通用的、可移植的 元數據篩選器與 OpenSearch 一起使用。
例如,您可以使用文字表達式語言
-
SQL 篩選語法
-
Filter.Expression
DSL
vectorStore.similaritySearch(SearchRequest.defaults()
.withQuery("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression("author in ['john', 'jill'] && 'article_type' == 'blog'"));
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.defaults()
.withQuery("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression(b.and(
b.in("john", "jill"),
b.eq("article_type", "blog")).build()));
這些(可移植的)篩選表達式會自動轉換為專有的 OpenSearch Query string query。 |
例如,這個可移植的篩選表達式
author in ['john', 'jill'] && 'article_type' == 'blog'
被轉換為專有的 OpenSearch 篩選格式
(metadata.author:john OR jill) AND metadata.article_type:blog