-
-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11335 from anunnakian/langchain4j_sample
Add Spring Boot LangChain4j sample
- Loading branch information
Showing
13 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...er/springboot/mvc/sample/langchain4j/application/SampleLangChain4jApplicationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...rator/server/springboot/mvc/sample/langchain4j/domain/SampleLangChain4jModuleFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package tech.jhipster.lite.generator.server.springboot.mvc.sample.langchain4j.domain; | ||
|
||
import static tech.jhipster.lite.module.domain.JHipsterModule.*; | ||
|
||
import tech.jhipster.lite.module.domain.JHipsterModule; | ||
import tech.jhipster.lite.module.domain.file.JHipsterDestination; | ||
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 String SAMPLE = "sample"; | ||
|
||
private static final JHipsterSource SOURCE = from("server/springboot/mvc/sample/langchain4j"); | ||
|
||
private static final String PRIMARY = "infrastructure/primary"; | ||
|
||
public JHipsterModule buildModule(JHipsterModuleProperties properties) { | ||
Assert.notNull("properties", properties); | ||
|
||
String packagePath = properties.packagePath(); | ||
JHipsterDestination mainDestination = toSrcMainJava().append(packagePath).append(SAMPLE); | ||
JHipsterDestination testDestination = toSrcTestJava().append(packagePath).append(SAMPLE); | ||
|
||
//@formatter:off | ||
return moduleBuilder(properties) | ||
.files() | ||
.batch(SOURCE.append("main").append(PRIMARY), mainDestination.append(PRIMARY)) | ||
.addTemplate("ChatResource.java") | ||
.and() | ||
.batch(SOURCE.append("test").append(SAMPLE).append(PRIMARY), testDestination.append(PRIMARY)) | ||
.addTemplate("ChatResourceTest.java") | ||
.and() | ||
.and() | ||
.build(); | ||
//@formatter:on | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...t/mvc/sample/langchain4j/infrastructure/primary/SampleLangChain4jModuleConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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.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); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...a/tech/jhipster/lite/generator/server/springboot/mvc/sample/langchain4j/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
.../springboot/mvc/sample/langchain4j/main/infrastructure/primary/ChatResource.java.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package {{packageName}}.sample.infrastructure.primary; | ||
|
||
import dev.langchain4j.model.chat.ChatLanguageModel; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api") | ||
class ChatResource { | ||
private final ChatLanguageModel chatLanguageModel; | ||
public ChatResource(ChatLanguageModel chatLanguageModel) { | ||
this.chatLanguageModel = chatLanguageModel; | ||
} | ||
|
||
@GetMapping("/chat") | ||
public String send(@RequestParam(value = "message", defaultValue = "Hello") String message) { | ||
return chatLanguageModel.generate(message); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
.../mvc/sample/langchain4j/test/sample/infrastructure/primary/ChatResourceTest.java.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package {{packageName}}.sample.infrastructure.primary; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import {{packageName}}.UnitTest; | ||
import dev.langchain4j.model.chat.ChatLanguageModel; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@UnitTest | ||
@ExtendWith(MockitoExtension.class) | ||
class ChatResourceTest { | ||
private static final String ANSWER = "Hello! How can I assist you today?"; | ||
@Mock | ||
private ChatLanguageModel chatLanguageModel; | ||
@InjectMocks | ||
private ChatResource chat; | ||
@Test | ||
void shouldSendMessage() { | ||
when(chatLanguageModel.generate("Hello")).thenReturn(ANSWER); | ||
assertThat(chat.send("Hello")).isEqualTo(ANSWER); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters