SK_Ext 1.0.8

dotnet add package SK_Ext --version 1.0.8
                    
NuGet\Install-Package SK_Ext -Version 1.0.8
                    
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="SK_Ext" Version="1.0.8" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SK_Ext" Version="1.0.8" />
                    
Directory.Packages.props
<PackageReference Include="SK_Ext" />
                    
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 SK_Ext --version 1.0.8
                    
#r "nuget: SK_Ext, 1.0.8"
                    
#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.
#:package SK_Ext@1.0.8
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SK_Ext&version=1.0.8
                    
Install as a Cake Addin
#tool nuget:?package=SK_Ext&version=1.0.8
                    
Install as a Cake Tool

SK.Ext

SK.Ext is a .NET library that extends the functionality of the Microsoft Semantic Kernel. It provides additional utilities and extensions to enhance the development experience when working with Semantic Kernel.

Features

  • Chat Completion Extensions: Stream chat messages with functions and advanced result handling.
  • Chat History Extensions: Manage and manipulate chat history efficiently.
  • Async Enumerable Extensions: Utilities for working with asynchronous streams.
  • Completion Runtime: Abstraction for running completions with custom logic.
  • Result Types: Rich result types for LLM calls, including streaming and error handling.

Installation

To install SK.Ext, add the following package reference to your project file:

<PackageReference Include="SK.Ext" Version="1.0.7" />

Alternatively, you can install it via the .NET CLI:

dotnet add package SK.Ext --version 1.0.7

Main Extension Classes

  • ChatCompletionServiceExtentions: Extension methods for streaming chat completions with function support.
  • ChatHistoryExtentions: Methods for manipulating chat history, function calls, and results.
  • AsyncEnumerableExtentions: Utilities for merging and working with async enumerables.
  • CompletionRuntime / ICompletionRuntime: Abstractions for running completions with custom logic and streaming results.
  • CallingLLMResult, CallingLLMStreamedResult: Result types for LLM calls.

Usage

Chat Completion Extensions

The ChatCompletionServiceExtentions class provides methods to stream chat messages with functions. Here is an example:

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using SK.Ext;

var kernel = Kernel.CreateBuilder().Build();
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
var chatHistory = new ChatHistory();
var settings = new PromptExecutionSettings();

await foreach (var result in chatCompletionService.StreamChatMessagesWithFunctions(kernel, chatHistory, settings))
{
    switch (result)
    {
        case TextResult textResult:
            Console.Write(textResult.Text);
            break;
        case FunctionCall functionCall:
            Console.WriteLine($"\nCalling function: {functionCall.FunctionName}");
            break;
        case FunctionExecutionResult functionResult:
            Console.WriteLine($"Function result: {functionResult.Result}");
            break;
        case StreamedFunctionExecutionResult streamedResult:
            Console.WriteLine($"Streaming result: {streamedResult.Result}");
            break;
        case FunctionExceptionResult exceptionResult:
            Console.WriteLine($"Function error: {exceptionResult.Exception.Message}");
            break;
        case UsageResult usageResult:
            Console.WriteLine($"Tokens used: {usageResult.TotalTokenCount}");
            break;
        case CallingLLM callingLLM:
            Console.WriteLine("Calling LLM...");
            break;
        case CallingLLMResult llmResult:
            Console.WriteLine($"LLM result: {llmResult.Result}");
            break;
        case CallingLLMStreamedResult streamedLlmResult:
            Console.WriteLine($"LLM streamed result: {streamedLlmResult.Result}");
            break;
    }
}

Chat History Extensions

The ChatHistoryExtentions class provides methods to manage and manipulate chat history. Here is an example:

using Microsoft.SemanticKernel.ChatCompletion;
using SK.Ext;

var chatHistory = new ChatHistory();
chatHistory.RemoveFunctionCall("callId");
chatHistory.ReplaceFunctionCallResult("callId", new { Result = "result" });
chatHistory.RemoveDuplicatedFunctionCallResults();

Completion Runtime

You can use the CompletionRuntime class to run completions with custom logic and streaming support:

using SK.Ext;
using SK.Ext.Models;

ICompletionRuntime runtime = new CompletionRuntime(chatCompletionService);
var context = new CompletionContext();
await foreach (var result in runtime.Completion(context, CancellationToken.None))
{
    // Handle IContentResult (see result types above)
}

Async Enumerable Extensions

The AsyncEnumerableExtentions class provides utilities for working with async streams:

using SK.Ext;

var streams = new List<(int, IAsyncEnumerable<string>)>();
await foreach (var (taskId, item) in streams.MergeWithTaskId())
{
    Console.WriteLine($"Task {taskId}: {item}");
}

Tests

dotnet test --collect:"XPlat Code Coverage"
reportgenerator -reports:"C:\Users\ypyl\projects\sk_ext\SK.Ext.Tests\TestResults\*\coverage.cobertura.xml" -targetdir:"coveragereport" -reporttypes:Html

License

This project is licensed under the MIT License.

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

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
1.0.8 120 6/21/2025
1.0.7 207 6/8/2025
1.0.6 150 5/4/2025
1.0.5 148 5/4/2025
1.0.4 163 4/9/2025
1.0.2 165 3/23/2025
1.0.1 159 3/23/2025
1.0.0 166 3/23/2025