跳到主要内容

Simple

Simple code generator generates code for a tool from its spec.

Options of simple code generator
Usage: easy-llm-tools simple [-hV] [--overwrite-pom-file]
[--overwrite-tool-implementation]
[--artifact-id=<artifactId>]
[--artifact-version=<artifactVersion>]
[--group-id=<groupId>] [--output=<outputDir>]
[--package-name=<packageName>]
[--parent-artifact-id=<parentArtifactId>]
[--parent-artifact-version=<parentArtifactVersion>]
[--parent-group-id=<parentGroupId>]
[--tool-id-prefix=<toolIdPrefix>] <inputSpec>
Generate simple tool implementation
<inputSpec>
--artifact-id=<artifactId>
Artifact id
--artifact-version=<artifactVersion>
Artifact version
--group-id=<groupId> Group id
-h, --help Show this help message and exit.
--output=<outputDir> Output directory
--overwrite-pom-file Should pom file be overwritten
--overwrite-tool-implementation
Should tool implementation be overwritten
--package-name=<packageName>
Package name
--parent-artifact-id=<parentArtifactId>
Parent artifact id
--parent-artifact-version=<parentArtifactVersion>
Parent artifact version
--parent-group-id=<parentGroupId>
Parent group id
--tool-id-prefix=<toolIdPrefix>
Prefix of generated tool id
-V, --version Print version information and exit.

Below is the spec of a tool to get weather by location.

  • This tool has a single parameter location representing the location to get weather.
  • This tool returns an object contains condition, temperature, and temperatureUnit properties.
  • This tool allows configuring the unit of temperature.
Spec of get weather tool
{
"definition": {
"name": "Get weather",
"description": "Get weather by location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "Location"
}
}
},
"returnType": {
"type": "object",
"properties": {
"condition": {
"type": "string",
"description": "Weather condition"
},
"temperature": {
"type": "number",
"description": "Temperature"
},
"temperatureUnit": {
"type": "string",
"description": "Unit of temperature",
"enum": ["C", "F"]
}
}
},
"examples": [
{
"description": "Get weather of New York",
"parameters": {
"location": "New York"
},
"returnValue": {
"condition": "Sunny",
"temperature": 28,
"temperatureUnit": "C"
}
}
]
},
"configuration": {
"type": "object",
"properties": {
"temperatureUnit": {
"type": "string",
"description": "Unit of temperature",
"enum": ["C", "F"]
}
}
}
}

By using the command line tool, we can generate source code for this tool.

Use CLI to generate source code
java -jar code-generator-cli.jar simple \
--output=tool-get-weather-fake \
--artifact-id=get-weather --artifact-version=0.1.0 \
--package-name=com.javaaidev.easyllmtools.tools.getweather \
get-weather.json

The generated source code can be found at here.

Generated source code contains the model classes for parameters, return type and configuration. After the code is generated, we only need to implement the logic in the Tool interface.

Generated source code
.
├── AbstractGetWeather.java
├── GetWeather.java
├── GetWeatherFactory.java
└── model
├── GetWeatherConfiguration.java
├── GetWeatherParameters.java
└── GetWeatherReturnType.java

Below is an example implementation of this tool. It simply returns fake weather data.

Tool implementation
package com.javaaidev.easyllmtools.tools.getweather;

import com.javaaidev.easyllmtools.tools.getweather.model.GetWeatherConfiguration;
import com.javaaidev.easyllmtools.tools.getweather.model.GetWeatherParameters;
import com.javaaidev.easyllmtools.tools.getweather.model.GetWeatherReturnType;
import com.javaaidev.easyllmtools.tools.getweather.model.GetWeatherReturnType.TemperatureUnit;
import java.lang.reflect.Type;

public class GetWeather extends AbstractGetWeather {

public GetWeather(final GetWeatherConfiguration config) {
super(config);
}

@Override
public Type getRequestType() {
return GetWeatherParameters.class;
}

@Override
public GetWeatherReturnType call(final GetWeatherParameters parameters) {
GetWeatherReturnType result = new GetWeatherReturnType();
result.setCondition("Sunny");
result.setTemperature(28.0d);
result.setTemperatureUnit(TemperatureUnit.C);
return result;
}
}