Migrate weaviate store auto-config to its own module

Signed-off-by: Soby Chacko <soby.chacko@broadcom.com>

Fixing typo, adding new autoconfig module to starter module

Signed-off-by: Soby Chacko <soby.chacko@broadcom.com>
This commit is contained in:
Soby Chacko
2025-02-19 18:35:12 -05:00
committed by Ilayaperumal Gopinathan
parent 0719f44016
commit 413ab9692d
13 changed files with 189 additions and 7 deletions

View File

@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2023-2025 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../../pom.xml</relativePath>
</parent>
<artifactId>spring-ai-weaviate-store-spring-boot-autoconfigure</artifactId>
<packaging>jar</packaging>
<name>Spring AI Auto Configuration for Weaviate vector store</name>
<description>Spring AI Auto Configuration for Weaviate vector store</description>
<url>https://github.com/spring-projects/spring-ai</url>
<scm>
<url>https://github.com/spring-projects/spring-ai</url>
<connection>git://github.com/spring-projects/spring-ai.git</connection>
<developerConnection>git@github.com:spring-projects/spring-ai.git</developerConnection>
</scm>
<dependencies>
<!-- production dependencies -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-weaviate-store</artifactId>
<version>${project.parent.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-test</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>weaviate</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-transformers</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ai.autoconfigure.vectorstore.weaviate;
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
public interface WeaviateConnectionDetails extends ConnectionDetails {
String getHost();
}

View File

@@ -0,0 +1,111 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ai.autoconfigure.vectorstore.weaviate;
import io.micrometer.observation.ObservationRegistry;
import io.weaviate.client.Config;
import io.weaviate.client.WeaviateAuthClient;
import io.weaviate.client.WeaviateClient;
import io.weaviate.client.v1.auth.exception.AuthException;
import org.springframework.ai.embedding.BatchingStrategy;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
import org.springframework.ai.vectorstore.observation.VectorStoreObservationConvention;
import org.springframework.ai.vectorstore.weaviate.WeaviateVectorStore;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
/**
* {@link AutoConfiguration Auto-configuration} for Weaviate Vector Store.
*
* @author Christian Tzolov
* @author Eddú Meléndez
* @author Soby Chacko
*/
@AutoConfiguration
@ConditionalOnClass({ EmbeddingModel.class, WeaviateVectorStore.class })
@EnableConfigurationProperties({ WeaviateVectorStoreProperties.class })
public class WeaviateVectorStoreAutoConfiguration {
@Bean
@ConditionalOnMissingBean(WeaviateConnectionDetails.class)
public PropertiesWeaviateConnectionDetails weaviateConnectionDetails(WeaviateVectorStoreProperties properties) {
return new PropertiesWeaviateConnectionDetails(properties);
}
@Bean
@ConditionalOnMissingBean
public WeaviateClient weaviateClient(WeaviateVectorStoreProperties properties,
WeaviateConnectionDetails connectionDetails) {
try {
return WeaviateAuthClient.apiKey(
new Config(properties.getScheme(), connectionDetails.getHost(), properties.getHeaders()),
properties.getApiKey());
}
catch (AuthException e) {
throw new IllegalArgumentException("WeaviateClient could not be created.", e);
}
}
@Bean
@ConditionalOnMissingBean(BatchingStrategy.class)
BatchingStrategy batchingStrategy() {
return new TokenCountBatchingStrategy();
}
@Bean
@ConditionalOnMissingBean
public WeaviateVectorStore vectorStore(EmbeddingModel embeddingModel, WeaviateClient weaviateClient,
WeaviateVectorStoreProperties properties, ObjectProvider<ObservationRegistry> observationRegistry,
ObjectProvider<VectorStoreObservationConvention> customObservationConvention,
BatchingStrategy batchingStrategy) {
return WeaviateVectorStore.builder(weaviateClient, embeddingModel)
.objectClass(properties.getObjectClass())
.filterMetadataFields(properties.getFilterField()
.entrySet()
.stream()
.map(e -> new WeaviateVectorStore.MetadataField(e.getKey(), e.getValue()))
.toList())
.consistencyLevel(WeaviateVectorStore.ConsistentLevel.valueOf(properties.getConsistencyLevel().name()))
.observationRegistry(observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP))
.customObservationConvention(customObservationConvention.getIfAvailable(() -> null))
.batchingStrategy(batchingStrategy)
.build();
}
static class PropertiesWeaviateConnectionDetails implements WeaviateConnectionDetails {
private final WeaviateVectorStoreProperties properties;
PropertiesWeaviateConnectionDetails(WeaviateVectorStoreProperties properties) {
this.properties = properties;
}
@Override
public String getHost() {
return this.properties.getHost();
}
}
}

