Package com.microsoft.semantickernel
Class Kernel
- java.lang.Object
-
- com.microsoft.semantickernel.Kernel
-
public class Kernel extends Object
Provides state for use throughout a Semantic Kernel workload.An instance of
Kernelis passed through to every function invocation and service call throughout the system, providing to each the ability to access shared state and services.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classKernel.BuilderA fluent builder for creating a new instance ofKernel.
-
Constructor Summary
Constructors Constructor Description Kernel(AIServiceCollection services, Function<AIServiceCollection,AIServiceSelector> serviceSelectorProvider, List<KernelPlugin> plugins, KernelHooks globalKernelHooks)Initializes a new instance ofKernel.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static Kernel.Builderbuilder()Get the fluent builder for creating a new instance ofKernel.static Kernel.Builderfrom(Kernel kernel)Creates a Builder that can create a copy of theKernel.<T> KernelFunction<T>getFunction(String pluginName, String functionName)Gets the function with the specified name from the plugin with the specified name.List<KernelFunction<?>>getFunctions()Gets the functions available through the kernel.KernelHooksgetGlobalKernelHooks()Get theKernelHooksused throughout the kernel.KernelPlugingetPlugin(String pluginName)Gets the plugin with the specified name.Collection<KernelPlugin>getPlugins()Gets the plugins that were added to the kernel.<T extends AIService>
TgetService(Class<T> clazz)Get the service of the specified type from the kernel.AIServiceSelectorgetServiceSelector()Get the AIServiceSelector used to query for services available through the kernel.<T> FunctionResult<T>invoke(KernelFunction<T> function)Invokes aKernelFunction.<T> FunctionResult<T>invoke(String pluginName, String functionName)Invokes aKernelFunctionfunction by name.<T> FunctionInvocation<T>invokeAsync(KernelFunction<T> function)Invokes aKernelFunction.<T> FunctionInvocation<T>invokeAsync(String pluginName, String functionName)Invokes aKernelFunctionfunction by name.<T> FunctionInvocation<T>invokePromptAsync(String prompt)Invokes a Prompt.Kernel.BuildertoBuilder()Creates a Builder that can create a copy of the current instance ofKernel.
-
-
-
Constructor Detail
-
Kernel
public Kernel(AIServiceCollection services, @Nullable Function<AIServiceCollection,AIServiceSelector> serviceSelectorProvider, @Nullable List<KernelPlugin> plugins, @Nullable KernelHooks globalKernelHooks)
Initializes a new instance ofKernel.- Parameters:
services- The collection of services available through the kernel.serviceSelectorProvider- The service selector provider for the kernel. Ifnull, an ordered service selector will be used.plugins- The collection of plugins available through the kernel. Ifnull, an empty collection will be used.globalKernelHooks- The global hooks to be used throughout the kernel. Ifnull, an empty collection will be used.
-
-
Method Detail
-
builder
public static Kernel.Builder builder()
Get the fluent builder for creating a new instance ofKernel.- 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 theKernel. 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.
-
toBuilder
public Kernel.Builder toBuilder()
Creates a Builder that can create a copy of the current instance ofKernel. 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.
-
invokeAsync
public <T> FunctionInvocation<T> invokeAsync(String pluginName, String functionName)
Invokes aKernelFunctionfunction by name.- Type Parameters:
T- The return type of the function.- 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.
- Throws:
IllegalArgumentException- if the plugin or function is not found.- See Also:
KernelFunction.invokeAsync(Kernel),KernelPluginCollection.getFunction(String, String)
-
invoke
public <T> FunctionResult<T> invoke(String pluginName, String functionName)
Invokes aKernelFunctionfunction by name.- Type Parameters:
T- The return type of the function.- 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.
- Throws:
IllegalArgumentException- if the plugin or function is not found.- See Also:
KernelFunction.invokeAsync(Kernel),KernelPluginCollection.getFunction(String, String)
-
invokePromptAsync
public <T> FunctionInvocation<T> invokePromptAsync(@Nonnull String prompt)
Invokes a Prompt.- Type Parameters:
T- The return type of the prompt.- Parameters:
prompt- The prompt to invoke.- Returns:
- The result of the prompt invocation.
- See Also:
KernelFunction.invokeAsync(Kernel)
-
invokeAsync
public <T> FunctionInvocation<T> invokeAsync(KernelFunction<T> function)
Invokes aKernelFunction.- Type Parameters:
T- The return type of the function.- Parameters:
function- The function to invoke.- Returns:
- The result of the function invocation.
- See Also:
KernelFunction.invokeAsync(Kernel)
-
invoke
public <T> FunctionResult<T> invoke(KernelFunction<T> function)
Invokes aKernelFunction.- Type Parameters:
T- The return type of the function.- Parameters:
function- The function to invoke.- Returns:
- The result of the function invocation.
- See Also:
KernelFunction.invokeAsync(Kernel)
-
getPlugin
@Nullable 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
nullif no such plugin exists.
-
getPlugins
public Collection<KernelPlugin> getPlugins()
Gets the plugins that were added to the kernel.- Returns:
- The plugins available through the kernel (unmodifiable list).
- See Also:
getPlugins()
-
getFunction
public <T> KernelFunction<T> getFunction(String pluginName, String functionName)
Gets the function with the specified name from the plugin with the specified name.- Type Parameters:
T- The return type of the function.- 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.
- Throws:
IllegalArgumentException- if the plugin or function is not found.- See Also:
KernelPluginCollection.getFunction(String, String)
-
getFunctions
public List<KernelFunction<?>> 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.
- See Also:
getPlugins(),Kernel.Builder.withPlugin(KernelPlugin)
-
getGlobalKernelHooks
public KernelHooks getGlobalKernelHooks()
Get theKernelHooksused throughout the kernel. TheseKernelHooksare used in addition to any hooks provided to a function.- Returns:
- The
KernelHooksused throughout the kernel. - See Also:
KernelFunction.invokeAsync(Kernel, KernelFunctionArguments, ContextVariableType, InvocationContext)
-
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.
-
getService
public <T extends AIService> T getService(Class<T> clazz) throws ServiceNotFoundException
Get the service of the specified type from the kernel.- Type Parameters:
T- The type of the service to get.- 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.- See Also:
AIServiceSelector.trySelectAIService(Class, KernelFunction, com.microsoft.semantickernel.semanticfunctions.KernelFunctionArguments)
-
-