Soenneker.SemanticKernel.Cache 3.0.57

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.57
                    
NuGet\Install-Package Soenneker.SemanticKernel.Cache -Version 3.0.57
                    
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.57" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.SemanticKernel.Cache" Version="3.0.57" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.SemanticKernel.Cache" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Soenneker.SemanticKernel.Cache --version 3.0.57
                    
#r "nuget: Soenneker.SemanticKernel.Cache, 3.0.57"
                    
#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.
#addin nuget:?package=Soenneker.SemanticKernel.Cache&version=3.0.57
                    
Install Soenneker.SemanticKernel.Cache as a Cake Addin
#tool nuget:?package=Soenneker.SemanticKernel.Cache&version=3.0.57
                    
Install Soenneker.SemanticKernel.Cache as a Cake Tool

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.307 0 4/3/2025
3.0.306 33 4/2/2025
3.0.305 32 4/1/2025
3.0.304 31 4/1/2025
3.0.303 29 4/1/2025
3.0.302 26 4/1/2025
3.0.301 29 4/1/2025
3.0.300 28 4/1/2025
3.0.299 47 4/1/2025
3.0.298 53 4/1/2025
3.0.297 52 4/1/2025
3.0.296 47 4/1/2025
3.0.295 49 3/31/2025
3.0.294 61 3/31/2025
3.0.293 49 3/31/2025
3.0.292 64 3/31/2025
3.0.291 63 3/30/2025
3.0.290 67 3/29/2025
3.0.289 67 3/29/2025
3.0.288 64 3/29/2025
3.0.287 72 3/29/2025
3.0.286 70 3/29/2025
3.0.285 78 3/29/2025
3.0.284 113 3/27/2025
3.0.283 116 3/27/2025
3.0.282 114 3/27/2025
3.0.281 111 3/27/2025
3.0.280 115 3/26/2025
3.0.279 448 3/26/2025
3.0.278 443 3/26/2025
3.0.277 445 3/26/2025
3.0.276 445 3/25/2025
3.0.275 450 3/25/2025
3.0.274 446 3/25/2025
3.0.273 456 3/25/2025
3.0.272 451 3/25/2025
3.0.271 456 3/25/2025
3.0.270 467 3/25/2025
3.0.269 68 3/21/2025
3.0.268 61 3/21/2025
3.0.267 72 3/21/2025
3.0.266 92 3/21/2025
3.0.265 88 3/21/2025
3.0.264 114 3/21/2025
3.0.263 111 3/21/2025
3.0.262 122 3/20/2025
3.0.261 120 3/20/2025
3.0.260 125 3/19/2025
3.0.259 124 3/19/2025
3.0.258 124 3/18/2025
3.0.257 124 3/18/2025
3.0.256 122 3/18/2025
3.0.255 126 3/18/2025
3.0.254 128 3/18/2025
3.0.253 126 3/18/2025
3.0.252 123 3/18/2025
3.0.251 126 3/18/2025
3.0.250 56 3/15/2025
3.0.249 54 3/15/2025
3.0.248 53 3/15/2025
3.0.247 49 3/15/2025
3.0.246 51 3/15/2025
3.0.245 48 3/15/2025
3.0.244 134 3/12/2025
3.0.243 140 3/12/2025
3.0.242 140 3/12/2025
3.0.241 137 3/12/2025
3.0.240 130 3/12/2025
3.0.239 133 3/12/2025
3.0.238 135 3/12/2025
3.0.237 137 3/12/2025
3.0.236 135 3/12/2025
3.0.235 132 3/12/2025
3.0.234 134 3/12/2025
3.0.233 137 3/11/2025
3.0.232 142 3/11/2025
3.0.231 134 3/11/2025
3.0.230 140 3/11/2025
3.0.229 141 3/11/2025
3.0.228 146 3/11/2025
3.0.227 136 3/11/2025
3.0.226 138 3/11/2025
3.0.225 146 3/11/2025
3.0.224 139 3/11/2025
3.0.223 143 3/11/2025
3.0.222 141 3/11/2025
3.0.221 193 3/7/2025
3.0.220 190 3/7/2025
3.0.219 188 3/7/2025
3.0.218 190 3/7/2025
3.0.217 192 3/7/2025
3.0.216 186 3/7/2025
3.0.215 192 3/7/2025
3.0.214 187 3/7/2025
3.0.213 190 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 77 3/2/2025
3.0.208 78 3/2/2025
3.0.207 77 3/2/2025
3.0.206 79 3/2/2025
3.0.205 77 3/2/2025
3.0.204 89 3/2/2025
3.0.203 67 3/2/2025
3.0.202 75 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 85 3/1/2025
3.0.197 77 3/1/2025
3.0.196 78 3/1/2025
3.0.195 77 3/1/2025
3.0.194 77 3/1/2025
3.0.193 78 3/1/2025
3.0.192 76 3/1/2025
3.0.191 76 3/1/2025
3.0.190 72 3/1/2025
3.0.189 70 3/1/2025
3.0.188 75 3/1/2025
3.0.187 76 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 80 2/26/2025
3.0.180 77 2/25/2025
3.0.179 79 2/25/2025
3.0.178 84 2/25/2025
3.0.177 81 2/25/2025
3.0.176 88 2/25/2025
3.0.175 81 2/25/2025
3.0.174 77 2/25/2025
3.0.173 80 2/25/2025
3.0.172 75 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 77 2/23/2025
3.0.163 75 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 80 2/22/2025
3.0.157 80 2/22/2025
3.0.156 76 2/22/2025
3.0.155 80 2/22/2025
3.0.154 77 2/22/2025
3.0.153 81 2/22/2025
3.0.152 86 2/22/2025
3.0.151 77 2/22/2025
3.0.150 82 2/22/2025
3.0.149 73 2/22/2025
3.0.148 85 2/22/2025
3.0.147 78 2/22/2025
3.0.146 84 2/22/2025
3.0.145 74 2/22/2025
3.0.144 75 2/22/2025
3.0.143 72 2/22/2025
3.0.142 79 2/22/2025
3.0.141 76 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 74 2/21/2025
3.0.136 79 2/21/2025
3.0.135 80 2/21/2025
3.0.134 87 2/20/2025
3.0.133 82 2/19/2025
3.0.132 86 2/19/2025
3.0.131 88 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 85 2/18/2025
3.0.117 90 2/18/2025
3.0.116 102 2/18/2025
3.0.115 86 2/18/2025
3.0.114 87 2/16/2025
3.0.113 85 2/14/2025
3.0.112 80 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 90 2/14/2025
3.0.107 85 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 95 2/13/2025
3.0.102 76 2/13/2025
3.0.101 99 2/12/2025
3.0.100 82 2/12/2025
3.0.99 86 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 89 2/12/2025
3.0.90 90 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 90 2/9/2025
3.0.63 93 2/9/2025
3.0.62 73 2/9/2025
3.0.61 76 2/9/2025
3.0.60 85 2/9/2025
3.0.59 75 2/9/2025
3.0.58 89 2/8/2025
3.0.57 87 2/8/2025
3.0.56 80 2/8/2025
3.0.55 84 2/8/2025
3.0.54 87 2/8/2025
3.0.53 90 2/8/2025
3.0.52 84 2/8/2025
3.0.51 82 2/8/2025
3.0.50 88 2/8/2025
3.0.49 97 2/8/2025
3.0.48 84 2/8/2025
3.0.47 77 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 93 2/7/2025
3.0.39 96 2/7/2025
3.0.38 97 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 90 2/7/2025
3.0.31 88 2/7/2025
3.0.30 83 2/6/2025
3.0.29 88 2/6/2025
3.0.28 81 2/6/2025
3.0.27 74 2/6/2025
3.0.26 93 2/6/2025
3.0.25 86 2/5/2025
3.0.24 89 2/5/2025
3.0.23 84 2/5/2025
3.0.22 89 2/5/2025
3.0.21 88 2/5/2025
3.0.20 90 2/5/2025
3.0.19 97 2/5/2025
3.0.18 88 2/5/2025
3.0.17 83 2/5/2025
3.0.16 90 2/5/2025
3.0.15 86 2/5/2025
3.0.14 88 2/5/2025
3.0.13 81 2/5/2025
3.0.12 85 2/5/2025
3.0.11 86 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