From fb53f99fd32751ffd5319e1b0cd0b714693c98f2 Mon Sep 17 00:00:00 2001 From: Mohamed Date: Sun, 10 Nov 2024 16:43:48 +0100 Subject: [PATCH] Generate LangChain4j sample --- ...mpleJpaPersistenceModuleConfiguration.java | 34 ------------------- .../SampleLangChain4jApplicationService.java | 20 +++++++++++ .../SampleLangChain4jModuleFactory.java | 34 +++++++++++++++++++ .../SampleLangChain4jModuleConfiguration.java | 29 ++++++++++++++++ .../mvc/sample/langchain4j/package-info.java | 2 ++ .../shared/slug/domain/JHLiteModuleSlug.java | 3 +- .../secondary/ChatController.java.mustache | 21 ++++++++++++ ...> SampleLangChain4jModuleFactoryTest.java} | 2 +- 8 files changed, 109 insertions(+), 36 deletions(-) delete mode 100644 src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/jpapersistence/infrastructure/primary/SampleJpaPersistenceModuleConfiguration.java create mode 100644 src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/application/SampleLangChain4jApplicationService.java create mode 100644 src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/domain/SampleLangChain4jModuleFactory.java create mode 100644 src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/infrastructure/primary/SampleLangChain4jModuleConfiguration.java create mode 100644 src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/package-info.java create mode 100644 src/main/resources/generator/server/springboot/mvc/sample/langchain4j/main/infrastructure/secondary/ChatController.java.mustache rename src/test/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/jpapersistence/domain/{SampleJpaPersistenceModuleFactoryTest.java => SampleLangChain4jModuleFactoryTest.java} (98%) diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/jpapersistence/infrastructure/primary/SampleJpaPersistenceModuleConfiguration.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/jpapersistence/infrastructure/primary/SampleJpaPersistenceModuleConfiguration.java deleted file mode 100644 index 408d9f39ec6..00000000000 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/jpapersistence/infrastructure/primary/SampleJpaPersistenceModuleConfiguration.java +++ /dev/null @@ -1,34 +0,0 @@ -package tech.jhipster.lite.generator.server.springboot.mvc.sample.jpapersistence.infrastructure.primary; - -import static tech.jhipster.lite.shared.slug.domain.JHLiteFeatureSlug.SAMPLE_PERSISTENCE; -import static tech.jhipster.lite.shared.slug.domain.JHLiteFeatureSlug.SAMPLE_SCHEMA; -import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.SAMPLE_JPA_PERSISTENCE; -import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.SPRING_BOOT_CUCUMBER_JPA_RESET; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import tech.jhipster.lite.generator.server.springboot.mvc.sample.jpapersistence.application.SampleJpaPersistenceApplicationService; -import tech.jhipster.lite.module.domain.resource.JHipsterModuleOrganization; -import tech.jhipster.lite.module.domain.resource.JHipsterModulePropertiesDefinition; -import tech.jhipster.lite.module.domain.resource.JHipsterModuleResource; - -@Configuration -class SampleJpaPersistenceModuleConfiguration { - - @Bean - JHipsterModuleResource sampleJpaPersistenceModule(SampleJpaPersistenceApplicationService sampleJpaPersistence) { - return JHipsterModuleResource.builder() - .slug(SAMPLE_JPA_PERSISTENCE) - .propertiesDefinition(JHipsterModulePropertiesDefinition.builder().addBasePackage().build()) - .apiDoc("Sample Feature", "Add JPA persistence for sample feature") - .organization( - JHipsterModuleOrganization.builder() - .feature(SAMPLE_PERSISTENCE) - .addDependency(SAMPLE_SCHEMA) - .addDependency(SPRING_BOOT_CUCUMBER_JPA_RESET) - .build() - ) - .tags("server") - .factory(sampleJpaPersistence::buildModule); - } -} diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/application/SampleLangChain4jApplicationService.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/application/SampleLangChain4jApplicationService.java new file mode 100644 index 00000000000..e8fe52f63fc --- /dev/null +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/application/SampleLangChain4jApplicationService.java @@ -0,0 +1,20 @@ +package tech.jhipster.lite.generator.server.springboot.mvc.sample.langchain4j.application; + +import org.springframework.stereotype.Service; +import tech.jhipster.lite.generator.server.springboot.mvc.sample.langchain4j.domain.SampleLangChain4jModuleFactory; +import tech.jhipster.lite.module.domain.JHipsterModule; +import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; + +@Service +public class SampleLangChain4jApplicationService { + + private final SampleLangChain4jModuleFactory factory; + + public SampleLangChain4jApplicationService() { + factory = new SampleLangChain4jModuleFactory(); + } + + public JHipsterModule buildModule(JHipsterModuleProperties properties) { + return factory.buildModule(properties); + } +} diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/domain/SampleLangChain4jModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/domain/SampleLangChain4jModuleFactory.java new file mode 100644 index 00000000000..4ead66a68a0 --- /dev/null +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/domain/SampleLangChain4jModuleFactory.java @@ -0,0 +1,34 @@ +package tech.jhipster.lite.generator.server.springboot.mvc.sample.langchain4j.domain; + +import static tech.jhipster.lite.module.domain.JHipsterModule.from; +import static tech.jhipster.lite.module.domain.JHipsterModule.moduleBuilder; +import static tech.jhipster.lite.module.domain.JHipsterModule.toSrcMainJava; + +import tech.jhipster.lite.module.domain.JHipsterModule; +import tech.jhipster.lite.module.domain.file.JHipsterSource; +import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; +import tech.jhipster.lite.shared.error.domain.Assert; + +public class SampleLangChain4jModuleFactory { + + private static final JHipsterSource SOURCE = from("server/springboot/mvc/sample/langchain4j"); + + private static final String SECONDARY = "infrastructure/secondary"; + private static final String SECONDARY_DESTINATION = "sample/" + SECONDARY; + + public JHipsterModule buildModule(JHipsterModuleProperties properties) { + Assert.notNull("properties", properties); + + String packagePath = properties.packagePath(); + + //@formatter:off + return moduleBuilder(properties) + .files() + .batch(SOURCE.append("main").append(SECONDARY), toSrcMainJava().append(packagePath).append(SECONDARY_DESTINATION)) + .addTemplate("ChatController.java.mustache") + .and() + .and() + .build(); + //@formatter:on + } +} diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/infrastructure/primary/SampleLangChain4jModuleConfiguration.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/infrastructure/primary/SampleLangChain4jModuleConfiguration.java new file mode 100644 index 00000000000..6a246cbb878 --- /dev/null +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/infrastructure/primary/SampleLangChain4jModuleConfiguration.java @@ -0,0 +1,29 @@ +package tech.jhipster.lite.generator.server.springboot.mvc.sample.langchain4j.infrastructure.primary; + +import static tech.jhipster.lite.shared.slug.domain.JHLiteFeatureSlug.SPRING_MVC_SERVER; +import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.*; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import tech.jhipster.lite.generator.server.springboot.mvc.sample.langchain4j.application.SampleLangChain4jApplicationService; +import tech.jhipster.lite.generator.server.springboot.mvc.sample.langchain4j.domain.SampleLangChain4jModuleFactory; +import tech.jhipster.lite.module.domain.resource.JHipsterModuleOrganization; +import tech.jhipster.lite.module.domain.resource.JHipsterModulePropertiesDefinition; +import tech.jhipster.lite.module.domain.resource.JHipsterModuleResource; + +@Configuration +class SampleLangChain4jModuleConfiguration { + + @Bean + JHipsterModuleResource langChain4jResourceInit(SampleLangChain4jApplicationService applicationService) { + return JHipsterModuleResource.builder() + .slug(SPRING_BOOT_LANGCHAIN4J_SAMPLE) + .propertiesDefinition( + JHipsterModulePropertiesDefinition.builder().addBasePackage().addIndentation().addSpringConfigurationFormat().build() + ) + .apiDoc("Spring Boot - LangChain4j", "Add LangChain4j sample") + .organization(JHipsterModuleOrganization.builder().addDependency(SPRING_MVC_SERVER).addDependency(LANGCHAIN4J).build()) + .tags("spring-boot", "spring", "server", "langchain4j") + .factory(applicationService::buildModule); + } +} diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/package-info.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/package-info.java new file mode 100644 index 00000000000..a0e75e2f4d7 --- /dev/null +++ b/src/main/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/package-info.java @@ -0,0 +1,2 @@ +@tech.jhipster.lite.BusinessContext +package tech.jhipster.lite.generator.server.springboot.mvc.sample.langchain4j; diff --git a/src/main/java/tech/jhipster/lite/shared/slug/domain/JHLiteModuleSlug.java b/src/main/java/tech/jhipster/lite/shared/slug/domain/JHLiteModuleSlug.java index 7a10ba75582..66dd0ef692b 100644 --- a/src/main/java/tech/jhipster/lite/shared/slug/domain/JHLiteModuleSlug.java +++ b/src/main/java/tech/jhipster/lite/shared/slug/domain/JHLiteModuleSlug.java @@ -157,7 +157,8 @@ public enum JHLiteModuleSlug implements JHipsterModuleSlugFactory { VUE_PINIA("vue-pinia"), TS_PAGINATION_DOMAIN("ts-pagination-domain"), TS_REST_PAGINATION("ts-rest-pagination"), - LANGCHAIN4J("langchain4j"); + LANGCHAIN4J("langchain4j"), + SPRING_BOOT_LANGCHAIN4J_SAMPLE("spring-boot-langchain4j-sample"); private static final Map moduleSlugMap = Stream.of(values()).collect( Collectors.toMap(JHLiteModuleSlug::get, Function.identity()) diff --git a/src/main/resources/generator/server/springboot/mvc/sample/langchain4j/main/infrastructure/secondary/ChatController.java.mustache b/src/main/resources/generator/server/springboot/mvc/sample/langchain4j/main/infrastructure/secondary/ChatController.java.mustache new file mode 100644 index 00000000000..0eb3ae1c385 --- /dev/null +++ b/src/main/resources/generator/server/springboot/mvc/sample/langchain4j/main/infrastructure/secondary/ChatController.java.mustache @@ -0,0 +1,21 @@ +package {{packageName}}.sample.infrastructure.secondary; + +import dev.langchain4j.model.chat.ChatLanguageModel; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ChatController { + + ChatLanguageModel chatLanguageModel; + + public ChatController(ChatLanguageModel chatLanguageModel) { + this.chatLanguageModel = chatLanguageModel; + } + + @GetMapping("/chat") + public String model(@RequestParam(value = "message", defaultValue = "Hello") String message) { + return chatLanguageModel.generate(message); + } +} diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/jpapersistence/domain/SampleJpaPersistenceModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/jpapersistence/domain/SampleLangChain4jModuleFactoryTest.java similarity index 98% rename from src/test/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/jpapersistence/domain/SampleJpaPersistenceModuleFactoryTest.java rename to src/test/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/jpapersistence/domain/SampleLangChain4jModuleFactoryTest.java index 6c4f0e074a0..276c4b665a6 100644 --- a/src/test/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/jpapersistence/domain/SampleJpaPersistenceModuleFactoryTest.java +++ b/src/test/java/tech/jhipster/lite/generator/server/springboot/mvc/sample/jpapersistence/domain/SampleLangChain4jModuleFactoryTest.java @@ -10,7 +10,7 @@ import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; @UnitTest -class SampleJpaPersistenceModuleFactoryTest { +class SampleLangChain4jModuleFactoryTest { private static final SampleJpaPersistenceModuleFactory factory = new SampleJpaPersistenceModuleFactory();