Skip to content

Commit 19df4da

Browse files
committed
format code and add unit tests
1 parent cd793e2 commit 19df4da

11 files changed

+243
-20
lines changed

BotSharp.sln

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.MongoStorag
6363
EndProject
6464
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.GoogleAI", "src\Plugins\BotSharp.Plugin.GoogleAI\BotSharp.Plugin.GoogleAI.csproj", "{8BC29F8A-78D6-422C-B522-10687ADC38ED}"
6565
EndProject
66-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotSharp.Plugin.SemanticKernel", "src\Plugins\BotSharp.Plugin.SemanticKernel\BotSharp.Plugin.SemanticKernel.csproj", "{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}"
66+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BotSharp.Plugin.SemanticKernel", "src\Plugins\BotSharp.Plugin.SemanticKernel\BotSharp.Plugin.SemanticKernel.csproj", "{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}"
67+
EndProject
68+
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}"
6769
EndProject
6870
Global
6971
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -233,6 +235,14 @@ Global
233235
{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Release|Any CPU.Build.0 = Release|Any CPU
234236
{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Release|x64.ActiveCfg = Release|Any CPU
235237
{73EE2CD0-3B27-4F02-A67B-762CBDD740D0}.Release|x64.Build.0 = Release|Any CPU
238+
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
239+
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}.Debug|Any CPU.Build.0 = Debug|Any CPU
240+
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}.Debug|x64.ActiveCfg = Debug|Any CPU
241+
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}.Debug|x64.Build.0 = Debug|Any CPU
242+
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}.Release|Any CPU.ActiveCfg = Release|Any CPU
243+
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}.Release|Any CPU.Build.0 = Release|Any CPU
244+
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}.Release|x64.ActiveCfg = Release|Any CPU
245+
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247}.Release|x64.Build.0 = Release|Any CPU
236246
EndGlobalSection
237247
GlobalSection(SolutionProperties) = preSolution
238248
HideSolutionNode = FALSE
@@ -265,6 +275,7 @@ Global
265275
{DB3DE37B-1208-4ED3-9615-A52AD0AAD69C} = {5CD330E1-9E5A-4112-8346-6E31CA98EF78}
266276
{8BC29F8A-78D6-422C-B522-10687ADC38ED} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
267277
{73EE2CD0-3B27-4F02-A67B-762CBDD740D0} = {D5293208-2BEF-42FC-A64C-5954F61720BA}
278+
{BC57D428-A1A4-4D38-A2D0-AC6CA943F247} = {32FAFFFE-A4CB-4FEE-BF7C-84518BBC6DCC}
268279
EndGlobalSection
269280
GlobalSection(ExtensibilityGlobals) = postSolution
270281
SolutionGuid = {A9969D89-C98B-40A5-A12B-FC87E55B3A19}

src/Plugins/BotSharp.Plugin.SemanticKernel/BotSharp.Plugin.SemanticKernel.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77

88
<ItemGroup>
99
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta6" />
10+
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="17.6.11" />
1011
</ItemGroup>
1112

1213
<ItemGroup>
1314
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
1415
</ItemGroup>
1516

17+
<ItemGroup>
18+
<InternalsVisibleTo Include="BotSharp.Plugin.SemanticKernel.UnitTests"/>
19+
</ItemGroup>
20+
1621
</Project>

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
using BotSharp.Abstraction.Agents.Enums;
21
using BotSharp.Abstraction.Agents;
2+
using BotSharp.Abstraction.Agents.Enums;
33
using BotSharp.Abstraction.Agents.Models;
4+
using BotSharp.Abstraction.Conversations;
45
using BotSharp.Abstraction.Conversations.Models;
56
using BotSharp.Abstraction.MLTasks;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.SemanticKernel;
69
using System;
710
using System.Collections.Generic;
811
using System.Linq;
9-
using System.Runtime;
1012
using System.Threading.Tasks;
11-
using BotSharp.Abstraction.Conversations;
12-
using Microsoft.SemanticKernel;
13-
using Microsoft.Extensions.DependencyInjection;
14-
using BotSharp.Abstraction.Models;
1513

