From 865d429451d01ba0f777f2f37ce1754d29197c8f Mon Sep 17 00:00:00 2001 From: Soby Chacko Date: Thu, 7 Nov 2024 18:07:58 -0500 Subject: [PATCH] Checkstyle changes --- .../ai/bedrock/api/AbstractBedrockApi.java | 2 +- .../ai/oci/cohere/OCICohereChatModel.java | 2 +- .../ai/oci/BaseOCIGenAITest.java | 6 +- .../ai/oci/cohere/OCICohereChatModelIT.java | 13 +-- .../chat/OpenAiChatModelResponseFormatIT.java | 7 +- .../genai/OCICohereChatModelProperties.java | 2 +- .../oci/genai/OCIGenAiAutoConfiguration.java | 2 +- .../genai/OCIGenAIAutoConfigurationTest.java | 6 +- .../genai/OCIGenAiAutoConfigurationIT.java | 6 +- src/checkstyle/checkstyle-suppressions.xml | 2 + .../ai/vectorstore/CoherenceVectorStore.java | 4 +- .../vectorstore/CoherenceVectorStoreIT.java | 102 +++++++++--------- 12 files changed, 81 insertions(+), 73 deletions(-) diff --git a/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/api/AbstractBedrockApi.java b/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/api/AbstractBedrockApi.java index 439c67251..ac84df672 100644 --- a/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/api/AbstractBedrockApi.java +++ b/models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/api/AbstractBedrockApi.java @@ -291,7 +291,7 @@ public abstract class AbstractBedrockApi { }) .onDefault(event -> { logger.error("Unknown or unhandled event: " + event.toString()); - eventSink.emitError(new Throwable("Unknown or unhandled event: " + event.toString()),DEFAULT_EMIT_FAILURE_HANDLER); + eventSink.emitError(new Throwable("Unknown or unhandled event: " + event.toString()), DEFAULT_EMIT_FAILURE_HANDLER); }) .build(); diff --git a/models/spring-ai-oci-genai/src/main/java/org/springframework/ai/oci/cohere/OCICohereChatModel.java b/models/spring-ai-oci-genai/src/main/java/org/springframework/ai/oci/cohere/OCICohereChatModel.java index 5bd7756f8..941658cb8 100644 --- a/models/spring-ai-oci-genai/src/main/java/org/springframework/ai/oci/cohere/OCICohereChatModel.java +++ b/models/spring-ai-oci-genai/src/main/java/org/springframework/ai/oci/cohere/OCICohereChatModel.java @@ -170,7 +170,7 @@ public class OCICohereChatModel implements ChatModel { } private List getGenerations(Prompt prompt, OCICohereChatOptions options) { - com.oracle.bmc.generativeaiinference.responses.ChatResponse cr = genAi + com.oracle.bmc.generativeaiinference.responses.ChatResponse cr = this.genAi .chat(toCohereChatRequest(prompt, options)); return toGenerations(cr, options); diff --git a/models/spring-ai-oci-genai/src/test/java/org/springframework/ai/oci/BaseOCIGenAITest.java b/models/spring-ai-oci-genai/src/test/java/org/springframework/ai/oci/BaseOCIGenAITest.java index 358563cb0..e41026da3 100644 --- a/models/spring-ai-oci-genai/src/test/java/org/springframework/ai/oci/BaseOCIGenAITest.java +++ b/models/spring-ai-oci-genai/src/test/java/org/springframework/ai/oci/BaseOCIGenAITest.java @@ -1,11 +1,11 @@ /* - * Copyright 2024 the original author or authors. + * 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 + * 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, @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.ai.oci; import java.io.IOException; @@ -22,6 +23,7 @@ import com.oracle.bmc.Region; import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider; import com.oracle.bmc.generativeaiinference.GenerativeAiInference; import com.oracle.bmc.generativeaiinference.GenerativeAiInferenceClient; + import org.springframework.ai.oci.cohere.OCICohereChatOptions; public class BaseOCIGenAITest { diff --git a/models/spring-ai-oci-genai/src/test/java/org/springframework/ai/oci/cohere/OCICohereChatModelIT.java b/models/spring-ai-oci-genai/src/test/java/org/springframework/ai/oci/cohere/OCICohereChatModelIT.java index f12684caa..13f465ff4 100644 --- a/models/spring-ai-oci-genai/src/test/java/org/springframework/ai/oci/cohere/OCICohereChatModelIT.java +++ b/models/spring-ai-oci-genai/src/test/java/org/springframework/ai/oci/cohere/OCICohereChatModelIT.java @@ -1,11 +1,11 @@ /* - * Copyright 2024 the original author or authors. + * 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 + * 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, @@ -13,10 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.ai.oci.cohere; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; + import org.springframework.ai.chat.messages.SystemMessage; import org.springframework.ai.chat.messages.UserMessage; import org.springframework.ai.chat.model.ChatModel; @@ -25,11 +27,10 @@ import org.springframework.ai.chat.prompt.Prompt; import org.springframework.ai.oci.BaseOCIGenAITest; import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.ai.oci.BaseOCIGenAITest.OCI_CHAT_MODEL_ID_KEY; -import static org.springframework.ai.oci.BaseOCIGenAITest.OCI_COMPARTMENT_ID_KEY; -@EnabledIfEnvironmentVariable(named = OCI_COMPARTMENT_ID_KEY, matches = ".+") -@EnabledIfEnvironmentVariable(named = OCI_CHAT_MODEL_ID_KEY, matches = ".+") +@EnabledIfEnvironmentVariable(named = org.springframework.ai.oci.BaseOCIGenAITest.OCI_COMPARTMENT_ID_KEY, + matches = ".+") +@EnabledIfEnvironmentVariable(named = org.springframework.ai.oci.BaseOCIGenAITest.OCI_CHAT_MODEL_ID_KEY, matches = ".+") public class OCICohereChatModelIT extends BaseOCIGenAITest { private static final ChatModel chatModel = new OCICohereChatModel(getGenerativeAIClient(), options().build()); diff --git a/models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/OpenAiChatModelResponseFormatIT.java b/models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/OpenAiChatModelResponseFormatIT.java index 39da40ea3..bb0db5ce3 100644 --- a/models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/OpenAiChatModelResponseFormatIT.java +++ b/models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/OpenAiChatModelResponseFormatIT.java @@ -23,7 +23,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; import org.slf4j.Logger; @@ -42,7 +41,6 @@ import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Bean; -import static org.assertj.core.api.Assertions.as; import static org.assertj.core.api.Assertions.assertThat; /** @@ -161,7 +159,8 @@ public class OpenAiChatModelResponseFormatIT { } var outputConverter = new BeanOutputConverter<>(MathReasoning.class); - + // @formatter:off + // CHECKSTYLE:OFF var expectedJsonSchema = """ { "$schema" : "https://json-schema.org/draft/2020-12/schema", @@ -197,6 +196,8 @@ public class OpenAiChatModelResponseFormatIT { "required" : [ "steps", "final_answer" ], "additionalProperties" : false }"""; + // @formatter:on + // CHECKSTYLE:ON var jsonSchema1 = outputConverter.getJsonSchema(); assertThat(jsonSchema1).isNotNull(); diff --git a/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/oci/genai/OCICohereChatModelProperties.java b/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/oci/genai/OCICohereChatModelProperties.java index 58c7c8ef9..b4f0a5291 100644 --- a/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/oci/genai/OCICohereChatModelProperties.java +++ b/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/oci/genai/OCICohereChatModelProperties.java @@ -41,7 +41,7 @@ public class OCICohereChatModelProperties { .build(); public boolean isEnabled() { - return enabled; + return this.enabled; } public void setEnabled(boolean enabled) { diff --git a/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/oci/genai/OCIGenAiAutoConfiguration.java b/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/oci/genai/OCIGenAiAutoConfiguration.java index d97460702..164e149b7 100644 --- a/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/oci/genai/OCIGenAiAutoConfiguration.java +++ b/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/oci/genai/OCIGenAiAutoConfiguration.java @@ -48,7 +48,7 @@ import org.springframework.util.StringUtils; @AutoConfiguration @ConditionalOnClass({ GenerativeAiInferenceClient.class, OCIEmbeddingModel.class }) @EnableConfigurationProperties({ OCIConnectionProperties.class, OCIEmbeddingModelProperties.class, - OCICohereChatModelProperties.class, }) + OCICohereChatModelProperties.class }) public class OCIGenAiAutoConfiguration { private static BasicAuthenticationDetailsProvider authenticationProvider(OCIConnectionProperties properties) diff --git a/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/oci/genai/OCIGenAIAutoConfigurationTest.java b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/oci/genai/OCIGenAIAutoConfigurationTest.java index 397a113f9..d7b9573ff 100644 --- a/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/oci/genai/OCIGenAIAutoConfigurationTest.java +++ b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/oci/genai/OCIGenAIAutoConfigurationTest.java @@ -1,11 +1,11 @@ /* - * Copyright 2024 the original author or authors. + * 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 + * 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, @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.ai.autoconfigure.oci.genai; import java.nio.file.Files; @@ -23,6 +24,7 @@ import java.security.KeyPairGenerator; import com.oracle.bmc.http.client.pki.Pem; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; + import org.springframework.ai.oci.cohere.OCICohereChatModel; import org.springframework.ai.oci.cohere.OCICohereChatOptions; import org.springframework.boot.autoconfigure.AutoConfigurations; diff --git a/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/oci/genai/OCIGenAiAutoConfigurationIT.java b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/oci/genai/OCIGenAiAutoConfigurationIT.java index fd282f51b..24b7e5262 100644 --- a/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/oci/genai/OCIGenAiAutoConfigurationIT.java +++ b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/oci/genai/OCIGenAiAutoConfigurationIT.java @@ -57,10 +57,10 @@ public class OCIGenAiAutoConfigurationIT { private final ApplicationContextRunner cohereChatContextRunner = new ApplicationContextRunner().withPropertyValues( // @formatter:off "spring.ai.oci.genai.authenticationType=file", - "spring.ai.oci.genai.file=" + CONFIG_FILE, - "spring.ai.oci.genai.cohere.chat.options.compartment=" + COMPARTMENT_ID, + "spring.ai.oci.genai.file=" + this.CONFIG_FILE, + "spring.ai.oci.genai.cohere.chat.options.compartment=" + this.COMPARTMENT_ID, "spring.ai.oci.genai.cohere.chat.options.servingMode=on-demand", - "spring.ai.oci.genai.cohere.chat.options.model=" + CHAT_MODEL_ID + "spring.ai.oci.genai.cohere.chat.options.model=" + this.CHAT_MODEL_ID // @formatter:on ).withConfiguration(AutoConfigurations.of(OCIGenAiAutoConfiguration.class)); diff --git a/src/checkstyle/checkstyle-suppressions.xml b/src/checkstyle/checkstyle-suppressions.xml index b162458b7..85c9cf865 100644 --- a/src/checkstyle/checkstyle-suppressions.xml +++ b/src/checkstyle/checkstyle-suppressions.xml @@ -29,5 +29,7 @@ + + diff --git a/vector-stores/spring-ai-coherence-store/src/main/java/org/springframework/ai/vectorstore/CoherenceVectorStore.java b/vector-stores/spring-ai-coherence-store/src/main/java/org/springframework/ai/vectorstore/CoherenceVectorStore.java index 090fbc0aa..e9c64e794 100644 --- a/vector-stores/spring-ai-coherence-store/src/main/java/org/springframework/ai/vectorstore/CoherenceVectorStore.java +++ b/vector-stores/spring-ai-coherence-store/src/main/java/org/springframework/ai/vectorstore/CoherenceVectorStore.java @@ -231,7 +231,7 @@ public class CoherenceVectorStore implements VectorStore, InitializingBean { this.documentChunks = this.session.getMap(this.mapName); switch (this.indexType) { case HNSW -> this.documentChunks - .addIndex(new HnswIndex<>(DocumentChunk::vector, this.distanceType.name(), dimensions)); + .addIndex(new HnswIndex<>(DocumentChunk::vector, this.distanceType.name(), this.dimensions)); case BINARY -> this.documentChunks.addIndex(new BinaryQuantIndex<>(DocumentChunk::vector)); } } @@ -255,7 +255,7 @@ public class CoherenceVectorStore implements VectorStore, InitializingBean { } String getMapName() { - return mapName; + return this.mapName; } } diff --git a/vector-stores/spring-ai-coherence-store/src/test/java/org/springframework/ai/vectorstore/CoherenceVectorStoreIT.java b/vector-stores/spring-ai-coherence-store/src/test/java/org/springframework/ai/vectorstore/CoherenceVectorStoreIT.java index 55522ee36..8b1a0c52b 100644 --- a/vector-stores/spring-ai-coherence-store/src/test/java/org/springframework/ai/vectorstore/CoherenceVectorStoreIT.java +++ b/vector-stores/spring-ai-coherence-store/src/test/java/org/springframework/ai/vectorstore/CoherenceVectorStoreIT.java @@ -92,48 +92,6 @@ public class CoherenceVectorStoreIT { .withPropertyValues("test.spring.ai.vectorstore.coherence.distanceType=COSINE", "test.spring.ai.vectorstore.coherence.indexType=NONE"); - @SpringBootConfiguration - @EnableAutoConfiguration - public static class TestClient { - - @Value("${test.spring.ai.vectorstore.coherence.distanceType}") - CoherenceVectorStore.DistanceType distanceType; - - @Value("${test.spring.ai.vectorstore.coherence.indexType}") - CoherenceVectorStore.IndexType indexType; - - @Bean - public VectorStore vectorStore(EmbeddingModel embeddingModel, Session session) { - return new CoherenceVectorStore(embeddingModel, session).setDistanceType(distanceType) - .setIndexType(indexType) - .setForcedNormalization(distanceType == CoherenceVectorStore.DistanceType.COSINE - || distanceType == CoherenceVectorStore.DistanceType.IP); - } - - @Bean - public Session session(Coherence coherence) { - return coherence.getSession(); - } - - @Bean - public Coherence coherence() { - return Coherence.clusterMember().start().join(); - } - - @Bean - public EmbeddingModel embeddingModel() { - try { - TransformersEmbeddingModel tem = new TransformersEmbeddingModel(); - tem.afterPropertiesSet(); - return tem; - } - catch (Exception e) { - throw new RuntimeException("Failed initializing embedding model", e); - } - } - - } - private static void truncateMap(ApplicationContext context, String mapName) { Session session = context.getBean(Session.class); session.getMap(mapName).truncate(); @@ -153,24 +111,24 @@ public class CoherenceVectorStoreIT { @ParameterizedTest(name = "Distance {0}, Index {1} : {displayName}") @MethodSource("distanceAndIndex") public void addAndSearch(CoherenceVectorStore.DistanceType distanceType, CoherenceVectorStore.IndexType indexType) { - contextRunner.withPropertyValues("test.spring.ai.vectorstore.coherence.distanceType=" + distanceType) + this.contextRunner.withPropertyValues("test.spring.ai.vectorstore.coherence.distanceType=" + distanceType) .withPropertyValues("test.spring.ai.vectorstore.coherence.indexType=" + indexType) .run(context -> { VectorStore vectorStore = context.getBean(VectorStore.class); - vectorStore.add(documents); + vectorStore.add(this.documents); List results = vectorStore .similaritySearch(SearchRequest.query("What is Great Depression").withTopK(1)); assertThat(results).hasSize(1); Document resultDoc = results.get(0); - assertThat(resultDoc.getId()).isEqualTo(documents.get(2).getId()); + assertThat(resultDoc.getId()).isEqualTo(this.documents.get(2).getId()); assertThat(resultDoc.getMetadata()).containsKeys("meta2", "distance"); // Remove all documents from the store - vectorStore.delete(documents.stream().map(doc -> doc.getId()).toList()); + vectorStore.delete(this.documents.stream().map(doc -> doc.getId()).toList()); List results2 = vectorStore .similaritySearch(SearchRequest.query("Great Depression").withTopK(1)); @@ -184,7 +142,7 @@ public class CoherenceVectorStoreIT { @MethodSource("distanceAndIndex") public void searchWithFilters(CoherenceVectorStore.DistanceType distanceType, CoherenceVectorStore.IndexType indexType) { - contextRunner.withPropertyValues("test.spring.ai.vectorstore.coherence.distanceType=" + distanceType) + this.contextRunner.withPropertyValues("test.spring.ai.vectorstore.coherence.distanceType=" + distanceType) .withPropertyValues("test.spring.ai.vectorstore.coherence.indexType=" + indexType) .run(context -> { @@ -250,7 +208,7 @@ public class CoherenceVectorStoreIT { @Test public void documentUpdate() { - contextRunner.run(context -> { + this.contextRunner.run(context -> { VectorStore vectorStore = context.getBean(VectorStore.class); Document document = new Document(UUID.randomUUID().toString(), "Spring AI rocks!!", @@ -286,11 +244,11 @@ public class CoherenceVectorStoreIT { @Test public void searchWithThreshold() { - contextRunner.run(context -> { + this.contextRunner.run(context -> { VectorStore vectorStore = context.getBean(VectorStore.class); - vectorStore.add(documents); + vectorStore.add(this.documents); List fullResult = vectorStore .similaritySearch(SearchRequest.query("Time Shelter").withTopK(5).withSimilarityThresholdAll()); @@ -310,7 +268,7 @@ public class CoherenceVectorStoreIT { assertThat(results).hasSize(1); Document resultDoc = results.get(0); - assertThat(resultDoc.getId()).isEqualTo(documents.get(1).getId()); + assertThat(resultDoc.getId()).isEqualTo(this.documents.get(1).getId()); truncateMap(context, ((CoherenceVectorStore) vectorStore).getMapName()); }); @@ -338,4 +296,46 @@ public class CoherenceVectorStoreIT { return true; } + @SpringBootConfiguration + @EnableAutoConfiguration + public static class TestClient { + + @Value("${test.spring.ai.vectorstore.coherence.distanceType}") + CoherenceVectorStore.DistanceType distanceType; + + @Value("${test.spring.ai.vectorstore.coherence.indexType}") + CoherenceVectorStore.IndexType indexType; + + @Bean + public VectorStore vectorStore(EmbeddingModel embeddingModel, Session session) { + return new CoherenceVectorStore(embeddingModel, session).setDistanceType(this.distanceType) + .setIndexType(this.indexType) + .setForcedNormalization(this.distanceType == CoherenceVectorStore.DistanceType.COSINE + || this.distanceType == CoherenceVectorStore.DistanceType.IP); + } + + @Bean + public Session session(Coherence coherence) { + return coherence.getSession(); + } + + @Bean + public Coherence coherence() { + return Coherence.clusterMember().start().join(); + } + + @Bean + public EmbeddingModel embeddingModel() { + try { + TransformersEmbeddingModel tem = new TransformersEmbeddingModel(); + tem.afterPropertiesSet(); + return tem; + } + catch (Exception e) { + throw new RuntimeException("Failed initializing embedding model", e); + } + } + + } + }