Oracle Database 23ai - AI Vector Search
The AI Vector Search capabilities of the Oracle Database 23ai (23.4+) are available as a Spring AI VectorStore
to help you to store document embeddings and perform similarity searches. Of course, all other features are also available.
The Run Oracle Database 23ai locally appendix shows how to start a database with a lightweight Docker container. |
Auto-Configuration
Start by adding the Oracle Vector Store boot starter dependency to your project:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-oracle-store-spring-boot-starter</artifactId>
</dependency>
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-oracle-store-spring-boot-starter'
}
If you need this vector store to initialize the schema for you then you’ll need to pass true for the initializeSchema
boolean parameter in the appropriate constructor or by setting …initialize-schema=true
in the application.properties
file.
this is a breaking change! In earlier versions of Spring AI, this schema initialization happened by default. |
The Vector Store, also requires an EmbeddingModel
instance to calculate embeddings for the documents.
You can pick one of the available EmbeddingModel Implementations.
For example to use the OpenAI EmbeddingModel add the following dependency to your project:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file. Refer to the Repositories section to add Milestone and/or Snapshot Repositories to your build file. |
To connect to and configure the OracleVectorStore
, you need to provide access details for your database.
A simple configuration can either be provided via Spring Boot’s 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
Check the list of configuration parameters to learn about the default values and configuration options. |
Now you can Auto-wire the OracleVectorStore
in your application and use it:
@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.query("Spring").withTopK(5));
Configuration properties
You can use the following properties in your Spring Boot configuration to customize the OracleVectorStore
.
Property | Description | Default value |
---|---|---|
|
Nearest neighbor search index type. Options are |
NONE |
|
Search distance type among NOTE: If vectors are normalized, you can use |
COSINE |
|
Allows enabling vector normalization (if true) before insertion and for similarity search. CAUTION: Setting this to true is a requirement to allow for search request similarity threshold. NOTE: If vectors are normalized, you can use |
false |
|
Embeddings dimension. If not specified explicitly the OracleVectorStore will allow the maximum: 65535. Dimensions are set to the embedding column on table creation. If you change the dimensions your would have to re-create the table as well. |
65535 |
|
Drops the existing table on start up. |
false |
|
Whether to initialize the required schema. |
false |
|
Denote the requested accuracy target in the presence of index. Disabled by default. You need to provide an integer in the range [1,100] to override the default index accuracy (95). Using lower accuracy provides approximate similarity search trading off speed versus accuracy. |
-1 ( |
Metadata filtering
You can leverage the generic, portable metadata filters with the OracleVectorStore
.
For example, you can use either the text expression language:
vectorStore.similaritySearch(
SearchRequest.defaults()
.withQuery("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression("author in ['john', 'jill'] && article_type == 'blog'"));
or programmatically using the Filter.Expression
DSL:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.defaults()
.withQuery("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression(b.and(
b.in("author","john", "jill"),
b.eq("article_type", "blog")).build()));
These filter expressions are converted into the equivalent OracleVectorStore filters.
|
Manual Configuration
Instead of using the Spring Boot auto-configuration, you can manually configure the OracleVectorStore
.
For this you need to add the Oracle JDBC driver and JdbcTemplate
auto-configuration dependencies to your project:
<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>
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
To configure the OracleVectorStore
in your application, you can use the following setup:
@Bean
public VectorStore vectorStore(JdbcTemplate jdbcTemplate, EmbeddingModel embeddingModel) {
return new OracleVectorStore(jdbcTemplate, embeddingModel, true);
}