Oracle 数据库 23AI 版本 - 人工智能向量搜索

The Spring AI框架功能,可以在Oracle数据库23ai(23.4+版本)中使用,帮助您存储文档嵌入体并执行相似性搜索。当然,其他功能也是完全可用的。spring-doc.cadn.net.cn

自动配置

There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the 升级说明以获取更多信息。spring-doc.cadn.net.cn

首先将 Oracle 向量存储Starters依赖项添加到您的项目中:spring-doc.cadn.net.cn

<dependency>
	<groupId>org.springframework.ai</groupId>
	<artifactId>spring-ai-starter-vector-store-oracle</artifactId>
</dependency>

或者添加到您的Gradle 构建脚本文件中。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-vector-store-oracle'
}

如果需要这个向量存储来初始化 yourschema,则你需要在适当的构造函数中传递true作为initializeSchema布尔参数,或者在application.properties文件中设置…​initialize-schema=truespring-doc.cadn.net.cn

这是一个破坏性变更!在早期版本的 Spring AI 中,此模式初始化是默认进行的。

向量存储库也需要一个EmbeddingModel实例来为文档计算嵌入。 您可以选择可用的嵌入模型实现之一。spring-doc.cadn.net.cn

例如,使用OpenAI 插入模型,在项目中添加以下依赖项:spring-doc.cadn.net.cn

<dependency>
	<groupId>org.springframework.ai</groupId>
	<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>

或者添加到您的Gradle 构建脚本文件中。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-model-openai'
}
参考依赖管理部分,将Spring AI BOM添加到您的构建文件中。 参考制品仓库部分,将Maven中央仓库和/或快照仓库添加到您的构建文件中。

要连接到并配置 OracleVectorStore,你需要提供访问数据库的权限信息。 一个简单的配置可以通过 Spring Boot 的 application.yml 提供spring-doc.cadn.net.cn

spring:
  datasource:
    url: jdbc:oracle:thin:@//localhost:1521/freepdb1
    username: mlops
    password: mlops
  ai:
	vectorstore:
	  oracle:
		index-type: IVF
		distance-type: COSINE
		dimensions: 1536
查看配置参数列表以了解默认值和配置选项。

现在,你可以使用Auto-wire OracleVectorStore并在应用中使用它:spring-doc.cadn.net.cn

@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配置中进行自定义设置:OracleVectorStorespring-doc.cadn.net.cn

属性 描述 默认值

spring.ai.vectorstore.oracle.index-typespring-doc.cadn.net.cn

最近邻搜索索引类型。选项是: NONE - 精确最近邻搜索, IVF - 倒排文件索引。该方法的构建速度更快,占用内存更少,但查询性能(速度-召回率权衡)较低。 HNSW - 创建多层图。该方法构建速度较慢,占用内存更多,但查询性能(速度-召回率权衡)较好。spring-doc.cadn.net.cn

NONEspring-doc.cadn.net.cn

spring.ai.vectorstore.oracle.distance-typespring-doc.cadn.net.cn

搜索距离类型之一(默认值为0),可以选择0、1、2、3或4。spring-doc.cadn.net.cn

NOTE: 如果向量进行了归一化处理,您可以在最佳性能时使用0或1。spring-doc.cadn.net.cn

COSINEspring-doc.cadn.net.cn

spring.ai.vectorstore.oracle.forced-normalizationspring-doc.cadn.net.cn

允许在插入之前和相似性搜索中启用向量归一化。spring-doc.cadn.net.cn

注意:将此设置为true是允许进行搜索请求相似度阈值的必要条件。spring-doc.cadn.net.cn

NOTE: 如果向量进行了归一化处理,您可以在最佳性能时使用0或1。spring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

spring.ai.vectorstore.oracle.dimensionsspring-doc.cadn.net.cn

嵌入维度。若未明确指定,奥拉克尔向量存储将允许的最大值为65535。嵌入维度在表创建时设置。如果您更改了维度,您还需要重新创建表。spring-doc.cadn.net.cn

65535spring-doc.cadn.net.cn

spring.ai.vectorstore.oracle.remove-existing-vector-store-tablespring-doc.cadn.net.cn

在启动时删除现有的表。spring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

spring.ai.vectorstore.oracle.initialize-schemaspring-doc.cadn.net.cn

是否需要初始化所需的表结构?spring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

spring.ai.vectorstore.oracle.search-accuracyspring-doc.cadn.net.cn

在索引存在时表示请求的精度目标,默认情况下是禁用的。你需要提供一个整数,其值在1到100之间,以覆盖默认索引精度(95)。较低的精度提供了权衡速度和精度的近似相似性搜索。spring-doc.cadn.net.cn

-1 (DEFAULT_SEARCH_ACCURACY)spring-doc.cadn.net.cn

元数据过滤

你可以利用通用、易用的元数据过滤器 元数据过滤器,与 OracleVectorStore 一起使用。spring-doc.cadn.net.cn

例如,你可以使用文本表达式语言:spring-doc.cadn.net.cn

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:spring-doc.cadn.net.cn

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的自动配置,你可以手动配置代码中的数字0。 为此,你需要添加Oracle JDBC驱动程序和代码中的数字1自动配置依赖项到你的项目中:spring-doc.cadn.net.cn

<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到你的构建文件中。

To configure the OracleVectorStore in your application, you can use the following setup:spring-doc.cadn.net.cn

@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();
}

Type C0运行 Oracle 数据库 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

你然后就可以使用以下方式连接到数据库:spring-doc.cadn.net.cn

sql mlops/mlops@localhost/freepdb1

访问原生客户端

访问底层的 native Oracle 客户端(0),通过(1)方法:spring-doc.cadn.net.cn

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 接口暴露。spring-doc.cadn.net.cn