Skip to content

Commit

Permalink
add parameter types to tool call arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
johnoliver committed Nov 11, 2024
1 parent 9219810 commit 3b40384
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -149,7 +150,13 @@ public Map<String, JsonNode> getProperties() {
private static String getSchemaForFunctionParameter(@Nullable InputVariable parameter) {
List<String> entries = new ArrayList<>();

entries.add("\"type\":\"string\"");
String type = "string";

if (parameter != null && parameter.getType() != null) {
type = getJavaTypeToOpenAiFunctionType(parameter.getType());
}

entries.add("\"type\":\"" + type + "\"");

// Add description if present
if (parameter != null && parameter.getDescription() != null && !parameter.getDescription()
Expand Down Expand Up @@ -179,4 +186,28 @@ private static String getSchemaForFunctionParameter(@Nullable InputVariable para

return "{" + schema + "}";
}

private static String getJavaTypeToOpenAiFunctionType(String javaType) {
switch (javaType.toLowerCase(Locale.ROOT)) {
case "boolean":
return "boolean";
case "integer":
case "int":
case "long":
case "short":
case "byte":
return "integer";
case "double":
case "float":
return "number";
case "string":
return "string";
case "array":
return "array";
case "void":
return "null";
default:
return "object";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public List<LightModel> getLights() {

@DefineKernelFunction(name = "change_state", description = "Changes the state of the light")
public LightModel changeState(
@KernelFunctionParameter(name = "id", description = "The ID of the light to change") int id,
@KernelFunctionParameter(name = "isOn", description = "The new state of the light") boolean isOn) {
@KernelFunctionParameter(name = "id", description = "The ID of the light to change", type = int.class) int id,
@KernelFunctionParameter(name = "isOn", description = "The new state of the light", type = boolean.class) boolean isOn) {
System.out.println("Changing light " + id + " " + isOn);
Optional<LightModel> light = lights.stream()
.filter(l -> l.getId() == id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ private static Object toObjectType(
ContextVariableTypeConverter<?> c = sourceType.getConverter();

Object converted = c.toObject(invocationContext.getContextVariableTypes(), sourceValue,
targetArgType);
targetArgType, false);
if (converted != null) {
return converted;
}
Expand Down

0 comments on commit 3b40384

Please # to comment.