Oracle Database 23ai - AI 向量搜尋

Oracle Database 23ai (23.4+) 的 AI 向量搜尋 功能以 Spring AI VectorStore 的形式提供,以協助您儲存文件嵌入向量並執行相似性搜尋。當然,所有其他功能也皆可用。

在本機執行 Oracle Database 23ai 附錄說明如何使用輕量型 Docker 容器啟動資料庫。

自動設定

首先將 Oracle Vector Store 啟動器相依性新增至您的專案

<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 中,此綱要初始化預設會發生。

Vector Store 也需要 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 新增至您的建置檔案。請參閱儲存庫章節,將 Milestone 和/或 Snapshot 儲存庫新增至您的建置檔案。

若要連線並設定 OracleVectorStore,您需要提供資料庫的存取詳細資訊。可以透過 Spring Boot 的 application.yml 提供簡單的組態

spring:
  datasource:
    url: jdbc:oracle:thin:@//127.0.0.1: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.query("Spring").withTopK(5));

組態屬性

您可以在 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

是否初始化必要的綱要。

false

spring.ai.vectorstore.oracle.search-accuracy

表示在存在索引的情況下要求的精確度目標。預設為停用。您需要提供範圍 [1,100] 內的整數,以覆寫預設索引精確度 (95)。使用較低的精確度可提供近似相似性搜尋,從而在速度和精確度之間進行權衡。

-1 (DEFAULT_SEARCH_ACCURACY)

中繼資料篩選

您可以搭配 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()));
這些篩選表示式會轉換為等效的 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 new OracleVectorStore(jdbcTemplate, embeddingModel, true);
}

在本機執行 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