跳到主要内容
版本:2.0.1

Oracle Database 23ai - AI 向量搜索

QWen 3.8 中英对照 Oracle Oracle Database 23ai - AI Vector Search

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'
}

如果需要该向量存储(vector store)为您初始化模式(schema),则需要将 initializeSchema 布尔参数设置为 true,即在相应的构造函数中传入,或者在 application.properties 文件中设置 …​initialize-schema=true。

备注

这是一个重大变更!在 Spring AI 的早期版本中,此 schema 初始化操作默认会自动执行。

Vector Store 同样需要一个 EmbeddingModel 实例来计算文档的嵌入。你可以从可用的 EmbeddingModel 实现 中选择一个。

例如,若要使用 OpenAI EmbeddingModel,请将以下依赖项添加到您的项目中:

<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'
}
提示

请参阅 Dependency Management 部分,以将 Spring AI BOM 添加到你的构建文件中。请参阅 Artifact Repositories 部分,以将 Maven Central 和/或 Snapshot 仓库添加到你的构建文件中。

要连接并配置 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 之间的整数 [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 向量存储实现通过 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 接口暴露。