Oracle Database 23ai - AI向量搜尋

Oracle Database 23ai (23.4+) 的 AI向量搜尋 功能作為 Spring AI 的 VectorStore 提供,用於儲存文件嵌入並執行相似度搜索。當然,所有其他功能也可用。

附錄在本地執行 Oracle Database 23ai 展示瞭如何使用輕量級 Docker 容器啟動資料庫。

自動配置

Spring AI 自動配置、starter 模組的 artifact 名稱發生了重大變化。詳情請參閱升級說明

首先向你的專案新增 Oracle Vector Store boot starter 依賴。

<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'
}

如果你需要此向量儲存為你初始化 schema,你需要在相應的建構函式中為 initializeSchema 布林引數傳入 true,或在 application.properties 檔案中設定 ...initialize-schema=true

這是一項重大變更!在早期版本的 Spring AI 中,schema 初始化是預設進行的。

Vector Store 還需要一個 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'
}
請參閱依賴管理部分將 Spring AI BOM 新增到你的構建檔案。請參閱倉庫部分新增 Maven Central 和/或 Snapshot 倉庫到你的構建檔案。

要連線和配置 OracleVectorStore,你需要提供資料庫的訪問詳細資訊。簡單的配置可以透過 Spring Boot 的 application.yml 提供:

spring:
  datasource:
    url: jdbc:oracle:thin:@//: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

屬性 描述 預設值

spring.ai.vectorstore.oracle.index-type

最近鄰搜尋索引型別。選項包括 NONE - 精確最近鄰搜尋,IVF - 倒排檔案索引。它的構建時間比 HNSW 快,使用的記憶體更少,但在查詢效能(速度-召回率權衡方面)較低。HNSW - 建立多層圖。它的構建時間比 IVF 慢,使用的記憶體更多,但在查詢效能(速度-召回率權衡方面)更好。

NONE

spring.ai.vectorstore.oracle.distance-type

搜尋距離型別,包括 COSINE(預設)、DOTEUCLIDEANEUCLIDEAN_SQUAREDMANHATTAN

注意:如果向量已歸一化,可以使用 DOTCOSINE 以獲得最佳效能。

COSINE

spring.ai.vectorstore.oracle.forced-normalization

允許在插入前和進行相似度搜索時啟用向量歸一化(如果為 true)。

注意:將其設定為 true 是允許搜尋請求相似度閾值的必要條件。

注意:如果向量已歸一化,可以使用 DOTCOSINE 以獲得最佳效能。

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 (DEFAULT_SEARCH_ACCURACY)

元資料過濾

你可以將通用、可移植的元資料過濾器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 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

然後你可以使用以下方式連線到資料庫:

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
}

原生客戶端允許你訪問 Oracle 特有的功能和操作,這些功能和操作可能不會透過 VectorStore 介面公開。