Oracle Database 23ai - AI Vector Search
Oracle 数据库 23ai(23.4+)的 AI 向量搜索 功能现作为 Spring AI VectorStore 提供,助您存储文档嵌入并执行相似性搜索。当然,所有其他功能也同样可用。
自动配置
首先,将 Oracle Vector Store 引导启动器依赖项添加到您的项目中:
<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'
}
若需此向量存储为您初始化架构,则需在相应构造函数中将 initializeSchema 布尔参数设为 true,或通过在 application.properties
文件中设置 initialize-schema=true
来实现。
向量存储同样需要一个 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'
}
要连接并配置 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。
属性 (Property) | 描述 (Description) | 默认值 (Default value) |
---|---|---|
spring.ai.vectorstore.oracle.index-type |
最近邻搜索索引类型。选项为 NONE - 精确最近邻搜索,IVF - 倒排平面文件索引(构建时间更快且使用更少内存,但查询性能较低),HNSW - 创建多层图(构建时间更慢且使用更多内存,但查询性能更好)。 |
NONE |
spring.ai.vectorstore.oracle.distance-type |
搜索距离类型,可选 COSINE (默认,余弦)、DOT (点积)、EUCLIDEAN (欧几里得)、EUCLIDEAN_SQUARED (平方欧几里得) 和 MANHATTAN (曼哈顿)。注意:如果向量已归一化,可使用 DOT 或 COSINE 以获得最佳性能。 |
COSINE |
spring.ai.vectorstore.oracle.forced-normalization |
允许在插入和相似性搜索之前启用向量归一化(如果为 true)。警告:设置为 true 是支持搜索请求相似性阈值的要求。注意:如果向量已归一化,可使用 DOT 或 COSINE 以获得最佳性能。 |
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 (默认搜索精度) |
元数据过滤
您可以将通用的、可移植的 元数据过滤器 与 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 数据库 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
}
原生客户端让您能够访问可能未通过 VectorStore 接口公开的 Oracle 特定功能和操作。
最后编辑:Jeebiz 更新时间:2025-09-10 21:18