Share via


KernelFunction<T> Class

  • java.lang.Object
    • com.microsoft.semantickernel.semanticfunctions.KernelFunction<T>

Type Parameters

T

The type of the result of the function

public abstract class KernelFunction

Semantic Kernel callable function interface. The Semantic Kernel creates KernelFunctions from prompts, templates, or plugin methods.

Constructor Summary

Modifier Constructor Description
protected KernelFunction(KernelFunctionMetadata<?> metadata, Map<String,PromptExecutionSettings> executionSettings)

Create a new instance of KernelFunction.

Method Summary

Modifier and Type Method and Description
static Builder<T> createFromMethod(Method method, Object target)

Creates a KernelFunction<T> instance for a method, specified via a Method instance

static FromPromptBuilder<T> createFromPrompt(PromptTemplateConfig promptTemplateConfiguration)

Builder for creating a KernelFunction<T> instance for a given PromptTemplateConfig.

static FromPromptBuilder<T> createFromPrompt(String prompt)

Creates a KernelFunction<T> instance based on a given prompt

java.lang.String getDescription()

Get the description of the function.

java.util.Map<java.lang.String,PromptExecutionSettings> getExecutionSettings()

Get an unmodifiable map of the execution settings for the function.

KernelFunctionMetadata<?> getMetadata()

Get the metadata for the function.

java.lang.String getName()

Get the name of the function.

java.lang.String getPluginName()

Get the plugin name of the function.

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.

java.lang.String toEmbeddingString()

Create a string for generating an embedding for a function.

java.lang.String toManualString(boolean includeOutputs)

Create a manual-friendly string for a function.

Methods inherited from java.lang.Object

java.lang.Object.clone java.lang.Object.equals java.lang.Object.finalize java.lang.Object.getClass java.lang.Object.hashCode java.lang.Object.notify java.lang.Object.notifyAll java.lang.Object.toString java.lang.Object.wait java.lang.Object.wait java.lang.Object.wait

Constructor Details

KernelFunction

protected KernelFunction(KernelFunctionMetadata metadata, Map executionSettings)

Create a new instance of KernelFunction.

Parameters:

metadata - The metadata for the function
executionSettings - The execution settings for the function

Method Details

createFromMethod

public static KernelFunctionFromMethod.Builder createFromMethod(Method method, Object target)

Creates a KernelFunction<T> instance for a method, specified via a Method instance

Parameters:

method - The method to be represented via the created KernelFunction<T>.
target - The target object for the method if it represents an instance method. This should be null if and only if method is a static method.

Returns:

The created KernelFunction<T> wrapper for method.

createFromPrompt

public static KernelFunction.FromPromptBuilder createFromPrompt(PromptTemplateConfig promptTemplateConfiguration)

Builder for creating a KernelFunction<T> instance for a given PromptTemplateConfig.

Parameters:

promptTemplateConfiguration - The configuration for the prompt template.

Returns:

The builder for creating a KernelFunction<T> instance.

createFromPrompt

public static KernelFunction.FromPromptBuilder createFromPrompt(String prompt)

Creates a KernelFunction<T> instance based on a given prompt

Parameters:

prompt - The prompt to be used for the created KernelFunction<T>.

Returns:

The builder for creating a KernelFunction<T> instance.

getDescription

public String getDescription()

Get the description of the function.

Returns:

A description of the function

getExecutionSettings

public Map 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

getName

public String getName()

Get the name of the function.

Returns:

The name of this function

getPluginName

public String getPluginName()

Get the plugin name of the function.

Returns:

The name of the plugin that this function is within

invoke

public FunctionResult 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.

invoke

public FunctionResult invoke(Kernel kernel, KernelFunctionArguments arguments, ContextVariableType variableType, InvocationContext invocationContext)

Invokes this KernelFunction.

If the variableType parameter is provided, the ContextVariableType<T> is used to convert the result of the function to the appropriate FunctionResult<T>. The variableType is not required for converting well-known types such as String and Integer which have pre-defined ContextVariableTypes.

The InvocationContext allows for customization of the behavior of function, including the ability to pass in KernelHooks PromptExecutionSettings, and ToolCallBehavior.

The difference between calling the KernelFunction.invokeAsync method directly and calling the Kernel.invokeAsync method is that the latter adds the global KernelHooks (if any) to the InvocationContext. Calling KernelFunction.invokeAsync directly 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 invocation
variableType - The type of the ContextVariable<T> returned in the FunctionResult<T>
invocationContext - The arguments to pass to the function's invocation

Returns:

The result of the function's execution.

invokeAsync

public FunctionInvocation 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.

invokeAsync

public abstract Mono> invokeAsync(Kernel kernel, KernelFunctionArguments arguments, ContextVariableType variableType, InvocationContext invocationContext)

Invokes this KernelFunction.

If the variableType parameter is provided, the ContextVariableType<T> is used to convert the result of the function to the appropriate FunctionResult<T>. The variableType is not required for converting well-known types such as String and Integer which have pre-defined ContextVariableTypes.

The InvocationContext allows for customization of the behavior of function, including the ability to pass in KernelHooks PromptExecutionSettings, and ToolCallBehavior.

The difference between calling the KernelFunction.invokeAsync method directly and calling the Kernel.invokeAsync method is that the latter adds the global KernelHooks (if any) to the InvocationContext. Calling KernelFunction.invokeAsync directly 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 invocation
variableType - The type of the ContextVariable<T> returned in the FunctionResult<T>
invocationContext - The arguments to pass to the function's invocation

Returns:

The result of the function's execution.

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.

Applies to