Skip to content

Commit c319b3e

Browse files
authored
Merge pull request #279 from SciSharp/sk1.2
feat: upgrade semantic kernel to 1.2
2 parents 3ac6e08 + 3f2d63c commit c319b3e

11 files changed

+102
-105
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
@@ -11,8 +11,8 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.SemanticKernel.Abstractions" Version="1.0.0-beta8" />
15-
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.0.0-beta8" />
14+
<PackageReference Include="Microsoft.SemanticKernel.Abstractions" Version="1.2.0" />
15+
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.2.0-alpha" />
1616
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="17.8.8" />
1717
</ItemGroup>
1818

@@ -24,4 +24,8 @@
2424
<InternalsVisibleTo Include="BotSharp.Plugin.SemanticKernel.UnitTests" />
2525
</ItemGroup>
2626

27+
<ItemGroup>
28+
<Folder Include="packages\" />
29+
</ItemGroup>
30+
2731
</Project>

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using BotSharp.Abstraction.Loggers;
77
using BotSharp.Abstraction.MLTasks;
88
using Microsoft.Extensions.DependencyInjection;
9-
using Microsoft.SemanticKernel;
9+
using Microsoft.SemanticKernel.ChatCompletion;
1010
using System;
1111
using System.Collections.Generic;
1212
using System.Linq;
@@ -19,7 +19,7 @@ namespace BotSharp.Plugin.SemanticKernel
1919
/// </summary>
2020
public class SemanticKernelChatCompletionProvider : IChatCompletion
2121
{
22-
private Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion _kernelChatCompletion;
22+
private Microsoft.SemanticKernel.ChatCompletion.IChatCompletionService _kernelChatCompletion;
2323
private IServiceProvider _services;
2424
private ITokenStatistics _tokenStatistics;
2525
private string? _model = null;
@@ -33,7 +33,7 @@ public class SemanticKernelChatCompletionProvider : IChatCompletion
3333
/// <param name="chatCompletion"></param>
3434
/// <param name="services"></param>
3535
/// <param name="tokenStatistics"></param>
36-
public SemanticKernelChatCompletionProvider(Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion chatCompletion,
36+
public SemanticKernelChatCompletionProvider(IChatCompletionService chatCompletion,
3737
IServiceProvider services,
3838
ITokenStatistics tokenStatistics)
3939
{
@@ -55,7 +55,7 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
5555
var agentService = _services.GetRequiredService<IAgentService>();
5656
var instruction = agentService.RenderedInstruction(agent);
5757

58-
var chatHistory = completion.CreateNewChat(instruction);
58+
ChatHistory chatHistory = new ChatHistory(instruction);
5959

6060
foreach (var message in conversations)
6161
{
@@ -69,14 +69,9 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
6969
}
7070
}
7171

72-
var response = await completion.GetChatCompletionsAsync(chatHistory)
73-
.ContinueWith(async t =>
74-
{
75-
var result = await t;
76-
var message = await result.First().GetChatMessageAsync();
77-
return message.Content;
78-
}).ConfigureAwait(false).GetAwaiter().GetResult();
79-
72+
var ChatMessage = await completion.GetChatMessageContentsAsync(chatHistory);
73+
var chatMessageContent = ChatMessage?.FirstOrDefault();
74+
var response = chatMessageContent != null ? chatMessageContent.Content :string.Empty;
8075
var msg = new RoleDialogModel(AgentRole.Assistant, response)
8176
{
8277
CurrentAgentId = agent.Id

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ namespace BotSharp.Plugin.SemanticKernel
99
{
1010
internal class SemanticKernelMemoryStoreProvider : IVectorDb
1111
{
12+
#pragma warning disable SKEXP0003 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
1213
private readonly IMemoryStore _memoryStore;
14+
#pragma warning restore SKEXP0003 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
1315

16+
#pragma warning disable SKEXP0003 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
1417
public SemanticKernelMemoryStoreProvider(IMemoryStore memoryStore)
18+
#pragma warning restore SKEXP0003 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
1519
{
1620
this._memoryStore = memoryStore;
1721
}
@@ -46,7 +50,9 @@ public async Task<List<string>> Search(string collectionName, float[] vector, in
4650

4751
public async Task Upsert(string collectionName, int id, float[] vector, string text)
4852
{
53+
#pragma warning disable SKEXP0003 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
4954
await _memoryStore.UpsertAsync(collectionName, MemoryRecord.LocalRecord(id.ToString(), text, null, vector));
55+
#pragma warning restore SKEXP0003 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
5056
}
5157
}
5258
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
using BotSharp.Abstraction.Conversations;
44
using BotSharp.Abstraction.Conversations.Models;
55
using BotSharp.Abstraction.Loggers;
6-
using Microsoft;
76
using Microsoft.Extensions.DependencyInjection;
8-
using Microsoft.SemanticKernel;
9-
using Microsoft.SemanticKernel.AI.TextCompletion;
107
using System;
118
using System.Collections.Generic;
129
using System.Linq;
@@ -19,7 +16,7 @@ namespace BotSharp.Plugin.SemanticKernel
1916
/// </summary>
2017
public class SemanticKernelTextCompletionProvider : Abstraction.MLTasks.ITextCompletion
2118
{
22-
private readonly Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion _kernelTextCompletion;
19+
private readonly Microsoft.SemanticKernel.TextGeneration.ITextGenerationService _kernelTextCompletion;
2320
private readonly IServiceProvider _services;
2421
private readonly ITokenStatistics _tokenStatistics;
2522
private string? _model = null;
@@ -33,7 +30,7 @@ public class SemanticKernelTextCompletionProvider : Abstraction.MLTasks.ITextCom
3330
/// <param name="textCompletion"></param>
3431
/// <param name="services"></param>
3532
/// <param name="tokenStatistics"></param>
36-
public SemanticKernelTextCompletionProvider(Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion textCompletion,
33+
public SemanticKernelTextCompletionProvider(Microsoft.SemanticKernel.TextGeneration.ITextGenerationService textCompletion,
3734
IServiceProvider services,
3835
ITokenStatistics tokenStatistics)
3936
{
@@ -61,9 +58,11 @@ public async Task<string> GetCompletion(string text, string agentId, string mess
6158

6259
var completion = this._kernelTextCompletion;
6360
_tokenStatistics.StartTimer();
64-
var result = await completion.CompleteAsync(text);
61+
var textContent = await completion.GetTextContentsAsync(text);
62+
var result = textContent.FirstOrDefault().Text;
6563
_tokenStatistics.StopTimer();
6664

65+
6766
// After chat completion hook
6867
Task.WaitAll(hooks.Select(hook =>
6968
hook.AfterGenerated(new RoleDialogModel(AgentRole.Assistant, result), new TokenStatsModel

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using BotSharp.Abstraction.MLTasks;
22
using Microsoft.Extensions.Configuration;
3-
using Microsoft.SemanticKernel.AI.Embeddings;
3+
using Microsoft.SemanticKernel.Embeddings;
44
using System.Collections.Generic;
55
using System.Linq;
66
using System.Threading.Tasks;
@@ -12,13 +12,17 @@ namespace BotSharp.Plugin.SemanticKernel
1212
/// </summary>
1313
public class SemanticKernelTextEmbeddingProvider : ITextEmbedding
1414
{
15-
private readonly ITextEmbeddingGeneration _embedding;
15+
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
16+
private readonly ITextEmbeddingGenerationService _embedding;
17+
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
1618
private readonly IConfiguration _configuration;
1719

1820
/// <summary>
1921
/// Constructor of <see cref="SemanticKernelTextEmbeddingProvider"/>
2022
/// </summary>
21-
public SemanticKernelTextEmbeddingProvider(ITextEmbeddingGeneration embedding, IConfiguration configuration)
23+
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
24+
public SemanticKernelTextEmbeddingProvider(ITextEmbeddingGenerationService embedding, IConfiguration configuration)
25+
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
2226
{
2327
this._embedding = embedding;
2428
this._configuration = configuration;
@@ -33,7 +37,9 @@ public SemanticKernelTextEmbeddingProvider(ITextEmbeddingGeneration embedding, I
3337
/// <inheritdoc/>
3438
public async Task<float[]> GetVectorAsync(string text)
3539
{
40+
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
3641
return (await this._embedding.GenerateEmbeddingAsync(text)).ToArray();
42+
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
3743
}
3844

3945
/// <inheritdoc/>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
1313
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
15-
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta6" />
15+
<PackageReference Include="Microsoft.SemanticKernel" Version="1.2.0" />
1616
<PackageReference Include="Moq" Version="4.20.70" />
17-
<PackageReference Include="xunit" Version="2.6.4" />
17+
<PackageReference Include="xunit" Version="2.6.6" />
1818
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2020
<PrivateAssets>all</PrivateAssets>
Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
1-
using Microsoft.SemanticKernel.AI.ChatCompletion;
2-
using Microsoft.SemanticKernel.AI.TextCompletion;
3-
using Microsoft.SemanticKernel.Orchestration;
1+
using Microsoft.SemanticKernel;
2+
using Microsoft.SemanticKernel.ChatCompletion;
43

5-
namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers
6-
{
7-
public class ResultHelper : IChatResult, ITextResult
8-
{
9-
public ModelResult ModelResult { get; set; }
10-
private string _response;
4+
//namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers
5+
//{
6+
// public class ResultHelper : KernelContent
7+
// {
8+
// public TextContent ModelResult { get; set; }
9+
// private string _response;
1110

12-
public ResultHelper(string response)
13-
{
14-
ModelResult = new ModelResult(response);
15-
_response = response;
16-
}
11+
// public ResultHelper(string response)
12+
// {
13+
// ModelResult = new TextContent(response);
14+
// _response = response;
15+
// }
1716

18-
public async Task<ChatMessage> GetChatMessageAsync(CancellationToken cancellationToken = default)
19-
{
20-
return await Task.FromResult(new MockModelResult(_response));
21-
}
17+
// public async Task<ChatMessageContent> GetChatMessageAsync(CancellationToken cancellationToken = default)
18+
// {
19+
// return await Task.FromResult(new MockModelResult(_response));
20+
// }
2221

23-
public Task<string> GetCompletionAsync(CancellationToken cancellationToken = default)
24-
{
25-
return Task.FromResult(_response);
26-
}
22+
// public Task<string> GetCompletionAsync(CancellationToken cancellationToken = default)
23+
// {
24+
// return Task.FromResult(_response);
25+
// }
2726

28-
public class MockModelResult : ChatMessage
29-
{
30-
public MockModelResult(string content) : base(AuthorRole.Assistant, content, null)
31-
{
32-
}
33-
}
34-
}
35-
}
27+
// public class MockModelResult : ChatMessageContent
28+
// {
29+
// public MockModelResult(string content) : base(AuthorRole.Assistant, content, null)
30+
// {
31+
// }
32+
// }
33+
// }
34+
//}

tests/BotSharp.Plugin.SemanticKernel.UnitTests/Helpers/SemanticKernelHelper.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
using Microsoft.SemanticKernel.AI;
2-
using Microsoft.SemanticKernel.AI.ChatCompletion;
3-
using Microsoft.SemanticKernel.AI.TextCompletion;
1+
using Microsoft.SemanticKernel;
2+
using Microsoft.SemanticKernel.ChatCompletion;
43
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;
4+
using Microsoft.SemanticKernel.TextGeneration;
105

116
namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers
127
{
13-
internal class SemanticKernelHelper : IChatCompletion, ITextCompletion, IAIService
8+
internal class SemanticKernelHelper : IChatCompletionService, ITextGenerationService, IAIService
149
{
1510
private Dictionary<string, string> _attributes = new();
1611
private readonly string _excepted;
@@ -22,27 +17,30 @@ public SemanticKernelHelper(string excepted)
2217

2318
public IReadOnlyDictionary<string, string> Attributes => _attributes;
2419

20+
IReadOnlyDictionary<string, object?> IAIService.Attributes => throw new NotImplementedException();
21+
2522
public ChatHistory CreateNewChat(string? instructions = null)
2623
{
2724
return new ChatHistory();
2825
}
2926

30-
public Task<IReadOnlyList<IChatResult>> GetChatCompletionsAsync(ChatHistory chat, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
27+
public Task<IReadOnlyList<ChatMessageContent>> GetChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default)
3128
{
32-
return Task.FromResult<IReadOnlyList<IChatResult>>(new List<IChatResult> { new ResultHelper(_excepted) });
29+
return Task.FromResult<IReadOnlyList<ChatMessageContent>>(new List<ChatMessageContent> { new ChatMessageContent(AuthorRole.Assistant, _excepted) });
30+
3331
}
3432

35-
public Task<IReadOnlyList<ITextResult>> GetCompletionsAsync(string text, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
33+
public IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default)
3634
{
37-
return Task.FromResult<IReadOnlyList<ITextResult>>(new List<ITextResult> { new ResultHelper(_excepted) });
35+
throw new NotImplementedException();
3836
}
3937

40-
public IAsyncEnumerable<IChatStreamingResult> GetStreamingChatCompletionsAsync(ChatHistory chat, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
38+
public Task<IReadOnlyList<TextContent>> GetTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default)
4139
{
42-
throw new NotImplementedException();
40+
return Task.FromResult<IReadOnlyList<TextContent>>(new List<TextContent> { new TextContent(_excepted) });
4341
}
4442

45-
public IAsyncEnumerable<ITextStreamingResult> GetStreamingCompletionsAsync(string text, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
43+
public IAsyncEnumerable<StreamingTextContent> GetStreamingTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default)
4644
{
4745
throw new NotImplementedException();
4846
}

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
using BotSharp.Abstraction.Agents;
2-
using BotSharp.Abstraction.Conversations.Models;
3-
using Moq;
42
using BotSharp.Abstraction.Agents.Enums;
53
using BotSharp.Abstraction.Agents.Models;
64
using BotSharp.Abstraction.Conversations;
7-
using Microsoft.SemanticKernel.AI.ChatCompletion;
8-
using Microsoft.SemanticKernel.AI;
9-
using BotSharp.Plugin.SemanticKernel.UnitTests.Helpers;
5+
using BotSharp.Abstraction.Conversations.Models;
106
using BotSharp.Abstraction.Loggers;
7+
using Microsoft.SemanticKernel;
8+
using Microsoft.SemanticKernel.ChatCompletion;
9+
using Moq;
1110

1211
namespace BotSharp.Plugin.SemanticKernel.Tests
1312
{
1413
public class SemanticKernelChatCompletionProviderTests
1514
{
16-
private readonly Mock<IChatCompletion> _chatCompletionMock;
15+
private readonly Mock<IChatCompletionService> _chatCompletionMock;
1716
private readonly Mock<IServiceProvider> _servicesMock;
1817
private readonly Mock<ITokenStatistics> _tokenStatisticsMock;
1918
private readonly SemanticKernelChatCompletionProvider _provider;
2019

2120
public SemanticKernelChatCompletionProviderTests()
2221
{
23-
_chatCompletionMock = new Mock<IChatCompletion>();
22+
_chatCompletionMock = new Mock<IChatCompletionService>();
2423
_servicesMock = new Mock<IServiceProvider>();
2524
_tokenStatisticsMock = new Mock<ITokenStatistics>();
2625
_provider = new SemanticKernelChatCompletionProvider(_chatCompletionMock.Object, _servicesMock.Object, _tokenStatisticsMock.Object);
@@ -39,18 +38,18 @@ public async void GetChatCompletions_Returns_RoleDialogModel()
3938
_servicesMock.Setup(x => x.GetService(typeof(IEnumerable<IContentGeneratingHook>)))
4039
.Returns(new List<IContentGeneratingHook>());
4140
var agentService = new Mock<IAgentService>();
42-
agentService.Setup(x => x.RenderedInstruction(agent)).Returns("");
41+
agentService.Setup(x => x.RenderedInstruction(agent)).Returns("How can I help you?");
4342
_servicesMock.Setup(x => x.GetService(typeof(IAgentService)))
4443
.Returns(agentService.Object);
4544

4645
var chatHistoryMock = new Mock<ChatHistory>();
47-
_chatCompletionMock.Setup(x => x.CreateNewChat(It.IsAny<string>())).Returns(chatHistoryMock.Object);
48-
_chatCompletionMock.Setup(x => x.GetChatCompletionsAsync(chatHistoryMock.Object, It.IsAny<AIRequestSettings>(), It.IsAny<CancellationToken>()))
49-
.ReturnsAsync(new List<IChatResult>
50-
{
51-
new ResultHelper("How can I help you?")
52-
});
53-
46+
//_chatCompletionMock.Setup(x => new ChatHistory(It.IsAny<string>())).Returns(chatHistoryMock.Object);
47+
48+
_chatCompletionMock.Setup(x => x.GetChatMessageContentsAsync(chatHistoryMock.Object, It.IsAny<PromptExecutionSettings>(), It.IsAny<Kernel>(), It.IsAny<CancellationToken>()))
49+
.ReturnsAsync(new List<ChatMessageContent>
50+
{
51+
new ChatMessageContent(AuthorRole.Assistant,"How can I help you?")
52+
});
5453

5554
// Act
5655
var result = await _provider.GetChatCompletions(agent, conversations);

0 commit comments

Comments
 (0)