1614
namespace BotSharp.Plugin.SemanticKernel
1715
{
@@ -33,7 +31,6 @@ public SemanticKernelChatCompletionProvider(IKernel kernel,
3331
this._tokenStatistics = tokenStatistics;
3432
}
3533

36-
3734
public RoleDialogModel GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
3835
{
3936
var hooks = _services.GetServices<IContentGeneratingHook>().ToList();
@@ -61,7 +58,6 @@ public RoleDialogModel GetChatCompletions(Agent agent, List<RoleDialogModel> con
6158
}
6259
}
6360

64-
6561
var response = completion.GetChatCompletionsAsync(chatHistory)
6662
.ContinueWith(async t =>
6763
{
@@ -71,7 +67,6 @@ public RoleDialogModel GetChatCompletions(Agent agent, List<RoleDialogModel> con
7167
}).ConfigureAwait(false).GetAwaiter().GetResult()
7268
.ConfigureAwait(false).GetAwaiter().GetResult();
7369

74-
7570
var msg = new RoleDialogModel(AgentRole.Assistant, response)
7671
{
7772
CurrentAgentId = agent.Id
@@ -102,4 +97,4 @@ public void SetModelName(string model)
10297
this._model = model;
10398
}
10499
}
105-
}
100+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
using BotSharp.Abstraction.Plugins;
33
using Microsoft.Extensions.Configuration;
44
using Microsoft.Extensions.DependencyInjection;
5-
using System.Text;
65

76
namespace BotSharp.Plugin.SemanticKernel
87
{
98
public class SemanticKernelPlugin : IBotSharpPlugin
109
{
1110
public string Name => "Semantic Kernel";
1211
public string Description => "Semantic Kernel Service";
12+
1313
public void RegisterDI(IServiceCollection services, IConfiguration config)
1414
{
1515
services.AddScoped<ITextCompletion, SemanticKernelTextCompletionProvider>();
1616
services.AddScoped<IChatCompletion, SemanticKernelChatCompletionProvider>();
1717
}
1818
}
19-
}
19+
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
using BotSharp.Abstraction.Agents.Enums;
22
using BotSharp.Abstraction.Agents.Models;
3-
using BotSharp.Abstraction.Conversations.Models;
43
using BotSharp.Abstraction.Conversations;
4+
using BotSharp.Abstraction.Conversations.Models;
55
using BotSharp.Abstraction.MLTasks;
6+
using Microsoft;
7+
using Microsoft.Extensions.DependencyInjection;
68
using Microsoft.SemanticKernel;
9+
using Microsoft.SemanticKernel.AI.TextCompletion;
710
using System;
8-
using System.Threading.Tasks;
9-
using Microsoft.Extensions.DependencyInjection;
10-
using System.Linq;
1111
using System.Collections.Generic;
12-
using Microsoft.SemanticKernel.AI.TextCompletion;
12+
using System.Linq;
13+
using System.Threading.Tasks;
1314

