Oracle Database 23ai - AI 向量搜索

本站(springdoc.cn)中的内容来源于 spring.io ,原始版权归属于 spring.io。由 springdoc.cn 进行翻译,整理。可供个人学习、研究,未经许可,不得进行任何转载、商用或与之相关的行为。 商标声明:Spring 是 Pivotal Software, Inc. 在美国以及其他国家的商标。

Oracle Database 23ai(23.4+ 版本)的 AI 向量搜索 功能现已作为 Spring AI VectorStore 提供,可用于存储文档嵌入并执行相似性搜索。当然,所有其他功能也同时可用。

本地运行 Oracle Database 23ai》附录展示了如何通过轻量级 Docker 容器启动数据库。

自动配置

Spring AI 的自动配置及 Starter 模块的构件名称发生重大变更。详情请查阅 升级说明

首先在项目中添加 Oracle Vector Store 的 boot starter 依赖:

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

若需要该向量存储自动初始化 Schema,需在构造函数中为 initializeSchema 布尔参数传递 true,或在 application.properties 文件中设置 …​initialize-schema=true

注意:此为破坏性变更!旧版 Spring AI 中该 Schema 初始化默认为自动执行。

向量存储还需一个 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'
}
参考 依赖管理 部分将 Spring AI BOM 添加至构建文件,并参考 “Artifact 仓库” 部分添加 Maven Central 和/或 Snapshot Repositories 至构建配置。

要连接并配置 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(默认)、DOTEUCLIDEANEUCLIDEAN_SQUAREDMANHATTAN

注意:若向量已归一化,使用 DOTCOSINE 可获得最佳性能。

COSINE

spring.ai.vectorstore.oracle.forced-normalization

允许在插入和相似性搜索前启用向量归一化(若设为 true)。

CAUTION: 将此设为 true 是支持搜索 请求相似度阈值 的必要条件。

NOTE: 若向量已归一化,使用 DOTCOSINE 可获得最佳性能。

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

是否初始化所需 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 向量存储实现通过 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 接口暴露。