variableType,
@Nullable InvocationContext invocationContext);
+ /**
+ * Invokes this KernelFunction.
+ *
+ * If the {@code variableType} parameter is provided, the {@link ContextVariableType} is used to
+ * convert the result of the function to the appropriate {@link FunctionResult}. The
+ * {@code variableType} is not required for converting well-known types such as {@link String}
+ * and {@link Integer} which have pre-defined {@code ContextVariableType}s.
+ *
+ * The {@link InvocationContext} allows for customization of the behavior of function, including
+ * the ability to pass in {@link KernelHooks} {@link PromptExecutionSettings}, and
+ * {@link ToolCallBehavior}.
+ *
+ * The difference between calling the {@code KernelFunction.invokeAsync} method directly and
+ * calling the {@code Kernel.invokeAsync} method is that the latter adds the global KernelHooks
+ * (if any) to the {@link InvocationContext}. Calling {@code KernelFunction.invokeAsync}
+ * directly does not add the global hooks.
+ *
+ * @param kernel The Kernel containing services, plugins, and other state for use
+ * throughout the operation.
+ * @param arguments The arguments to pass to the function's invocation
+ * @param variableType The type of the {@link ContextVariable} returned in the
+ * {@link FunctionResult}
+ * @param invocationContext The arguments to pass to the function's invocation
+ * @return The result of the function's execution.
+ * @see FunctionResult#getResultVariable()
+ */
+ public FunctionResult invoke(
+ Kernel kernel,
+ @Nullable KernelFunctionArguments arguments,
+ @Nullable ContextVariableType variableType,
+ @Nullable InvocationContext invocationContext) {
+ return invokeAsync(kernel, arguments, variableType, invocationContext).block();
+ }
+
/**
* Invokes this KernelFunction.
*
@@ -202,6 +236,17 @@ public abstract class KernelFunction implements Buildable {
return new FunctionInvocation<>(kernel, this);
}
+ /**
+ * Invokes this KernelFunction.
+ *
+ * @param kernel The Kernel containing services, plugins, and other state for use throughout the
+ * operation.
+ * @return The result of the function's execution.
+ */
+ public FunctionResult invoke(Kernel kernel) {
+ return invokeAsync(kernel).block();
+ }
+
/**
* Builder for creating a {@link KernelFunction} from a prompt.
*
diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/KernelFunctionArguments.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/KernelFunctionArguments.java
index 99d26764df..2cb9baf1eb 100644
--- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/KernelFunctionArguments.java
+++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/KernelFunctionArguments.java
@@ -32,7 +32,7 @@ public class KernelFunctionArguments implements Buildable, Map> variables) {
if (variables == null) {
this.variables = new CaseInsensitiveMap<>();
@@ -46,7 +46,7 @@ public class KernelFunctionArguments implements Buildable, Map content) {
+ protected KernelFunctionArguments(@NonNull ContextVariable> content) {
this.variables = new CaseInsensitiveMap<>();
this.variables.put(MAIN_KEY, content);
}
@@ -54,7 +54,7 @@ public class KernelFunctionArguments implements Buildable, Map();
}
@@ -257,7 +257,10 @@ public class KernelFunctionArguments implements Buildable, Map> map) {
+ public Builder withVariables(@Nullable Map> map) {
+ if (map == null) {
+ return this;
+ }
variables.putAll(map);
return this;
}
diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/KernelFunctionFromMethod.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/KernelFunctionFromMethod.java
index d7b356d3ab..906fa33ba1 100644
--- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/KernelFunctionFromMethod.java
+++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/KernelFunctionFromMethod.java
@@ -563,7 +563,7 @@ public class KernelFunctionFromMethod extends KernelFunction implements Bu
@Nullable KernelFunctionArguments arguments,
@Nullable ContextVariableType variableType,
@Nullable InvocationContext invocationContext) {
- return function.invoke(kernel, this, arguments, variableType, invocationContext);
+ return function.invokeAsync(kernel, this, arguments, variableType, invocationContext);
}
/**
@@ -581,12 +581,32 @@ public class KernelFunctionFromMethod extends KernelFunction implements Bu
* @param invocationContext the invocation context
* @return a {@link Mono} that emits the result of the function invocation
*/
- Mono> invoke(
+ Mono> invokeAsync(
Kernel kernel,
KernelFunction function,
@Nullable KernelFunctionArguments arguments,
@Nullable ContextVariableType variableType,
@Nullable InvocationContext invocationContext);
+
+ /**
+ * Invokes the function.
+ *
+ * @param kernel the kernel to invoke the function on
+ * @param function the function to invoke
+ * @param arguments the arguments to the function
+ * @param variableType the variable type of the function
+ * @param invocationContext the invocation context
+ * @return a {@link Mono} that emits the result of the function invocation
+ */
+ default FunctionResult invoke(
+ Kernel kernel,
+ KernelFunction function,
+ @Nullable KernelFunctionArguments arguments,
+ @Nullable ContextVariableType variableType,
+ @Nullable InvocationContext invocationContext) {
+ return invokeAsync(kernel, function, arguments, variableType,
+ invocationContext).block();
+ }
}
/**
diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/KernelFunctionFromPrompt.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/KernelFunctionFromPrompt.java
index aeec1ca6eb..fe2c5cb649 100644
--- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/KernelFunctionFromPrompt.java
+++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/KernelFunctionFromPrompt.java
@@ -48,7 +48,7 @@ public class KernelFunctionFromPrompt extends KernelFunction {
* @param promptConfig the configuration for the prompt
* @param executionSettings the execution settings to use when invoking the function
*/
- public KernelFunctionFromPrompt(
+ protected KernelFunctionFromPrompt(
PromptTemplate template,
PromptTemplateConfig promptConfig,
@Nullable Map executionSettings) {
diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/PromptTemplateConfig.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/PromptTemplateConfig.java
index fcde2da7aa..b74f1984f8 100644
--- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/PromptTemplateConfig.java
+++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/PromptTemplateConfig.java
@@ -56,7 +56,7 @@ public class PromptTemplateConfig implements Buildable {
*
* @param template Template string
*/
- public PromptTemplateConfig(String template) {
+ protected PromptTemplateConfig(String template) {
this(
CURRENT_SCHEMA,
DEFAULT_CONFIG_NAME,
@@ -121,7 +121,7 @@ public class PromptTemplateConfig implements Buildable {
* @param outputVariable Output variable
* @param executionSettings Execution settings
*/
- public PromptTemplateConfig(
+ protected PromptTemplateConfig(
@Nullable String name,
@Nullable String template,
@Nullable String templateFormat,
diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/templateengine/handlebars/HandlebarsPromptTemplate.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/templateengine/handlebars/HandlebarsPromptTemplate.java
index 48951c5ae7..4ac3558ebd 100644
--- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/templateengine/handlebars/HandlebarsPromptTemplate.java
+++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/templateengine/handlebars/HandlebarsPromptTemplate.java
@@ -68,7 +68,7 @@ public class HandlebarsPromptTemplate implements PromptTemplate {
template);
if (arguments == null) {
- arguments = new KernelFunctionArguments();
+ arguments = KernelFunctionArguments.builder().build();
}
return handler.render(arguments);
}