Azure AI 服务
本部分将引导您完成设置AzureVectorStore
以存储文档嵌入并使用 Azure AI 搜索服务执行相似性搜索。
Azure AI 搜索是一种多功能的云托管云信息检索系统,是 Microsoft 更大的 AI 平台的一部分。除其他功能外,它还允许用户使用基于向量的存储和检索来查询信息。
配置
启动时,AzureVectorStore
可以尝试在 AI Search 服务实例中创建新索引(如果您已通过设置相关的initialize-schema
boolean
property 设置为true
在构造函数中,或者如果使用 Spring Boot,则设置…initialize-schema=true
在application.properties
文件。
这是一个突破性的变化!在早期版本的 Spring AI 中,默认情况下会进行此架构初始化。 |
或者,您可以手动创建索引。
若要设置 AzureVectorStore,需要从上述先决条件中检索到的设置以及索引名称:
-
Azure AI 搜索终结点
-
Azure AI 搜索密钥
-
(可选)Azure OpenAI API 终结点
-
(可选)Azure OpenAI API 密钥
您可以将这些值作为作系统环境变量提供。
export AZURE_AI_SEARCH_API_KEY=<My AI Search API Key>
export AZURE_AI_SEARCH_ENDPOINT=<My AI Search Index>
export OPENAI_API_KEY=<My Azure AI API Key> (Optional)
可以将 Azure Open AI 实现替换为支持 Embeddings 接口的任何有效 OpenAI 实现。例如,您可以使用 Spring AI 的 Open AI 或 |
依赖
将这些依赖项添加到您的项目中:
1. 选择 Embeddings 接口实现。您可以选择:
-
OpenAI Embedding
-
Azure AI Embedding
-
Local Sentence Transformers Embedding
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-transformers-spring-boot-starter</artifactId>
</dependency>
2. Azure (AI Search) 矢量存储
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-azure-store</artifactId>
</dependency>
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
配置属性
您可以在 Spring Boot 配置中使用以下属性来自定义 Azure 矢量存储。
财产 | 默认值 |
---|---|
|
|
|
|
|
假 |
|
假 |
|
spring_ai_azure_vector_store |
|
4 |
|
0.0 |
|
嵌入 |
|
spring-ai-document-index (英文) |
示例代码
配置 AzureSearchIndexClient
在您的应用程序中,您可以使用以下代码:
@Bean
public SearchIndexClient searchIndexClient() {
return new SearchIndexClientBuilder().endpoint(System.getenv("AZURE_AI_SEARCH_ENDPOINT"))
.credential(new AzureKeyCredential(System.getenv("AZURE_AI_SEARCH_API_KEY")))
.buildClient();
}
要创建向量存储,您可以通过注入SearchIndexClient
bean 以及EmbeddingModel
由实现所需 Embeddings 接口的 Spring AI 库提供。
@Bean
public VectorStore vectorStore(SearchIndexClient searchIndexClient, EmbeddingModel embeddingModel) {
return new AzureVectorStore(searchIndexClient, embeddingModel,
// Define the metadata fields to be used
// in the similarity search filters.
List.of(MetadataField.text("country"),
MetadataField.int64("year"),
MetadataField.bool("active")));
}
您必须明确列出筛选条件表达式中使用的任何元数据键的所有元数据字段名称和类型。上面的列表注册了可筛选的元数据字段: 如果可筛选的元数据字段使用新条目展开,则必须使用此元数据(重新)上传/更新文档。 |
在您的主代码中,创建一些文档:
List<Document> documents = List.of(
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("country", "BG", "year", 2020)),
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("country", "NL", "year", 2023)));
将文档添加到您的 vector 存储中:
vectorStore.add(documents);
最后,检索类似于查询的文档:
List<Document> results = vectorStore.similaritySearch(
SearchRequest
.query("Spring")
.withTopK(5));
如果一切顺利,您应该检索包含文本 “Spring AI rocks!!” 的文档。
元数据筛选
还可以将通用的可移植元数据筛选器与 AzureVectorStore 结合使用。
例如,您可以使用文本表达式语言:
vectorStore.similaritySearch(
SearchRequest
.query("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression("country in ['UK', 'NL'] && year >= 2020"));
或者以编程方式使用表达式 DSL:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(
SearchRequest
.query("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression(b.and(
b.in("country", "UK", "NL"),
b.gte("year", 2020)).build()));
可移植筛选器表达式会自动转换为专有的 Azure 搜索 OData 筛选器。例如,以下可移植筛选条件表达式:
country in ['UK', 'NL'] && year >= 2020
转换为以下 Azure OData 筛选器表达式:
$filter search.in(meta_country, 'UK,NL', ',') and meta_year ge 2020