Soenneker.SemanticKernel.Cache 3.0.175

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Soenneker.SemanticKernel.Cache --version 3.0.175                
NuGet\Install-Package Soenneker.SemanticKernel.Cache -Version 3.0.175                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Soenneker.SemanticKernel.Cache" Version="3.0.175" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Soenneker.SemanticKernel.Cache --version 3.0.175                
#r "nuget: Soenneker.SemanticKernel.Cache, 3.0.175"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Soenneker.SemanticKernel.Cache as a Cake Addin
#addin nuget:?package=Soenneker.SemanticKernel.Cache&version=3.0.175

// Install Soenneker.SemanticKernel.Cache as a Cake Tool
#tool nuget:?package=Soenneker.SemanticKernel.Cache&version=3.0.175                

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

alternate text is missing from this package README image Soenneker.SemanticKernel.Cache

Providing async thread-safe singleton Semantic Kernel instances

Why?

When using Microsoft.SemanticKernel, it's recommended to maintain long-lived kernel instances rather than re-creating them for each consumer or request. This avoids the overhead of reconfiguring connectors or plugins every time you need to perform a semantic operation. The SemanticKernelCache provides a thread-safe singleton cache per key via dependency injection. Kernel instances are created lazily using customizable options and disposed on application shutdown (or manually if needed).

Installation

Install the package via the .NET CLI:

dotnet add package Soenneker.SemanticKernel.Cache

Usage

1. Register the Cache in Dependency Injection

In your Program.cs (or equivalent startup file), register the cache with the DI container:

using Soenneker.SemanticKernel.Cache;

public static async Task Main(string[] args)
{
    var builder = WebApplication.CreateBuilder(args);

    // Register SemanticKernelCache as a singleton service.
    builder.Services.AddSemanticKernelCacheAsSingleton();

    // Other configuration...
}

2. Inject and Retrieve a Kernel Instance

Inject ISemanticKernelCache into your classes and retrieve a Microsoft.SemanticKernel.Kernel instance by providing the required options.

using System.Threading;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Chat;
using Soenneker.SemanticKernel.Cache;

public class TestClass
{
    private readonly ISemanticKernelCache _semanticKernelCache;
    private readonly SemanticKernelOptions _options;

    public TestClass(ISemanticKernelCache semanticKernelCache)
    {
        _semanticKernelCache = semanticKernelCache;
        
        // Create the options object once. Replace these with your actual values.
        var options = new SemanticKernelOptions
        {
            ModelId = "deepseek-r1:32b",
            Endpoint = "http://localhost:11434",
            KernelFactory = (opts, ct) =>
            {
                IKernelBuilder builder = Kernel.CreateBuilder().AddOllamaChatCompletion(opts.ModelId, new Uri(opts.Endpoint));

                return ValueTask.FromResult(builder);
            }
        };
    }

    public async async ValueTask<string> GetKernelResponse(string input, CancellationToken cancellationToken = default)
    {
        // Retrieve (or create) the kernel instance using a key (here, nameof(TestClass)).
        Kernel kernel = await _semanticKernelCache.Get(nameof(TestClass), _options, cancellationToken);

        // Retrieve the chat completion service from the kernel.
        var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();

        // Create a chat history and add the user's message.
        var history = new ChatHistory();
        history.AddUserMessage(input);

        // Request a chat completion using the chat service.
        var chatResult = await chatCompletionService.GetChatMessageContentAsync(history, kernel: kernel);

        // Return the chat result (or process it further as needed).
        return chatResult.ToString();
    }
}

Extending for Different Connectors/Plugins

The SemanticKernelOptions class includes an optional KernelFactory delegate. This allows you to override the default behavior (which uses the Azure Text Completion service) and create the kernel using a different connector or plugin. For example:

var openAiOptions = new SemanticKernelOptions
{
    ModelId = "openai-model-id",
    Endpoint = "https://api.openai.com/v1/",
    ApiKey = "your-openai-api-key",
    KernelFactory = (opts, ct) =>
    {
        Kernel kernel = new KernelBuilder().AddOpenAITextCompletionService(opts.ModelId, opts.Endpoint, opts.ApiKey);

        return ValueTask.FromResult(kernel);
    },
    ConfigureKernelAsync = async kernel =>
    {
        // Optionally, import skills or perform additional configuration.
        await ValueTask.CompletedTask;
    }
};

Kernel openAiKernel = await semanticKernelCache.Get("openaiKernel", openAiOptions);

