Encamina.Enmarcha.SemanticKernel.Plugins.Chat 8.1.6

There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Encamina.Enmarcha.SemanticKernel.Plugins.Chat --version 8.1.6
NuGet\Install-Package Encamina.Enmarcha.SemanticKernel.Plugins.Chat -Version 8.1.6
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="Encamina.Enmarcha.SemanticKernel.Plugins.Chat" Version="8.1.6" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Encamina.Enmarcha.SemanticKernel.Plugins.Chat --version 8.1.6
#r "nuget: Encamina.Enmarcha.SemanticKernel.Plugins.Chat, 8.1.6"
#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 Encamina.Enmarcha.SemanticKernel.Plugins.Chat as a Cake Addin
#addin nuget:?package=Encamina.Enmarcha.SemanticKernel.Plugins.Chat&version=8.1.6

// Install Encamina.Enmarcha.SemanticKernel.Plugins.Chat as a Cake Tool
#tool nuget:?package=Encamina.Enmarcha.SemanticKernel.Plugins.Chat&version=8.1.6

Semantic Kernel - Chat Plugin

Nuget package

Chat Plugin is a project that provides Chat functionality in the form of a Semantic Kernel Plugin. It allows users to interact while chatting and asking questions to an Artificial Intelligence, usually a Large Language Model (LLM). Additionally, it stores the conversation history.

Setup

Nuget package

First, install NuGet. Then, install Encamina.Enmarcha.SemanticKernel.Plugins.Chat from the package manager console:

PM> Install-Package Encamina.Enmarcha.SemanticKernel.Plugins.Chat

.NET CLI:

First, install .NET CLI. Then, install Encamina.Enmarcha.SemanticKernel.Plugins.Chat from the .NET CLI:

dotnet add package Encamina.Enmarcha.SemanticKernel.Plugins.Chat

How to use

To use ChatWithHistoryPlugin, the usual approach is to import it as a plugin within Semantic Kernel. The simplest way to do this is by using the extension method ImportChatWithHistoryPlugin, which handles the import of the Plugin into Semantic Kernel. However, some previous configuration is required before importing it. First, you need to add the SemanticKernelOptions, ChatWithHistoryPluginOptions and ChatHistoryProviderOptions to your project configuration. You can achieve this by using any configuration provider. The followng code is an example of how the settings should look like using the appsettings.json file:

  {
    // ...
    "SemanticKernelOptions": {
        "ChatModelName": "gpt-35-turbo", // Name (sort of a unique identifier) of the model to use for chat
        "ChatModelDeploymentName": "gpt-35-turbo", // Model deployment name on the LLM (for example OpenAI) to use for chat
        "EmbeddingsModelName": "text-embedding-ada-002", // Name (sort of a unique identifier) of the model to use for embeddings
        "EmbeddingsModelDeploymentName": "text-embedding-ada-002", // Model deployment name on the LLM (for example OpenAI) to use for embeddings
        "Endpoint": "https://your-url.openai.azure.com/", // Uri for an LLM resource (like OpenAI). This should include protocol and hostname.
        "Key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", // Key credential used to authenticate to an LLM resource
    },
    "ChatWithHistoryPluginOptions": {
        "ChatRequestSettings": {
            "MaxTokens": 1000, // Maximum number of tokens to generate in the completion
            "Temperature": 0.8, // Controls the randomness of the completion. The higher the temperature, the more random the completion
            "TopP": 0.5, // Controls the diversity of the completion. The higher the TopP, the more diverse the completion.
        }
    },
    "ChatHistoryProviderOptions": {
        HistoryMaxMessages": 12,
    }
    // ...
  }

Next, in Program.cs or a similar entry point file in your project, add the following code.

// Entry point
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
   // ...
});

// ...

var tokenLengthFunction = ILengthFunctions.LengthByTokenCount;
string cosmosContainer = "cosmosDbContainer"; // You probably want to save this in the appsettings or similar

// Or others configuration providers...
builder.Configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

// Requires Encamina.Enmarcha.SemanticKernel.Abstractions nuget
builder.Services.AddOptions<SemanticKernelOptions>().Bind(builder.Configuration.GetSection(nameof(SemanticKernelOptions)))
    .ValidateDataAnnotations()
    .ValidateOnStart();
builder.Services.AddOptions<ChatWithHistoryPluginOptions>().Bind(builder.Configuration.GetSection(nameof(ChatWithHistoryPluginOptions)))
    .ValidateDataAnnotations()
    .ValidateOnStart();
builder.Services.AddOptions<ChatHistoryProviderOptions>().Bind(builder.Configuration.GetSection(nameof(ChatHistoryProviderOptions)))
    .ValidateDataAnnotations()
    .ValidateOnStart();

// Requieres Encamina.Enmarcha.Data.Cosmos
builder.Services.AddCosmos(builder.Configuration);

builder.Services.AddCosmosChatHistoryProvider(cosmosContainer, tokenLengthFunction);

builder.Services.AddScoped(sp =>
{
    var kernel = new KernelBuilder()
        .WithAzureChatCompletionService("<YOUR DEPLOYMENT NAME>", "<YOUR AZURE ENDPOINT>", "<YOUR API KEY>")
        //.WithOpenAIChatCompletionService("<YOUR MODEL ID>", "<YOUR API KEY>", "<YOUR API KEY>")
        /// ...
        .Build();

    // ...

    kernel.ImportChatWithHistoryPlugin(sp, openAIOptions, tokenLengthFunction);

    return kernel;
});

