Oracle データベース 23ai - AI ベクトル検索
Oracle データベース 23ai (23.4+) の AI ベクトル検索 [Oracle] (英語) 機能は、Spring AI VectorStore
として利用でき、ドキュメントの埋め込みを保存し、類似性検索を実行できます。もちろん、他のすべての機能も利用できます。
Run Oracle Database 23ai locally 付録では、軽量の Docker コンテナーを使用してデータベースを起動する方法を示します。 |
自動構成
まず、Oracle ベクトルストア Boot スターター依存関係をプロジェクトに追加します。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-oracle-store-spring-boot-starter</artifactId>
</dependency>
または、Gradle build.gradle
ビルドファイルに保存します。
dependencies {
implementation 'org.springframework.ai:spring-ai-oracle-store-spring-boot-starter'
}
このベクトルストアを使用してスキーマを初期化する必要がある場合は、適切なコンストラクターで initializeSchema
ブールパラメーターに true を渡すか、application.properties
ファイルで …initialize-schema=true
を設定する必要があります。
これは重大な変更です。Spring AI の以前のバージョンでは、このスキーマの初期化はデフォルトで行われていました。 |
ベクトルストア では、ドキュメントの埋め込みを計算するために EmbeddingModel
インスタンスも必要です。利用可能な EmbeddingModel の実装のいずれかを選択できます。
たとえば、OpenAI EmbeddingModel を使用するには、次の依存関係をプロジェクトに追加します。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
または、Gradle build.gradle
ビルドファイルに保存します。
dependencies {
implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
}
Spring AI BOM をビルドファイルに追加するには、"依存関係管理" セクションを参照してください。マイルストーンやスナップショットリポジトリをビルドファイルに追加するには、リポジトリセクションを参照してください。 |
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(List.of(document));
// Retrieve documents similar to a query
List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));
構成プロパティ
Spring Boot 構成で次のプロパティを使用して、OracleVectorStore
をカスタマイズできます。
プロパティ | 説明 | デフォルト値 |
---|---|---|
| 最近傍検索インデックス型。オプションは、 | 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 ( |
メタデータのフィルタリング
You can leverage the generic, portable metadata filters with the OracleVectorStore
.
例: 次のいずれかのテキスト式言語を使用できます。
vectorStore.similaritySearch(
SearchRequest.defaults()
.withQuery("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression("author in ['john', 'jill'] && article_type == 'blog'"));
または、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. |
手動構成
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>
Spring AI BOM をビルドファイルに追加するには、"依存関係管理" セクションを参照してください。 |
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);
}