Skip to content

Commit

Permalink
Refactor to adopt Microsoft.Extensions.AI. #1 #2 #8
Browse files Browse the repository at this point in the history
Refactored SemanticTestBase to use ScenarioAssert and IChatClient. Updated KernelChatTests to use ScenarioAssert. Removed obsolete test classes and methods, including FunctionTests and KernelTests. Deleted config.json, sktest.md, and skprompt.txt files. Updated skUnit.Tests.csproj to remove references to deleted files and scenarios. Updated ScenarioAssert_Initialize.cs and SemanticAssert.cs to use IChatClient. Removed PocomoPlugin.cs and related configurations. Updated skUnit.csproj to target .NET 8.0 and updated package references. Added new ChatClientTests class in ChatClientTests.cs. Introduced new methods in ScenarioAssert for chat scenario validation.
  • Loading branch information
mehrandvd committed Nov 26, 2024
1 parent 299d6db commit df9a947
Show file tree
Hide file tree
Showing 38 changed files with 209 additions and 1,498 deletions.
17 changes: 15 additions & 2 deletions src/skUnit.Tests/Infrastructure/SemanticTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Reflection;
using Azure.AI.OpenAI;
using Markdig.Helpers;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
Expand All @@ -15,7 +17,8 @@ public class SemanticTestBase
private readonly string _endpoint;
private readonly string _deploymentName;
protected Kernel Kernel { get; set; }
protected SemanticKernelAssert SemanticKernelAssert { get; set; }
protected IChatClient ChatClient { get; set; }
protected ScenarioAssert ScenarioAssert { get; set; }
protected ITestOutputHelper Output { get; set; }

public SemanticTestBase(ITestOutputHelper output)
Expand All @@ -29,8 +32,18 @@ public SemanticTestBase(ITestOutputHelper output)
Environment.GetEnvironmentVariable("openai-deployment-name", EnvironmentVariableTarget.User) ??
throw new Exception("No DeploymentName in environment variables.");

SemanticKernelAssert = new SemanticKernelAssert(_deploymentName, _endpoint, _apiKey, message => Output.WriteLine(message));
ScenarioAssert = new ScenarioAssert(
new AzureOpenAIClient(
new Uri(_endpoint),
new System.ClientModel.ApiKeyCredential(_apiKey)
).AsChatClient(_deploymentName)
, message => Output.WriteLine(message));
Kernel = CreateKernel();

ChatClient = new AzureOpenAIClient(
new Uri(_endpoint),
new System.ClientModel.ApiKeyCredential(_apiKey)
).AsChatClient(_deploymentName);
}

Kernel CreateKernel()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.SemanticKernel;
using skUnit.Tests.Infrastructure;
using Xunit.Abstractions;

namespace skUnit.Tests.ScenarioAssertTests.ChatScenarioTests
{
public class ChatClientTests : SemanticTestBase
{
public ChatClientTests(ITestOutputHelper output) : base(output)
{

}

[Fact]
public async Task EiffelTallChat_MustWork()
{
var scenarios = await LoadChatScenarioAsync("EiffelTallChat");
await ScenarioAssert.PassAsync(scenarios, ChatClient);
}
}


}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using Microsoft.SemanticKernel;
using skUnit.Tests.Infrastructure;
using skUnit.Tests.SemanticKernelTests.ChatScenarioTests.Plugins;
using skUnit.Tests.SemanticKernelTests.InvokeScenarioTests;
using Xunit.Abstractions;

namespace skUnit.Tests.ScenarioAssertTests.ChatScenarioTests
{
public class ScenarioAssertTests : SemanticTestBase
public class KernelChatTests : SemanticTestBase
{
public ScenarioAssertTests(ITestOutputHelper output) : base(output)
public KernelChatTests(ITestOutputHelper output) : base(output)
{
var func = Kernel.CreateFunctionFromPrompt("""
[[INPUT]]
Expand All @@ -26,19 +24,7 @@ [[END OF INPUT]]
public async Task EiffelTallChat_MustWork()
{
var scenarios = await LoadChatScenarioAsync("EiffelTallChat");
await SemanticKernelAssert.CheckChatScenarioAsync(Kernel, scenarios);
}

[Fact(Skip = "It doesn't work functions yet.")]
public async Task PocomoPriceChat_MustWork()
{
//var dir = Path.Combine(Environment.CurrentDirectory, "SemanticKernel", "ChatScenarioTests", "Plugins");
//Kernel.ImportPluginFromPromptDirectory(dir);

Kernel.ImportPluginFromType<PocomoPlugin>();

var scenarios = await LoadChatScenarioAsync("PocomoPriceChat");
await SemanticKernelAssert.CheckChatScenarioAsync(Kernel, scenarios);
await ScenarioAssert.PassAsync(scenarios, Kernel);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ What about a mouse?
No it is not tall.

### CHECK SemanticCondition
The sentence is negative.
The sentence is negative or mentions that mouse is not tall.

## [USER]
Give me a json containing the Eiffel height.
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

9 changes: 8 additions & 1 deletion src/skUnit.Tests/SemanticAssertTests/SemanticAssertTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Azure.AI.OpenAI;
using Microsoft.Extensions.AI;
using skUnit.Exceptions;
using Xunit.Abstractions;

Expand All @@ -22,7 +24,12 @@ public SemanticAssertTests(ITestOutputHelper output)
Environment.GetEnvironmentVariable("openai-deployment-name", EnvironmentVariableTarget.User) ??
throw new Exception("No DeploymentName in environment variables.");

SemanticAssert = new SemanticAssert(deploymentName, endpoint, apiKey);
SemanticAssert = new SemanticAssert(
new AzureOpenAIClient(
new Uri(endpoint),
new System.ClientModel.ApiKeyCredential(apiKey)
).AsChatClient(deploymentName)
);
}

[Theory]
Expand Down
Loading

0 comments on commit df9a947

Please sign in to comment.