From f59e19ce1bd66f3585b0b162da3da8663a93c72f Mon Sep 17 00:00:00 2001 From: John Oliver <1615532+johnoliver@users.noreply.github.com> Date: Thu, 5 Dec 2024 18:54:07 +0000 Subject: [PATCH] Fix bad type matching on java types Fix npe --- CHANGELOG.md | 4 ++++ .../aiservices/openai/chatcompletion/OpenAIFunction.java | 9 +++++++++ .../com/microsoft/semantickernel/hooks/KernelHook.java | 7 +++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aff20f82..592ca820 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.4.2 + +- Fix bug effecting using native Java methods with OpenAI tool calling + # 1.4.1 - Add Otel Telemetry on function invocations diff --git a/aiservices/openai/src/main/java/com/microsoft/semantickernel/aiservices/openai/chatcompletion/OpenAIFunction.java b/aiservices/openai/src/main/java/com/microsoft/semantickernel/aiservices/openai/chatcompletion/OpenAIFunction.java index 79c7d617..ad6dbadb 100644 --- a/aiservices/openai/src/main/java/com/microsoft/semantickernel/aiservices/openai/chatcompletion/OpenAIFunction.java +++ b/aiservices/openai/src/main/java/com/microsoft/semantickernel/aiservices/openai/chatcompletion/OpenAIFunction.java @@ -189,21 +189,30 @@ private static String getSchemaForFunctionParameter(@Nullable InputVariable para private static String getJavaTypeToOpenAiFunctionType(String javaType) { switch (javaType.toLowerCase(Locale.ROOT)) { + case "java.lang.boolean": case "boolean": return "boolean"; + case "java.lang.integer": case "integer": case "int": + case "java.lang.long": case "long": + case "java.lang.short": case "short": + case "java.lang.byte": case "byte": return "integer"; + case "java.lang.double": case "double": + case "java.lang.float": case "float": return "number"; + case "java.lang.string": case "string": return "string"; case "array": return "array"; + case "java.lang.void": case "void": return "null"; default: diff --git a/semantickernel-api/src/main/java/com/microsoft/semantickernel/hooks/KernelHook.java b/semantickernel-api/src/main/java/com/microsoft/semantickernel/hooks/KernelHook.java index f8f4b939..93842049 100644 --- a/semantickernel-api/src/main/java/com/microsoft/semantickernel/hooks/KernelHook.java +++ b/semantickernel-api/src/main/java/com/microsoft/semantickernel/hooks/KernelHook.java @@ -115,8 +115,11 @@ static ChatCompletionsOptions cloneOptionsWithMessages( .setN(options.getN()) .setResponseFormat(options.getResponseFormat()) .setSeed(options.getSeed()) - .setStream(options.isStream()) - .setToolChoice(options.getToolChoice()); + .setStream(options.isStream()); + + if (options.getToolChoice() != null) { + newOptions.setToolChoice(options.getToolChoice()); + } if (options.getFunctionCall() != null) { newOptions = newOptions.setFunctionCall(options.getFunctionCall());