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

Added MimeTypeConverters class #1022

Closed
wants to merge 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,23 @@

import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.MongoDBAtlasVectorStore;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.ai.vectorstore.convert.MimeTypeConverters;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.util.MimeType;
import org.springframework.util.StringUtils;

import java.util.Arrays;

/**
* @author Eddú Meléndez
* @author Christian Tzolov
* @since 1.0.0
*/
@AutoConfiguration(after = MongoDataAutoConfiguration.class)
@AutoConfiguration(before = MongoDataAutoConfiguration.class)
@ConditionalOnClass({ MongoDBAtlasVectorStore.class, EmbeddingModel.class, MongoTemplate.class })
@EnableConfigurationProperties(MongoDBAtlasVectorStoreProperties.class)
public class MongoDBAtlasVectorStoreAutoConfiguration {
Expand All @@ -65,37 +60,8 @@ MongoDBAtlasVectorStore vectorStore(MongoTemplate mongoTemplate, EmbeddingModel
}

@Bean
public Converter<MimeType, String> mimeTypeToStringConverter() {
return new Converter<MimeType, String>() {
@Override
public String convert(MimeType source) {
return source.toString();
}
};
}

@Bean
public Converter<String, MimeType> stringToMimeTypeConverter() {
return new Converter<String, MimeType>() {
@Override
public MimeType convert(String source) {
return MimeType.valueOf(source);
}
};
}

@Bean
public BeanPostProcessor mongoCustomConversionsPostProcessor() {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof MongoCustomConversions) {
return new MongoCustomConversions(
Arrays.asList(mimeTypeToStringConverter(), stringToMimeTypeConverter()));
}
return bean;
}
};
public MongoCustomConversions mongoCustomConversions() {
return new MongoCustomConversions(MimeTypeConverters.getConvertersToRegister());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.ai.autoconfigure.vectorstore.mongo;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.ai.autoconfigure.openai.OpenAiAutoConfiguration;
import org.springframework.ai.autoconfigure.retry.SpringAiRetryAutoConfiguration;
import org.springframework.ai.document.Document;
Expand All @@ -42,6 +43,7 @@
* @author Eddú Meléndez
*/
@Testcontainers
@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+")
class MongoDBAtlasVectorStoreAutoConfigurationIT {

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

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.util.MimeType;

import java.util.List;

import static java.util.Arrays.asList;

/**
* Provides converters for {@link org.springframework.util.MimeType } for use with Mongo.
*
* @author Ross Lawley
* @since 1.0.0
*/
public class MimeTypeConverters {

/**
* @return the converters to be registered.
*/
public static List<Converter<?, ?>> getConvertersToRegister() {
return asList(MimeTypeReadConverter.INSTANCE, MimeTypeWriteConverter.INSTANCE);
}

/**
* Simple singleton to convert {@link String}s to their {@link MimeType}
* representation.
*/
@ReadingConverter
enum MimeTypeReadConverter implements Converter<String, MimeType> {

INSTANCE;

public MimeType convert(String source) {
return MimeType.valueOf(source);
}

}

/**
* Simple singleton to convert {@link MimeType}s to their {@link String}
* representation.
*/
@WritingConverter
enum MimeTypeWriteConverter implements Converter<MimeType, String> {

INSTANCE;

public String convert(MimeType source) {
return source.toString();
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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.
*/
@org.springframework.lang.NonNullApi
package org.springframework.ai.vectorstore.convert;
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class MongoDBAtlasContainer extends GenericContainer<MongoDBAtlasContainer> {

public MongoDBAtlasContainer() {
super("mongodb/atlas:v1.15.1");
super("mongodb/atlas:v1.24.0");
withPrivilegedMode(true);
withCommand("/bin/bash", "-c",
"atlas deployments setup local-test --type local --port 27778 --bindIpAll --username root --password root --force && tail -f /dev/null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@
*/
package org.springframework.ai.vectorstore;

import com.mongodb.client.MongoClient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.ai.vectorstore.convert.MimeTypeConverters;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

Expand All @@ -42,9 +43,10 @@

/**
* @author Chris Smith
* @author Ross Lawley
*/
@Testcontainers
@Disabled("Disabled due to https://github.com/spring-projects/spring-ai/issues/698")
@EnabledIfEnvironmentVariable(named = "OPENAI_API_KEY", matches = ".+")
class MongoDBAtlasVectorStoreIT {

@Container
Expand Down Expand Up @@ -191,6 +193,17 @@ void searchWithFilters() {
@EnableAutoConfiguration
public static class TestApplication {

@Value("${spring.data.mongodb.uri}")
String mongoUri;

@Value("${spring.data.mongodb.database}")
String databaseName;

@Bean
public MongoCustomConversions mongoCustomConversions() {
return new MongoCustomConversions(MimeTypeConverters.getConvertersToRegister());
}

@Bean
public VectorStore vectorStore(MongoTemplate mongoTemplate, EmbeddingModel embeddingModel) {
return new MongoDBAtlasVectorStore(mongoTemplate, embeddingModel,
Expand All @@ -200,11 +213,6 @@ public VectorStore vectorStore(MongoTemplate mongoTemplate, EmbeddingModel embed
true);
}

@Bean
public MongoTemplate mongoTemplate(MongoClient mongoClient) {
return new MongoTemplate(mongoClient, "springaisample");
}

@Bean
public EmbeddingModel embeddingModel() {
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("OPENAI_API_KEY")));
Expand Down