-
-
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 #11273 from anunnakian/langchain4j
Allow using langchain4j module
- Loading branch information
Showing
9 changed files
with
189 additions
and
1 deletion.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
...te/generator/server/springboot/langchain4j/application/LangChain4JApplicationService.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.langchain4j.application; | ||
|
||
import org.springframework.stereotype.Service; | ||
import tech.jhipster.lite.generator.server.springboot.langchain4j.domain.LangChain4JModuleFactory; | ||
import tech.jhipster.lite.module.domain.JHipsterModule; | ||
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; | ||
|
||
@Service | ||
public class LangChain4JApplicationService { | ||
|
||
private final LangChain4JModuleFactory factory; | ||
|
||
public LangChain4JApplicationService() { | ||
factory = new LangChain4JModuleFactory(); | ||
} | ||
|
||
public JHipsterModule buildModule(JHipsterModuleProperties properties) { | ||
return factory.buildModule(properties); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...hipster/lite/generator/server/springboot/langchain4j/domain/LangChain4JModuleFactory.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,46 @@ | ||
package tech.jhipster.lite.generator.server.springboot.langchain4j.domain; | ||
|
||
import static tech.jhipster.lite.module.domain.JHipsterModule.*; | ||
|
||
import tech.jhipster.lite.module.domain.JHipsterModule; | ||
import tech.jhipster.lite.module.domain.file.JHipsterSource; | ||
import tech.jhipster.lite.module.domain.javabuild.ArtifactId; | ||
import tech.jhipster.lite.module.domain.javabuild.GroupId; | ||
import tech.jhipster.lite.module.domain.javabuild.VersionSlug; | ||
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; | ||
import tech.jhipster.lite.shared.error.domain.Assert; | ||
|
||
public class LangChain4JModuleFactory { | ||
|
||
private static final String API_KEY_DEMO_COMMENT = | ||
"You can temporarily use 'demo' key, which is provided for free for demonstration purposes"; | ||
|
||
private static final JHipsterSource SOURCE = from("server/springboot/langchain4j"); | ||
private static final GroupId GROUP_ID = groupId("dev.langchain4j"); | ||
private static final ArtifactId ARTIFACT_ID = artifactId("langchain4j-spring-boot-starter"); | ||
private static final ArtifactId OPEN_AI_ARTIFACT_ID = artifactId("langchain4j-open-ai-spring-boot-starter"); | ||
private static final VersionSlug VERSION_SLUG = versionSlug("langchain4j"); | ||
|
||
private static final String PROPERTIES = "properties"; | ||
|
||
public JHipsterModule buildModule(JHipsterModuleProperties properties) { | ||
Assert.notNull(PROPERTIES, properties); | ||
|
||
//@formatter:off | ||
return moduleBuilder(properties) | ||
.documentation(documentationTitle("Dev tools"), SOURCE.template("langchain4j.md")) | ||
.javaDependencies() | ||
.addDependency(GROUP_ID, ARTIFACT_ID, VERSION_SLUG) | ||
.addDependency(GROUP_ID, OPEN_AI_ARTIFACT_ID, VERSION_SLUG) | ||
.and() | ||
.springMainProperties() | ||
.set(propertyKey("langchain4j.open-ai.chat-model.api-key"), propertyValue("${OPENAI_API_KEY}")) | ||
.comment(propertyKey("langchain4j.open-ai.chat-model.api-key"), comment(API_KEY_DEMO_COMMENT)) | ||
.set(propertyKey("langchain4j.open-ai.chat-model.model-name"), propertyValue("gpt-4o-mini")) | ||
.set(propertyKey("langchain4j.open-ai.chat-model.log-requests"), propertyValue("true")) | ||
.set(propertyKey("langchain4j.open-ai.chat-model.log-responses"), propertyValue("true")) | ||
.and() | ||
.build(); | ||
//@formatter:on | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
.../server/springboot/langchain4j/infrastructure/primary/LangChain4JModuleConfiguration.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,26 @@ | ||
package tech.jhipster.lite.generator.server.springboot.langchain4j.infrastructure.primary; | ||
|
||
import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.LANGCHAIN4J; | ||
import static tech.jhipster.lite.shared.slug.domain.JHLiteModuleSlug.SPRING_BOOT; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import tech.jhipster.lite.generator.server.springboot.langchain4j.application.LangChain4JApplicationService; | ||
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 LangChain4JModuleConfiguration { | ||
|
||
@Bean | ||
JHipsterModuleResource langChain4JModule(LangChain4JApplicationService langChain4J) { | ||
return JHipsterModuleResource.builder() | ||
.slug(LANGCHAIN4J) | ||
.propertiesDefinition(JHipsterModulePropertiesDefinition.builder().addProjectBaseName().addIndentation().build()) | ||
.apiDoc("LangChain4J", "Add LangChain4j") | ||
.organization(JHipsterModuleOrganization.builder().addDependency(SPRING_BOOT).build()) | ||
.tags("server", "spring", "spring-boot", "langchain4j") | ||
.factory(langChain4J::buildModule); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/java/tech/jhipster/lite/generator/server/springboot/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.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
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
5 changes: 5 additions & 0 deletions
5
src/main/resources/generator/server/springboot/langchain4j/langchain4j.md.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,5 @@ | ||
# LangChain4j | ||
|
||
How to use it: | ||
|
||
[Official documentation](https://docs.langchain4j.dev/tutorials/spring-boot-integration/) |
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,10 @@ | ||
Feature: LangChain4j module | ||
|
||
Scenario: Should add Spring Boot LangChain4j Starter | ||
When I apply modules to default project | ||
| maven-java | | ||
| spring-boot | | ||
| langchain4j | | ||
Then I should have entries in "src/main/resources/config/application.yml" | ||
| open-ai | | ||
| langchain4j | |
67 changes: 67 additions & 0 deletions
67
...ter/lite/generator/server/springboot/langchain4j/domain/LangChain4JModuleFactoryTest.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,67 @@ | ||
package tech.jhipster.lite.generator.server.springboot.langchain4j.domain; | ||
|
||
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.assertThatModuleWithFiles; | ||
import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.pomFile; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import tech.jhipster.lite.TestFileUtils; | ||
import tech.jhipster.lite.UnitTest; | ||
import tech.jhipster.lite.module.domain.JHipsterModule; | ||
import tech.jhipster.lite.module.domain.JHipsterModulesFixture; | ||
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; | ||
|
||
@UnitTest | ||
@ExtendWith(MockitoExtension.class) | ||
class LangChain4JModuleFactoryTest { | ||
|
||
@InjectMocks | ||
private LangChain4JModuleFactory factory; | ||
|
||
@Test | ||
void shouldCreateModule() { | ||
JHipsterModuleProperties properties = JHipsterModulesFixture.propertiesBuilder(TestFileUtils.tmpDirForTest()) | ||
.projectBaseName("myApp") | ||
.build(); | ||
|
||
JHipsterModule module = factory.buildModule(properties); | ||
|
||
assertThatModuleWithFiles(module, pomFile()) | ||
.hasFile("pom.xml") | ||
.containing("<langchain4j.version>") | ||
.containing( | ||
""" | ||
<dependency> | ||
<groupId>dev.langchain4j</groupId> | ||
<artifactId>langchain4j-spring-boot-starter</artifactId> | ||
<version>${langchain4j.version}</version> | ||
</dependency> | ||
""" | ||
) | ||
.containing( | ||
""" | ||
<dependency> | ||
<groupId>dev.langchain4j</groupId> | ||
<artifactId>langchain4j-open-ai-spring-boot-starter</artifactId> | ||
<version>${langchain4j.version}</version> | ||
</dependency> | ||
""" | ||
) | ||
.and() | ||
.hasFile("src/main/resources/config/application.yml") | ||
.containing( | ||
""" | ||
langchain4j: | ||
open-ai: | ||
chat-model: | ||
# You can temporarily use 'demo' key, which is provided for free for demonstration purposes | ||
api-key: ${OPENAI_API_KEY} | ||
log-requests: 'true' | ||
log-responses: 'true' | ||
model-name: gpt-4o-mini | ||
""" | ||
); | ||
} | ||
} |