Encamina.Enmarcha.SemanticKernel.Plugins.Chat 8.1.3-preview-02

This is a prerelease version of Encamina.Enmarcha.SemanticKernel.Plugins.Chat.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Encamina.Enmarcha.SemanticKernel.Plugins.Chat --version 8.1.3-preview-02                
NuGet\Install-Package Encamina.Enmarcha.SemanticKernel.Plugins.Chat -Version 8.1.3-preview-02                
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.3-preview-02" />                
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.3-preview-02                
#r "nuget: Encamina.Enmarcha.SemanticKernel.Plugins.Chat, 8.1.3-preview-02"                
#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.3-preview-02&prerelease

// Install Encamina.Enmarcha.SemanticKernel.Plugins.Chat as a Cake Tool
#tool nuget:?package=Encamina.Enmarcha.SemanticKernel.Plugins.Chat&version=8.1.3-preview-02&prerelease                

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 ImportChatWithHistoryPluginUsingCosmosDb, 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 and ChatWithHistoryPluginOptions 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": {
        "HistoryMaxMessages": "gpt-35-turbo", // Name (sort of a unique identifier) of the model to use for chat
        "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.
        }
    },
    // ...
  }

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
{
   // ...
});

// ...

// 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();

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

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();

    // ...

    string cosmosContainer = "cosmosDbContainer"; // You probably want to save this in the appsettings or similar

    kernel.ImportChatWithHistoryPluginUsingCosmosDb(sp, cosmosContainer, ILengthFunctions.LengthByTokenCount);

    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

Take into consideration that the above code uses a Cosmos DB implementation as IAsyncRepository as an example. You can use other implementations.

If you want to disable chat history, simply configure the HistoryMaxMessages de ChatWithHistoryPluginOptions 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, IAsyncRepository<ChatMessageHistoryRecord> chatMessagesHistoryRepository, IOptionsMonitor<ChatWithHistoryPluginOptions> options)
        : base(kernel, chatModelName, tokensLengthFunction, chatMessagesHistoryRepository, 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.2.0 406 10/22/2024
8.2.0-preview-01-m01 97 9/17/2024
8.1.9-preview-03 55 11/19/2024
8.1.9-preview-02 67 10/22/2024
8.1.9-preview-01 292 10/4/2024
8.1.8 225 9/23/2024
8.1.8-preview-07 629 9/12/2024
8.1.8-preview-06 219 9/11/2024
8.1.8-preview-05 79 9/10/2024
8.1.8-preview-04 357 8/16/2024
8.1.8-preview-03 176 8/13/2024
8.1.8-preview-02 100 8/13/2024
8.1.8-preview-01 100 8/12/2024
8.1.7 98 8/7/2024
8.1.7-preview-09 153 7/3/2024
8.1.7-preview-08 95 7/2/2024
8.1.7-preview-07 93 6/10/2024
8.1.7-preview-06 94 6/10/2024
8.1.7-preview-05 105 6/6/2024
8.1.7-preview-04 95 6/6/2024
8.1.7-preview-03 88 5/24/2024
8.1.7-preview-02 94 5/10/2024
8.1.7-preview-01 103 5/8/2024
8.1.6 1,269 5/7/2024
8.1.6-preview-08 62 5/2/2024
8.1.6-preview-07 92 4/29/2024
8.1.6-preview-06 372 4/26/2024
8.1.6-preview-05 89 4/24/2024
8.1.6-preview-04 106 4/22/2024
8.1.6-preview-03 94 4/22/2024
8.1.6-preview-02 125 4/17/2024
8.1.6-preview-01 184 4/15/2024
8.1.5 118 4/15/2024
8.1.5-preview-15 115 4/10/2024
8.1.5-preview-14 136 3/20/2024
8.1.5-preview-13 83 3/18/2024
8.1.5-preview-12 100 3/13/2024
8.1.5-preview-11 87 3/13/2024
8.1.5-preview-10 113 3/13/2024
8.1.5-preview-09 97 3/12/2024
8.1.5-preview-08 93 3/12/2024
8.1.5-preview-07 106 3/8/2024
8.1.5-preview-06 208 3/8/2024
8.1.5-preview-05 95 3/7/2024
8.1.5-preview-04 101 3/7/2024
8.1.5-preview-03 78 3/7/2024
8.1.5-preview-02 136 2/28/2024
8.1.5-preview-01 114 2/19/2024
8.1.4 155 2/15/2024
8.1.3 124 2/13/2024
8.1.3-preview-07 70 2/13/2024
8.1.3-preview-06 95 2/12/2024
8.1.3-preview-05 105 2/9/2024
8.1.3-preview-04 91 2/8/2024
8.1.3-preview-03 87 2/7/2024
8.1.3-preview-02 92 2/2/2024
8.1.3-preview-01 87 2/2/2024
8.1.2 128 2/1/2024
8.1.2-preview-9 107 1/22/2024
8.1.2-preview-8 89 1/19/2024
8.1.2-preview-7 92 1/19/2024
8.1.2-preview-6 86 1/19/2024
8.1.2-preview-5 97 1/19/2024
8.1.2-preview-4 87 1/19/2024
8.1.2-preview-3 91 1/18/2024
8.1.2-preview-2 88 1/18/2024
8.1.2-preview-16 90 1/31/2024
8.1.2-preview-15 85 1/31/2024
8.1.2-preview-14 191 1/25/2024
8.1.2-preview-13 88 1/25/2024
8.1.2-preview-12 99 1/23/2024
8.1.2-preview-11 108 1/23/2024
8.1.2-preview-10 96 1/22/2024
8.1.2-preview-1 85 1/18/2024
8.1.1 141 1/18/2024
8.1.0 107 1/18/2024
8.0.3 160 12/29/2023
8.0.1 134 12/14/2023
8.0.0 160 12/7/2023
6.0.4.3 158 12/29/2023
6.0.4.2 151 12/20/2023
6.0.4.1 220 12/19/2023
6.0.4 176 12/4/2023
6.0.3.20 149 11/27/2023
6.0.3.19 151 11/22/2023