Skip to content

Commit 0dbecdf

Browse files
committed
update SK Provider and UnitTest
1 parent 83e3c38 commit 0dbecdf

9 files changed

+104
-22
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
55
<Nullable>enable</Nullable>
6+
<LangVersion>$(LangVersion)</LangVersion>
7+
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
8+
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
9+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
610
</PropertyGroup>
711

812
<ItemGroup>
9-
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta6" />
13+
<PackageReference Include="Microsoft.SemanticKernel.Abstractions" Version="1.0.0-beta6" />
1014
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="17.6.11" />
1115
</ItemGroup>
1216

@@ -15,7 +19,7 @@
1519
</ItemGroup>
1620

1721
<ItemGroup>
18-
<InternalsVisibleTo Include="BotSharp.Plugin.SemanticKernel.UnitTests"/>
22+
<InternalsVisibleTo Include="BotSharp.Plugin.SemanticKernel.UnitTests" />
1923
</ItemGroup>
2024

2125
</Project>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class SemanticKernelChatCompletionProvider : IChatCompletion
2020
private ITokenStatistics _tokenStatistics;
2121
private string? _model = null;
2222

23-
public string Provider => throw new NotImplementedException();
23+
public string Provider => "semantic-kernel";
2424

2525
public SemanticKernelChatCompletionProvider(IKernel kernel,
2626
IServiceProvider services,
@@ -92,9 +92,11 @@ public Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogM
9292
throw new NotImplementedException();
9393
}
9494

95+
9596
public void SetModelName(string model)
9697
{
97-
this._model = model;
98+
if (!string.IsNullOrWhiteSpace(model))
99+
this._model = model;
98100
}
99101
}
100102
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,31 @@
1414

