Class KernelFunction<T>
- java.lang.Object
-
- com.microsoft.semantickernel.semanticfunctions.KernelFunction<T>
-
- Type Parameters:
T- The type of the result of the function
- Direct Known Subclasses:
KernelFunctionFromMethod,KernelFunctionFromPrompt
public abstract class KernelFunction<T> extends Object
Semantic Kernel callable function interface. The Semantic Kernel createsKernelFunctions from prompts, templates, or plugin methods.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceKernelFunction.FromPromptBuilder<T>Builder for creating aKernelFunctionfrom a prompt.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static <T> KernelFunctionFromMethod.Builder<T>createFromMethod(Method method, Object target)Creates aKernelFunctioninstance for a method, specified via aMethodinstancestatic <T> KernelFunction.FromPromptBuilder<T>createFromPrompt(PromptTemplateConfig promptTemplateConfiguration)Builder for creating aKernelFunctioninstance for a givenPromptTemplateConfig.static <T> KernelFunction.FromPromptBuilder<T>createFromPrompt(String prompt)Creates aKernelFunctioninstance based on a given promptStringgetDescription()Map<String,PromptExecutionSettings>getExecutionSettings()Get an unmodifiable map of the execution settings for the function.KernelFunctionMetadata<?>getMetadata()Get the metadata for the function.StringgetName()StringgetPluginName()FunctionResult<T>invoke(Kernel kernel)Invokes this KernelFunction.FunctionResult<T>invoke(Kernel kernel, KernelFunctionArguments arguments, ContextVariableType<T> variableType, InvocationContext invocationContext)Invokes this KernelFunction.FunctionInvocation<T>invokeAsync(Kernel kernel)Invokes this KernelFunction.abstract reactor.core.publisher.Mono<FunctionResult<T>>invokeAsync(Kernel kernel, KernelFunctionArguments arguments, ContextVariableType<T> variableType, InvocationContext invocationContext)Invokes this KernelFunction.StringtoEmbeddingString()Create a string for generating an embedding for a function.StringtoManualString(boolean includeOutputs)Create a manual-friendly string for a function.
-
-
-
Method Detail
-
createFromMethod
public static <T> KernelFunctionFromMethod.Builder<T> createFromMethod(Method method, Object target)
Creates aKernelFunctioninstance for a method, specified via aMethodinstance- Type Parameters:
T- The return type of the method.- Parameters:
method- The method to be represented via the createdKernelFunction.target- The target object for themethodif it represents an instance method. This should benullif and only ifmethodis a static method.- Returns:
- The created
KernelFunctionwrapper formethod.
-
createFromPrompt
public static <T> KernelFunction.FromPromptBuilder<T> createFromPrompt(String prompt)
Creates aKernelFunctioninstance based on a given prompt- Type Parameters:
T- The return type of the method- Parameters:
prompt- The prompt to be used for the createdKernelFunction.- Returns:
- The builder for creating a
KernelFunctioninstance.
-
createFromPrompt
public static <T> KernelFunction.FromPromptBuilder<T> createFromPrompt(PromptTemplateConfig promptTemplateConfiguration)
Builder for creating aKernelFunctioninstance for a givenPromptTemplateConfig.- Type Parameters:
T- The return type of the method- Parameters:
promptTemplateConfiguration- The configuration for the prompt template.- Returns:
- The builder for creating a
KernelFunctioninstance.
-
getPluginName
@Nullable public String getPluginName()
- Returns:
- The name of the plugin that this function is within
-
getName
public String getName()
- Returns:
- The name of this function
-
toEmbeddingString
public String toEmbeddingString()
Create a string for generating an embedding for a function.- Returns:
- A string for generating an embedding for a function.
-
toManualString
public String toManualString(boolean includeOutputs)
Create a manual-friendly string for a function.- Parameters:
includeOutputs- Whether to include function outputs in the string.- Returns:
- A manual-friendly string for a function.
-
getExecutionSettings
public Map<String,PromptExecutionSettings> getExecutionSettings()
Get an unmodifiable map of the execution settings for the function.- Returns:
- An unmodifiable map of the execution settings for the function
-
getMetadata
public KernelFunctionMetadata<?> getMetadata()
Get the metadata for the function.- Returns:
- The metadata for the function
-
invokeAsync
public abstract reactor.core.publisher.Mono<FunctionResult<T>> invokeAsync(Kernel kernel, @Nullable KernelFunctionArguments arguments, @Nullable ContextVariableType<T> variableType, @Nullable InvocationContext invocationContext)
Invokes this KernelFunction.If the
variableTypeparameter is provided, theContextVariableTypeis used to convert the result of the function to the appropriateFunctionResult. ThevariableTypeis not required for converting well-known types such asStringandIntegerwhich have pre-definedContextVariableTypes.The
InvocationContextallows for customization of the behavior of function, including the ability to pass inKernelHooksPromptExecutionSettings, andToolCallBehavior.The difference between calling the
KernelFunction.invokeAsyncmethod directly and calling theKernel.invokeAsyncmethod is that the latter adds the global KernelHooks (if any) to theInvocationContext. CallingKernelFunction.invokeAsyncdirectly does not add the global hooks.- Parameters:
kernel- The Kernel containing services, plugins, and other state for use throughout the operation.arguments- The arguments to pass to the function's invocationvariableType- The type of theContextVariablereturned in theFunctionResultinvocationContext- The arguments to pass to the function's invocation- Returns:
- The result of the function's execution.
- See Also:
FunctionResult.getResultVariable()
-
invoke
public FunctionResult<T> invoke(Kernel kernel, @Nullable KernelFunctionArguments arguments, @Nullable ContextVariableType<T> variableType, @Nullable InvocationContext invocationContext)
Invokes this KernelFunction.If the
variableTypeparameter is provided, theContextVariableTypeis used to convert the result of the function to the appropriateFunctionResult. ThevariableTypeis not required for converting well-known types such asStringandIntegerwhich have pre-definedContextVariableTypes.The
InvocationContextallows for customization of the behavior of function, including the ability to pass inKernelHooksPromptExecutionSettings, andToolCallBehavior.The difference between calling the
KernelFunction.invokeAsyncmethod directly and calling theKernel.invokeAsyncmethod is that the latter adds the global KernelHooks (if any) to theInvocationContext. CallingKernelFunction.invokeAsyncdirectly does not add the global hooks.- Parameters:
kernel- The Kernel containing services, plugins, and other state for use throughout the operation.arguments- The arguments to pass to the function's invocationvariableType- The type of theContextVariablereturned in theFunctionResultinvocationContext- The arguments to pass to the function's invocation- Returns:
- The result of the function's execution.
- See Also:
FunctionResult.getResultVariable()
-
invokeAsync
public FunctionInvocation<T> invokeAsync(Kernel kernel)
Invokes this KernelFunction.- Parameters:
kernel- The Kernel containing services, plugins, and other state for use throughout the operation.- Returns:
- The result of the function's execution.
-
invoke
public FunctionResult<T> invoke(Kernel kernel)
Invokes this KernelFunction.- Parameters:
kernel- The Kernel containing services, plugins, and other state for use throughout the operation.- Returns:
- The result of the function's execution.
-
-