BotSharp/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelTextCompletionProvider.cs

86 lines
2.9 KiB
C#
Raw Normal View History

2023-11-12 09:12:34 +00:00
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations;
2023-11-12 14:04:00 +00:00
using BotSharp.Abstraction.Conversations.Models;
2023-11-12 09:12:34 +00:00
using BotSharp.Abstraction.MLTasks;
2023-11-12 14:04:00 +00:00
using Microsoft;
using Microsoft.Extensions.DependencyInjection;
2023-11-12 09:12:34 +00:00
using Microsoft.SemanticKernel;
2023-11-12 14:04:00 +00:00
using Microsoft.SemanticKernel.AI.TextCompletion;
2023-11-12 09:12:34 +00:00
using System;
using System.Collections.Generic;
2023-11-12 14:04:00 +00:00
using System.Linq;
using System.Threading.Tasks;
2023-11-12 09:12:34 +00:00
namespace BotSharp.Plugin.SemanticKernel
{
2023-11-13 15:11:49 +00:00
/// <summary>
/// User Semantic Kernel as text completion provider
/// </summary>
2023-11-12 09:12:34 +00:00
public class SemanticKernelTextCompletionProvider : Abstraction.MLTasks.ITextCompletion
{
private readonly IKernel _kernel;
private readonly IServiceProvider _services;
private readonly ITokenStatistics _tokenStatistics;
private string? _model = null;
2023-11-13 15:12:24 +00:00
/// <inheritdoc/>
2023-11-12 09:12:34 +00:00
public string Provider => "semantic-kernel";
2023-11-13 15:12:24 +00:00
/// <summary>
/// Create a new instance of <see cref="SemanticKernelTextCompletionProvider"/>
/// </summary>
/// <param name="kernel"></param>
/// <param name="services"></param>
/// <param name="tokenStatistics"></param>
2023-11-12 09:12:34 +00:00
public SemanticKernelTextCompletionProvider(IKernel kernel,
IServiceProvider services,
ITokenStatistics tokenStatistics)
{
2023-11-13 15:11:49 +00:00
Requires.NotNull(kernel, nameof(IKernel));
2023-11-12 14:04:00 +00:00
2023-11-12 09:12:34 +00:00
this._kernel = kernel;
this._services = services;
this._tokenStatistics = tokenStatistics;
}
2023-11-13 15:11:49 +00:00
/// <inheritdoc/>
2023-11-12 09:12:34 +00:00
public async Task<string> GetCompletion(string text, string agentId, string messageId)
{
var hooks = _services.GetServices<IContentGeneratingHook>().ToList();
// Before chat completion hook
var agent = new Agent()
{
Id = agentId
};
var userMessage = new RoleDialogModel(AgentRole.User, text)
{
MessageId = messageId
};
Task.WaitAll(hooks.Select(hook =>
hook.BeforeGenerating(agent, new List<RoleDialogModel> { userMessage })).ToArray());
var completion = _kernel.GetService<Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion>(_model);
_tokenStatistics.StartTimer();
var result = await completion.CompleteAsync(text);
_tokenStatistics.StopTimer();
// After chat completion hook
Task.WaitAll(hooks.Select(hook =>
hook.AfterGenerated(new RoleDialogModel(AgentRole.Assistant, result), new TokenStatsModel
{
Model = _model ?? "default"
})).ToArray());
return result;
}
2023-11-13 15:11:49 +00:00
/// <inheritdoc/>
2023-11-12 09:12:34 +00:00
public void SetModelName(string model)
{
2023-11-13 15:11:49 +00:00
if (!string.IsNullOrWhiteSpace(model))
this._model = model;
2023-11-12 09:12:34 +00:00
}
}
2023-11-12 14:04:00 +00:00
}