Skip to content

Commit

Permalink
adjust gradle
Browse files Browse the repository at this point in the history
  • Loading branch information
hide212131 committed Mar 23, 2024
1 parent 4afd982 commit 904ca4a
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 39 deletions.
10 changes: 10 additions & 0 deletions .blueprint/generate-sample/templates/samples/sample_gradle.jdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
application {
config {
applicationType monolith
buildTool gradle
databaseType no
clientFramework react
enableSwaggerCodegen true
cacheProvider no
}
}
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ In the near future, we plan to support:
- RAG support using VectorDB
- [Langchain for Java](https://github.com/langchain4j/langchain4j) support
- Other Chat UI support
- gradle

# Prerequisites
- Java 17 or later
Expand All @@ -46,7 +45,7 @@ If you want to create the simplest application, execute the following command. A
```bash
mkdir myLlmApp
cd myLlmApp
jhipster-llm generate-sample sample.jdl
jhipster-llm generate-sample sample.jdl # This is for maven. For gradle, please use 'sample_gradle.jdl' instead.
```
(Note: Currently, only the minimal JHipster application configuration written in `.blueprint/generate-sample/templates/samples/sample.jdl` is supported. We plan to support various other configurations in the future.)

Expand Down
43 changes: 36 additions & 7 deletions generators/spring-ai-llamacpp/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export default class extends BaseApplicationGenerator {
...versions,
});
},
loadDependabot() {
const { devDependencies } = this.fs.readJSON(this.templatePath('../resources/package.json'));
this.addDevDependencies = devDependencies;
},
});
}

