Class Kernel


  • public class Kernel
    extends Object
    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 Detail

      • Kernel

        public Kernel​(AIServiceCollection services,
                      @Nullable
                      Function<AIServiceCollection,​AIServiceSelector> serviceSelectorProvider,
                      @Nullable
                      List<KernelPlugin> plugins,
                      @Nullable
                      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 Detail

      • 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.
      • 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.
      • invokeAsync

        public <T> FunctionInvocation<T> invokeAsync​(String pluginName,
                                                     String functionName)
        Invokes a KernelFunction function 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 a KernelFunction function 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)
      • 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 null if 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)
      • 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.