此版本仍在开发中,尚不被认为是稳定的。对于最新的快照版本,请使用 Spring AI 1.0.1spring-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 容器启动数据库。

自动配置

Spring AI 自动配置、入门模块的工件名称发生了重大变化。 有关更多信息,请参阅升级说明spring-doc.cadn.net.cn

首先,将 Oracle Vector Store 引导启动依赖项添加到项目:spring-doc.cadn.net.cn

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

或 Gradlebuild.gradle构建文件。spring-doc.cadn.net.cn

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

如果您需要此向量存储来为您初始化架构,则需要为initializeSchemaboolean 参数,或通过设置…​initialize-schema=trueapplication.properties文件。spring-doc.cadn.net.cn

这是一个重大变化!在早期版本的 Spring AI 中,默认情况下会发生此模式初始化。

矢量存储还需要一个EmbeddingModel实例来计算文档的嵌入。 您可以选择一个可用的 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>

或 Gradlebuild.gradle构建文件。spring-doc.cadn.net.cn

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

要连接到并配置OracleVectorStore,则需要提供数据库的访问详细信息。 可以通过 Spring Boot 的application.ymlspring-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 配置中使用以下属性来自定义OracleVectorStore.spring-doc.cadn.net.cn

属性 描述 默认值

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(默认),DOT,EUCLIDEAN,EUCLIDEAN_SQUAREDMANHATTAN.spring-doc.cadn.net.cn

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

余弦spring-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.ExpressionDSL: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过滤器。

手动配置

您可以手动配置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 实现提供对底层本机 Oracle 客户端 (OracleConnection) 通过getNativeClient()方法: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