Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ The following service connection factories are provided in the `spring-ai-spring
[cols="|,|"]
|====
| Connection Details | Matched on
| `AwsOpenSearchConnectionDetails`
| Containers named `localstack/localstack`

| `ChromaConnectionDetails`
| Containers named `chromadb/chroma`, `ghcr.io/chroma-core/chroma`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ The following service connection factories are provided in the `spring-ai-spring
[cols="|,|"]
|====
| Connection Details | Matched on

| `AwsOpenSearchConnectionDetails`
| Containers of type `LocalStackContainer`

| `ChromaConnectionDetails`
| Containers of type `ChromaDBContainer`

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.opensearch;

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

public interface AwsOpenSearchConnectionDetails extends ConnectionDetails {

String getRegion();

String getAccessKey();

String getSecretKey();

String getHost(String domainName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import org.opensearch.client.transport.aws.AwsSdk2Transport;
import org.opensearch.client.transport.aws.AwsSdk2TransportOptions;
import org.opensearch.client.transport.httpclient5.ApacheHttpClient5TransportBuilder;
import org.springframework.util.StringUtils;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.apache.ApacheHttpClient;
Expand Down Expand Up @@ -123,28 +125,35 @@ private HttpHost createHttpHost(String s) {
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Region.class, ApacheHttpClient.class })
@ConditionalOnClass({ AwsCredentialsProvider.class, Region.class, ApacheHttpClient.class })
static class AwsOpenSearchConfiguration {

@Bean
@ConditionalOnMissingBean(AwsOpenSearchConnectionDetails.class)
PropertiesAwsOpenSearchConnectionDetails awsOpenSearchConnectionDetails(
OpenSearchVectorStoreProperties properties) {
return new PropertiesAwsOpenSearchConnectionDetails(properties);
}

@Bean
@ConditionalOnMissingBean
OpenSearchClient openSearchClient(OpenSearchVectorStoreProperties properties, AwsSdk2TransportOptions options) {
OpenSearchVectorStoreProperties.Aws aws = properties.getAws();
Region region = Region.of(aws.getRegion());
OpenSearchClient openSearchClient(OpenSearchVectorStoreProperties properties,
AwsOpenSearchConnectionDetails connectionDetails, AwsSdk2TransportOptions options) {
Region region = Region.of(connectionDetails.getRegion());

SdkHttpClient httpClient = ApacheHttpClient.builder().build();
OpenSearchTransport transport = new AwsSdk2Transport(httpClient, aws.getHost(), aws.getServiceName(),
region, options);
OpenSearchTransport transport = new AwsSdk2Transport(httpClient,
connectionDetails.getHost(properties.getAws().getDomainName()),
properties.getAws().getServiceName(), region, options);
return new OpenSearchClient(transport);
}

@Bean
@ConditionalOnMissingBean
AwsSdk2TransportOptions options(OpenSearchVectorStoreProperties properties) {
OpenSearchVectorStoreProperties.Aws aws = properties.getAws();
AwsSdk2TransportOptions options(AwsOpenSearchConnectionDetails connectionDetails) {
return AwsSdk2TransportOptions.builder()
.setCredentials(StaticCredentialsProvider
.create(AwsBasicCredentials.create(aws.getAccessKey(), aws.getSecretKey())))
.setCredentials(StaticCredentialsProvider.create(
AwsBasicCredentials.create(connectionDetails.getAccessKey(), connectionDetails.getSecretKey())))
.build();
}

Expand Down Expand Up @@ -175,4 +184,37 @@ public String getPassword() {

}

static class PropertiesAwsOpenSearchConnectionDetails implements AwsOpenSearchConnectionDetails {

private final OpenSearchVectorStoreProperties.Aws aws;

public PropertiesAwsOpenSearchConnectionDetails(OpenSearchVectorStoreProperties properties) {
this.aws = properties.getAws();
}

@Override
public String getRegion() {
return this.aws.getRegion();
}

@Override
public String getAccessKey() {
return this.aws.getAccessKey();
}

@Override
public String getSecretKey() {
return this.aws.getSecretKey();
}

@Override
public String getHost(String domainName) {
if (StringUtils.hasText(domainName)) {
return "%s.%s".formatted(this.aws.getDomainName(), this.aws.getHost());
}
return this.aws.getHost();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public void setAws(Aws aws) {

static class Aws {

private String domainName;

private String host;

private String serviceName;
Expand All @@ -101,6 +103,14 @@ static class Aws {

private String region;

public String getDomainName() {
return this.domainName;
}

public void setDomainName(String domainName) {
this.domainName = domainName;
}

public String getHost() {
return this.host;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ class AwsOpenSearchVectorStoreAutoConfigurationIT {
.withConfiguration(AutoConfigurations.of(OpenSearchVectorStoreAutoConfiguration.class,
SpringAiRetryAutoConfiguration.class))
.withUserConfiguration(Config.class)
.withPropertyValues("spring.ai.vectorstore.opensearch.initialize-schema=true")
.withPropertyValues(
.withPropertyValues("spring.ai.vectorstore.opensearch.initialize-schema=true",
OpenSearchVectorStoreProperties.CONFIG_PREFIX + ".aws.host="
+ String.format("testcontainers-domain.%s.opensearch.localhost.localstack.cloud:%s",
localstack.getRegion(), localstack.getMappedPort(4566)),
OpenSearchVectorStoreProperties.CONFIG_PREFIX + ".aws.service-name=opensearch",
OpenSearchVectorStoreProperties.CONFIG_PREFIX + ".aws.service-name=es",
OpenSearchVectorStoreProperties.CONFIG_PREFIX + ".aws.region=" + localstack.getRegion(),
OpenSearchVectorStoreProperties.CONFIG_PREFIX + ".aws.access-key=" + localstack.getAccessKey(),
OpenSearchVectorStoreProperties.CONFIG_PREFIX + ".aws.secret-key=" + localstack.getSecretKey(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.docker.compose.service.connection.opensearch;

import org.springframework.ai.autoconfigure.vectorstore.opensearch.AwsOpenSearchConnectionDetails;
import org.springframework.ai.autoconfigure.vectorstore.opensearch.OpenSearchConnectionDetails;
import org.springframework.boot.docker.compose.core.RunningService;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionDetailsFactory;
import org.springframework.boot.docker.compose.service.connection.DockerComposeConnectionSource;

/**
* @author Eddú Meléndez
*/
class AwsOpenSearchDockerComposeConnectionDetailsFactory
extends DockerComposeConnectionDetailsFactory<AwsOpenSearchConnectionDetails> {

private static final int LOCALSTACK_PORT = 4566;

protected AwsOpenSearchDockerComposeConnectionDetailsFactory() {
super("localstack/localstack");
}

@Override
protected AwsOpenSearchConnectionDetails getDockerComposeConnectionDetails(DockerComposeConnectionSource source) {
return new AwsOpenSearchDockerComposeConnectionDetails(source.getRunningService());
}

/**
* {@link OpenSearchConnectionDetails} backed by a {@code OpenSearch}
* {@link RunningService}.
*/
static class AwsOpenSearchDockerComposeConnectionDetails extends DockerComposeConnectionDetails
implements AwsOpenSearchConnectionDetails {

private final AwsOpenSearchEnvironment environment;

private final int port;

AwsOpenSearchDockerComposeConnectionDetails(RunningService service) {
super(service);
this.environment = new AwsOpenSearchEnvironment(service.env());
this.port = service.ports().get(LOCALSTACK_PORT);
}

@Override
public String getRegion() {
return this.environment.getRegion();
}

@Override
public String getAccessKey() {
return this.environment.getAccessKey();
}

@Override
public String getSecretKey() {
return this.environment.getSecretKey();
}

@Override
public String getHost(String domainName) {
return "%s.%s.opensearch.localhost.localstack.cloud:%s".formatted(domainName, this.environment.getRegion(),
this.port);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.docker.compose.service.connection.opensearch;

import java.util.Map;

class AwsOpenSearchEnvironment {

private final String region;

private final String accessKey;

private final String secretKey;

AwsOpenSearchEnvironment(Map<String, String> env) {
this.region = env.getOrDefault("DEFAULT_REGION", "us-east-1");
this.accessKey = env.getOrDefault("AWS_ACCESS_KEY_ID", "test");
this.secretKey = env.getOrDefault("AWS_SECRET_ACCESS_KEY", "test");
}

public String getRegion() {
return this.region;
}

public String getAccessKey() {
return this.accessKey;
}

public String getSecretKey() {
return this.secretKey;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFacto
org.springframework.ai.docker.compose.service.connection.chroma.ChromaDockerComposeConnectionDetailsFactory,\
org.springframework.ai.docker.compose.service.connection.mongo.MongoDbAtlasLocalDockerComposeConnectionDetailsFactory,\
org.springframework.ai.docker.compose.service.connection.ollama.OllamaDockerComposeConnectionDetailsFactory,\
org.springframework.ai.docker.compose.service.connection.opensearch.AwsOpenSearchDockerComposeConnectionDetailsFactory,\
org.springframework.ai.docker.compose.service.connection.opensearch.OpenSearchDockerComposeConnectionDetailsFactory,\
org.springframework.ai.docker.compose.service.connection.qdrant.QdrantDockerComposeConnectionDetailsFactory,\
org.springframework.ai.docker.compose.service.connection.typesense.TypesenseDockerComposeConnectionDetailsFactory,\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.docker.compose.service.connection.opensearch;

import org.junit.jupiter.api.Test;
import org.springframework.ai.autoconfigure.vectorstore.opensearch.AwsOpenSearchConnectionDetails;
import org.springframework.boot.docker.compose.service.connection.test.AbstractDockerComposeIntegrationTests;
import org.testcontainers.utility.DockerImageName;

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

class AwsOpenSearchDockerComposeConnectionDetailsFactoryTests extends AbstractDockerComposeIntegrationTests {

AwsOpenSearchDockerComposeConnectionDetailsFactoryTests() {
super("localstack-compose.yaml", DockerImageName.parse("localstack/localstack:3.5.0"));
}

@Test
void runCreatesConnectionDetails() {
AwsOpenSearchConnectionDetails connectionDetails = run(AwsOpenSearchConnectionDetails.class);
assertThat(connectionDetails.getAccessKey()).isEqualTo("test");
assertThat(connectionDetails.getSecretKey()).isEqualTo("test");
assertThat(connectionDetails.getRegion()).isEqualTo("us-east-1");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
localstack:
image: '{imageName}'
ports:
- '4566'
Loading