Expand Down Expand Up @@ -72,6 +76,10 @@ export default class extends BaseApplicationGenerator {
'service/llm/LlamaCppChatClient.java',
],
},
{
condition: generator => generator.buildToolGradle,
templates: ['gradle/llm.gradle'],
},
],
},
context: application,
Expand All @@ -83,6 +91,11 @@ export default class extends BaseApplicationGenerator {

get [BaseApplicationGenerator.POST_WRITING]() {
return this.asPostWritingTaskGroup({
async customizePackageJson() {
this.packageJson.merge({
devDependencies: this.addDevDependencies,
});
},
async customizeApplicationYml({ application: { llmModelName } }) {
this.editFile(`src/main/resources/config/application-dev.yml`, { ignoreNonExisting: true }, content =>
content.replace(
Expand All @@ -99,16 +112,18 @@ export default class extends BaseApplicationGenerator {
),
);
},
async customizeBuildTool({ source, application: { buildToolMaven, javaDependencies, llmModelName, llmModelUrl } }) {
async customizeBuildTool({ source, application: { buildToolMaven, buildToolGradle, javaDependencies, llmModelName, llmModelUrl } }) {
if (buildToolMaven) {
source.addMavenProperty?.({ property: 'java-llamacpp.version', value: javaDependencies['java-llamacpp'] });
source.addMavenProperty?.({ property: 'llm.model.home', value: '${project.basedir}/models' });
source.addMavenProperty?.({ property: 'llm.model.name', value: llmModelName });
source.addMavenProperty?.({ property: 'llm.model.url', value: llmModelUrl });
source.addMavenProperty?.([
{ property: 'javaLlamacpp.version', value: javaDependencies['javaLlamacpp'] },
{ property: 'llm.model.home', value: 'models' },
{ property: 'llm.model.name', value: llmModelName },
{ property: 'llm.model.url', value: llmModelUrl },
]);
source.addMavenDependency?.({
groupId: 'de.kherud',
artifactId: 'llama',
version: '${java-llamacpp.version}',
version: '${javaLlamacpp.version}',
});
source.addMavenPlugin?.({
additionalContent: `
Expand All @@ -125,12 +140,26 @@ export default class extends BaseApplicationGenerator {
<goal>npm</goal>
</goals>
<configuration>
<arguments>run llm:download-model -- \${llm.model.home} \${llm.model.name} \${llm.model.url}</arguments>
<arguments>run llm:download-model -- \${project.basedir}/\${llm.model.home} \${llm.model.name} \${llm.model.url}</arguments>
</configuration>
</execution>
</executions>
`,
});
} else if (buildToolGradle) {
[
{ property: 'javaLlamacppVersion', value: javaDependencies['javaLlamacpp'] },
{ property: 'llm.model.home', value: 'models' },
{ property: 'llm.model.name', value: llmModelName },
{ property: 'llm.model.url', value: llmModelUrl },
].forEach(({ property, value }) => source.addGradleProperty?.({ property, value }));
source.addGradleDependency?.({
groupId: 'de.kherud',
artifactId: 'llama',
version: '${javaLlamacppVersion}',
scope: 'implementation',
});
source.applyFromGradle?.({ script: 'gradle/llm.gradle' });
}
},
});
Expand Down
6 changes: 6 additions & 0 deletions generators/spring-ai-llamacpp/resources/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"devDependencies": {
"node-fetch": "3.3.2",
"readable-stream": "4.5.2"
}
}
4 changes: 2 additions & 2 deletions generators/spring-ai-llamacpp/resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

<properties>
<!-- Dependency versions -->
<java-llamacpp.version>2.3.5</java-llamacpp.version>
<javaLlamacpp.version>2.3.5</javaLlamacpp.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>de.kherud</groupId>
<artifactId>llama</artifactId>
<version>${java-llamacpp.version}</version>
<version>${javaLlamacpp.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down
9 changes: 9 additions & 0 deletions generators/spring-ai-llamacpp/templates/gradle/llm.gradle.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node {
download = true
}

task downloadLLMModel(type: NpmTask) {
args = ['run', 'llm:download-model', '--', "${projectDir}/${project.findProperty('llm.model.home')}", project.findProperty('llm.model.name'), project.findProperty('llm.model.url')]
}

processResources.dependsOn downloadLLMModel
34 changes: 19 additions & 15 deletions generators/spring-ai-ollama/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export default class extends BaseApplicationGenerator {
path: 'src/main/docker',
templates: ['ollama.yml'],
},
{
condition: generator => generator.buildToolGradle,
templates: ['gradle/llm.gradle'],
},
],
},
context: application,
Expand All @@ -61,22 +65,13 @@ export default class extends BaseApplicationGenerator {

get [BaseApplicationGenerator.POST_WRITING]() {
return this.asPostWritingTaskGroup({
async customizeDockerServices() {
this.editFile(`src/main/docker/services.yml`, { ignoreNonExisting: true }, contents =>
contents.replace(
'\nservices:',
`\nservices:
ollama:
extends:
file: ./ollama.yml
service: ollama
`,
),
);
async customizeDockerServices({ source }) {
source.addDockerExtendedServiceToApplicationAndServices({ serviceName: 'ollama' });
},
async customizePackageJson() {
async customizePackageJson({ application }) {
this.packageJson.merge({
scripts: {
'services:up': `docker compose -f ${application.dockerServicesDir}services.yml up --wait`,
'llm:ollama': 'docker exec -i ollama ollama',
},
});
Expand All @@ -93,13 +88,13 @@ export default class extends BaseApplicationGenerator {
),
);
},
async customizeBuildTool({ source, application: { buildToolMaven, llmModelName } }) {
async customizeBuildTool({ source, application: { buildToolMaven, buildToolGradle, llmModelName } }) {
if (buildToolMaven) {
source.addMavenProperty?.({ property: 'llm.model.name', value: llmModelName });
source.addMavenDependency?.({
groupId: 'org.springframework.ai',
artifactId: 'spring-ai-ollama-spring-boot-starter',
version: '${spring-ai.version}',
version: '${springAi.version}',
});
source.addMavenPlugin?.({
additionalContent: `
Expand All @@ -122,6 +117,15 @@ export default class extends BaseApplicationGenerator {
</executions>
`,
});
} else if (buildToolGradle) {
source.addGradleProperty?.({ property: 'llm.model.name', value: llmModelName });
source.addGradleDependency?.({
groupId: 'org.springframework.ai',
artifactId: 'spring-ai-ollama-spring-boot-starter',
version: '${springAiVersion}',
scope: 'implementation',
});
source.applyFromGradle?.({ script: 'gradle/llm.gradle' });
}
},
});
Expand Down
9 changes: 9 additions & 0 deletions generators/spring-ai-ollama/templates/gradle/llm.gradle.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node {
download = true
}

task downloadLLMModel(type: NpmTask) {
args = ['run', 'llm:ollama', 'pull', project.findProperty('llm.model.name')]
}

processResources.dependsOn downloadLLMModel
39 changes: 36 additions & 3 deletions generators/spring-ai/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ openapi.my-llm-app.base-path: /api/llm/v1
async customizeBuildTool({ source, application: { buildToolMaven, buildToolGradle, javaDependencies, reactive } }) {
// add required third party dependencies
if (buildToolMaven) {
source.addMavenProperty?.({ property: 'spring-ai.version', value: javaDependencies['spring-ai'] });
source.addMavenProperty?.({ property: 'springAi.version', value: javaDependencies['springAi'] });
source.addMavenRepository?.([
{
id: 'spring-milestones',
Expand All @@ -174,7 +174,7 @@ openapi.my-llm-app.base-path: /api/llm/v1
source.addMavenDependency?.({
groupId: 'org.springframework.ai',
artifactId: 'spring-ai-core',
version: '${spring-ai.version}',
version: '${springAi.version}',
});
this.editFile(`pom.xml`, content => {
if (content.includes('<artifactId>openapi-generator-maven-plugin</artifactId>')) {
Expand All @@ -196,7 +196,40 @@ openapi.my-llm-app.base-path: /api/llm/v1
return content;
});
} else if (buildToolGradle) {
// TODO
source.addGradleProperty?.({ property: 'springAiVersion', value: javaDependencies['springAi'] });
[
{
id: 'spring-milestones',
name: 'Spring Milestones',
url: 'https://repo.spring.io/milestone',
},
{
id: 'spring-snapshots',
name: 'Spring Snapshots',
url: 'https://repo.spring.io/snapshot',
},
].forEach(repo => source.addGradleMavenRepository?.(repo));
source.addGradleDependency?.({
groupId: 'org.springframework.ai',
artifactId: 'spring-ai-core',
version: '${springAiVersion}',
scope: 'implementation',
});
this.editFile(`gradle/swagger.gradle`, { ignoreNonExisting: true }, content => {
content = content.replace(
'openApiGenerate {',
`openApiGenerate {
typeMappings = [
"integer": "Long",
"int" : "Long"
]`,
);
content = content.replace(
'configOptions = [',
`configOptions = [interfaceOnly: "true", openApiNullable: "false", ${!reactive ? 'reactive: "true", ' : ''}`, // for chat stream
);
return content;
});
}
},
});
Expand Down
10 changes: 2 additions & 8 deletions generators/spring-ai/resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,15 @@

<properties>
<!-- Dependency versions -->
<spring-ai.version>0.8.0</spring-ai.version>
<java-llamacpp.version>2.3.5</java-llamacpp.version>
<springAi.version>0.8.0</springAi.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-core</artifactId>
<version>${spring-ai.version}</version>
</dependency>
<dependency>
<groupId>de.kherud</groupId>
<artifactId>llama</artifactId>
<version>${java-llamacpp.version}</version>
<version>${springAi.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
Expand Down
4 changes: 2 additions & 2 deletions generators/spring-ai/templates/build-chat.mjs.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ async function buildChat() {
try {
const rootDir = process.cwd();
// Ensure the target/better-chat directory exists
const targetDir = path.join(rootDir, 'target', 'better-chat');
const targetDir = path.join(rootDir, '<%= this.relativeDir(clientRootDir, temporaryDir) %>', 'better-chat');
// Clear the directory if it exists
fs.emptyDirSync(targetDir);

Expand All @@ -22,7 +22,7 @@ async function buildChat() {

// Copy dist/ to target/classes/static/chat-ui
const distDir = path.join(targetDir, 'dist');
const staticDir = path.join(rootDir, 'target/classes/static/chat-ui');
const staticDir = path.join(rootDir, '<%= this.relativeDir(clientRootDir, clientDistDir) %>chat-ui');
fs.copySync(distDir, staticDir);
} catch (error) {
console.error(`Error occurred: ${error.message}`);
Expand Down

0 comments on commit 904ca4a

Please sign in to comment.