1515
namespace BotSharp.Plugin.SemanticKernel
1616
{
17+
/// <summary>
18+
/// User Semantic Kernel as text completion provider
19+
/// </summary>
1720
public class SemanticKernelTextCompletionProvider : Abstraction.MLTasks.ITextCompletion
1821
{
1922
private readonly IKernel _kernel;
2023
private readonly IServiceProvider _services;
2124
private readonly ITokenStatistics _tokenStatistics;
2225
private string? _model = null;
2326

27+
// <inheritdoc/>
2428
public string Provider => "semantic-kernel";
2529

2630
public SemanticKernelTextCompletionProvider(IKernel kernel,
2731
IServiceProvider services,
2832
ITokenStatistics tokenStatistics)
2933
{
30-
Requires.NotNull(kernel, nameof(kernel));
34+
Requires.NotNull(kernel, nameof(IKernel));
3135

3236
this._kernel = kernel;
3337
this._services = services;
3438
this._tokenStatistics = tokenStatistics;
3539
}
3640

41+
/// <inheritdoc/>
3742
public async Task<string> GetCompletion(string text, string agentId, string messageId)
3843
{
3944
var hooks = _services.GetServices<IContentGeneratingHook>().ToList();
@@ -65,9 +70,11 @@ public async Task<string> GetCompletion(string text, string agentId, string mess
6570
return result;
6671
}
6772

73+
/// <inheritdoc/>
6874
public void SetModelName(string model)
6975
{
70-
this._model = model;
76+
if (!string.IsNullOrWhiteSpace(model))
77+
this._model = model;
7178
}
7279
}
7380
}

tests/BotSharp.Plugin.SemanticKernel.UnitTests/BotSharp.Plugin.SemanticKernel.UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
1313
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
15+
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta6" />
1516
<PackageReference Include="Moq" Version="4.20.69" />
1617
<PackageReference Include="xunit" Version="2.4.1" />
1718
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">

tests/BotSharp.Plugin.SemanticKernel.UnitTests/Helpers/MockChatResult.cs renamed to tests/BotSharp.Plugin.SemanticKernel.UnitTests/Helpers/ResultHelper.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
using Microsoft.SemanticKernel.AI.ChatCompletion;
2+
using Microsoft.SemanticKernel.AI.TextCompletion;
23
using Microsoft.SemanticKernel.Orchestration;
34

45
namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers
56
{
6-
public class MockChatResult : IChatResult
7+
public class ResultHelper : IChatResult, ITextResult
78
{
89
public ModelResult ModelResult { get; set; }
910
private string _response;
1011

11-
public MockChatResult(string response)
12+
public ResultHelper(string response)
1213
{
1314
ModelResult = new ModelResult(response);
1415
_response = response;
@@ -19,6 +20,11 @@ public async Task<ChatMessageBase> GetChatMessageAsync(CancellationToken cancell
1920
return await Task.FromResult(new MockModelResult(_response));
2021
}
2122

23+
public Task<string> GetCompletionAsync(CancellationToken cancellationToken = default)
24+
{
25+
return Task.FromResult(_response);
26+
}
27+
2228
public class MockModelResult : ChatMessageBase
2329
{
2430
public MockModelResult(string content) : base(AuthorRole.Assistant, content, null)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.SemanticKernel.AI;
2+
using Microsoft.SemanticKernel.AI.ChatCompletion;
3+
using Microsoft.SemanticKernel.AI.TextCompletion;
4+
using Microsoft.SemanticKernel.Services;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers
12+
{
13+
internal class SemanticKernelHelper : IChatCompletion, ITextCompletion, IAIService
14+
{
15+
private readonly string _excepted;
16+
17+
public SemanticKernelHelper(string excepted)
18+
{
19+
this._excepted = excepted;
20+
}
21+
22+
public ChatHistory CreateNewChat(string? instructions = null)
23+
{
24+
return new ChatHistory();
25+
}
26+
27+
public Task<IReadOnlyList<IChatResult>> GetChatCompletionsAsync(ChatHistory chat, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
28+
{
29+
return Task.FromResult<IReadOnlyList<IChatResult>>( new List<IChatResult> { new ResultHelper(_excepted) });
30+
}
31+
32+
public Task<IReadOnlyList<ITextResult>> GetCompletionsAsync(string text, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
33+
{
34+
return Task.FromResult<IReadOnlyList<ITextResult>>(new List<ITextResult> { new ResultHelper(_excepted) });
35+
}
36+
37+
public IAsyncEnumerable<IChatStreamingResult> GetStreamingChatCompletionsAsync(ChatHistory chat, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
38+
{
39+
throw new NotImplementedException();
40+
}
41+
42+
public IAsyncEnumerable<ITextStreamingResult> GetStreamingCompletionsAsync(string text, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
43+
{
44+
throw new NotImplementedException();
45+
}
46+
}
47+
}

tests/BotSharp.Plugin.SemanticKernel.UnitTests/SemanticKernelChatCompletionProviderTests.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Microsoft.SemanticKernel.AI.ChatCompletion;
1919
using Microsoft.SemanticKernel.AI;
2020
using Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk;
21+
using BotSharp.Plugin.SemanticKernel.UnitTests.Helpers;
2122

2223
namespace BotSharp.Plugin.SemanticKernel.Tests
2324
{
@@ -46,13 +47,20 @@ public void GetChatCompletions_Returns_RoleDialogModel()
4647
new RoleDialogModel(AgentRole.User, "Hello")
4748
};
4849

50+
_servicesMock.Setup(x => x.GetService(typeof(IEnumerable<IContentGeneratingHook>)))
51+
.Returns(new List<IContentGeneratingHook>());
52+
var agentService = new Mock<IAgentService>();
53+
agentService.Setup(x => x.RenderedInstruction(agent)).Returns("");
54+
_servicesMock.Setup(x => x.GetService(typeof(IAgentService)))
55+
.Returns(agentService.Object);
56+
4957
var chatHistoryMock = new Mock<ChatHistory>();
5058
var chatCompletionMock = new Mock<Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion>();
5159
chatCompletionMock.Setup(x => x.CreateNewChat(It.IsAny<string>())).Returns(chatHistoryMock.Object);
5260
chatCompletionMock.Setup(x => x.GetChatCompletionsAsync(chatHistoryMock.Object, It.IsAny<AIRequestSettings>(), It.IsAny<CancellationToken>()))
5361
.ReturnsAsync(new List<IChatResult>
5462
{
55-
new MockChatResult("How can I help you?")
63+
new ResultHelper("How can I help you?")
5664
});
5765

5866
_kernelMock.Setup(x => x.GetService<Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion>(null)).Returns(chatCompletionMock.Object);
@@ -64,5 +72,5 @@ public void GetChatCompletions_Returns_RoleDialogModel()
6472
Assert.IsType<RoleDialogModel>(result);
6573
}
6674
}
67-
75+
6876
}

tests/BotSharp.Plugin.SemanticKernel.UnitTests/SemanticKernelPluginTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
using BotSharp.Abstraction.Conversations;
12
using BotSharp.Abstraction.MLTasks;
23
using Microsoft.Extensions.Configuration;
34
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.SemanticKernel;
6+
using Moq;
47

58
namespace BotSharp.Plugin.SemanticKernel.Tests
69
{
@@ -12,6 +15,14 @@ public void TestRegisterDI()
1215
var services = new ServiceCollection();
1316
var config = new ConfigurationBuilder().Build();
1417
var plugin = new SemanticKernelPlugin();
18+
services.AddScoped(x =>
19+
{
20+
return new KernelBuilder()
21+
.WithAzureOpenAIChatCompletionService("test", "test", "test")
22+
.Build();
23+
});
24+
services.AddScoped<ITokenStatistics>(x=> Mock.Of<ITokenStatistics>());
25+
1526

1627
plugin.RegisterDI(services, config);
1728

tests/BotSharp.Plugin.SemanticKernel.UnitTests/SemanticKernelTextCompletionProviderTests.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using BotSharp.Abstraction.Conversations.Models;
2-
using BotSharp.Abstraction.MLTasks;
32
using BotSharp.Plugin.SemanticKernel;
43
using Microsoft.Extensions.DependencyInjection;
54
using Microsoft.SemanticKernel;
@@ -15,18 +14,18 @@
1514
using System.Linq;
1615
using Microsoft;
1716
using Microsoft.SemanticKernel.AI;
17+
using BotSharp.Plugin.SemanticKernel.UnitTests.Helpers;
1818

1919
namespace BotSharp.Plugin.SemanticKernel.Tests
2020
{
2121
public class SemanticKernelTextCompletionProviderTests
2222
{
23-
private readonly Mock<IKernel> _kernel;
2423
private readonly IServiceProvider _services;
2524
private readonly ITokenStatistics _tokenStatistics;
2625

2726
public SemanticKernelTextCompletionProviderTests()
2827
{
29-
_kernel = new Mock<IKernel>();
28+
3029
_services = new ServiceCollection().BuildServiceProvider();
3130
_tokenStatistics = Mock.Of<ITokenStatistics>();
3231
}
@@ -35,22 +34,19 @@ public SemanticKernelTextCompletionProviderTests()
3534
public async Task GetCompletion_ReturnsExpectedResult()
3635
{
3736
// Arrange
38-
var provider = new SemanticKernelTextCompletionProvider(_kernel.Object, _services, _tokenStatistics);
37+
3938
var text = "Hello";
40-
var agentId = "agent1";
41-
var messageId = "message1";
4239
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);
40+
var _kernel = new KernelBuilder()
41+
.WithAIService<ITextCompletion>("", new SemanticKernelHelper(expected))
42+
.Build();
43+
var provider = new SemanticKernelTextCompletionProvider(_kernel, _services, _tokenStatistics);
4744

4845
// Act
49-
var result = await provider.GetCompletion(text, agentId, messageId);
46+
var result = await provider.GetCompletion(text, "agent1", "message1");
5047

5148
// Assert
5249
Assert.Equal(expected, result);
53-
mockCompletion.Verify(c => c.CompleteAsync(text, null, default(CancellationToken)), Times.Once);
5450

5551
}
5652
}

0 commit comments

Comments
 (0)