Merge pull request #279 from SciSharp/sk1.2

feat: upgrade semantic kernel  to 1.2
This commit is contained in:
Haiping 2024-01-30 05:29:23 -06:00 committed by GitHub
commit c319b3ef81
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 104 additions and 107 deletions

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
@ -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>
@ -24,4 +24,8 @@
<InternalsVisibleTo Include="BotSharp.Plugin.SemanticKernel.UnitTests" />
</ItemGroup>
<ItemGroup>
<Folder Include="packages\" />
</ItemGroup>
</Project>

View file

@ -6,7 +6,7 @@ using BotSharp.Abstraction.Conversations.Models;
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;
@ -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;
@ -33,7 +33,7 @@ namespace BotSharp.Plugin.SemanticKernel
/// <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)
{
@ -55,7 +55,7 @@ namespace BotSharp.Plugin.SemanticKernel
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)
{
@ -69,14 +69,9 @@ namespace BotSharp.Plugin.SemanticKernel
}
}
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

View file

@ -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;
}
@ -46,7 +50,9 @@ namespace BotSharp.Plugin.SemanticKernel
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.
}
}
}

View file

@ -3,10 +3,7 @@ using BotSharp.Abstraction.Agents.Models;
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;
@ -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;
@ -33,7 +30,7 @@ namespace BotSharp.Plugin.SemanticKernel
/// <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)
{
@ -61,9 +58,11 @@ namespace BotSharp.Plugin.SemanticKernel
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

View file

@ -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;
@ -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;
@ -33,7 +37,9 @@ namespace BotSharp.Plugin.SemanticKernel
/// <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/>

View file

@ -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>

View file

@ -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)
// {
// }
// }
// }
//}

View file

@ -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;
@ -22,27 +17,30 @@ namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers
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)
{
return Task.FromResult<IReadOnlyList<ITextResult>>(new List<ITextResult> { new ResultHelper(_excepted) });
}
public IAsyncEnumerable<IChatStreamingResult> GetStreamingChatCompletionsAsync(ChatHistory chat, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
public IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public IAsyncEnumerable<ITextStreamingResult> GetStreamingCompletionsAsync(string text, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
public Task<IReadOnlyList<TextContent>> GetTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default)
{
return Task.FromResult<IReadOnlyList<TextContent>>(new List<TextContent> { new TextContent(_excepted) });
}
public IAsyncEnumerable<StreamingTextContent> GetStreamingTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

View file

@ -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);
@ -39,18 +38,18 @@ namespace BotSharp.Plugin.SemanticKernel.Tests
_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);

View file

@ -20,10 +20,14 @@ namespace BotSharp.Plugin.SemanticKernel.Tests
services.AddSingleton<IConfiguration>(config);
var plugin = new SemanticKernelPlugin();
services.AddScoped(x => Mock.Of<Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion>());
services.AddScoped(x => Mock.Of<Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion>());
services.AddScoped(x => Mock.Of<Microsoft.SemanticKernel.TextGeneration.ITextGenerationService>());
services.AddScoped(x => Mock.Of<Microsoft.SemanticKernel.ChatCompletion.IChatCompletionService>());
#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.
services.AddScoped(x => Mock.Of<Microsoft.SemanticKernel.Memory.IMemoryStore>());
services.AddScoped(x => Mock.Of<Microsoft.SemanticKernel.AI.Embeddings.ITextEmbeddingGeneration>());
#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 SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
services.AddScoped(x => Mock.Of<Microsoft.SemanticKernel.Embeddings.ITextEmbeddingGenerationService>());
#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.
services.AddScoped(x => Mock.Of<ITokenStatistics>());

View file

@ -1,20 +1,7 @@
using BotSharp.Abstraction.Conversations.Models;
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;
using BotSharp.Plugin.SemanticKernel.UnitTests.Helpers;
using Microsoft.Extensions.DependencyInjection;
using Moq;
namespace BotSharp.Plugin.SemanticKernel.Tests
{