Oracle Database 23ai - AI 向量搜索
Oracle Database 23ai(23.4+)的 AI Vector Search 功能可作为 Spring AI VectorStore 使用,以帮助您存储文档嵌入并执行相似性搜索。当然,所有其他功能也同样可用。
在本地运行 Oracle Database 23ai 附录展示了如何使用轻量级 Docker 容器启动数据库。
自动配置
Spring AI 的自动配置和 starter 模块的 artifact 名称发生了重大变化。更多信息请参阅 升级说明。
首先,将 Oracle Vector Store 启动器依赖项添加到您的项目中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-oracle</artifactId>
</dependency>
或添加到您的 Gradle build.gradle 构建文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-oracle'
}
若需此向量存储库为您初始化架构,则需在相应构造函数中将 initializeSchema 布尔参数设置为 true,或在 application.properties 文件中配置 …initialize-schema=true。
这是一个破坏性变更! 在 Spring AI 的早期版本中,这个模式初始化是默认发生的。
向量存储(Vector Store)还需要一个 EmbeddingModel 实例来计算文档的嵌入向量。你可以从可用的嵌入模型实现中选择一个。
例如,要使用 OpenAI 嵌入模型,请在项目中添加以下依赖:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
或将其添加到你的 Gradle build.gradle 构建文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-openai'
}
要连接和配置 OracleVectorStore,你需要提供数据库的访问详细信息。一个简单的配置可以通过 Spring Boot 的 application.yml 来提供。
spring:
datasource:
url: jdbc:oracle:thin:@//localhost:1521/freepdb1
username: mlops
password: mlops
ai:
vectorstore:
oracle:
index-type: IVF
distance-type: COSINE
dimensions: 1536
:::提示
查看配置参数列表以了解默认值和配置选项。
:::
现在你可以在应用中自动装配OracleVectorStore并开始使用:
@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 Oracle Vector Store
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());
配置属性
你可以在Spring Boot配置中使用以下属性来自定义 OracleVectorStore。
| 属性 | 描述 | 默认值 |
|---|---|---|
spring.ai.vectorstore.oracle.index-type | 最近邻搜索索引类型。选项为 NONE - 精确最近邻搜索,IVF - 倒排扁平文件索引。与 HNSW 相比,它的构建时间更快,内存使用更少,但查询性能(在速度-召回权衡方面)较低。HNSW - 创建多层图。与 IVF 相比,它的构建时间更慢,内存使用更多,但查询性能(在速度-召回权衡方面)更好。 | NONE |
spring.ai.vectorstore.oracle.distance-type | 搜索距离类型,可选 COSINE (默认)、DOT、EUCLIDEAN、EUCLIDEAN_SQUARED 和 MANHATTAN。注意:如果向量已归一化,可以使用 DOT 或 COSINE 以获得最佳性能。 | COSINE |
spring.ai.vectorstore.oracle.forced-normalization | 允许在插入前和进行相似性搜索时启用向量归一化(如果为 true)。 警告:将此设置为 true 是允许使用搜索请求相似性阈值 的必要条件。 注意:如果向量已归一化,可以使用 DOT 或 COSINE 以获得最佳性能。 | false |
spring.ai.vectorstore.oracle.dimensions | 嵌入维度。如果未显式指定,OracleVectorStore 将允许最大值:65535。维度在创建表时设置到嵌入列上。如果更改维度,也必须重新创建表。 | 65535 |
spring.ai.vectorstore.oracle.remove-existing-vector-store-table | 在启动时删除现有表。 | false |
spring.ai.vectorstore.oracle.initialize-schema | 是否初始化所需模式。 | false |
spring.ai.vectorstore.oracle.search-accuracy | 表示存在索引时的请求精度目标。默认禁用。需要提供一个范围在 [1,100] 内的整数来覆盖默认的索引精度 (95)。使用较低的精度提供近似相似性搜索,在速度与精度之间进行权衡。 | -1 (DEFAULT_SEARCH_ACCURACY) |
元数据过滤
你可以利用通用的、可移植的元数据过滤器来配合 OracleVectorStore 使用。
例如,你可以使用文本表达式语言:
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());
这些筛选表达式会被转换为等效的 OracleVectorStore 筛选器。
手动配置
除了使用 Spring Boot 自动配置,你也可以手动配置 OracleVectorStore。为此,你需要将 Oracle JDBC 驱动和 JdbcTemplate 自动配置依赖添加到你的项目中:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-oracle-store</artifactId>
</dependency>
:::提示
请参考依赖管理章节,将 Spring AI BOM 添加到您的构建文件中。
:::
要在您的应用程序中配置 OracleVectorStore,可以采用以下设置:
@Bean
public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) {
return OracleVectorStore.builder(jdbcTemplate, embeddingModel)
.tableName("my_vectors")
.indexType(OracleVectorStoreIndexType.IVF)
.distanceType(OracleVectorStoreDistanceType.COSINE)
.dimensions(1536)
.searchAccuracy(95)
.initializeSchema(true)
.build();
}
本地运行 Oracle Database 23ai
docker run --rm --name oracle23ai -p 1521:1521 -e APP_USER=mlops -e APP_USER_PASSWORD=mlops -e ORACLE_PASSWORD=mlops gvenzl/oracle-free:23-slim
您随后可以通过以下方式连接到数据库:
sql mlops/mlops@localhost/freepdb1
访问原生客户端
Oracle Vector Store实现通过getNativeClient()方法提供对底层原生Oracle客户端(OracleConnection)的访问:
OracleVectorStore vectorStore = context.getBean(OracleVectorStore.class);
Optional<OracleConnection> nativeClient = vectorStore.getNativeClient();
if (nativeClient.isPresent()) {
OracleConnection connection = nativeClient.get();
// Use the native client for Oracle-specific operations
}
原生客户端使您能够访问Oracle特定的功能和操作,这些可能无法通过VectorStore接口直接使用。