Weaviate

本節將引導您設定 Weaviate VectorStore 來儲存文件嵌入並執行相似性搜尋。

Weaviate 是一個開源向量資料庫,它允許您儲存資料物件和來自您喜愛的 ML 模型的向量嵌入,並無縫擴充套件到數十億個資料物件。它提供了儲存文件嵌入、內容和元資料以及透過這些嵌入進行搜尋的工具,包括元資料過濾。

先決條件

  • 執行中的 Weaviate 例項。以下選項可用

  • 如果需要,需要 EmbeddingModel 的 API 金鑰,用於生成由 WeaviateVectorStore 儲存的嵌入。

依賴關係

Spring AI 自動配置、啟動模組的工件名稱發生了重大變化。請參閱 升級說明 以獲取更多資訊。

將 Weaviate Vector Store 依賴項新增到您的專案

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-weaviate-store</artifactId>
</dependency>

或新增到您的 Gradle build.gradle 構建檔案中。

dependencies {
    implementation 'org.springframework.ai:spring-ai-weaviate-store'
}
請參閱 依賴項管理 部分,將 Spring AI BOM 新增到您的構建檔案中。

配置

要連線到 Weaviate 並使用 WeaviateVectorStore,您需要提供您的例項的訪問詳細資訊。配置可以透過 Spring Boot 的 application.properties 提供

spring.ai.vectorstore.weaviate.host=<host_of_your_weaviate_instance>
spring.ai.vectorstore.weaviate.scheme=<http_or_https>
spring.ai.vectorstore.weaviate.api-key=<your_api_key>
# API key if needed, e.g. OpenAI
spring.ai.openai.api-key=<api-key>

如果您更喜歡將環境變數用於 API 金鑰等敏感資訊,您有多種選擇

選項 1:使用 Spring Expression Language (SpEL)

您可以使用自定義環境變數名稱並在您的應用程式配置中引用它們

# In application.yml
spring:
  ai:
    vectorstore:
      weaviate:
        host: ${WEAVIATE_HOST}
        scheme: ${WEAVIATE_SCHEME}
        api-key: ${WEAVIATE_API_KEY}
    openai:
      api-key: ${OPENAI_API_KEY}
# In your environment or .env file
export WEAVIATE_HOST=<host_of_your_weaviate_instance>
export WEAVIATE_SCHEME=<http_or_https>
export WEAVIATE_API_KEY=<your_api_key>
export OPENAI_API_KEY=<api-key>

選項 2:以程式設計方式訪問環境變數

或者,您可以在 Java 程式碼中訪問環境變數

String weaviateApiKey = System.getenv("WEAVIATE_API_KEY");
String openAiApiKey = System.getenv("OPENAI_API_KEY");
如果您選擇建立 shell 指令碼來管理環境變數,請務必在啟動應用程式之前透過“source”檔案來執行它,即 source <您的指令碼名稱>.sh

自動配置

Spring AI 為 Weaviate Vector Store 提供了 Spring Boot 自動配置。要啟用它,請將以下依賴項新增到您的專案的 Maven pom.xml 檔案中

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-vector-store-weaviate</artifactId>
</dependency>

或新增到您的 Gradle build.gradle 構建檔案中。

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-vector-store-weaviate'
}
請參閱 依賴項管理 部分,將 Spring AI BOM 新增到您的構建檔案中。

請查看向量儲存的配置引數列表,以瞭解預設值和配置選項。

請參閱Artifact Repositories(工件倉庫)部分,將 Maven Central 和/或 Snapshot Repositories 新增到您的構建檔案中。

此外,您還需要一個配置的 `EmbeddingModel` bean。有關更多資訊,請參閱EmbeddingModel部分。

這是一個所需 bean 的示例

@Bean
public EmbeddingModel embeddingModel() {
    // Retrieve API key from a secure source or environment variable
    String apiKey = System.getenv("OPENAI_API_KEY");

    // Can be any other EmbeddingModel implementation
    return new OpenAiEmbeddingModel(OpenAiApi.builder().apiKey(apiKey).build());
}

現在您可以將 WeaviateVectorStore 自動裝配為應用程式中的向量儲存。

手動配置

除了使用 Spring Boot 自動配置,您還可以使用構建器模式手動配置 WeaviateVectorStore

