MariaDB 向量
本节将引导您完成 MariaDB 的设置VectorStore
来存储文档嵌入并执行相似性搜索。
MariaDB 向量是 MariaDB 11.7 的一部分,支持存储和搜索机器学习生成的嵌入。
自动配置
将 MariaDBVectorStore 启动Starters依赖项添加到您的项目中:
<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'
}
矢量存储实现可以为您初始化所需的架构,但您必须通过指定initializeSchema
boolean 或设置…initialize-schema=true
在application.properties
文件。
Vector Store 还需要EmbeddingModel
实例来计算文档的嵌入。
您可以选择一个可用的 EmbeddingModel Implementations。
例如,要使用 OpenAI EmbeddingModel,请将以下依赖项添加到您的项目中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 请参阅 Repositories 部分,将 Milestone 和/或 Snapshot Repositories 添加到您的构建文件中。 |
要连接并配置MariaDBVectorStore
,您需要提供实例的访问详细信息。
可以通过 Spring Boot 的application.yml
.
spring: datasource: url: jdbc:mariadb://localhost/db username: myUser password: myPassword ai: vectorstore: mariadbvector: distance-type: COSINE dimensions: 1536
如果您通过 Docker Compose 或 Testcontainers 将 MariaDBvector 作为 Spring Boot 开发服务运行, 您无需配置 URL、用户名和密码,因为它们是由 Spring Boot 自动配置的。 |
检查配置参数列表以了解默认值和配置选项。 |
现在,您可以自动连接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 PGVector
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));
配置属性
您可以在 Spring Boot 配置中使用以下属性来自定义 MariaDB 矢量存储。
财产 | 描述 | 默认值 |
---|---|---|
|
搜索距离类型。默认为 |
余弦 |
|
嵌入维度。如果未明确指定,则 PgVectorStore 将从提供的 |
- |
|
删除现有的 |
假 |
|
是否初始化所需的 schema |
假 |
|
矢量存储架构名称 |
零 |
|
矢量存储表名称 |
|
|
启用 Schema 和 Table 名称验证,以确保它们是有效的现有对象。 |
假 |
如果配置自定义架构和/或表名称,请考虑通过设置spring.ai.vectorstore.mariadb.schema-validation=true .
这样可以确保名称的正确性,并降低 SQL 注入攻击的风险。 |
元数据筛选
您可以将通用的可移植元数据过滤器与 MariaDB 矢量存储结合使用。
例如,您可以使用文本表达式语言:
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()));
这些筛选表达式将转换为等效的 PgVector 筛选条件。 |
手动配置
除了使用 Spring Boot 自动配置,您还可以手动配置MariaDBVectorStore
.
为此,您需要添加 MariaDB 连接器和JdbcTemplate
auto-configuration 依赖项添加到项目中:
<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 添加到您的构建文件中。 |
要在应用程序中配置 MariaDB Vector,您可以使用以下设置:
@Bean
public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) {
return new MariaDBVectorStore(jdbcTemplate, embeddingModel);
}