Skip to content

Commit

Permalink
Generate LangChain4j sample
Browse files Browse the repository at this point in the history
  • Loading branch information
anunnakian committed Nov 10, 2024
1 parent de3ae98 commit fb53f99
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 36 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@tech.jhipster.lite.BusinessContext
package tech.jhipster.lite.generator.server.springboot.mvc.sample.langchain4j;
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, JHLiteModuleSlug> moduleSlugMap = Stream.of(values()).collect(
Collectors.toMap(JHLiteModuleSlug::get, Function.identity())
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;

@UnitTest
class SampleJpaPersistenceModuleFactoryTest {
class SampleLangChain4jModuleFactoryTest {

private static final SampleJpaPersistenceModuleFactory factory = new SampleJpaPersistenceModuleFactory();

Expand Down

0 comments on commit fb53f99

Please sign in to comment.