Skip to content

Commit 54e18da

Browse files
committed
update tool spec
1 parent 85a807f commit 54e18da

File tree

21 files changed

+121
-53
lines changed

21 files changed

+121
-53
lines changed

code-generator/cli/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.javaaidev.easyllmtools</groupId>
88
<artifactId>code-generator</artifactId>
9-
<version>0.1.4</version>
9+
<version>0.1.5</version>
1010
</parent>
1111

1212
<artifactId>code-generator-cli</artifactId>

code-generator/cli/src/main/kotlin/com/javaaidev/easyllmtools/codegenerator/cli/CodeGeneratorCli.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import kotlin.system.exitProcess
88
@CommandLine.Command(
99
name = "easy-llm-tools",
1010
mixinStandardHelpOptions = true,
11-
version = ["0.1.4"],
11+
version = ["0.1.5"],
1212
description = ["Generate code for LLM tools"],
1313
scope = CommandLine.ScopeType.INHERIT,
1414
subcommands = [

code-generator/openapi/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.javaaidev.easyllmtools</groupId>
88
<artifactId>code-generator</artifactId>
9-
<version>0.1.4</version>
9+
<version>0.1.5</version>
1010
</parent>
1111

1212
<artifactId>code-generator-openapi</artifactId>

code-generator/openapi/src/main/resources/Java/libraries/okhttp-gson/pom.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@
392392
<dependency>
393393
<groupId>com.javaaidev.easyllmtools</groupId>
394394
<artifactId>llm-tool-spec</artifactId>
395-
<version>0.1.4</version>
395+
<version>0.1.5</version>
396396
</dependency>
397397
<!-- test dependencies -->
398398
<dependency>

code-generator/openapi/src/main/resources/Java/llmToolkit.mustache

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import {{invokerPackage}}.ApiClient;
4141

4242
import com.javaaidev.easyllmtools.llmtoolspec.Tool;
4343
import com.javaaidev.easyllmtools.llmtoolspec.Toolkit;
44-
import com.javaaidev.easyllmtools.llmtoolspec.ToolFactory;
44+
import com.javaaidev.easyllmtools.llmtoolspec.ConfigurableToolFactory;
4545

4646
{{>generatedAnnotation}}
4747
{{#operations}}
@@ -173,11 +173,16 @@ public class {{classname}}LLMToolkit implements Toolkit {
173173
}
174174
}
175175

176-
public static class LLMToolFactory_{{operationId}} implements ToolFactory<LLMTool_{{operationId}}, {{classname}}ToolConfiguration> {
176+
public static class LLMToolFactory_{{operationId}} implements ConfigurableToolFactory<Request_{{operationId}}, Response_{{operationId}}, LLMTool_{{operationId}}, {{classname}}ToolConfiguration> {
177177
@Override
178178
public LLMTool_{{operationId}} create({{classname}}ToolConfiguration config) {
179179
return new LLMTool_{{operationId}}(config);
180180
}
181+
182+
@Override
183+
public String toolId() {
184+
return "{{operationId}}";
185+
}
181186
}
182187

183188
{{/operation}}

code-generator/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.javaaidev.easyllmtools</groupId>
88
<artifactId>easyllmtools-parent</artifactId>
9-
<version>0.1.4</version>
9+
<version>0.1.5</version>
1010
</parent>
1111

1212
<artifactId>code-generator</artifactId>

code-generator/shared/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.javaaidev.easyllmtools</groupId>
88
<artifactId>code-generator</artifactId>
9-
<version>0.1.4</version>
9+
<version>0.1.5</version>
1010
</parent>
1111

1212
<artifactId>code-generator-shared</artifactId>

code-generator/simple/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.javaaidev.easyllmtools</groupId>
88
<artifactId>code-generator</artifactId>
9-
<version>0.1.4</version>
9+
<version>0.1.5</version>
1010
</parent>
1111

1212
<artifactId>code-generator-simple</artifactId>

code-generator/simple/src/main/kotlin/com/javaaidev/easyllmtools/codegenerator/simple/SimpleCodeGenerator.kt

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import org.apache.commons.text.CaseUtils
1212
import org.apache.commons.text.StringEscapeUtils
1313
import org.jsonschema2pojo.*
1414
import org.jsonschema2pojo.rules.RuleFactory
15+
import org.slf4j.LoggerFactory
1516
import java.io.File
1617
import java.nio.file.Files
1718
import java.nio.file.Path
1819
import java.util.function.Supplier
20+
import kotlin.io.path.exists
1921

2022
data class Definition(
2123
val id: String,
@@ -34,21 +36,25 @@ data class TemplateContext(
3436
val specPackageName: String,
3537
val generatedSpecPackageName: String,
3638
val toolClassName: String,
39+
val hasConfiguration: Boolean,
3740
val configurationClassName: String,
3841
val parametersClassName: String,
3942
val returnTypeClassName: String,
43+
val toolFactoryClassName: String,
4044
val definition: Definition,
4145
)
4246

4347
data class GeneratedFile(
4448
val templateName: String,
4549
val dir: Path,
50+
val overwrite: Boolean = true,
4651
val fileNameSupplier: Supplier<String>,
4752
)
4853

4954
object SimpleCodeGenerator {
5055
private val objectMapper = ObjectMapper()
5156
private val handlebars: Handlebars
57+
private val logger = LoggerFactory.getLogger(SimpleCodeGenerator::class.java)
5258

5359
init {
5460
val loader = ClassPathTemplateLoader()
@@ -65,13 +71,14 @@ object SimpleCodeGenerator {
6571
artifactId: String,
6672
artifactVersion: String,
6773
packageName: String,
68-
outputDir: Path
74+
outputDir: Path,
75+
overwriteToolImplementation: Boolean,
6976
) {
77+
logger.info("Generate code from {}", inputSpec.toPath().toAbsolutePath().normalize())
7078
val jsonNode = objectMapper.readTree(inputSpec)
7179
val definitionNode =
7280
jsonNode.get("definition") ?: throw InvalidToolSpecException("Definition is a required")
7381
val name = definitionNode.get("name")?.textValue() ?: ""
74-
val id = definitionNode.get("id")?.textValue() ?: name
7582
val description = definitionNode.get("description")?.textValue() ?: ""
7683
val output = Files.createTempDirectory("tool_")
7784
val sourceRootDir = outputDir.resolve(Path.of("src", "main", "java"))
@@ -84,18 +91,41 @@ object SimpleCodeGenerator {
8491
val configurationJson = jsonNodeString(jsonNode.get("configuration"))
8592
val modelPackageName = "$packageName.model"
8693
val toolName = CaseUtils.toCamelCase(name, true)
87-
val configurationClassName = "${toolName}Configuration"
88-
val parametersClassName = "${toolName}Parameters"
89-
val returnTypeClassName = "${toolName}ReturnType"
90-
jsonNodeToCode(output, sourceRootDir, parametersJson, parametersClassName, modelPackageName)
91-
jsonNodeToCode(output, sourceRootDir, returnTypeJson, returnTypeClassName, modelPackageName)
92-
jsonNodeToCode(
93-
output,
94-
sourceRootDir,
95-
configurationJson,
96-
configurationClassName,
97-
modelPackageName
98-
)
94+
val toolId = definitionNode.get("id")?.textValue() ?: toolName
95+
val hasConfiguration = notEmptyJson(configurationJson)
96+
val configurationClassName =
97+
if (hasConfiguration) "${toolName}Configuration" else "Void"
98+
val parametersClassName =
99+
if (notEmptyJson(parametersJson)) "${toolName}Parameters" else "Void"
100+
val returnTypeClassName =
101+
if (notEmptyJson(returnTypeJson)) "${toolName}ReturnType" else "Void"
102+
if (notEmptyJson(parametersJson)) {
103+
jsonNodeToCode(
104+
output,
105+
sourceRootDir,
106+
parametersJson,
107+
parametersClassName,
108+
modelPackageName
109+
)
110+
}
111+
if (notEmptyJson(returnTypeJson)) {
112+
jsonNodeToCode(
113+
output,
114+
sourceRootDir,
115+
returnTypeJson,
116+
returnTypeClassName,
117+
modelPackageName
118+
)
119+
}
120+
if (hasConfiguration) {
121+
jsonNodeToCode(
122+
output,
123+
sourceRootDir,
124+
configurationJson,
125+
configurationClassName,
126+
modelPackageName
127+
)
128+
}
99129
val context = TemplateContext(
100130
groupId,
101131
artifactId,
@@ -104,11 +134,13 @@ object SimpleCodeGenerator {
104134
"com.javaaidev.easyllmtools.llmtoolspec",
105135
modelPackageName,
106136
toolName,
137+
hasConfiguration,
107138
configurationClassName,
108139
parametersClassName,
109140
returnTypeClassName,
141+
if (hasConfiguration) "ConfigurableToolFactory" else "UnconfigurableToolFactory",
110142
Definition(
111-
toolName,
143+
toolId,
112144
toolName,
113145
description,
114146
parametersJson,
@@ -123,7 +155,7 @@ object SimpleCodeGenerator {
123155
val files = listOf(
124156
GeneratedFile("abstractTool", sourceDir) { "Abstract${toolName}.java" },
125157
GeneratedFile("toolFactory", sourceDir) { "${toolName}Factory.java" },
126-
GeneratedFile("tool", sourceDir) { "${toolName}.java" },
158+
GeneratedFile("tool", sourceDir, overwriteToolImplementation) { "${toolName}.java" },
127159
GeneratedFile("pom", outputDir) { "pom.xml" },
128160
GeneratedFile(
129161
"services",
@@ -132,9 +164,15 @@ object SimpleCodeGenerator {
132164
)
133165

134166
files.forEach { file ->
167+
val outputFile = file.dir.resolve(file.fileNameSupplier.get())
168+
if (outputFile.exists() && !file.overwrite) {
169+
logger.info("Skip generation of tool implementation")
170+
return
171+
}
135172
val template = handlebars.compile(file.templateName)
136173
template.apply(context).run {
137-
Files.writeString(file.dir.resolve(file.fileNameSupplier.get()), this)
174+
logger.info("Created file {}", outputFile.toAbsolutePath().normalize())
175+
Files.writeString(outputFile, this)
138176
}
139177
}
140178

@@ -212,4 +250,6 @@ object SimpleCodeGenerator {
212250
Files.createDirectories(output)
213251
codeModel.build(output.toFile())
214252
}
253+
254+
private fun notEmptyJson(json: String) = json.isNotEmpty() || json != "{}"
215255
}

code-generator/simple/src/main/kotlin/com/javaaidev/easyllmtools/codegenerator/simple/SimpleCodeGeneratorCommand.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ class SimpleCodeGeneratorCommand : Callable<Int> {
1010
@CommandLine.Mixin
1111
lateinit var options: ReusableOptions
1212

13+
@CommandLine.Option(
14+
names = ["--overwrite-tool-implementation"],
15+
defaultValue = "false",
16+
description = ["Should tool implementation be overwritten"]
17+
)
18+
var overwriteToolImplementation: Boolean = false
19+
1320
override fun call(): Int {
1421
SimpleCodeGenerator.generate(
1522
options.inputSpec,
1623
options.groupId,
1724
options.artifactId,
1825
options.artifactVersion,
1926
options.packageName,
20-
options.outputDir.toPath()
27+
options.outputDir.toPath(),
28+
overwriteToolImplementation,
2129
)
2230
return 0
2331
}

0 commit comments

Comments
 (0)