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

102 lines
3.5 KiB
C#
Raw Normal View History

2023-11-12 09:12:34 +00:00
using BotSharp.Abstraction.Agents;
2023-11-12 14:04:00 +00:00
using BotSharp.Abstraction.Agents.Enums;
2023-11-12 09:12:34 +00:00
using BotSharp.Abstraction.Agents.Models;
2023-11-12 14:04:00 +00:00
using BotSharp.Abstraction.Conversations;
2023-11-12 09:12:34 +00:00
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.MLTasks;
2023-11-12 14:04:00 +00:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
2023-11-12 09:12:34 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BotSharp.Plugin.SemanticKernel
{
public class SemanticKernelChatCompletionProvider : IChatCompletion
{
private IKernel _kernel;
private IServiceProvider _services;
private ITokenStatistics _tokenStatistics;
private string? _model = null;
2023-11-13 15:11:49 +00:00
public string Provider => "semantic-kernel";
2023-11-12 09:12:34 +00:00
public SemanticKernelChatCompletionProvider(IKernel kernel,
IServiceProvider services,
ITokenStatistics tokenStatistics)
{
this._kernel = kernel;
this._services = services;
this._tokenStatistics = tokenStatistics;
}
public RoleDialogModel GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
{
var hooks = _services.GetServices<IContentGeneratingHook>().ToList();
// Before chat completion hook
Task.WaitAll(hooks.Select(hook =>
hook.BeforeGenerating(agent, conversations)).ToArray());
var completion = _kernel.GetService<Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion>(_model);
var agentService = _services.GetRequiredService<IAgentService>();
var instruction = agentService.RenderedInstruction(agent);
var chatHistory = completion.CreateNewChat(instruction);
foreach (var message in conversations)
{
if (message.Role == AgentRole.User)
{
chatHistory.AddUserMessage(message.Content);
}
else
{
chatHistory.AddAssistantMessage(message.Content);
}
}
var response = completion.GetChatCompletionsAsync(chatHistory)
.ContinueWith(async t =>
{
var result = await t;
var message = await result.First().GetChatMessageAsync();
return message.Content;
}).ConfigureAwait(false).GetAwaiter().GetResult()
.ConfigureAwait(false).GetAwaiter().GetResult();
var msg = new RoleDialogModel(AgentRole.Assistant, response)
{
CurrentAgentId = agent.Id
};
// After chat completion hook
Task.WaitAll(hooks.Select(hook =>
hook.AfterGenerated(msg, new TokenStatsModel
{
Model = _model ?? "default"
})).ToArray());
return msg;
}
public Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived, Func<RoleDialogModel, Task> onFunctionExecuting)
{
throw new NotImplementedException();
}
public Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived)
{
throw new NotImplementedException();
}
2023-11-13 15:11:49 +00:00
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
}