Oracle Database 23ai - AI向量搜索

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

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

自动配置

Spring AI 自动配置、starter 模块的 artifact 名称发生了重大变化。详情请参阅升级说明

首先向你的项目添加 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 初始化是默认进行的。

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'
}
请参阅依赖管理部分将 Spring AI BOM 添加到你的构建文件。请参阅仓库部分添加 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(默认)、DOTEUCLIDEANEUCLIDEAN_SQUAREDMANHATTAN

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

COSINE

spring.ai.vectorstore.oracle.forced-normalization

允许在插入前和进行相似度搜索时启用向量归一化(如果为 true)。

注意:将其设置为 true 是允许搜索请求相似度阈值的必要条件。

注意:如果向量已归一化,可以使用 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 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 接口公开。