Kernel Class

  • java.lang.Object
    • com.microsoft.semantickernel.Kernel

public class Kernel

Provides state for use throughout a Semantic Kernel workload.

An instance of Kernel is passed through to every function invocation and service call throughout the system, providing to each the ability to access shared state and services.

Constructor Summary

Method Summary

Modifier and Type Method and Description
KernelFunction<T> getFunction(String pluginName, String functionName)

Gets the function with the specified name from the plugin with the specified name.

T getService(Class<T> clazz)

Get the service of the specified type from the kernel.

FunctionResult<T> invoke(KernelFunction<T> function)

Invokes a KernelFunction.

FunctionResult<T> invoke(String pluginName, String functionName)

Invokes a KernelFunction function by name.

FunctionInvocation<T> invokeAsync(KernelFunction<T> function)

Invokes a KernelFunction.

FunctionInvocation<T> invokeAsync(String pluginName, String functionName)

Invokes a KernelFunction function by name.

FunctionInvocation<T> invokePromptAsync(String prompt)

Invokes a Prompt.

FunctionInvocation<T> invokePromptAsync(String prompt, KernelFunctionArguments arguments)

Invokes a Prompt.

FunctionInvocation<T> invokePromptAsync(String prompt, KernelFunctionArguments arguments, InvocationContext invocationContext)

Invokes a Prompt.

static Builder builder()

Get the fluent builder for creating a new instance of Kernel.

static Builder from(Kernel kernel)

Creates a Builder that can create a copy of the Kernel.

java.util.List<KernelFunction<?>> getFunctions()

Gets the functions available through the kernel.

KernelHooks getGlobalKernelHooks()

Get the KernelHooks used throughout the kernel.

KernelPlugin getPlugin(String pluginName)

Gets the plugin with the specified name.

java.util.Collection<KernelPlugin> getPlugins()

Gets the plugins that were added to the kernel.

AIServiceSelector getServiceSelector()

Get the AIServiceSelector used to query for services available through the kernel.

Builder toBuilder()

Creates a Builder that can create a copy of the current instance of Kernel.

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

Kernel

public Kernel(AIServiceCollection services, Function serviceSelectorProvider, List plugins, KernelHooks globalKernelHooks)

Initializes a new instance of Kernel.

Parameters:

services - The collection of services available through the kernel.
serviceSelectorProvider - The service selector provider for the kernel. If null, an ordered service selector will be used.
plugins - The collection of plugins available through the kernel. If null, an empty collection will be used.
globalKernelHooks - The global hooks to be used throughout the kernel. If null, an empty collection will be used.

Method Details

getFunction

public KernelFunction getFunction(String pluginName, String functionName)

Gets the function with the specified name from the plugin with the specified name.

Parameters:

pluginName - The name of the plugin containing the function.
functionName - The name of the function to get.

Returns:

The function with the specified name from the plugin with the specified name.

getService

public T getService(Class clazz)

Get the service of the specified type from the kernel.

Parameters:

clazz - The class of the service to get.

Returns:

The service of the specified type from the kernel.

Throws:

ServiceNotFoundException - if the service is not found.

invoke

public FunctionResult invoke(KernelFunction function)

Invokes a KernelFunction.

Parameters:

function - The function to invoke.

Returns:

The result of the function invocation.

invoke

public FunctionResult invoke(String pluginName, String functionName)

Invokes a KernelFunction function by name.

Parameters:

pluginName - The name of the plugin containing the function.
functionName - The name of the function to invoke.

Returns:

The result of the function invocation.

invokeAsync

public FunctionInvocation invokeAsync(KernelFunction function)

Invokes a KernelFunction.

Parameters:

function - The function to invoke.

Returns:

The result of the function invocation.

invokeAsync

public FunctionInvocation invokeAsync(String pluginName, String functionName)

Invokes a KernelFunction function by name.

Parameters:

pluginName - The name of the plugin containing the function.
functionName - The name of the function to invoke.

Returns:

The result of the function invocation.

invokePromptAsync

public FunctionInvocation invokePromptAsync(String prompt)

Invokes a Prompt.

Parameters:

prompt - The prompt to invoke.

Returns:

The result of the prompt invocation.

invokePromptAsync

public FunctionInvocation invokePromptAsync(String prompt, KernelFunctionArguments arguments)

Invokes a Prompt.

Parameters:

prompt - The prompt to invoke.
arguments - The arguments to pass to the prompt.

Returns:

The result of the prompt invocation.

invokePromptAsync

public FunctionInvocation invokePromptAsync(String prompt, KernelFunctionArguments arguments, InvocationContext invocationContext)

Invokes a Prompt.

Parameters:

prompt - The prompt to invoke.
arguments - The arguments to pass to the prompt.
invocationContext - Additional context to used when invoking the prompt.

Returns:

The result of the prompt invocation.

builder

public static Kernel.Builder builder()

Get the fluent builder for creating a new instance of Kernel.

Returns:

The fluent builder for creating a new instance of Kernel.

from

public static Kernel.Builder from(Kernel kernel)

Creates a Builder that can create a copy of the Kernel. Use this method if you wish to modify the state of the kernel such as adding new plugins or services.

Parameters:

kernel - The kernel to copy.

Returns:

A Builder that can create a copy of the instance of Kernel.

getFunctions

public List> getFunctions()

Gets the functions available through the kernel. Functions are collected from all plugins available through the kernel.

Returns:

The functions available through the kernel.

getGlobalKernelHooks

public KernelHooks getGlobalKernelHooks()

Get the KernelHooks used throughout the kernel. These KernelHooks are used in addition to any hooks provided to a function.

Returns:

The KernelHooks used throughout the kernel.

getPlugin

public KernelPlugin getPlugin(String pluginName)

Gets the plugin with the specified name.

Parameters:

pluginName - The name of the plugin to get.

Returns:

The plugin with the specified name, or null if no such plugin exists.

getPlugins

public Collection getPlugins()

Gets the plugins that were added to the kernel.

Returns:

The plugins available through the kernel (unmodifiable list).

getServiceSelector

public AIServiceSelector getServiceSelector()

Get the AIServiceSelector used to query for services available through the kernel.

Returns:

The AIServiceSelector used to query for services available through the kernel.

toBuilder

public Kernel.Builder toBuilder()

Creates a Builder that can create a copy of the current instance of Kernel. Use this method if you wish to modify the state of the kernel such as adding new plugins or services.

Returns:

A Builder that can create a copy of the current instance of Kernel.

Applies to