1415
namespace BotSharp.Plugin.SemanticKernel
1516
{
@@ -26,12 +27,13 @@ public SemanticKernelTextCompletionProvider(IKernel kernel,
2627
IServiceProvider services,
2728
ITokenStatistics tokenStatistics)
2829
{
30+
Requires.NotNull(kernel, nameof(kernel));
31+
2932
this._kernel = kernel;
3033
this._services = services;
3134
this._tokenStatistics = tokenStatistics;
3235
}
3336

34-
3537
public async Task<string> GetCompletion(string text, string agentId, string messageId)
3638
{
3739
var hooks = _services.GetServices<IContentGeneratingHook>().ToList();
@@ -68,4 +70,4 @@ public void SetModelName(string model)
6870
this._model = model;
6971
}
7072
}
71-
}
73+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
8+
<IsPackable>false</IsPackable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
13+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
15+
<PackageReference Include="Moq" Version="4.20.69" />
16+
<PackageReference Include="xunit" Version="2.4.1" />
17+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
<PrivateAssets>all</PrivateAssets>
20+
</PackageReference>
21+
<PackageReference Include="coverlet.collector" Version="3.1.2">
22+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
23+
<PrivateAssets>all</PrivateAssets>
24+
</PackageReference>
25+
</ItemGroup>
26+
27+
<ItemGroup>
28+
<ProjectReference Include="..\..\src\Plugins\BotSharp.Plugin.SemanticKernel\BotSharp.Plugin.SemanticKernel.csproj" />
29+
</ItemGroup>
30+
31+
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.SemanticKernel.AI.ChatCompletion;
2+
using Microsoft.SemanticKernel.Orchestration;
3+
4+
namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers
5+
{
6+
public class MockChatResult : IChatResult
7+
{
8+
public ModelResult ModelResult { get; set; }
9+
private string _response;
10+
11+
public MockChatResult(string response)
12+
{
13+
ModelResult = new ModelResult(response);
14+
_response = response;
15+
}
16+
17+
public async Task<ChatMessageBase> GetChatMessageAsync(CancellationToken cancellationToken = default)
18+
{
19+
return await Task.FromResult(new MockModelResult(_response));
20+
}
21+
22+
public class MockModelResult : ChatMessageBase
23+
{
24+
public MockModelResult(string content) : base(AuthorRole.Assistant, content, null)
25+
{
26+
}
27+
}
28+
}
29+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using BotSharp.Abstraction.Agents;
5+
using BotSharp.Abstraction.Conversations.Models;
6+
using BotSharp.Abstraction.MLTasks;
7+
using BotSharp.Plugin.SemanticKernel;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.SemanticKernel;
10+
using Moq;
11+
using Xunit;
12+
using BotSharp.Abstraction.Agents.Enums;
13+
using BotSharp.Abstraction.Agents.Models;
14+
using System.Linq;
15+
using System.Runtime;
16+
using BotSharp.Abstraction.Conversations;
17+
using BotSharp.Abstraction.Models;
18+
using Microsoft.SemanticKernel.AI.ChatCompletion;
19+
using Microsoft.SemanticKernel.AI;
20+
using Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk;
21+
22+
namespace BotSharp.Plugin.SemanticKernel.Tests
23+
{
24+
public class SemanticKernelChatCompletionProviderTests
25+
{
26+
private readonly Mock<IKernel> _kernelMock;
27+
private readonly Mock<IServiceProvider> _servicesMock;
28+
private readonly Mock<ITokenStatistics> _tokenStatisticsMock;
29+
private readonly SemanticKernelChatCompletionProvider _provider;
30+
31+
public SemanticKernelChatCompletionProviderTests()
32+
{
33+
_kernelMock = new Mock<IKernel>();
34+
_servicesMock = new Mock<IServiceProvider>();
35+
_tokenStatisticsMock = new Mock<ITokenStatistics>();
36+
_provider = new SemanticKernelChatCompletionProvider(_kernelMock.Object, _servicesMock.Object, _tokenStatisticsMock.Object);
37+
}
38+
39+
[Fact]
40+
public void GetChatCompletions_Returns_RoleDialogModel()
41+
{
42+
// Arrange
43+
var agent = new Agent();
44+
var conversations = new List<RoleDialogModel>
45+
{
46+
new RoleDialogModel(AgentRole.User, "Hello")
47+
};
48+
49+
var chatHistoryMock = new Mock<ChatHistory>();
50+
var chatCompletionMock = new Mock<Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion>();
51+
chatCompletionMock.Setup(x => x.CreateNewChat(It.IsAny<string>())).Returns(chatHistoryMock.Object);
52+
chatCompletionMock.Setup(x => x.GetChatCompletionsAsync(chatHistoryMock.Object, It.IsAny<AIRequestSettings>(), It.IsAny<CancellationToken>()))
53+
.ReturnsAsync(new List<IChatResult>
54+
{
55+
new MockChatResult("How can I help you?")
56+
});
57+
58+
_kernelMock.Setup(x => x.GetService<Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion>(null)).Returns(chatCompletionMock.Object);
59+
60+
// Act
61+
var result = _provider.GetChatCompletions(agent, conversations);
62+
63+
// Assert
64+
Assert.IsType<RoleDialogModel>(result);
65+
}
66+
}
67+
68+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using BotSharp.Abstraction.MLTasks;
2+
using Microsoft.Extensions.Configuration;
3+
using Microsoft.Extensions.DependencyInjection;
4+
5+
namespace BotSharp.Plugin.SemanticKernel.Tests
6+
{
7+
public class SemanticKernelPluginTests
8+
{
9+
[Fact]
10+
public void TestRegisterDI()
11+
{
12+
var services = new ServiceCollection();
13+
var config = new ConfigurationBuilder().Build();
14+
var plugin = new SemanticKernelPlugin();
15+
16+
plugin.RegisterDI(services, config);
17+
18+
var provider = services.BuildServiceProvider();
19+
20+
Assert.NotNull(provider.GetService<ITextCompletion>());
21+
Assert.NotNull(provider.GetService<IChatCompletion>());
22+
}
23+
}
24+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using BotSharp.Abstraction.Conversations.Models;
2+
using BotSharp.Abstraction.MLTasks;
3+
using BotSharp.Plugin.SemanticKernel;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.SemanticKernel;
6+
using Microsoft.SemanticKernel.AI.TextCompletion;
7+
using Moq;
8+
using System.Collections.Generic;
9+
using System.Threading.Tasks;
10+
using Xunit;
11+
using BotSharp.Abstraction.Agents.Enums;
12+
using BotSharp.Abstraction.Agents.Models;
13+
using BotSharp.Abstraction.Conversations;
14+
using System;
15+
using System.Linq;
16+
using Microsoft;
17+
using Microsoft.SemanticKernel.AI;
18+
19+
namespace BotSharp.Plugin.SemanticKernel.Tests
20+
{
21+
public class SemanticKernelTextCompletionProviderTests
22+
{
23+
private readonly Mock<IKernel> _kernel;
24+
private readonly IServiceProvider _services;
25+
private readonly ITokenStatistics _tokenStatistics;
26+
27+
public SemanticKernelTextCompletionProviderTests()
28+
{
29+
_kernel = new Mock<IKernel>();
30+
_services = new ServiceCollection().BuildServiceProvider();
31+
_tokenStatistics = Mock.Of<ITokenStatistics>();
32+
}
33+
34+
[Fact]
35+
public async Task GetCompletion_ReturnsExpectedResult()
36+
{
37+
// Arrange
38+
var provider = new SemanticKernelTextCompletionProvider(_kernel.Object, _services, _tokenStatistics);
39+
var text = "Hello";
40+
var agentId = "agent1";
41+
var messageId = "message1";
42+
var expected = "Hello, world!";
43+
44+
var mockCompletion = new Mock<Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion>();
45+
mockCompletion.Setup(c => c.CompleteAsync(text, It.IsAny<AIRequestSettings>(), It.IsAny<CancellationToken>())).ReturnsAsync(expected);
46+
_kernel.Setup(c => c.GetService<Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion>(It.IsAny<string>())).Returns(mockCompletion.Object);
47+
48+
// Act
49+
var result = await provider.GetCompletion(text, agentId, messageId);
50+
51+
// Assert
52+
Assert.Equal(expected, result);
53+
mockCompletion.Verify(c => c.CompleteAsync(text, null, default(CancellationToken)), Times.Once);
54+
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)