Soenneker.SemanticKernel.Cache 3.0.197

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.197                
NuGet\Install-Package Soenneker.SemanticKernel.Cache -Version 3.0.197                
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.197" />                
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.197                
#r "nuget: Soenneker.SemanticKernel.Cache, 3.0.197"                
#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.197

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

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