@Bean
public WeaviateClient weaviateClient() {
    return new WeaviateClient(new Config("http", "localhost:8080"));
}

@Bean
public VectorStore vectorStore(WeaviateClient weaviateClient, EmbeddingModel embeddingModel) {
    return WeaviateVectorStore.builder(weaviateClient, embeddingModel)
        .options(options)                              // Optional: use custom options
        .consistencyLevel(ConsistentLevel.QUORUM)      // Optional: defaults to ConsistentLevel.ONE
        .filterMetadataFields(List.of(                 // Optional: fields that can be used in filters
            MetadataField.text("country"),
            MetadataField.number("year")))
        .build();
}

元資料過濾

您也可以在 Weaviate 儲存中使用通用的、可移植的元資料過濾器

例如,您可以使用文字表示式語言

vectorStore.similaritySearch(
    SearchRequest.builder()
        .query("The World")
        .topK(TOP_K)
        .similarityThreshold(SIMILARITY_THRESHOLD)
        .filterExpression("country in ['UK', 'NL'] && year >= 2020").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("country", "UK", "NL"),
        b.gte("year", 2020)).build()).build());
這些(可移植的)過濾表示式會自動轉換為專有的 Weaviate where filters

例如,這個可移植的過濾器表示式

country in ['UK', 'NL'] && year >= 2020

被轉換為專有的 Weaviate GraphQL 過濾器格式

operator: And
operands:
    [{
        operator: Or
        operands:
            [{
                path: ["meta_country"]
                operator: Equal
                valueText: "UK"
            },
            {
                path: ["meta_country"]
                operator: Equal
                valueText: "NL"
            }]
    },
    {
        path: ["meta_year"]
        operator: GreaterThanEqual
        valueNumber: 2020
    }]

在 Docker 中執行 Weaviate

要快速開始使用本地 Weaviate 例項,您可以在 Docker 中執行它

docker run -it --rm --name weaviate \
    -e AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED=true \
    -e PERSISTENCE_DATA_PATH=/var/lib/weaviate \
    -e QUERY_DEFAULTS_LIMIT=25 \
    -e DEFAULT_VECTORIZER_MODULE=none \
    -e CLUSTER_HOSTNAME=node1 \
    -p 8080:8080 \
    semitechnologies/weaviate:1.22.4

這將啟動一個可在 localhost:8080 訪問的 Weaviate 例項。

WeaviateVectorStore 屬性

您可以在 Spring Boot 配置中使用以下屬性來自定義 Weaviate 向量儲存。

財產 描述 預設值

spring.ai.vectorstore.weaviate.host

Weaviate 伺服器的主機

localhost:8080

spring.ai.vectorstore.weaviate.scheme

連線方案

http

spring.ai.vectorstore.weaviate.api-key

用於身份驗證的 API 金鑰

spring.ai.vectorstore.weaviate.object-class

用於儲存文件的類名。

SpringAiWeaviate

spring.ai.vectorstore.weaviate.content-field-name

內容的欄位名

content

spring.ai.vectorstore.weaviate.meta-field-prefix

元資料的欄位字首

meta_

spring.ai.vectorstore.weaviate.consistency-level

一致性和速度之間所需的權衡

ConsistentLevel.ONE

spring.ai.vectorstore.weaviate.filter-field

配置可在過濾器中使用的元資料欄位。格式:spring.ai.vectorstore.weaviate.filter-field.<欄位名>=<欄位型別>

物件類名應以大寫字母開頭,欄位名應以小寫字母開頭。參見 data-object-concepts

訪問原生客戶端

Weaviate Vector Store 實現透過 getNativeClient() 方法提供對底層原生 Weaviate 客戶端(WeaviateClient)的訪問

WeaviateVectorStore vectorStore = context.getBean(WeaviateVectorStore.class);
Optional<WeaviateClient> nativeClient = vectorStore.getNativeClient();

if (nativeClient.isPresent()) {
    WeaviateClient client = nativeClient.get();
    // Use the native client for Weaviate-specific operations
}

原生客戶端使您能夠訪問 VectorStore 介面可能未公開的 Weaviate 特定功能和操作。

© . This site is unofficial and not affiliated with VMware.