Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using langchain4j module #11273

Merged
merged 12 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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.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 JHipsterSource SOURCE = from("server/springboot/langchain4j");
private static final GroupId LANGCHAIN4J_GROUP_ID = groupId("dev.langchain4j");
private static final VersionSlug LANGCHAIN4J_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(LANGCHAIN4J_GROUP_ID,
artifactId("langchain4j-spring-boot-starter"),
LANGCHAIN4J_VERSION_SLUG)
.addDependency(LANGCHAIN4J_GROUP_ID,
artifactId("langchain4j-open-ai-spring-boot-starter"),
LANGCHAIN4J_VERSION_SLUG)
.and()
.files()
.and()
.springMainProperties()
.set(propertyKey("langchain4j.open-ai.chat-model.api-key"), propertyValue("${OPENAI_API_KEY}"))
.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
}
}
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 Spring dependency")
.organization(JHipsterModuleOrganization.builder().addDependency(SPRING_BOOT).build())
.tags("server", "spring", "spring-boot", "langchain4j")
.factory(langChain4J::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.langchain4j;
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ public enum JHLiteModuleSlug implements JHipsterModuleSlugFactory {
VUE_OAUTH2_KEYCLOAK("vue-oauth2-keycloak"),
VUE_PINIA("vue-pinia"),
TS_PAGINATION_DOMAIN("ts-pagination-domain"),
TS_REST_PAGINATION("ts-rest-pagination");
TS_REST_PAGINATION("ts-rest-pagination"),
LANGCHAIN4J("langchain4j");

private static final Map<String, JHLiteModuleSlug> moduleSlugMap = Stream.of(values()).collect(
Collectors.toMap(JHLiteModuleSlug::get, Function.identity())
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/generator/dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<asciidoctor-maven-plugin.version>3.0.0</asciidoctor-maven-plugin.version>
<openapi-maven-plugin.version>0.0.21</openapi-maven-plugin.version>
<openapi-backwards-compat-maven-plugin.version>1.0.3</openapi-backwards-compat-maven-plugin.version>
<langchain4j.version>0.35.0</langchain4j.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -406,6 +407,16 @@
<artifactId>openapi-backwards-compat-maven-plugin</artifactId>
<version>${openapi-backwards-compat-maven-plugin.version}</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-spring-boot-starter</artifactId>
<version>${langchain4j.version}</version>
</dependency>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
<version>${langchain4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# LangChain4J
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# LangChain4J
# LangChain4j

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I do that mistake all the time!!!!!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CamelCase syndrome 😄


How to use it :

[Official documentation](https://docs.langchain4j.dev/tutorials/spring-boot-integration/)
10 changes: 10 additions & 0 deletions src/test/features/server/springboot/langchain4j.feature
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 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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:
api-key: ${OPENAI_API_KEY}
log-requests: 'true'
log-responses: 'true'
model-name: gpt-4o-mini
"""
);
}
}