Weaviate
本節將引導您設定 Weaviate VectorStore,以儲存文件嵌入並執行相似性搜尋。
什麼是 Weaviate?
Weaviate 是一個開源向量資料庫。它允許您儲存來自您最愛的 ML 模型的資料物件和向量嵌入,並無縫擴展到數十億個資料物件。它提供工具來儲存文件嵌入、內容和元數據,並搜尋這些嵌入,包括元數據過濾。
先決條件
-
EmbeddingModel
實例以計算文件嵌入。有多種選項可供選擇 -
Weaviate 叢集
。您可以在 Docker 容器中在本機設定叢集,或建立 Weaviate Cloud Service。對於後者,您需要建立一個 Weaviate 帳戶、設定叢集,並從 儀表板詳細資訊 取得您的存取 API 金鑰。
在啟動時,如果尚未佈建,WeaviateVectorStore
會建立所需的 SpringAiWeaviate
物件結構描述。
自動配置
然後將 WeaviateVectorStore 啟動器依賴項新增至您的專案
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-weaviate-store-spring-boot-starter</artifactId>
</dependency>
或新增至您的 Gradle build.gradle
組建檔案。
dependencies {
implementation 'org.springframework.ai:spring-ai-weaviate-store-spring-boot-starter'
}
向量儲存實作可以為您初始化必要的結構描述,但您必須選擇加入,方法是在適當的建構函式中指定 initializeSchema
布林值,或在 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'
}
若要連線到 Weaviate 並使用 WeaviateVectorStore
,您需要提供您實例的存取詳細資訊。可以透過 Spring Boot 的 application.properties 提供簡單的配置,
spring.ai.vectorstore.weaviate.host=<host of your Weaviate instance>
spring.ai.vectorstore.weaviate.api-key=<your api key>
spring.ai.vectorstore.weaviate.scheme=http
# API key if needed, e.g. OpenAI
spring.ai.openai.api.key=<api-key>
請查看 配置參數 清單,以了解預設值和配置選項。 |
現在您可以在您的應用程式中自動注入 Weaviate Vector Store 並使用它
@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
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));
配置屬性
您可以使用 Spring Boot 配置中的下列屬性來自訂 Weaviate 向量儲存。
屬性 | 描述 | 預設值 |
---|---|---|
|
Weaviate 伺服器的主機。 |
localhost:8080 |
|
連線結構描述。 |
http |
|
用於向 Weaviate 伺服器進行身份驗證的 API 金鑰。 |
- |
|
"SpringAiWeaviate" |
|
|
一致性和速度之間所需的權衡 |
ConsistentLevel.ONE |
|
spring.ai.vectorstore.weaviate.filter-field.<field-name>=<field-type> |
- |
|
- |
|
|
是否初始化所需的結構描述 |
|
元數據過濾
您也可以將通用、可移植的 元數據過濾器 與 WeaviateVectorStore 一起使用。
例如,您可以使用文字表達式語言
vectorStore.similaritySearch(
SearchRequest
.query("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression("country in ['UK', 'NL'] && year >= 2020"));
或以程式設計方式使用表達式 DSL
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(
SearchRequest
.query("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression(b.and(
b.in("country", "UK", "NL"),
b.gte("year", 2020)).build()));
可移植的過濾器表達式會自動轉換為專有的 Weaviate where 過濾器。例如,下列可移植的過濾器表達式
country in ['UK', 'NL'] && year >= 2020
會轉換為 Weaviate GraphQL where 過濾器表達式
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
}]
手動配置
除了使用 Spring Boot 自動配置之外,您還可以手動配置 WeaviateVectorStore
。為此,您需要將 spring-ai-weaviate-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'
}
若要在您的應用程式中配置 Weaviate,您可以建立 WeaviateClient
@Bean
public WeaviateClient weaviateClient() {
try {
return WeaviateAuthClient.apiKey(
new Config(<YOUR SCHEME>, <YOUR HOST>, <YOUR HEADERS>),
<YOUR API KEY>);
}
catch (AuthException e) {
throw new IllegalArgumentException("WeaviateClient could not be created.", e);
}
}
透過將 Spring Boot OpenAI 啟動器新增至您的專案,與 OpenAI 的嵌入整合。這為您提供了 Embeddings 用戶端的實作
@Bean
public WeaviateVectorStore vectorStore(EmbeddingModel embeddingModel, WeaviateClient weaviateClient) {
WeaviateVectorStoreConfig.Builder configBuilder = WeaviateVectorStore.WeaviateVectorStoreConfig.builder()
.withObjectClass(<YOUR OBJECT CLASS>)
.withConsistencyLevel(<YOUR CONSISTENCY LEVEL>);
return new WeaviateVectorStore(configBuilder.build(), embeddingModel, weaviateClient);
}
在 Docker 容器中執行 Weaviate 叢集
在 Docker 容器中啟動 Weaviate
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/v1 啟動 Weaviate 叢集,結構描述為 http,主機為 localhost:8080,API 金鑰為 ""。然後按照使用說明操作。