create semantic kernel plugin
This commit is contained in:
parent
1d703591a5
commit
cd793e228d
11
BotSharp.sln
11
BotSharp.sln
|
|
@ -63,6 +63,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.MongoStorag
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.GoogleAI", "src\Plugins\BotSharp.Plugin.GoogleAI\BotSharp.Plugin.GoogleAI.csproj", "{8BC29F8A-78D6-422C-B522-10687ADC38ED}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.GoogleAI", "src\Plugins\BotSharp.Plugin.GoogleAI\BotSharp.Plugin.GoogleAI.csproj", "{8BC29F8A-78D6-422C-B522-10687ADC38ED}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.SemanticKernel", "src\Plugins\BotSharp.Plugin.SemanticKernel\BotSharp.Plugin.SemanticKernel.csproj", "{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
|
@ -223,6 +225,14 @@ Global
|
||||||
{8BC29F8A-78D6-422C-B522-10687ADC38ED}.Release|Any CPU.Build.0 = Release|Any CPU
|
{8BC29F8A-78D6-422C-B522-10687ADC38ED}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{8BC29F8A-78D6-422C-B522-10687ADC38ED}.Release|x64.ActiveCfg = Release|Any CPU
|
{8BC29F8A-78D6-422C-B522-10687ADC38ED}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
{8BC29F8A-78D6-422C-B522-10687ADC38ED}.Release|x64.Build.0 = Release|Any CPU
|
{8BC29F8A-78D6-422C-B522-10687ADC38ED}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Release|x64.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|
@ -254,6 +264,7 @@ Global
|
||||||
{5CD330E1-9E5A-4112-8346-6E31CA98EF78} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
|
{5CD330E1-9E5A-4112-8346-6E31CA98EF78} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
|
||||||
{DB3DE37B-1208-4ED3-9615-A52AD0AAD69C} = {5CD330E1-9E5A-4112-8346-6E31CA98EF78}
|
{DB3DE37B-1208-4ED3-9615-A52AD0AAD69C} = {5CD330E1-9E5A-4112-8346-6E31CA98EF78}
|
||||||
{8BC29F8A-78D6-422C-B522-10687ADC38ED} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
|
{8BC29F8A-78D6-422C-B522-10687ADC38ED} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
|
||||||
|
{73EE2CD0-3B27-4F02-A67B-762CBDD740D0} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}
|
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.1</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta6" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
|
|
@ -0,0 +1,105 @@
|
||||||
|
using BotSharp.Abstraction.Agents.Enums;
|
||||||
|
using BotSharp.Abstraction.Agents;
|
||||||
|
using BotSharp.Abstraction.Agents.Models;
|
||||||
|
using BotSharp.Abstraction.Conversations.Models;
|
||||||
|
using BotSharp.Abstraction.MLTasks;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Runtime;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using BotSharp.Abstraction.Conversations;
|
||||||
|
using Microsoft.SemanticKernel;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using BotSharp.Abstraction.Models;
|
||||||
|
|
||||||
|
namespace BotSharp.Plugin.SemanticKernel
|
||||||
|
{
|
||||||
|
public class SemanticKernelChatCompletionProvider : IChatCompletion
|
||||||
|
{
|
||||||
|
private IKernel _kernel;
|
||||||
|
private IServiceProvider _services;
|
||||||
|
private ITokenStatistics _tokenStatistics;
|
||||||
|
private string? _model = null;
|
||||||
|
|
||||||
|
public string Provider => throw new NotImplementedException();
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetModelName(string model)
|
||||||
|
{
|
||||||
|
this._model = model;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
using BotSharp.Abstraction.MLTasks;
|
||||||
|
using BotSharp.Abstraction.Plugins;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace BotSharp.Plugin.SemanticKernel
|
||||||
|
{
|
||||||
|
public class SemanticKernelPlugin : IBotSharpPlugin
|
||||||
|
{
|
||||||
|
public string Name => "Semantic Kernel";
|
||||||
|
public string Description => "Semantic Kernel Service";
|
||||||
|
public void RegisterDI(IServiceCollection services, IConfiguration config)
|
||||||
|
{
|
||||||
|
services.AddScoped<ITextCompletion, SemanticKernelTextCompletionProvider>();
|
||||||
|
services.AddScoped<IChatCompletion, SemanticKernelChatCompletionProvider>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,71 @@
|
||||||
|
using BotSharp.Abstraction.Agents.Enums;
|
||||||
|
using BotSharp.Abstraction.Agents.Models;
|
||||||
|
using BotSharp.Abstraction.Conversations.Models;
|
||||||
|
using BotSharp.Abstraction.Conversations;
|
||||||
|
using BotSharp.Abstraction.MLTasks;
|
||||||
|
using Microsoft.SemanticKernel;
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Microsoft.SemanticKernel.AI.TextCompletion;
|
||||||
|
|
||||||
|
namespace BotSharp.Plugin.SemanticKernel
|
||||||
|
{
|
||||||
|
public class SemanticKernelTextCompletionProvider : Abstraction.MLTasks.ITextCompletion
|
||||||
|
{
|
||||||
|
private readonly IKernel _kernel;
|
||||||
|
private readonly IServiceProvider _services;
|
||||||
|
private readonly ITokenStatistics _tokenStatistics;
|
||||||
|
private string? _model = null;
|
||||||
|
|
||||||
|
public string Provider => "semantic-kernel";
|
||||||
|
|
||||||
|
public SemanticKernelTextCompletionProvider(IKernel kernel,
|
||||||
|
IServiceProvider services,
|
||||||
|
ITokenStatistics tokenStatistics)
|
||||||
|
{
|
||||||
|
this._kernel = kernel;
|
||||||
|
this._services = services;
|
||||||
|
this._tokenStatistics = tokenStatistics;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetModelName(string model)
|
||||||
|
{
|
||||||
|
this._model = model;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue