Merge branch 'master' of https://github.com/SciSharp/BotSharp
This commit is contained in:
commit
8378af8009
22
BotSharp.sln
22
BotSharp.sln
|
|
@ -63,6 +63,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.MongoStorag
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.GoogleAI", "src\Plugins\BotSharp.Plugin.GoogleAI\BotSharp.Plugin.GoogleAI.csproj", "{8BC29F8A-78D6-422C-B522-10687ADC38ED}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.SemanticKernel", "src\Plugins\BotSharp.Plugin.SemanticKernel\BotSharp.Plugin.SemanticKernel.csproj", "{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.SemanticKernel.UnitTests", "tests\BotSharp.Plugin.SemanticKernel.UnitTests\BotSharp.Plugin.SemanticKernel.UnitTests.csproj", "{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.Twilio", "src\Plugins\BotSharp.Plugin.Twilio\BotSharp.Plugin.Twilio.csproj", "{E627F1E3-BE03-443A-83A2-86A855A278EB}"
|
||||
EndProject
|
||||
Global
|
||||
|
|
@ -225,6 +229,22 @@ Global
|
|||
{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.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
|
||||
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}.Release|x64.Build.0 = Release|Any CPU
|
||||
{E627F1E3-BE03-443A-83A2-86A855A278EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E627F1E3-BE03-443A-83A2-86A855A278EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E627F1E3-BE03-443A-83A2-86A855A278EB}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
|
|
@ -264,6 +284,8 @@ Global
|
|||
{5CD330E1-9E5A-4112-8346-6E31CA98EF78} = {2635EC9B-2E5F-4313-AC21-0B847F31F36C}
|
||||
{DB3DE37B-1208-4ED3-9615-A52AD0AAD69C} = {5CD330E1-9E5A-4112-8346-6E31CA98EF78}
|
||||
{8BC29F8A-78D6-422C-B522-10687ADC38ED} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
|
||||
{73EE2CD0-3B27-4F02-A67B-762CBDD740D0} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
|
||||
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247} = {32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC}
|
||||
{E627F1E3-BE03-443A-83A2-86A855A278EB} = {64264688-0F5C-4AB0-8F2B-B59B717CCE00}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
|
|
|
|||
|
|
@ -0,0 +1,25 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.1</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
|
||||
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.SemanticKernel.Abstractions" Version="1.0.0-beta6" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="17.6.11" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="BotSharp.Plugin.SemanticKernel.UnitTests" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,112 @@
|
|||
using BotSharp.Abstraction.Agents;
|
||||
using BotSharp.Abstraction.Agents.Enums;
|
||||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Conversations;
|
||||
using BotSharp.Abstraction.Conversations.Models;
|
||||
using BotSharp.Abstraction.MLTasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.SemanticKernel;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BotSharp.Plugin.SemanticKernel
|
||||
{
|
||||
/// <summary>
|
||||
/// Use Semantic Kernel as chat completion provider
|
||||
/// </summary>
|
||||
public class SemanticKernelChatCompletionProvider : IChatCompletion
|
||||
{
|
||||
private IKernel _kernel;
|
||||
private IServiceProvider _services;
|
||||
private ITokenStatistics _tokenStatistics;
|
||||
private string? _model = null;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string Provider => "semantic-kernel";
|
||||
|
||||
/// <summary>
|
||||
/// Create a new instance of <see cref="SemanticKernelChatCompletionProvider"/>
|
||||
/// </summary>
|
||||
/// <param name="kernel"></param>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="tokenStatistics"></param>
|
||||
public SemanticKernelChatCompletionProvider(IKernel kernel,
|
||||
IServiceProvider services,
|
||||
ITokenStatistics tokenStatistics)
|
||||
{
|
||||
this._kernel = kernel;
|
||||
this._services = services;
|
||||
this._tokenStatistics = tokenStatistics;
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
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;
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived, Func<RoleDialogModel, Task> onFunctionExecuting)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
/// <inheritdoc/>
|
||||
public Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void SetModelName(string model)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(model))
|
||||
this._model = model;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
using BotSharp.Abstraction.MLTasks;
|
||||
using BotSharp.Abstraction.Plugins;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
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,86 @@
|
|||
using BotSharp.Abstraction.Agents.Enums;
|
||||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Conversations;
|
||||
using BotSharp.Abstraction.Conversations.Models;
|
||||
using BotSharp.Abstraction.MLTasks;
|
||||
using Microsoft;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.SemanticKernel;
|
||||
using Microsoft.SemanticKernel.AI.TextCompletion;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BotSharp.Plugin.SemanticKernel
|
||||
{
|
||||
/// <summary>
|
||||
/// User Semantic Kernel as text completion provider
|
||||
/// </summary>
|
||||
public class SemanticKernelTextCompletionProvider : Abstraction.MLTasks.ITextCompletion
|
||||
{
|
||||
private readonly IKernel _kernel;
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ITokenStatistics _tokenStatistics;
|
||||
private string? _model = null;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string Provider => "semantic-kernel";
|
||||
|
||||
/// <summary>
|
||||
/// Create a new instance of <see cref="SemanticKernelTextCompletionProvider"/>
|
||||
/// </summary>
|
||||
/// <param name="kernel"></param>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="tokenStatistics"></param>
|
||||
public SemanticKernelTextCompletionProvider(IKernel kernel,
|
||||
IServiceProvider services,
|
||||
ITokenStatistics tokenStatistics)
|
||||
{
|
||||
Requires.NotNull(kernel, nameof(IKernel));
|
||||
|
||||
this._kernel = kernel;
|
||||
this._services = services;
|
||||
this._tokenStatistics = tokenStatistics;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void SetModelName(string model)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(model))
|
||||
this._model = model;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
|
||||
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta6" />
|
||||
<PackageReference Include="Moq" Version="4.20.69" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.2">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Plugins\BotSharp.Plugin.SemanticKernel\BotSharp.Plugin.SemanticKernel.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
using Microsoft.SemanticKernel.AI.ChatCompletion;
|
||||
using Microsoft.SemanticKernel.AI.TextCompletion;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
|
||||
namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers
|
||||
{
|
||||
public class ResultHelper : IChatResult, ITextResult
|
||||
{
|
||||
public ModelResult ModelResult { get; set; }
|
||||
private string _response;
|
||||
|
||||
public ResultHelper(string response)
|
||||
{
|
||||
ModelResult = new ModelResult(response);
|
||||
_response = response;
|
||||
}
|
||||
|
||||
public async Task<ChatMessageBase> GetChatMessageAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await Task.FromResult(new MockModelResult(_response));
|
||||
}
|
||||
|
||||
public Task<string> GetCompletionAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return Task.FromResult(_response);
|
||||
}
|
||||
|
||||
public class MockModelResult : ChatMessageBase
|
||||
{
|
||||
public MockModelResult(string content) : base(AuthorRole.Assistant, content, null)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
using Microsoft.SemanticKernel.AI;
|
||||
using Microsoft.SemanticKernel.AI.ChatCompletion;
|
||||
using Microsoft.SemanticKernel.AI.TextCompletion;
|
||||
using Microsoft.SemanticKernel.Services;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers
|
||||
{
|
||||
internal class SemanticKernelHelper : IChatCompletion, ITextCompletion, IAIService
|
||||
{
|
||||
private readonly string _excepted;
|
||||
|
||||
public SemanticKernelHelper(string excepted)
|
||||
{
|
||||
this._excepted = excepted;
|
||||
}
|
||||
|
||||
public ChatHistory CreateNewChat(string? instructions = null)
|
||||
{
|
||||
return new ChatHistory();
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<IChatResult>> GetChatCompletionsAsync(ChatHistory chat, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return Task.FromResult<IReadOnlyList<IChatResult>>( new List<IChatResult> { new ResultHelper(_excepted) });
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<ITextResult>> GetCompletionsAsync(string text, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return Task.FromResult<IReadOnlyList<ITextResult>>(new List<ITextResult> { new ResultHelper(_excepted) });
|
||||
}
|
||||
|
||||
public IAsyncEnumerable<IChatStreamingResult> GetStreamingChatCompletionsAsync(ChatHistory chat, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IAsyncEnumerable<ITextStreamingResult> GetStreamingCompletionsAsync(string text, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using BotSharp.Abstraction.Agents;
|
||||
using BotSharp.Abstraction.Conversations.Models;
|
||||
using BotSharp.Abstraction.MLTasks;
|
||||
using BotSharp.Plugin.SemanticKernel;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.SemanticKernel;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using BotSharp.Abstraction.Agents.Enums;
|
||||
using BotSharp.Abstraction.Agents.Models;
|
||||
using System.Linq;
|
||||
using System.Runtime;
|
||||
using BotSharp.Abstraction.Conversations;
|
||||
using BotSharp.Abstraction.Models;
|
||||
using Microsoft.SemanticKernel.AI.ChatCompletion;
|
||||
using Microsoft.SemanticKernel.AI;
|
||||
using Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk;
|
||||
using BotSharp.Plugin.SemanticKernel.UnitTests.Helpers;
|
||||
|
||||
namespace BotSharp.Plugin.SemanticKernel.Tests
|
||||
{
|
||||
public class SemanticKernelChatCompletionProviderTests
|
||||
{
|
||||
private readonly Mock<IKernel> _kernelMock;
|
||||
private readonly Mock<IServiceProvider> _servicesMock;
|
||||
private readonly Mock<ITokenStatistics> _tokenStatisticsMock;
|
||||
private readonly SemanticKernelChatCompletionProvider _provider;
|
||||
|
||||
public SemanticKernelChatCompletionProviderTests()
|
||||
{
|
||||
_kernelMock = new Mock<IKernel>();
|
||||
_servicesMock = new Mock<IServiceProvider>();
|
||||
_tokenStatisticsMock = new Mock<ITokenStatistics>();
|
||||
_provider = new SemanticKernelChatCompletionProvider(_kernelMock.Object, _servicesMock.Object, _tokenStatisticsMock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetChatCompletions_Returns_RoleDialogModel()
|
||||
{
|
||||
// Arrange
|
||||
var agent = new Agent();
|
||||
var conversations = new List<RoleDialogModel>
|
||||
{
|
||||
new RoleDialogModel(AgentRole.User, "Hello")
|
||||
};
|
||||
|
||||
_servicesMock.Setup(x => x.GetService(typeof(IEnumerable<IContentGeneratingHook>)))
|
||||
.Returns(new List<IContentGeneratingHook>());
|
||||
var agentService = new Mock<IAgentService>();
|
||||
agentService.Setup(x => x.RenderedInstruction(agent)).Returns("");
|
||||
_servicesMock.Setup(x => x.GetService(typeof(IAgentService)))
|
||||
.Returns(agentService.Object);
|
||||
|
||||
var chatHistoryMock = new Mock<ChatHistory>();
|
||||
var chatCompletionMock = new Mock<Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion>();
|
||||
chatCompletionMock.Setup(x => x.CreateNewChat(It.IsAny<string>())).Returns(chatHistoryMock.Object);
|
||||
chatCompletionMock.Setup(x => x.GetChatCompletionsAsync(chatHistoryMock.Object, It.IsAny<AIRequestSettings>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<IChatResult>
|
||||
{
|
||||
new ResultHelper("How can I help you?")
|
||||
});
|
||||
|
||||
_kernelMock.Setup(x => x.GetService<Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion>(null)).Returns(chatCompletionMock.Object);
|
||||
|
||||
// Act
|
||||
var result = _provider.GetChatCompletions(agent, conversations);
|
||||
|
||||
// Assert
|
||||
Assert.IsType<RoleDialogModel>(result);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
using BotSharp.Abstraction.Conversations;
|
||||
using BotSharp.Abstraction.MLTasks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.SemanticKernel;
|
||||
using Moq;
|
||||
|
||||
namespace BotSharp.Plugin.SemanticKernel.Tests
|
||||
{
|
||||
public class SemanticKernelPluginTests
|
||||
{
|
||||
[Fact]
|
||||
public void TestRegisterDI()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
var config = new ConfigurationBuilder().Build();
|
||||
var plugin = new SemanticKernelPlugin();
|
||||
services.AddScoped(x =>
|
||||
{
|
||||
return new KernelBuilder()
|
||||
.WithAzureOpenAIChatCompletionService("test", "test", "test")
|
||||
.Build();
|
||||
});
|
||||
services.AddScoped<ITokenStatistics>(x=> Mock.Of<ITokenStatistics>());
|
||||
|
||||
|
||||
plugin.RegisterDI(services, config);
|
||||
|
||||
var provider = services.BuildServiceProvider();
|
||||
|
||||
Assert.NotNull(provider.GetService<ITextCompletion>());
|
||||
Assert.NotNull(provider.GetService<IChatCompletion>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
using BotSharp.Abstraction.Conversations.Models;
|
||||
using BotSharp.Plugin.SemanticKernel;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.SemanticKernel;
|
||||
using Microsoft.SemanticKernel.AI.TextCompletion;
|
||||
using Moq;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
using BotSharp.Abstraction.Agents.Enums;
|
||||
using BotSharp.Abstraction.Agents.Models;
|
||||
using BotSharp.Abstraction.Conversations;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft;
|
||||
using Microsoft.SemanticKernel.AI;
|
||||
using BotSharp.Plugin.SemanticKernel.UnitTests.Helpers;
|
||||
|
||||
namespace BotSharp.Plugin.SemanticKernel.Tests
|
||||
{
|
||||
public class SemanticKernelTextCompletionProviderTests
|
||||
{
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly ITokenStatistics _tokenStatistics;
|
||||
|
||||
public SemanticKernelTextCompletionProviderTests()
|
||||
{
|
||||
|
||||
_services = new ServiceCollection().BuildServiceProvider();
|
||||
_tokenStatistics = Mock.Of<ITokenStatistics>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetCompletion_ReturnsExpectedResult()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
var text = "Hello";
|
||||
var expected = "Hello, world!";
|
||||
var _kernel = new KernelBuilder()
|
||||
.WithAIService<ITextCompletion>("", new SemanticKernelHelper(expected))
|
||||
.Build();
|
||||
var provider = new SemanticKernelTextCompletionProvider(_kernel, _services, _tokenStatistics);
|
||||
|
||||
// Act
|
||||
var result = await provider.GetCompletion(text, "agent1", "message1");
|
||||
|
||||
// Assert
|
||||
Assert.Equal(expected, result);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
1
tests/BotSharp.Plugin.SemanticKernel.UnitTests/Usings.cs
Normal file
1
tests/BotSharp.Plugin.SemanticKernel.UnitTests/Usings.cs
Normal file
|
|
@ -0,0 +1 @@
|
|||
global using Xunit;
|
||||
Loading…
Reference in a new issue