Skip to content

Commit

Permalink
Merge pull request #279 from SciSharp/sk1.2
Browse files Browse the repository at this point in the history
feat: upgrade semantic kernel  to 1.2
  • Loading branch information
Oceania2018 authored Jan 30, 2024
2 parents 3ac6e08 + 3f2d63c commit c319b3e
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
Expand All @@ -11,8 +11,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SemanticKernel.Abstractions" Version="1.0.0-beta8" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.0.0-beta8" />
<PackageReference Include="Microsoft.SemanticKernel.Abstractions" Version="1.2.0" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.2.0-alpha" />
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="17.8.8" />
</ItemGroup>

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

<ItemGroup>
<Folder Include="packages\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using BotSharp.Abstraction.Loggers;
using BotSharp.Abstraction.MLTasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -19,7 +19,7 @@ namespace BotSharp.Plugin.SemanticKernel
/// </summary>
public class SemanticKernelChatCompletionProvider : IChatCompletion
{
private Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion _kernelChatCompletion;
private Microsoft.SemanticKernel.ChatCompletion.IChatCompletionService _kernelChatCompletion;
private IServiceProvider _services;
private ITokenStatistics _tokenStatistics;
private string? _model = null;
Expand All @@ -33,7 +33,7 @@ public class SemanticKernelChatCompletionProvider : IChatCompletion
/// <param name="chatCompletion"></param>
/// <param name="services"></param>
/// <param name="tokenStatistics"></param>
public SemanticKernelChatCompletionProvider(Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion chatCompletion,
public SemanticKernelChatCompletionProvider(IChatCompletionService chatCompletion,
IServiceProvider services,
ITokenStatistics tokenStatistics)
{
Expand All @@ -55,7 +55,7 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
var agentService = _services.GetRequiredService<IAgentService>();
var instruction = agentService.RenderedInstruction(agent);

var chatHistory = completion.CreateNewChat(instruction);
ChatHistory chatHistory = new ChatHistory(instruction);

foreach (var message in conversations)
{
Expand All @@ -69,14 +69,9 @@ public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDial
}
}

var response = await completion.GetChatCompletionsAsync(chatHistory)
.ContinueWith(async t =>
{
var result = await t;
var message = await result.First().GetChatMessageAsync();
return message.Content;
}).ConfigureAwait(false).GetAwaiter().GetResult();

var ChatMessage = await completion.GetChatMessageContentsAsync(chatHistory);
var chatMessageContent = ChatMessage?.FirstOrDefault();
var response = chatMessageContent != null ? chatMessageContent.Content :string.Empty;
var msg = new RoleDialogModel(AgentRole.Assistant, response)
{
CurrentAgentId = agent.Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ namespace BotSharp.Plugin.SemanticKernel
{
internal class SemanticKernelMemoryStoreProvider : IVectorDb
{
#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.
private readonly IMemoryStore _memoryStore;
#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.

#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.
public SemanticKernelMemoryStoreProvider(IMemoryStore memoryStore)
#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.
{
this._memoryStore = memoryStore;
}
Expand Down Expand Up @@ -46,7 +50,9 @@ public async Task<List<string>> Search(string collectionName, float[] vector, in

public async Task Upsert(string collectionName, int id, float[] vector, string text)
{
#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.
await _memoryStore.UpsertAsync(collectionName, MemoryRecord.LocalRecord(id.ToString(), text, null, vector));
#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.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Loggers;
using Microsoft;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AI.TextCompletion;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -19,7 +16,7 @@ namespace BotSharp.Plugin.SemanticKernel
/// </summary>
public class SemanticKernelTextCompletionProvider : Abstraction.MLTasks.ITextCompletion
{
private readonly Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion _kernelTextCompletion;
private readonly Microsoft.SemanticKernel.TextGeneration.ITextGenerationService _kernelTextCompletion;
private readonly IServiceProvider _services;
private readonly ITokenStatistics _tokenStatistics;
private string? _model = null;
Expand All @@ -33,7 +30,7 @@ public class SemanticKernelTextCompletionProvider : Abstraction.MLTasks.ITextCom
/// <param name="textCompletion"></param>
/// <param name="services"></param>
/// <param name="tokenStatistics"></param>
public SemanticKernelTextCompletionProvider(Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion textCompletion,
public SemanticKernelTextCompletionProvider(Microsoft.SemanticKernel.TextGeneration.ITextGenerationService textCompletion,
IServiceProvider services,
ITokenStatistics tokenStatistics)
{
Expand Down Expand Up @@ -61,9 +58,11 @@ public async Task<string> GetCompletion(string text, string agentId, string mess

var completion = this._kernelTextCompletion;
_tokenStatistics.StartTimer();
var result = await completion.CompleteAsync(text);
var textContent = await completion.GetTextContentsAsync(text);
var result = textContent.FirstOrDefault().Text;
_tokenStatistics.StopTimer();


// After chat completion hook
Task.WaitAll(hooks.Select(hook =>
hook.AfterGenerated(new RoleDialogModel(AgentRole.Assistant, result), new TokenStatsModel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using BotSharp.Abstraction.MLTasks;
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel.AI.Embeddings;
using Microsoft.SemanticKernel.Embeddings;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -12,13 +12,17 @@ namespace BotSharp.Plugin.SemanticKernel
/// </summary>
public class SemanticKernelTextEmbeddingProvider : ITextEmbedding
{
private readonly ITextEmbeddingGeneration _embedding;
#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.
private readonly ITextEmbeddingGenerationService _embedding;
#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.
private readonly IConfiguration _configuration;

/// <summary>
/// Constructor of <see cref="SemanticKernelTextEmbeddingProvider"/>
/// </summary>
public SemanticKernelTextEmbeddingProvider(ITextEmbeddingGeneration embedding, IConfiguration configuration)
#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.
public SemanticKernelTextEmbeddingProvider(ITextEmbeddingGenerationService embedding, IConfiguration configuration)
#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.
{
this._embedding = embedding;
this._configuration = configuration;
Expand All @@ -33,7 +37,9 @@ public SemanticKernelTextEmbeddingProvider(ITextEmbeddingGeneration embedding, I
/// <inheritdoc/>
public async Task<float[]> GetVectorAsync(string text)
{
#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.
return (await this._embedding.GenerateEmbeddingAsync(text)).ToArray();
#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.
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta6" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.2.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.6.4" />
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
using Microsoft.SemanticKernel.AI.ChatCompletion;
using Microsoft.SemanticKernel.AI.TextCompletion;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;

namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers
{
public class ResultHelper : IChatResult, ITextResult
{
public ModelResult ModelResult { get; set; }
private string _response;
//namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers
//{
// public class ResultHelper : KernelContent
// {
// public TextContent ModelResult { get; set; }
// private string _response;

public ResultHelper(string response)
{
ModelResult = new ModelResult(response);
_response = response;
}
// public ResultHelper(string response)
// {
// ModelResult = new TextContent(response);
// _response = response;
// }

public async Task<ChatMessage> GetChatMessageAsync(CancellationToken cancellationToken = default)
{
return await Task.FromResult(new MockModelResult(_response));
}
// public async Task<ChatMessageContent> GetChatMessageAsync(CancellationToken cancellationToken = default)
// {
// return await Task.FromResult(new MockModelResult(_response));
// }

public Task<string> GetCompletionAsync(CancellationToken cancellationToken = default)
{
return Task.FromResult(_response);
}
// public Task<string> GetCompletionAsync(CancellationToken cancellationToken = default)
// {
// return Task.FromResult(_response);
// }

public class MockModelResult : ChatMessage
{
public MockModelResult(string content) : base(AuthorRole.Assistant, content, null)
{
}
}
}
}
// public class MockModelResult : ChatMessageContent
// {
// public MockModelResult(string content) : base(AuthorRole.Assistant, content, null)
// {
// }
// }
// }
//}
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
using Microsoft.SemanticKernel.AI;
using Microsoft.SemanticKernel.AI.ChatCompletion;
using Microsoft.SemanticKernel.AI.TextCompletion;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SemanticKernel.TextGeneration;

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

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

IReadOnlyDictionary<string, object?> IAIService.Attributes => throw new NotImplementedException();

public ChatHistory CreateNewChat(string? instructions = null)
{
return new ChatHistory();
}

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

}

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

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

public IAsyncEnumerable<ITextStreamingResult> GetStreamingCompletionsAsync(string text, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
public IAsyncEnumerable<StreamingTextContent> GetStreamingTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Conversations.Models;
using Moq;
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Conversations;
using Microsoft.SemanticKernel.AI.ChatCompletion;
using Microsoft.SemanticKernel.AI;
using BotSharp.Plugin.SemanticKernel.UnitTests.Helpers;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Loggers;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Moq;

namespace BotSharp.Plugin.SemanticKernel.Tests
{
public class SemanticKernelChatCompletionProviderTests
{
private readonly Mock<IChatCompletion> _chatCompletionMock;
private readonly Mock<IChatCompletionService> _chatCompletionMock;
private readonly Mock<IServiceProvider> _servicesMock;
private readonly Mock<ITokenStatistics> _tokenStatisticsMock;
private readonly SemanticKernelChatCompletionProvider _provider;

public SemanticKernelChatCompletionProviderTests()
{
_chatCompletionMock = new Mock<IChatCompletion>();
_chatCompletionMock = new Mock<IChatCompletionService>();
_servicesMock = new Mock<IServiceProvider>();
_tokenStatisticsMock = new Mock<ITokenStatistics>();
_provider = new SemanticKernelChatCompletionProvider(_chatCompletionMock.Object, _servicesMock.Object, _tokenStatisticsMock.Object);
Expand All @@ -39,18 +38,18 @@ public async void GetChatCompletions_Returns_RoleDialogModel()
_servicesMock.Setup(x => x.GetService(typeof(IEnumerable<IContentGeneratingHook>)))
.Returns(new List<IContentGeneratingHook>());
var agentService = new Mock<IAgentService>();
agentService.Setup(x => x.RenderedInstruction(agent)).Returns("");
agentService.Setup(x => x.RenderedInstruction(agent)).Returns("How can I help you?");
_servicesMock.Setup(x => x.GetService(typeof(IAgentService)))
.Returns(agentService.Object);

var chatHistoryMock = new Mock<ChatHistory>();
_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?")
});

//_chatCompletionMock.Setup(x => new ChatHistory(It.IsAny<string>())).Returns(chatHistoryMock.Object);

_chatCompletionMock.Setup(x => x.GetChatMessageContentsAsync(chatHistoryMock.Object, It.IsAny<PromptExecutionSettings>(), It.IsAny<Kernel>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new List<ChatMessageContent>
{
new ChatMessageContent(AuthorRole.Assistant,"How can I help you?")
});

// Act
var result = await _provider.GetChatCompletions(agent, conversations);
Expand Down
Loading

0 comments on commit c319b3e

Please sign in to comment.