MariaDB 矢量存储
本节将指导您完成设置MariaDBVectorStore
来存储文档嵌入并执行相似性搜索。
MariaDB Vector 是 MariaDB 11.7 的一部分,支持存储和搜索机器学习生成的嵌入。 它使用向量索引提供高效的向量相似性搜索功能,同时支持余弦相似度和欧几里得距离度量。
先决条件
-
一个正在运行的 MariaDB (11.7+) 实例。以下选项可用:
-
如果需要,EmbeddingModel 的 API 密钥,用于生成由
MariaDBVectorStore
.
自动配置
Spring AI 为 MariaDB Vector Store 提供 Spring Boot 自动配置。
要启用它,请将以下依赖项添加到项目的 Maven 中pom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mariadb-store-spring-boot-starter</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-mariadb-store-spring-boot-starter'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
矢量存储实现可以为您初始化所需的架构,但您必须通过指定initializeSchema
boolean 或设置…initialize-schema=true
在application.properties
文件。
这是一个突破性的变化!在早期版本的 Spring AI 中,默认情况下会进行此架构初始化。 |
此外,您还需要配置一个EmbeddingModel
豆。请参阅 EmbeddingModel 部分以了解更多信息。
例如,要使用 OpenAI EmbeddingModel,请添加以下依赖项:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
请参阅 Repositories 部分,将 Maven Central 和/或 Snapshot Repositories 添加到您的构建文件中。 |
现在,您可以自动连接MariaDBVectorStore
在您的应用程序中:
@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 MariaDB
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());
配置属性
要连接到 MariaDB 并使用MariaDBVectorStore
,您需要提供实例的访问详细信息。
可以通过 Spring Boot 的application.yml
:
spring:
datasource:
url: jdbc:mariadb://localhost/db
username: myUser
password: myPassword
ai:
vectorstore:
mariadb:
initialize-schema: true
distance-type: COSINE
dimensions: 1536
如果您通过 Docker Compose 或 Testcontainers 将 MariaDB Vector 作为 Spring Boot 开发服务运行, 您无需配置 URL、用户名和密码,因为它们是由 Spring Boot 自动配置的。 |
属性以spring.ai.vectorstore.mariadb.*
用于配置MariaDBVectorStore
:
财产 | 描述 | 默认值 |
---|---|---|
|
是否初始化所需的 schema |
|
|
搜索距离类型。用 |
|
|
嵌入维度。如果未明确指定,将从提供的 |
|
|
启动时删除现有的 vector store 表。 |
|
|
矢量存储架构名称 |
|
|
矢量存储表名称 |
|
|
启用 Schema 和 Table 名称验证,以确保它们是有效的现有对象。 |
|
如果配置自定义架构和/或表名称,请考虑通过设置spring.ai.vectorstore.mariadb.schema-validation=true .
这样可以确保名称的正确性,并降低 SQL 注入攻击的风险。 |
手动配置
您可以手动配置 MariaDB 向量存储,而不是使用 Spring Boot 自动配置。为此,您需要将以下依赖项添加到您的项目中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mariadb-store</artifactId>
</dependency>
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
然后创建MariaDBVectorStore
使用 Builder 模式的 Bean:
@Bean
public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) {
return MariaDBVectorStore.builder(jdbcTemplate, embeddingModel)
.dimensions(1536) // Optional: defaults to 1536
.distanceType(MariaDBDistanceType.COSINE) // Optional: defaults to COSINE
.schemaName("mydb") // Optional: defaults to null
.vectorTableName("custom_vectors") // Optional: defaults to "vector_store"
.contentFieldName("text") // Optional: defaults to "content"
.embeddingFieldName("embedding") // Optional: defaults to "embedding"
.idFieldName("doc_id") // Optional: defaults to "id"
.metadataFieldName("meta") // Optional: defaults to "metadata"
.initializeSchema(true) // Optional: defaults to false
.schemaValidation(true) // Optional: defaults to false
.removeExistingVectorStoreTable(false) // Optional: defaults to false
.maxDocumentBatchSize(10000) // Optional: defaults to 10000
.build();
}
// This can be any EmbeddingModel implementation
@Bean
public EmbeddingModel embeddingModel() {
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("OPENAI_API_KEY")));
}
元数据筛选
您可以将通用的可移植元数据过滤器与 MariaDB Vector store 结合使用。
例如,您可以使用文本表达式语言:
vectorStore.similaritySearch(
SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression("author in ['john', 'jill'] && article_type == 'blog'").build());
或者以编程方式使用Filter.Expression
DSL:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression(b.and(
b.in("author", "john", "jill"),
b.eq("article_type", "blog")).build()).build());
这些筛选表达式会自动转换为等效的 MariaDB JSON 路径表达式。 |
访问 Native Client
MariaDB Vector Store 实现提供对底层原生 JDBC 客户端 (JdbcTemplate
) 通过getNativeClient()
方法:
MariaDBVectorStore vectorStore = context.getBean(MariaDBVectorStore.class);
Optional<JdbcTemplate> nativeClient = vectorStore.getNativeClient();
if (nativeClient.isPresent()) {
JdbcTemplate jdbc = nativeClient.get();
// Use the native client for MariaDB-specific operations
}
本机客户端允许您访问特定于 MariaDB 的功能和作,这些功能和作可能不会通过VectorStore
接口。