View File

@@ -0,0 +1,109 @@
/*
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ai.autoconfigure.vectorstore.weaviate;
import java.util.Map;
import org.springframework.ai.vectorstore.weaviate.WeaviateVectorStore;
import org.springframework.ai.vectorstore.weaviate.WeaviateVectorStore.ConsistentLevel;
import org.springframework.ai.vectorstore.weaviate.WeaviateVectorStore.MetadataField;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Configuration properties for Weaviate Vector Store.
*
* @author Christian Tzolov
*/
@ConfigurationProperties(WeaviateVectorStoreProperties.CONFIG_PREFIX)
public class WeaviateVectorStoreProperties {
public static final String CONFIG_PREFIX = "spring.ai.vectorstore.weaviate";
private String scheme = "http";
private String host = "localhost:8080";
private String apiKey = "";
private String objectClass = "SpringAiWeaviate";
private ConsistentLevel consistencyLevel = WeaviateVectorStore.ConsistentLevel.ONE;
/**
* spring.ai.vectorstore.weaviate.filter-field.<field-name>=<field-type>
*/
private Map<String, MetadataField.Type> filterField = Map.of();
private Map<String, String> headers = Map.of();
public String getScheme() {
return this.scheme;
}
public void setScheme(String scheme) {
this.scheme = scheme;
}
public String getHost() {
return this.host;
}
public void setHost(String host) {
this.host = host;
}
public String getApiKey() {
return this.apiKey;
}
public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
public String getObjectClass() {
return this.objectClass;
}
public void setObjectClass(String indexName) {
this.objectClass = indexName;
}
public ConsistentLevel getConsistencyLevel() {
return this.consistencyLevel;
}
public void setConsistencyLevel(ConsistentLevel consistencyLevel) {
this.consistencyLevel = consistencyLevel;
}
public Map<String, String> getHeaders() {
return this.headers;
}
public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}
public Map<String, MetadataField.Type> getFilterField() {
return this.filterField;
}
public void setFilterField(Map<String, MetadataField.Type> filterMetadataFields) {
this.filterField = filterMetadataFields;
}
}

View File

@@ -0,0 +1,16 @@
#
# Copyright 2025-2025 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
org.springframework.ai.autoconfigure.vectorstore.weaviate.WeaviateVectorStoreAutoConfiguration

View File