Now you can inject the kernel via constructor, and the chat capabilities are already available.

public class MyClass
{
    private readonly Kernel kernel;

    public MyClass(Kernel kernel)
    {
        this.kernel = kernel;
    }

    public async Task TestChatAsync()
    {
        var contextVariables = new ContextVariables();
        contextVariables.Set(PluginsInfo.ChatWithHistoryPlugin.Functions.Chat.Parameters.Ask, "What is the weather like in Madrid?");
        contextVariables.Set(PluginsInfo.ChatWithHistoryPlugin.Functions.Chat.Parameters.UserId, "123456");
        contextVariables.Set(PluginsInfo.ChatWithHistoryPlugin.Functions.Chat.Parameters.UserName, "John Doe");
        contextVariables.Set(PluginsInfo.ChatWithHistoryPlugin.Functions.Chat.Parameters.Locale, "en");

        var functionChat = kernel.Func(PluginsInfo.ChatWithHistoryPlugin.Name, PluginsInfo.ChatWithHistoryPlugin.Functions.Chat.Name);

        var resultContext = await kernel.RunAsync(contextVariables, functionChat);
    }
}

Advanced configurations

If you want to disable chat history, simply configure the HistoryMaxMessages of ChatHistoryProviderOptions with a value of 0.

You can also inherit from the ChatWithHistoryPlugin class and add the customizations you need.

public class MyCustomChatWithHistoryPlugin : ChatWithHistoryPlugin
{
    public MyCustomChatWithHistoryPlugin(Kernel kernel, string chatModelName, Func<string, int> tokensLengthFunction, IChatHistoryProvider chatHistoryProvider, IOptionsMonitor<ChatWithHistoryPluginOptions> options)
        : base(kernel, chatModelName, tokensLengthFunction, chatHistoryProvider, options)
    {
    }

    protected override string SystemPrompt => "You are a Virtual Assistant who only talks about the weather.";

    // There are more overridable methods/properties
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.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
8.1.7-preview-02 69 5/10/2024
8.1.7-preview-01 70 5/8/2024
8.1.6 156 5/7/2024
8.1.6-preview-08 35 5/2/2024
8.1.6-preview-07 59 4/29/2024
8.1.6-preview-06 95 4/26/2024
8.1.6-preview-05 65 4/24/2024
8.1.6-preview-04 77 4/22/2024
8.1.6-preview-03 66 4/22/2024
8.1.6-preview-02 104 4/17/2024
8.1.6-preview-01 151 4/15/2024
8.1.5 86 4/15/2024
8.1.5-preview-15 74 4/10/2024
8.1.5-preview-14 95 3/20/2024
8.1.5-preview-13 52 3/18/2024
8.1.5-preview-12 79 3/13/2024
8.1.5-preview-11 47 3/13/2024
8.1.5-preview-10 72 3/13/2024
8.1.5-preview-09 60 3/12/2024
8.1.5-preview-08 53 3/12/2024
8.1.5-preview-07 57 3/8/2024
8.1.5-preview-06 160 3/8/2024
8.1.5-preview-05 54 3/7/2024
8.1.5-preview-04 70 3/7/2024
8.1.5-preview-03 59 3/7/2024
8.1.5-preview-02 109 2/28/2024
8.1.5-preview-01 92 2/19/2024
8.1.4 122 2/15/2024
8.1.3 90 2/13/2024
8.1.3-preview-07 45 2/13/2024
8.1.3-preview-06 67 2/12/2024
8.1.3-preview-05 65 2/9/2024
8.1.3-preview-04 67 2/8/2024
8.1.3-preview-03 62 2/7/2024
8.1.3-preview-02 65 2/2/2024
8.1.3-preview-01 60 2/2/2024
8.1.2 100 2/1/2024
8.1.2-preview-9 78 1/22/2024
8.1.2-preview-8 63 1/19/2024
8.1.2-preview-7 62 1/19/2024
8.1.2-preview-6 61 1/19/2024
8.1.2-preview-5 61 1/19/2024
8.1.2-preview-4 64 1/19/2024
8.1.2-preview-3 62 1/18/2024
8.1.2-preview-2 63 1/18/2024
8.1.2-preview-16 61 1/31/2024
8.1.2-preview-15 63 1/31/2024
8.1.2-preview-14 149 1/25/2024
8.1.2-preview-13 66 1/25/2024
8.1.2-preview-12 59 1/23/2024
8.1.2-preview-11 58 1/23/2024
8.1.2-preview-10 63 1/22/2024
8.1.2-preview-1 60 1/18/2024
8.1.1 103 1/18/2024
8.1.0 77 1/18/2024
8.0.3 132 12/29/2023
8.0.1 106 12/14/2023
8.0.0 123 12/7/2023
6.0.4.3 108 12/29/2023
6.0.4.2 121 12/20/2023
6.0.4.1 184 12/19/2023
6.0.4 144 12/4/2023
6.0.3.20 121 11/27/2023
6.0.3.19 125 11/22/2023