This design makes it straightforward to support multiple types of Semantic Kernel configurations using the same caching mechanism.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.249 0 3/15/2025
3.0.248 0 3/15/2025
3.0.247 0 3/15/2025
3.0.246 22 3/15/2025
3.0.245 21 3/15/2025
3.0.244 107 3/12/2025
3.0.243 113 3/12/2025
3.0.242 107 3/12/2025
3.0.241 110 3/12/2025
3.0.240 103 3/12/2025
3.0.239 106 3/12/2025
3.0.238 108 3/12/2025
3.0.237 112 3/12/2025
3.0.236 112 3/12/2025
3.0.235 105 3/12/2025
3.0.234 107 3/12/2025
3.0.233 116 3/11/2025
3.0.232 121 3/11/2025
3.0.231 114 3/11/2025
3.0.230 119 3/11/2025
3.0.229 120 3/11/2025
3.0.228 125 3/11/2025
3.0.227 115 3/11/2025
3.0.226 117 3/11/2025
3.0.225 125 3/11/2025
3.0.224 118 3/11/2025
3.0.223 122 3/11/2025
3.0.222 120 3/11/2025
3.0.221 192 3/7/2025
3.0.220 189 3/7/2025
3.0.219 186 3/7/2025
3.0.218 188 3/7/2025
3.0.217 190 3/7/2025
3.0.216 183 3/7/2025
3.0.215 190 3/7/2025
3.0.214 186 3/7/2025
3.0.213 183 3/7/2025
3.0.212 189 3/3/2025
3.0.211 89 3/2/2025
3.0.210 97 3/2/2025
3.0.209 72 3/2/2025
3.0.208 76 3/2/2025
3.0.207 75 3/2/2025
3.0.206 78 3/2/2025
3.0.205 75 3/2/2025
3.0.204 88 3/2/2025
3.0.203 67 3/2/2025
3.0.202 74 3/2/2025
3.0.201 80 3/2/2025
3.0.200 78 3/2/2025
3.0.199 79 3/2/2025
3.0.198 84 3/1/2025
3.0.197 76 3/1/2025
3.0.196 77 3/1/2025
3.0.195 76 3/1/2025
3.0.194 76 3/1/2025
3.0.193 77 3/1/2025
3.0.192 75 3/1/2025
3.0.191 75 3/1/2025
3.0.190 72 3/1/2025
3.0.189 70 3/1/2025
3.0.188 74 3/1/2025
3.0.187 75 3/1/2025
3.0.186 83 2/28/2025
3.0.185 80 2/26/2025
3.0.184 81 2/26/2025
3.0.183 80 2/26/2025
3.0.182 85 2/26/2025
3.0.181 79 2/26/2025
3.0.180 76 2/25/2025
3.0.179 78 2/25/2025
3.0.178 84 2/25/2025
3.0.177 81 2/25/2025
3.0.176 87 2/25/2025
3.0.175 81 2/25/2025
3.0.174 77 2/25/2025
3.0.173 79 2/25/2025
3.0.172 74 2/25/2025
3.0.171 80 2/24/2025
3.0.170 79 2/24/2025
3.0.169 74 2/24/2025
3.0.168 109 2/23/2025
3.0.167 68 2/23/2025
3.0.166 79 2/23/2025
3.0.165 76 2/23/2025
3.0.164 76 2/23/2025
3.0.163 74 2/23/2025
3.0.162 81 2/23/2025
3.0.161 72 2/23/2025
3.0.160 78 2/22/2025
3.0.159 74 2/22/2025
3.0.158 79 2/22/2025
3.0.157 80 2/22/2025
3.0.156 76 2/22/2025
3.0.155 79 2/22/2025
3.0.154 76 2/22/2025
3.0.153 81 2/22/2025
3.0.152 85 2/22/2025
3.0.151 76 2/22/2025
3.0.150 81 2/22/2025
3.0.149 72 2/22/2025
3.0.148 85 2/22/2025
3.0.147 78 2/22/2025
3.0.146 83 2/22/2025
3.0.145 73 2/22/2025
3.0.144 74 2/22/2025
3.0.143 72 2/22/2025
3.0.142 79 2/22/2025
3.0.141 75 2/21/2025
3.0.140 77 2/21/2025
3.0.139 80 2/21/2025
3.0.138 81 2/21/2025
3.0.137 73 2/21/2025
3.0.136 79 2/21/2025
3.0.135 80 2/21/2025
3.0.134 86 2/20/2025
3.0.133 82 2/19/2025
3.0.132 86 2/19/2025
3.0.131 87 2/19/2025
3.0.130 79 2/19/2025
3.0.129 89 2/19/2025
3.0.128 85 2/19/2025
3.0.127 95 2/19/2025
3.0.126 84 2/19/2025
3.0.125 83 2/19/2025
3.0.124 82 2/19/2025
3.0.123 89 2/19/2025
3.0.122 85 2/18/2025
3.0.121 83 2/18/2025
3.0.120 94 2/18/2025
3.0.119 86 2/18/2025
3.0.118 84 2/18/2025
3.0.117 89 2/18/2025
3.0.116 102 2/18/2025
3.0.115 86 2/18/2025
3.0.114 86 2/16/2025
3.0.113 85 2/14/2025
3.0.112 79 2/14/2025
3.0.111 81 2/14/2025
3.0.110 83 2/14/2025
3.0.109 91 2/14/2025
3.0.108 89 2/14/2025
3.0.107 84 2/14/2025
3.0.106 94 2/14/2025
3.0.105 82 2/13/2025
3.0.104 80 2/13/2025
3.0.103 94 2/13/2025
3.0.102 75 2/13/2025
3.0.101 98 2/12/2025
3.0.100 81 2/12/2025
3.0.99 85 2/12/2025
3.0.98 94 2/12/2025
3.0.97 91 2/12/2025
3.0.96 88 2/12/2025
3.0.95 86 2/12/2025
3.0.94 82 2/12/2025
3.0.93 88 2/12/2025
3.0.92 91 2/12/2025
3.0.91 88 2/12/2025
3.0.90 89 2/12/2025
3.0.89 86 2/12/2025
3.0.88 85 2/12/2025
3.0.87 91 2/12/2025
3.0.86 83 2/12/2025
3.0.85 95 2/12/2025
3.0.84 90 2/12/2025
3.0.83 82 2/12/2025
3.0.82 83 2/11/2025
3.0.81 83 2/11/2025
3.0.80 90 2/11/2025
3.0.79 88 2/11/2025
3.0.78 94 2/11/2025
3.0.77 83 2/11/2025
3.0.76 85 2/11/2025
3.0.75 91 2/11/2025
3.0.74 87 2/11/2025
3.0.73 97 2/11/2025
3.0.72 87 2/11/2025
3.0.71 87 2/11/2025
3.0.70 89 2/10/2025
3.0.69 90 2/10/2025
3.0.68 95 2/10/2025
3.0.67 89 2/10/2025
3.0.66 83 2/10/2025
3.0.65 86 2/10/2025
3.0.64 84 2/9/2025
3.0.63 92 2/9/2025
3.0.62 72 2/9/2025
3.0.61 75 2/9/2025
3.0.60 84 2/9/2025
3.0.59 74 2/9/2025
3.0.58 88 2/8/2025
3.0.57 87 2/8/2025
3.0.56 80 2/8/2025
3.0.55 83 2/8/2025
3.0.54 86 2/8/2025
3.0.53 89 2/8/2025
3.0.52 83 2/8/2025
3.0.51 81 2/8/2025
3.0.50 87 2/8/2025
3.0.49 96 2/8/2025
3.0.48 83 2/8/2025
3.0.47 76 2/8/2025
3.0.46 82 2/7/2025
3.0.45 84 2/7/2025
3.0.44 87 2/7/2025
3.0.43 83 2/7/2025
3.0.42 84 2/7/2025
3.0.41 84 2/7/2025
3.0.40 92 2/7/2025
3.0.39 94 2/7/2025
3.0.38 96 2/7/2025
3.0.37 93 2/7/2025
3.0.36 79 2/7/2025
3.0.35 81 2/7/2025
3.0.34 81 2/7/2025
3.0.33 86 2/7/2025
3.0.32 89 2/7/2025
3.0.31 86 2/7/2025
3.0.30 82 2/6/2025
3.0.29 86 2/6/2025
3.0.28 79 2/6/2025
3.0.27 73 2/6/2025
3.0.26 92 2/6/2025
3.0.25 84 2/5/2025
3.0.24 87 2/5/2025
3.0.23 82 2/5/2025
3.0.22 89 2/5/2025
3.0.21 87 2/5/2025
3.0.20 89 2/5/2025
3.0.19 96 2/5/2025
3.0.18 87 2/5/2025
3.0.17 82 2/5/2025
3.0.16 89 2/5/2025
3.0.15 86 2/5/2025
3.0.14 88 2/5/2025
3.0.13 80 2/5/2025
3.0.12 84 2/5/2025
3.0.11 85 2/5/2025
3.0.10 89 2/5/2025
3.0.9 88 2/5/2025
3.0.8 86 2/5/2025
3.0.7 91 2/3/2025
3.0.6 88 2/3/2025
3.0.5 89 2/3/2025
3.0.4 89 2/3/2025
3.0.3 91 2/3/2025