Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Gemfire VectorStore Auto-Configuration #599

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions spring-ai-spring-boot-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,13 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-gemfire-store</artifactId>
<version>${project.parent.version}</version>
<optional>true</optional>
</dependency>

<!-- test dependencies -->

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2024 - 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.gemfire;

import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;

/**
* @author Philipp Kessler
*/
public interface GemFireConnectionDetails extends ConnectionDetails {

String getHost();

int getPort();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2024 - 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.gemfire;

import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.GemFireVectorStore;
import org.springframework.ai.vectorstore.GemFireVectorStore.GemFireVectorStoreConfig;
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;

/**
* @author Philipp Kessler
*/
@AutoConfiguration
@ConditionalOnClass({ GemFireVectorStore.class, EmbeddingModel.class })
@EnableConfigurationProperties({ GemFireVectorStoreProperties.class })
public class GemFireVectorStoreAutoConfiguration {

@Bean
@ConditionalOnMissingBean(GemFireConnectionDetails.class)
public PropertiesGemFireConnectionDetails gemFireConnectionDetails(GemFireVectorStoreProperties properties) {
return new PropertiesGemFireConnectionDetails(properties);
}

@Bean
@ConditionalOnMissingBean
public GemFireVectorStore vectorStore(EmbeddingModel embeddingModel, GemFireConnectionDetails connectionDetails,
GemFireVectorStoreProperties properties) {
var vectoreStoreConfig = GemFireVectorStoreConfig.builder()
.withHost(connectionDetails.getHost())
.withPort(connectionDetails.getPort())
.withIndex(properties.getIndex())
.withDocumentField(properties.getDocumentField())
.withTopK(properties.getTopK())
.withTopKPerBucket(properties.getTopKPerBucket())
.withSslEnabled(properties.isSslEnabled())
.withConnectionTimeout(properties.getConnectionTimeout())
.withRequestTimeout(properties.getRequestTimeout())
.build();

var vectorStore = new GemFireVectorStore(vectoreStoreConfig, embeddingModel);

vectorStore.setIndexName(properties.getIndex());

return vectorStore;
}

private static class PropertiesGemFireConnectionDetails implements GemFireConnectionDetails {

private final GemFireVectorStoreProperties properties;

public PropertiesGemFireConnectionDetails(GemFireVectorStoreProperties properties) {
this.properties = properties;
}

@Override
public String getHost() {
return properties.getHost();
}

@Override
public int getPort() {
return properties.getPort();
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2024 - 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.gemfire;

import org.springframework.ai.vectorstore.GemFireVectorStore;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
* @author Philipp Kessler
*/
@ConfigurationProperties(GemFireVectorStoreProperties.CONFIG_PREFIX)
public class GemFireVectorStoreProperties {

public static final String CONFIG_PREFIX = "spring.ai.vectorstore.gemfire";

private String host;

private int port = GemFireVectorStore.DEFAULT_PORT;

private boolean sslEnabled;

private long connectionTimeout;

private long requestTimeout;

private String index;

private int topK = GemFireVectorStore.DEFAULT_TOP_K;

private int topKPerBucket = GemFireVectorStore.DEFAULT_TOP_K_PER_BUCKET;

private String documentField = GemFireVectorStore.DEFAULT_DOCUMENT_FIELD;

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public boolean isSslEnabled() {
return sslEnabled;
}

public void setSslEnabled(boolean sslEnabled) {
this.sslEnabled = sslEnabled;
}

public long getConnectionTimeout() {
return connectionTimeout;
}

public void setConnectionTimeout(long connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}

public long getRequestTimeout() {
return requestTimeout;
}

public void setRequestTimeout(long requestTimeout) {
this.requestTimeout = requestTimeout;
}

public String getIndex() {
return index;
}

public void setIndex(String index) {
this.index = index;
}

public int getTopKPerBucket() {
return topKPerBucket;
}

public void setTopKPerBucket(int topKPerBucket) {
this.topKPerBucket = topKPerBucket;
}

public int getTopK() {
return topK;
}

public void setTopK(int topK) {
this.topK = topK;
}

public String getDocumentField() {
return documentField;
}

public void setDocumentField(String documentField) {
this.documentField = documentField;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2024 - 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.gemfire;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.time.Duration;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.ai.ResourceUtils;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.transformers.TransformersEmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
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 org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Testcontainers;

/**
* @author Philipp Kessler
*/
@Testcontainers
class GemFireVectorStoreAutoConfigurationIT {

private static DockerComposeContainer gemFireContainer;

@BeforeAll
public static void beforeAll() {

gemFireContainer = new DockerComposeContainer(new File("src/test/resources/gemfire/docker-compose.yml"))
.withExposedService("gemfire", 7070,
Wait.forHttp("/gemfire-api/v1/ping")
.forStatusCode(200)
.withStartupTimeout(Duration.ofSeconds(100)));
gemFireContainer.start();
}

@AfterAll
public static void afterAll() {
gemFireContainer.stop();
}

List<Document> documents = List.of(
new Document(ResourceUtils.getText("classpath:/test/data/spring.ai.txt"), Map.of("spring", "great")),
new Document(ResourceUtils.getText("classpath:/test/data/time.shelter.txt")), new Document(
ResourceUtils.getText("classpath:/test/data/great.depression.txt"), Map.of("depression", "bad")));

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(GemFireVectorStoreAutoConfiguration.class))
.withUserConfiguration(Config.class)
.withPropertyValues("spring.ai.vectorstore.gemfire.index=test_index",
"spring.ai.vectorstore.gemfire.documentField=doc_chunk",
"spring.ai.vectorstore.gemfire.host=" + gemFireContainer.getServiceHost("gemfire", 7070),
"spring.ai.vectorstore.gemfire.port=" + gemFireContainer.getServicePort("gemfire", 7070));

@Test
void addAndSearch() {
contextRunner

.run(context -> {
VectorStore vectorStore = context.getBean(VectorStore.class);
vectorStore.add(documents);

List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(1));

assertThat(results).hasSize(1);
Document resultDoc = results.get(0);
assertThat(resultDoc.getId()).isEqualTo(documents.get(0).getId());
assertThat(resultDoc.getContent()).contains(
"Spring AI provides abstractions that serve as the foundation for developing AI applications.");

// Remove all documents from the store
vectorStore.delete(documents.stream().map(doc -> doc.getId()).toList());

results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(1));
assertThat(results).isEmpty();
});
}

@Configuration(proxyBeanMethods = false)
static class Config {

@Bean
public EmbeddingModel embeddingModel() {
return new TransformersEmbeddingModel();
}

}

}
Loading