@@ -0,0 +1,164 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ai.autoconfigure.vectorstore.weaviate;
import java.util.List;
import java.util.Map;
import io.micrometer.observation.tck.TestObservationRegistry;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.weaviate.WeaviateContainer;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.observation.conventions.VectorStoreProvider;
import org.springframework.ai.transformers.TransformersEmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.observation.VectorStoreObservationContext;
import org.springframework.ai.vectorstore.weaviate.WeaviateVectorStore.MetadataField;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.ai.test.vectorstore.ObservationTestUtil.assertObservationRegistry;
/**
* @author Christian Tzolov
* @author Eddú Meléndez
* @author Soby Chacko
* @author Thomas Vitale
*/
@Testcontainers
public class WeaviateVectorStoreAutoConfigurationIT {
@Container
static WeaviateContainer weaviate = new WeaviateContainer("semitechnologies/weaviate:1.25.4")
.waitingFor(Wait.forHttp("/v1/.well-known/ready").forPort(8080));
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(WeaviateVectorStoreAutoConfiguration.class))
.withUserConfiguration(Config.class)
.withPropertyValues("spring.ai.vectorstore.weaviate.scheme=http",
"spring.ai.vectorstore.weaviate.host=" + weaviate.getHttpHostAddress(),
"spring.ai.vectorstore.weaviate.filter-field.country=TEXT",
"spring.ai.vectorstore.weaviate.filter-field.year=NUMBER",
"spring.ai.vectorstore.weaviate.filter-field.active=BOOLEAN",
"spring.ai.vectorstore.weaviate.filter-field.price=NUMBER");
@Test
public void addAndSearchWithFilters() {
this.contextRunner.run(context -> {
WeaviateVectorStoreProperties properties = context.getBean(WeaviateVectorStoreProperties.class);
assertThat(properties.getFilterField()).hasSize(4);
assertThat(properties.getFilterField().get("country")).isEqualTo(MetadataField.Type.TEXT);
assertThat(properties.getFilterField().get("year")).isEqualTo(MetadataField.Type.NUMBER);
assertThat(properties.getFilterField().get("active")).isEqualTo(MetadataField.Type.BOOLEAN);
assertThat(properties.getFilterField().get("price")).isEqualTo(MetadataField.Type.NUMBER);
VectorStore vectorStore = context.getBean(VectorStore.class);
TestObservationRegistry observationRegistry = context.getBean(TestObservationRegistry.class);
var bgDocument = new Document("The World is Big and Salvation Lurks Around the Corner",
Map.of("country", "Bulgaria", "price", 3.14, "active", true, "year", 2020));
var nlDocument = new Document("The World is Big and Salvation Lurks Around the Corner",
Map.of("country", "Netherlands", "price", 1.57, "active", false, "year", 2023));
vectorStore.add(List.of(bgDocument, nlDocument));
assertObservationRegistry(observationRegistry, VectorStoreProvider.WEAVIATE,
VectorStoreObservationContext.Operation.ADD);
observationRegistry.clear();
var request = SearchRequest.builder().query("The World").topK(5).build();
List<Document> results = vectorStore.similaritySearch(request);
assertThat(results).hasSize(2);
results = vectorStore.similaritySearch(SearchRequest.from(request)
.similarityThresholdAll()
.filterExpression("country == 'Bulgaria'")
.build());
assertThat(results).hasSize(1);
assertThat(results.get(0).getId()).isEqualTo(bgDocument.getId());
assertObservationRegistry(observationRegistry, VectorStoreProvider.WEAVIATE,
VectorStoreObservationContext.Operation.QUERY);
results = vectorStore.similaritySearch(SearchRequest.from(request)
.similarityThresholdAll()
.filterExpression("country == 'Netherlands'")
.build());
assertThat(results).hasSize(1);
assertThat(results.get(0).getId()).isEqualTo(nlDocument.getId());
results = vectorStore.similaritySearch(SearchRequest.from(request)
.similarityThresholdAll()
.filterExpression("price > 1.57 && active == true")
.build());
assertThat(results).hasSize(1);
assertThat(results.get(0).getId()).isEqualTo(bgDocument.getId());
results = vectorStore.similaritySearch(SearchRequest.from(request)
.similarityThresholdAll()
.filterExpression("year in [2020, 2023]")
.build());
assertThat(results).hasSize(2);
results = vectorStore.similaritySearch(SearchRequest.from(request)
.similarityThresholdAll()
.filterExpression("year > 2020 && year <= 2023")
.build());
assertThat(results).hasSize(1);
assertThat(results.get(0).getId()).isEqualTo(nlDocument.getId());
observationRegistry.clear();
// Remove all documents from the store
vectorStore.delete(List.of(bgDocument, nlDocument).stream().map(doc -> doc.getId()).toList());
assertObservationRegistry(observationRegistry, VectorStoreProvider.WEAVIATE,
VectorStoreObservationContext.Operation.DELETE);
});
}
@Configuration(proxyBeanMethods = false)
static class Config {
@Bean
public TestObservationRegistry observationRegistry() {
return TestObservationRegistry.create();
}
@Bean
public EmbeddingModel embeddingModel() {
return new TransformersEmbeddingModel();
}
}
}