Soenneker.SemanticKernel.Cache 3.0.221

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.221
                    
NuGet\Install-Package Soenneker.SemanticKernel.Cache -Version 3.0.221
                    
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.221" />
                    
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.221" />
                    
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.221
                    
#r "nuget: Soenneker.SemanticKernel.Cache, 3.0.221"
                    
#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.221
                    
Install Soenneker.SemanticKernel.Cache as a Cake Addin
#tool nuget:?package=Soenneker.SemanticKernel.Cache&version=3.0.221
                    
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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Soenneker.SemanticKernel.Cache:

Package Downloads
Soenneker.SemanticKernel.Pool

Manages a pool of Semantic Kernel instances with per-entry rate limiting.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.450 141 3 days ago
3.0.449 140 3 days ago
3.0.448 137 4 days ago
3.0.447 99 4 days ago
3.0.446 121 4 days ago
3.0.445 102 4 days ago
3.0.444 141 4 days ago
3.0.443 105 4 days ago
3.0.442 138 4 days ago
3.0.441 99 4 days ago
3.0.440 114 4 days ago
3.0.439 239 5 days ago
3.0.438 119 6 days ago
3.0.437 120 6 days ago
3.0.436 116 8 days ago
3.0.435 133 8 days ago
3.0.434 134 8 days ago
3.0.433 91 8 days ago
3.0.432 123 8 days ago
3.0.431 101 8 days ago
3.0.430 128 8 days ago
3.0.429 145 8 days ago
3.0.428 113 8 days ago
3.0.427 128 9 days ago
3.0.426 121 9 days ago
3.0.425 149 9 days ago
3.0.424 339 10 days ago
3.0.423 148 10 days ago
3.0.422 180 11 days ago
3.0.421 120 11 days ago
3.0.420 180 12 days ago
3.0.419 286 13 days ago
3.0.418 141 13 days ago
3.0.417 145 13 days ago
3.0.416 157 13 days ago
3.0.414 84 14 days ago
3.0.413 145 15 days ago
3.0.412 171 15 days ago
3.0.411 207 17 days ago
3.0.410 208 17 days ago
3.0.409 206 17 days ago
3.0.408 210 17 days ago
3.0.407 212 17 days ago
3.0.406 128 23 days ago
3.0.405 125 23 days ago
3.0.404 128 23 days ago
3.0.403 128 23 days ago
3.0.402 125 23 days ago
3.0.401 130 23 days ago
3.0.400 125 23 days ago
3.0.399 130 24 days ago
3.0.398 131 25 days ago
3.0.397 125 25 days ago
3.0.396 129 a month ago
3.0.395 127 a month ago
3.0.394 130 a month ago
3.0.393 127 a month ago
3.0.392 130 a month ago
3.0.391 133 a month ago
3.0.390 128 a month ago
3.0.389 125 a month ago
3.0.388 128 a month ago
3.0.387 129 a month ago
3.0.386 130 a month ago
3.0.385 126 a month ago
3.0.384 121 a month ago
3.0.383 78 a month ago
3.0.382 69 a month ago
3.0.381 72 a month ago
3.0.380 163 a month ago
3.0.379 118 2 months ago
3.0.378 158 2 months ago
3.0.377 145 2 months ago
3.0.376 160 2 months ago
3.0.375 160 2 months ago
3.0.374 156 2 months ago
3.0.373 155 2 months ago
3.0.372 148 2 months ago
3.0.371 157 2 months ago
3.0.370 155 2 months ago
3.0.369 145 2 months ago
3.0.368 154 2 months ago
3.0.367 149 2 months ago
3.0.366 148 2 months ago
3.0.365 145 2 months ago
3.0.364 148 2 months ago
3.0.363 156 2 months ago
3.0.362 151 2 months ago
3.0.361 154 2 months ago
3.0.360 155 2 months ago
3.0.359 152 2 months ago
3.0.358 140 2 months ago
3.0.357 154 2 months ago
3.0.356 152 2 months ago
3.0.355 144 2 months ago
3.0.354 146 2 months ago
3.0.353 145 2 months ago
3.0.352 152 2 months ago
3.0.351 144 2 months ago
3.0.350 151 2 months ago
3.0.349 140 2 months ago
3.0.348 151 2 months ago
3.0.347 148 2 months ago
3.0.346 146 2 months ago
3.0.345 149 2 months ago
3.0.344 147 2 months ago
3.0.343 148 2 months ago
3.0.342 153 2 months ago
3.0.341 148 2 months ago
3.0.340 148 2 months ago
3.0.339 147 2 months ago
3.0.338 145 2 months ago
3.0.337 150 2 months ago
3.0.336 152 2 months ago
3.0.335 140 2 months ago
3.0.334 123 2 months ago
3.0.333 121 2 months ago
3.0.332 122 2 months ago
3.0.331 116 2 months ago
3.0.330 134 2 months ago
3.0.329 130 2 months ago
3.0.328 95 2 months ago
3.0.327 102 2 months ago
3.0.326 89 2 months ago
3.0.325 98 2 months ago
3.0.324 102 2 months ago
3.0.323 73 2 months ago
3.0.322 71 2 months ago
3.0.321 74 2 months ago
3.0.320 74 2 months ago
3.0.319 79 2 months ago
3.0.318 81 2 months ago
3.0.317 78 2 months ago
3.0.316 91 2 months ago
3.0.315 86 2 months ago
3.0.314 90 2 months ago
3.0.313 135 2 months ago
3.0.312 136 2 months ago
3.0.311 133 2 months ago
3.0.310 151 2 months ago
3.0.309 142 2 months ago
3.0.308 147 2 months ago
3.0.307 147 2 months ago
3.0.306 138 2 months ago
3.0.305 147 2 months ago
3.0.304 145 2 months ago
3.0.303 144 2 months ago
3.0.302 140 2 months ago
3.0.301 144 2 months ago
3.0.300 135 2 months ago
3.0.299 148 2 months ago
3.0.298 139 2 months ago
3.0.297 139 2 months ago
3.0.296 135 2 months ago
3.0.295 135 2 months ago
3.0.294 149 2 months ago
3.0.293 135 2 months ago
3.0.292 150 2 months ago
3.0.291 145 2 months ago
3.0.290 142 2 months ago
3.0.289 80 2 months ago
3.0.288 78 2 months ago
3.0.287 85 2 months ago
3.0.286 83 2 months ago
3.0.285 91 2 months ago
3.0.284 125 2 months ago
3.0.283 130 2 months ago
3.0.282 127 2 months ago
3.0.281 125 2 months ago
3.0.280 128 2 months ago
3.0.279 461 2 months ago
3.0.278 461 2 months ago
3.0.277 464 2 months ago
3.0.276 461 2 months ago
3.0.275 465 2 months ago
3.0.274 462 2 months ago
3.0.273 470 2 months ago
3.0.272 465 2 months ago
3.0.271 471 2 months ago
3.0.270 483 2 months ago
3.0.269 81 2 months ago
3.0.268 76 2 months ago
3.0.267 86 2 months ago
3.0.266 104 2 months ago
3.0.265 101 2 months ago
3.0.264 127 2 months ago
3.0.263 124 2 months ago
3.0.262 137 2 months ago
3.0.261 133 2 months ago
3.0.260 134 2 months ago
3.0.259 133 2 months ago
3.0.258 132 2 months ago
3.0.257 132 2 months ago
3.0.256 130 2 months ago
3.0.255 135 2 months ago
3.0.254 137 2 months ago
3.0.253 134 2 months ago
3.0.252 130 2 months ago
3.0.251 134 2 months ago
3.0.250 64 3 months ago
3.0.249 62 3 months ago
3.0.248 61 3 months ago
3.0.247 59 3 months ago
3.0.246 59 3 months ago
3.0.245 56 3 months ago
3.0.244 144 3 months ago
3.0.243 150 3 months ago
3.0.242 147 3 months ago
3.0.241 146 3 months ago
3.0.240 138 3 months ago
3.0.239 141 3 months ago
3.0.238 144 3 months ago
3.0.237 144 3 months ago
3.0.236 143 3 months ago
3.0.235 141 3 months ago
3.0.234 142 3 months ago
3.0.233 147 3 months ago
3.0.232 151 3 months ago
3.0.231 143 3 months ago
3.0.230 152 3 months ago
3.0.229 149 3 months ago
3.0.228 153 3 months ago
3.0.227 149 3 months ago
3.0.226 147 3 months ago
3.0.225 153 3 months ago
3.0.224 148 3 months ago
3.0.223 152 3 months ago
3.0.222 155 3 months ago
3.0.221 201 3 months ago
3.0.220 199 3 months ago
3.0.219 196 3 months ago
3.0.218 198 3 months ago
3.0.217 199 3 months ago
3.0.216 197 3 months ago
3.0.215 200 3 months ago
3.0.214 196 3 months ago
3.0.213 198 3 months ago
3.0.212 197 3 months ago
3.0.211 101 3 months ago
3.0.210 105 3 months ago
3.0.209 86 3 months ago
3.0.208 86 3 months ago
3.0.207 86 3 months ago
3.0.206 87 3 months ago
3.0.205 85 3 months ago
3.0.204 97 3 months ago
3.0.203 75 3 months ago
3.0.202 85 3 months ago
3.0.201 88 3 months ago
3.0.200 86 3 months ago
3.0.199 87 3 months ago
3.0.198 92 3 months ago
3.0.197 84 3 months ago
3.0.196 86 3 months ago
3.0.195 85 3 months ago
3.0.194 85 3 months ago
3.0.193 87 3 months ago
3.0.192 84 3 months ago
3.0.191 84 3 months ago
3.0.190 80 3 months ago
3.0.189 79 3 months ago
3.0.188 84 3 months ago
3.0.187 82 3 months ago
3.0.186 91 3 months ago
3.0.185 89 3 months ago
3.0.184 90 3 months ago
3.0.183 89 3 months ago
3.0.182 93 3 months ago
3.0.181 89 3 months ago
3.0.180 86 3 months ago
3.0.179 87 3 months ago
3.0.178 93 3 months ago
3.0.177 89 3 months ago
3.0.176 96 3 months ago
3.0.175 89 3 months ago
3.0.174 86 3 months ago
3.0.173 88 3 months ago
3.0.172 83 3 months ago
3.0.171 88 3 months ago
3.0.170 88 3 months ago
3.0.169 82 3 months ago
3.0.168 117 3 months ago
3.0.167 77 3 months ago
3.0.166 87 3 months ago
3.0.165 84 3 months ago
3.0.164 85 3 months ago
3.0.163 83 3 months ago
3.0.162 89 3 months ago
3.0.161 79 3 months ago
3.0.160 85 3 months ago
3.0.159 84 3 months ago
3.0.158 89 3 months ago
3.0.157 88 3 months ago
3.0.156 84 3 months ago
3.0.155 88 3 months ago
3.0.154 84 3 months ago
3.0.153 89 3 months ago
3.0.152 94 3 months ago
3.0.151 85 3 months ago
3.0.150 90 3 months ago
3.0.149 82 3 months ago
3.0.148 93 3 months ago
3.0.147 86 3 months ago
3.0.146 92 3 months ago
3.0.145 82 3 months ago
3.0.144 84 3 months ago
3.0.143 80 3 months ago
3.0.142 87 3 months ago
3.0.141 87 3 months ago
3.0.140 88 3 months ago
3.0.139 88 3 months ago
3.0.138 89 3 months ago
3.0.137 82 3 months ago
3.0.136 87 3 months ago
3.0.135 89 3 months ago
3.0.134 95 3 months ago
3.0.133 90 3 months ago
3.0.132 94 3 months ago
3.0.131 96 3 months ago
3.0.130 87 3 months ago
3.0.129 97 3 months ago
3.0.128 94 3 months ago
3.0.127 103 3 months ago
3.0.126 91 3 months ago
3.0.125 92 3 months ago
3.0.124 92 3 months ago
3.0.123 98 3 months ago
3.0.122 93 3 months ago
3.0.121 91 3 months ago
3.0.120 102 3 months ago
3.0.119 93 3 months ago
3.0.118 97 3 months ago
3.0.117 98 3 months ago
3.0.116 113 3 months ago
3.0.115 94 3 months ago
3.0.114 95 4 months ago
3.0.113 93 4 months ago
3.0.112 88 4 months ago
3.0.111 89 4 months ago
3.0.110 91 4 months ago
3.0.109 99 4 months ago
3.0.108 98 4 months ago
3.0.107 94 4 months ago
3.0.106 102 4 months ago
3.0.105 94 4 months ago
3.0.104 88 4 months ago
3.0.103 104 4 months ago
3.0.102 85 4 months ago
3.0.101 106 4 months ago
3.0.100 90 4 months ago
3.0.99 97 4 months ago
3.0.98 102 4 months ago
3.0.97 99 4 months ago
3.0.96 96 4 months ago
3.0.95 95 4 months ago
3.0.94 91 4 months ago
3.0.93 96 4 months ago
3.0.92 100 4 months ago
3.0.91 97 4 months ago
3.0.90 98 4 months ago
3.0.89 93 4 months ago
3.0.88 93 4 months ago
3.0.87 100 4 months ago
3.0.86 91 4 months ago
3.0.85 103 4 months ago
3.0.84 98 4 months ago
3.0.83 91 4 months ago
3.0.82 91 4 months ago
3.0.81 91 4 months ago
3.0.80 98 4 months ago
3.0.79 96 4 months ago
3.0.78 103 4 months ago
3.0.77 92 4 months ago
3.0.76 93 4 months ago
3.0.75 99 4 months ago
3.0.74 95 4 months ago
3.0.73 109 4 months ago
3.0.72 99 4 months ago
3.0.71 100 4 months ago
3.0.70 99 4 months ago
3.0.69 100 4 months ago
3.0.68 106 4 months ago
3.0.67 100 4 months ago
3.0.66 94 4 months ago
3.0.65 96 4 months ago
3.0.64 99 4 months ago
3.0.63 103 4 months ago
3.0.62 86 4 months ago
3.0.61 88 4 months ago
3.0.60 95 4 months ago
3.0.59 85 4 months ago
3.0.58 98 4 months ago
3.0.57 96 4 months ago
3.0.56 91 4 months ago
3.0.55 95 4 months ago
3.0.54 96 4 months ago
3.0.53 99 4 months ago
3.0.52 93 4 months ago
3.0.51 91 4 months ago
3.0.50 97 4 months ago
3.0.49 107 4 months ago
3.0.48 93 4 months ago
3.0.47 86 4 months ago
3.0.46 93 4 months ago
3.0.45 95 4 months ago
3.0.44 101 4 months ago
3.0.43 95 4 months ago
3.0.42 97 4 months ago
3.0.41 97 4 months ago
3.0.40 106 4 months ago
3.0.39 105 4 months ago
3.0.38 106 4 months ago
3.0.37 102 4 months ago
3.0.36 90 4 months ago
3.0.35 90 4 months ago
3.0.34 90 4 months ago
3.0.33 95 4 months ago
3.0.32 97 4 months ago
3.0.31 97 4 months ago
3.0.30 92 4 months ago
3.0.29 97 4 months ago
3.0.28 90 4 months ago
3.0.27 83 4 months ago
3.0.26 102 4 months ago
3.0.25 94 4 months ago
3.0.24 97 4 months ago
3.0.23 93 4 months ago
3.0.22 96 4 months ago
3.0.21 97 4 months ago
3.0.20 99 4 months ago
3.0.19 105 4 months ago
3.0.18 97 4 months ago
3.0.17 91 4 months ago
3.0.16 100 4 months ago
3.0.15 94 4 months ago
3.0.14 97 4 months ago
3.0.13 89 4 months ago
3.0.12 93 4 months ago
3.0.11 100 4 months ago
3.0.10 97 4 months ago
3.0.9 96 4 months ago
3.0.8 95 4 months ago
3.0.7 100 4 months ago
3.0.6 96 4 months ago
3.0.5 97 4 months ago
3.0.4 99 4 months ago
3.0.3 99 4 months ago