此版本仍在开发中,尚未被视为稳定版。如需最新的快照版本,请使用 Spring AI 1.1.3spring-doc.cadn.net.cn

Oracle 数据库 23ai - AI 向量搜索

Oracle Database 23ai (23.4+) 的 AI 向量搜索 功能可作为 Spring AI VectorStore 使用,帮助您存储文档嵌入并执行相似性搜索。当然,所有其他功能也可用。spring-doc.cadn.net.cn

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

Auto-Configuration

Spring AI自动配置和starter模块的artifact名称有了重大变化。 请参阅升级说明获取更多信息。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 build.gradle 构建文件中。spring-doc.cadn.net.cn

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

如果您需要此向量存储为您初始化架构,那么您需要在相应的构造函数中为 initializeSchema 布尔参数传递 true,或者通过在 application.properties 文件中设置 …​initialize-schema=true 来实现。spring-doc.cadn.net.cn

这是一个破坏性变更!在早期版本的 Spring AI 中,此架构初始化是默认发生的。

Vector Store(向量存储)还需要一个 EmbeddingModel 实例来计算文档的嵌入(embeddings)。 你可以选择一个可用的 EmbeddingModel 实现spring-doc.cadn.net.cn

例如,要使用 OpenAI EmbeddingModel,请将以下依赖项添加到您的项目中:spring-doc.cadn.net.cn

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

请将以下内容添加到您的Gradle build.gradle 构建文件中。spring-doc.cadn.net.cn

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

要连接并配置 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
查看 配置参数 列表以了解默认值和配置选项。

现在您可以在您的应用程序中自动装配 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

<property> </property> <description> </description> 默认值

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

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

spring-doc.cadn.net.cn

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

COSINE(默认)、DOTEUCLIDEANEUCLIDEAN_SQUAREDMANHATTAN 之间搜索距离类型。spring-doc.cadn.net.cn

注意:如果向量已归一化,您可以使用 DOTCOSINE 以获得最佳性能。spring-doc.cadn.net.cn

COSINEspring-doc.cadn.net.cn

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

允许在插入之前和进行相似性搜索时启用向量归一化(如果为 true)。spring-doc.cadn.net.cn

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

注意:如果向量已归一化,您可以使用 DOTCOSINE 以获得最佳性能。spring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

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

嵌入维度。如果没有明确指定,OracleVectorStore 将允许最大值: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 自动配置,您可以手动配置 OracleVectorStore。 为此,您需要将 Oracle JDBC 驱动和 JdbcTemplate 自动配置依赖项添加到您的项目中: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添加到您的构建文件中。

要在您的应用程序中配置 OracleVectorStore,您可以使用以下设置: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();
}

在本地运行 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

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

sql mlops/mlops@localhost/freepdb1

访问原生客户端

Oracle Vector Store 实现通过 getNativeClient() 方法提供对底层原生 Oracle 客户端(OracleConnection)的访问: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