refactor semantic kernel Plugin
This commit is contained in:
parent
eb1b77904b
commit
fe3e324fb8
|
|
@ -21,10 +21,9 @@ namespace BotSharp.Plugin.SemanticKernel
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Constructor of <see cref="SemanticKernelTextEmbeddingProvider"/>
|
/// Constructor of <see cref="SemanticKernelTextEmbeddingProvider"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SemanticKernelTextEmbeddingProvider(ITextEmbeddingGeneration embedding, int dimension)
|
public SemanticKernelTextEmbeddingProvider(ITextEmbeddingGeneration embedding)
|
||||||
{
|
{
|
||||||
this._embedding = embedding;
|
this._embedding = embedding;
|
||||||
Dimension = dimension;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ namespace BotSharp.Plugin.SemanticKernel.Tests
|
||||||
_chatCompletionMock = new Mock<Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion>();
|
_chatCompletionMock = new Mock<Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion>();
|
||||||
_servicesMock = new Mock<IServiceProvider>();
|
_servicesMock = new Mock<IServiceProvider>();
|
||||||
_tokenStatisticsMock = new Mock<ITokenStatistics>();
|
_tokenStatisticsMock = new Mock<ITokenStatistics>();
|
||||||
_provider = new SemanticKernelChatCompletionProvider(_chatCompletionMock, _servicesMock.Object, _tokenStatisticsMock.Object);
|
_provider = new SemanticKernelChatCompletionProvider(_chatCompletionMock.Object, _servicesMock.Object, _tokenStatisticsMock.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -55,15 +55,13 @@ namespace BotSharp.Plugin.SemanticKernel.Tests
|
||||||
.Returns(agentService.Object);
|
.Returns(agentService.Object);
|
||||||
|
|
||||||
var chatHistoryMock = new Mock<ChatHistory>();
|
var chatHistoryMock = new Mock<ChatHistory>();
|
||||||
var chatCompletionMock = new Mock<Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion>();
|
_chatCompletionMock.Setup(x => x.CreateNewChat(It.IsAny<string>())).Returns(chatHistoryMock.Object);
|
||||||
chatCompletionMock.Setup(x => x.CreateNewChat(It.IsAny<string>())).Returns(chatHistoryMock.Object);
|
_chatCompletionMock.Setup(x => x.GetChatCompletionsAsync(chatHistoryMock.Object, It.IsAny<AIRequestSettings>(), It.IsAny<CancellationToken>()))
|
||||||
chatCompletionMock.Setup(x => x.GetChatCompletionsAsync(chatHistoryMock.Object, It.IsAny<AIRequestSettings>(), It.IsAny<CancellationToken>()))
|
|
||||||
.ReturnsAsync(new List<IChatResult>
|
.ReturnsAsync(new List<IChatResult>
|
||||||
{
|
{
|
||||||
new ResultHelper("How can I help you?")
|
new ResultHelper("How can I help you?")
|
||||||
});
|
});
|
||||||
|
|
||||||
_kernelMock.Setup(x => x.GetService<Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion>(null)).Returns(chatCompletionMock.Object);
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = _provider.GetChatCompletions(agent, conversations);
|
var result = _provider.GetChatCompletions(agent, conversations);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
using BotSharp.Abstraction.Conversations;
|
using BotSharp.Abstraction.Conversations;
|
||||||
using BotSharp.Abstraction.MLTasks;
|
using BotSharp.Abstraction.MLTasks;
|
||||||
|
using BotSharp.Abstraction.VectorStorage;
|
||||||
|
using BotSharp.Plugin.SemanticKernel.UnitTests.Helpers;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.SemanticKernel;
|
using Microsoft.SemanticKernel;
|
||||||
|
|
@ -15,21 +17,21 @@ namespace BotSharp.Plugin.SemanticKernel.Tests
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
var config = new ConfigurationBuilder().Build();
|
var config = new ConfigurationBuilder().Build();
|
||||||
var plugin = new SemanticKernelPlugin();
|
var plugin = new SemanticKernelPlugin();
|
||||||
services.AddScoped(x =>
|
services.AddScoped(x => Mock.Of<Microsoft.SemanticKernel.AI.TextCompletion.ITextCompletion>());
|
||||||
{
|
services.AddScoped(x => Mock.Of<Microsoft.SemanticKernel.AI.ChatCompletion.IChatCompletion>());
|
||||||
return new KernelBuilder()
|
services.AddScoped(x => Mock.Of<Microsoft.SemanticKernel.Memory.IMemoryStore>());
|
||||||
.WithAzureOpenAIChatCompletionService("test", "test", "test")
|
services.AddScoped(x => Mock.Of<Microsoft.SemanticKernel.AI.Embeddings.ITextEmbeddingGeneration>());
|
||||||
.Build();
|
services.AddScoped(x => Mock.Of<ITokenStatistics>());
|
||||||
});
|
|
||||||
services.AddScoped<ITokenStatistics>(x=> Mock.Of<ITokenStatistics>());
|
|
||||||
|
|
||||||
|
|
||||||
plugin.RegisterDI(services, config);
|
plugin.RegisterDI(services, config);
|
||||||
|
|
||||||
var provider = services.BuildServiceProvider();
|
var provider = services.BuildServiceProvider().CreateScope().ServiceProvider;
|
||||||
|
|
||||||
Assert.NotNull(provider.GetService<ITextCompletion>());
|
Assert.NotNull(provider.GetService<ITextCompletion>());
|
||||||
Assert.NotNull(provider.GetService<IChatCompletion>());
|
Assert.NotNull(provider.GetService<IChatCompletion>());
|
||||||
|
Assert.NotNull(provider.GetService<IVectorDb>());
|
||||||
|
Assert.NotNull(provider.GetService<ITextEmbedding>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -37,10 +37,7 @@ namespace BotSharp.Plugin.SemanticKernel.Tests
|
||||||
|
|
||||||
var text = "Hello";
|
var text = "Hello";
|
||||||
var expected = "Hello, world!";
|
var expected = "Hello, world!";
|
||||||
var _kernel = new KernelBuilder()
|
var provider = new SemanticKernelTextCompletionProvider(new SemanticKernelHelper(expected), _services, _tokenStatistics);
|
||||||
.WithAIService<ITextCompletion>("", new SemanticKernelHelper(expected))
|
|
||||||
.Build();
|
|
||||||
var provider = new SemanticKernelTextCompletionProvider(_kernel, _services, _tokenStatistics);
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = await provider.GetCompletion(text, "agent1", "message1");
|
var result = await provider.GetCompletion(text, "agent1", "message1");
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue