format code and add unit tests

This commit is contained in:
xbotter 2023-11-12 22:04:00 +08:00
parent cd793e228d
commit 19df4da111
No known key found for this signature in database
GPG key ID: D299220A7FE5CF1E
11 changed files with 244 additions and 21 deletions

View file

@ -63,7 +63,9 @@ 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("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.SemanticKernel", "src\Plugins\BotSharp.Plugin.SemanticKernel\BotSharp.Plugin.SemanticKernel.csproj", "{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}"
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
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -233,6 +235,14 @@ Global
{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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -265,6 +275,7 @@ Global
{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}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}

View file

@ -7,10 +7,15 @@
<ItemGroup>
<PackageReference Include="Microsoft.SemanticKernel" 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>

View file

@ -1,17 +1,15 @@
using BotSharp.Abstraction.Agents.Enums;
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.Runtime;
using System.Threading.Tasks;
using BotSharp.Abstraction.Conversations;
using Microsoft.SemanticKernel;
using Microsoft.Extensions.DependencyInjection;
using BotSharp.Abstraction.Models;
namespace BotSharp.Plugin.SemanticKernel
{
@ -33,7 +31,6 @@ namespace BotSharp.Plugin.SemanticKernel
this._tokenStatistics = tokenStatistics;
}
public RoleDialogModel GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
{
var hooks = _services.GetServices<IContentGeneratingHook>().ToList();
@ -61,7 +58,6 @@ namespace BotSharp.Plugin.SemanticKernel
}
}
var response = completion.GetChatCompletionsAsync(chatHistory)
.ContinueWith(async t =>
{
@ -71,7 +67,6 @@ namespace BotSharp.Plugin.SemanticKernel
}).ConfigureAwait(false).GetAwaiter().GetResult()
.ConfigureAwait(false).GetAwaiter().GetResult();
var msg = new RoleDialogModel(AgentRole.Assistant, response)
{
CurrentAgentId = agent.Id
@ -102,4 +97,4 @@ namespace BotSharp.Plugin.SemanticKernel
this._model = model;
}
}
}
}

View file

@ -2,7 +2,6 @@ using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Plugins;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Text;
namespace BotSharp.Plugin.SemanticKernel
{
@ -10,10 +9,11 @@ namespace BotSharp.Plugin.SemanticKernel
{
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>();
}
}
}
}

View file

@ -1,15 +1,16 @@
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.MLTasks;
using Microsoft.SemanticKernel;
using System;
using System.Threading.Tasks;
using Microsoft;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using System.Collections.Generic;
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
{
@ -26,12 +27,13 @@ namespace BotSharp.Plugin.SemanticKernel
IServiceProvider services,
ITokenStatistics tokenStatistics)
{
Requires.NotNull(kernel, nameof(kernel));
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();
@ -68,4 +70,4 @@ namespace BotSharp.Plugin.SemanticKernel
this._model = model;
}
}
}
}

View file

@ -0,0 +1,31 @@
<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="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>

View file

@ -0,0 +1,29 @@
using Microsoft.SemanticKernel.AI.ChatCompletion;
using Microsoft.SemanticKernel.Orchestration;
namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers
{
public class MockChatResult : IChatResult
{
public ModelResult ModelResult { get; set; }
private string _response;
public MockChatResult(string response)
{
ModelResult = new ModelResult(response);
_response = response;
}
public async Task<ChatMessageBase> GetChatMessageAsync(CancellationToken cancellationToken = default)
{
return await Task.FromResult(new MockModelResult(_response));
}
public class MockModelResult : ChatMessageBase
{
public MockModelResult(string content) : base(AuthorRole.Assistant, content, null)
{
}
}
}
}

View file

@ -0,0 +1,68 @@
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;
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")
};
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 MockChatResult("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);
}
}
}

View file

@ -0,0 +1,24 @@
using BotSharp.Abstraction.MLTasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
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();
plugin.RegisterDI(services, config);
var provider = services.BuildServiceProvider();
Assert.NotNull(provider.GetService<ITextCompletion>());
Assert.NotNull(provider.GetService<IChatCompletion>());
}
}
}

View file

@ -0,0 +1,57 @@
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.MLTasks;
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;
namespace BotSharp.Plugin.SemanticKernel.Tests
{
public class SemanticKernelTextCompletionProviderTests
{
private readonly Mock<IKernel> _kernel;
private readonly IServiceProvider _services;
private readonly ITokenStatistics _tokenStatistics;
public SemanticKernelTextCompletionProviderTests()
{
_kernel = new Mock<IKernel>();
_services = new ServiceCollection().BuildServiceProvider();
_tokenStatistics = Mock.Of<ITokenStatistics>();
}
[Fact]
public async Task GetCompletion_ReturnsExpectedResult()
{
// Arrange
var provider = new SemanticKernelTextCompletionProvider(_kernel.Object, _services, _tokenStatistics);
var text = "Hello";
var agentId = "agent1";
var messageId = "message1";
var expected = "Hello, world!";
var mockCompletion = new Mock<Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion>();
mockCompletion.Setup(c => c.CompleteAsync(text, It.IsAny<AIRequestSettings>(), It.IsAny<CancellationToken>())).ReturnsAsync(expected);
_kernel.Setup(c => c.GetService<Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion>(It.IsAny<string>())).Returns(mockCompletion.Object);
// Act
var result = await provider.GetCompletion(text, agentId, messageId);
// Assert
Assert.Equal(expected, result);
mockCompletion.Verify(c => c.CompleteAsync(text, null, default(CancellationToken)), Times.Once);
}
}
}

View file

@ -0,0 +1 @@
global using Xunit;