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

84 lines
3 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
{
2023-11-19 13:34:13 +00:00
private readonly Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion _kernelTextCompletion;
2023-11-12 09:12:34 +00:00
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>
2023-11-19 13:34:13 +00:00
/// <param name="textCompletion"></param>
2023-11-13 15:12:24 +00:00
/// <param name="services"></param>
/// <param name="tokenStatistics"></param>
2023-11-19 13:34:13 +00:00
public SemanticKernelTextCompletionProvider(Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion textCompletion,
2023-11-12 09:12:34 +00:00
IServiceProvider services,
ITokenStatistics tokenStatistics)
{
2023-11-19 13:34:13 +00:00
this._kernelTextCompletion = textCompletion;
2023-11-12 09:12:34 +00:00
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());
2023-11-19 13:34:13 +00:00
var completion = this._kernelTextCompletion;
2023-11-12 09:12:34 +00:00
_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
}