Skip to content

Commit c87e45e

Browse files
Merge branch 'microsoft:main' into update-modelid
2 parents 355eb68 + fe23d41 commit c87e45e

149 files changed

Lines changed: 5194 additions & 1618 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
param (
2+
[string]$JsonReportPath,
3+
[double]$CoverageThreshold
4+
)
5+
6+
$jsonContent = Get-Content $JsonReportPath -Raw | ConvertFrom-Json
7+
$coverageBelowThreshold = $false
8+
9+
function Get-FormattedValue($number) {
10+
$formattedNumber = "{0:N1}" -f $number
11+
$icon = if ($number -ge $CoverageThreshold) { '✅' } else { '❌' }
12+
13+
return "$formattedNumber% $icon"
14+
}
15+
16+
$lineCoverage = $jsonContent.summary.linecoverage
17+
$branchCoverage = $jsonContent.summary.branchcoverage
18+
19+
if ($lineCoverage -lt $CoverageThreshold -or $branchCoverage -lt $CoverageThreshold) {
20+
$coverageBelowThreshold = $true
21+
}
22+
23+
$totalTableData = [PSCustomObject]@{
24+
'Metric' = 'Total Coverage'
25+
'Line Coverage' = Get-FormattedValue $lineCoverage
26+
'Branch Coverage' = Get-FormattedValue $branchCoverage
27+
}
28+
29+
$totalTableData | Format-Table -AutoSize
30+
31+
$assemblyTableData = @()
32+
33+
foreach ($assembly in $jsonContent.coverage.assemblies) {
34+
$assemblyName = $assembly.name
35+
$assemblyLineCoverage = $assembly.coverage
36+
$assemblyBranchCoverage = $assembly.branchcoverage
37+
38+
if ($assemblyLineCoverage -lt $CoverageThreshold -or $assemblyBranchCoverage -lt $CoverageThreshold) {
39+
$coverageBelowThreshold = $true
40+
}
41+
42+
$assemblyTableData += [PSCustomObject]@{
43+
'Assembly Name' = $assemblyName
44+
'Line' = Get-FormattedValue $assemblyLineCoverage
45+
'Branch' = Get-FormattedValue $assemblyBranchCoverage
46+
}
47+
}
48+
49+
$assemblyTableData | Format-Table -AutoSize
50+
51+
if ($coverageBelowThreshold) {
52+
Write-Host "Code coverage is lower than defined threshold: $CoverageThreshold. Stopping the task."
53+
exit 1
54+
}

‎.github/workflows/dotnet-build-and-test.yml‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ on:
1212
merge_group:
1313
branches: ["main"]
1414

15+
env:
16+
COVERAGE_THRESHOLD: 80
17+
1518
concurrency:
1619
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
1720
cancel-in-progress: true
@@ -82,7 +85,7 @@ jobs:
8285
run: |
8386
export UT_PROJECTS=$(find ./dotnet -type f -name "*.UnitTests.csproj" | grep -v -E "(Planners.Core.UnitTests.csproj|Experimental.Orchestration.Flow.UnitTests.csproj|Experimental.Assistants.UnitTests.csproj)" | tr '\n' ' ')
8487
for project in $UT_PROJECTS; do
85-
dotnet test -c ${{ matrix.configuration }} $project --no-build -v Normal --logger trx
88+
dotnet test -c ${{ matrix.configuration }} $project --no-build -v Normal --logger trx --collect:"XPlat Code Coverage" --results-directory:"TestResults/Coverage/"
8689
done
8790
8891
- name: Run Integration Tests
@@ -109,6 +112,20 @@ jobs:
109112
Bing__ApiKey: ${{ secrets.BING__APIKEY }}
110113
OpenAI__ApiKey: ${{ secrets.OPENAI__APIKEY }}
111114

115+
# Generate test reports and check coverage
116+
- name: Generate test reports
117+
uses: danielpalme/[email protected]
118+
with:
119+
reports: "./TestResults/Coverage/**/coverage.cobertura.xml"
120+
targetdir: "./TestResults/Reports"
121+
reporttypes: "JsonSummary"
122+
# Report for production packages only
123+
assemblyfilters: "+Microsoft.SemanticKernel.Abstractions;+Microsoft.SemanticKernel.Core;+Microsoft.SemanticKernel.PromptTemplates.Handlebars;+Microsoft.SemanticKernel.Connectors.OpenAI;+Microsoft.SemanticKernel.Yaml;"
124+
125+
- name: Check coverage
126+
shell: pwsh
127+
run: .github/workflows/check-coverage.ps1 -JsonReportPath "TestResults/Reports/Summary.json" -CoverageThreshold $env:COVERAGE_THRESHOLD
128+
112129
# This final job is required to satisfy the merge queue. It must only run (or succeed) if no tests failed
113130
dotnet-build-and-test-check:
114131
if: always()

‎dotnet/Directory.Packages.props‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.6" />
5151
<PackageVersion Include="xretry" Version="1.9.0" />
5252
<PackageVersion Include="coverlet.collector" Version="6.0.0" />
53-
<PackageVersion Include="Polly" Version="8.2.0" />
5453
<!-- Plugins -->
5554
<PackageVersion Include="DocumentFormat.OpenXml" Version="3.0.0" />
5655
<PackageVersion Include="Microsoft.Data.Sqlite" Version="8.0.0" />
@@ -66,7 +65,6 @@
6665
<PackageVersion Include="Grpc.Net.Client" Version="2.60.0" />
6766
<PackageVersion Include="protobuf-net" Version="3.2.30" />
6867
<PackageVersion Include="protobuf-net.Reflection" Version="3.2.12" />
69-
<PackageVersion Include="CoreCLR-NCalc" Version="2.2.113" />
7068
<PackageVersion Include="YamlDotNet" Version="13.7.1" />
7169
<!-- Memory stores -->
7270
<PackageVersion Include="Pgvector" Version="0.2.0" />

‎dotnet/SK-dotnet.sln‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "System", "System", "{3CDE10
118118
src\InternalUtilities\src\System\EnvExtensions.cs = src\InternalUtilities\src\System\EnvExtensions.cs
119119
EndProjectSection
120120
EndProject
121-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plugins.Core", "src\Plugins\Plugins.Core\Plugins.Core.csproj", "{0D0C4DAD-E6BC-4504-AE3A-EEA4E35920C1}"
121+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Type", "Type", "{E85EA4D0-BB7E-4DFD-882F-A76EB8C0B8FF}"
122+
ProjectSection(SolutionItems) = preProject
123+
src\InternalUtilities\src\Type\TypeExtensions.cs = src\InternalUtilities\src\Type\TypeExtensions.cs
124+
EndProjectSection
122125
EndProject
123-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NCalcPlugins", "samples\NCalcPlugins\NCalcPlugins.csproj", "{E6EDAB8F-3406-4DBF-9AAB-DF40DC2CA0FA}"
126+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plugins.Core", "src\Plugins\Plugins.Core\Plugins.Core.csproj", "{0D0C4DAD-E6BC-4504-AE3A-EEA4E35920C1}"
124127
EndProject
125128
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TelemetryExample", "samples\TelemetryExample\TelemetryExample.csproj", "{C754950A-E16C-4F96-9CC7-9328E361B5AF}"
126129
EndProject
@@ -353,11 +356,6 @@ Global
353356
{0D0C4DAD-E6BC-4504-AE3A-EEA4E35920C1}.Publish|Any CPU.Build.0 = Publish|Any CPU
354357
{0D0C4DAD-E6BC-4504-AE3A-EEA4E35920C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
355358
{0D0C4DAD-E6BC-4504-AE3A-EEA4E35920C1}.Release|Any CPU.Build.0 = Release|Any CPU
356-
{E6EDAB8F-3406-4DBF-9AAB-DF40DC2CA0FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
357-
{E6EDAB8F-3406-4DBF-9AAB-DF40DC2CA0FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
358-
{E6EDAB8F-3406-4DBF-9AAB-DF40DC2CA0FA}.Publish|Any CPU.ActiveCfg = Release|Any CPU
359-
{E6EDAB8F-3406-4DBF-9AAB-DF40DC2CA0FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
360-
{E6EDAB8F-3406-4DBF-9AAB-DF40DC2CA0FA}.Release|Any CPU.Build.0 = Release|Any CPU
361359
{C754950A-E16C-4F96-9CC7-9328E361B5AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
362360
{C754950A-E16C-4F96-9CC7-9328E361B5AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
363361
{C754950A-E16C-4F96-9CC7-9328E361B5AF}.Publish|Any CPU.ActiveCfg = Release|Any CPU
@@ -500,8 +498,8 @@ Global
500498
{B00AD427-0047-4850-BEF9-BA8237EA9D8B} = {958AD708-F048-4FAF-94ED-D2F2B92748B9}
501499
{1C19D805-3573-4477-BF07-40180FCDE1BD} = {958AD708-F048-4FAF-94ED-D2F2B92748B9}
502500
{3CDE10B2-AE8F-4FC4-8D55-92D4AD32E144} = {958AD708-F048-4FAF-94ED-D2F2B92748B9}
501+
{E85EA4D0-BB7E-4DFD-882F-A76EB8C0B8FF} = {958AD708-F048-4FAF-94ED-D2F2B92748B9}
503502
{0D0C4DAD-E6BC-4504-AE3A-EEA4E35920C1} = {D6D598DF-C17C-46F4-B2B9-CDE82E2DE132}
504-
{E6EDAB8F-3406-4DBF-9AAB-DF40DC2CA0FA} = {FA3720F1-C99A-49B2-9577-A940257098BF}
505503
{C754950A-E16C-4F96-9CC7-9328E361B5AF} = {FA3720F1-C99A-49B2-9577-A940257098BF}
506504
{E07608CC-D710-4655-BB9E-D22CF3CDD193} = {24503383-A8C4-4255-9998-28D70FE8E99A}
507505
{D6D598DF-C17C-46F4-B2B9-CDE82E2DE132} = {831DDCA2-7D2C-4C31-80DB-6BDB3E1F7AE0}

‎dotnet/code-coverage.ps1‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# This script is for local use to analyze code coverage in more detail using HTML report.
2+
3+
Param(
4+
[switch]$ProdPackagesOnly = $false
5+
)
6+
7+
# Generate a timestamp for the current date and time
8+
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
9+
10+
# Define paths
11+
$scriptPath = Get-Item -Path $PSScriptRoot
12+
$coverageOutputPath = Join-Path $scriptPath "TestResults\Coverage\$timestamp"
13+
$reportOutputPath = Join-Path $scriptPath "TestResults\Reports\$timestamp"
14+
15+
# Create output directories
16+
New-Item -ItemType Directory -Force -Path $coverageOutputPath
17+
New-Item -ItemType Directory -Force -Path $reportOutputPath
18+
19+
# Find tests for projects ending with 'UnitTests.csproj'
20+
$testProjects = Get-ChildItem $scriptPath -Filter "*UnitTests.csproj" -Recurse
21+
22+
foreach ($project in $testProjects) {
23+
$testProjectPath = $project.FullName
24+
Write-Host "Running tests for project: $($testProjectPath)"
25+
26+
# Run tests
27+
dotnet test $testProjectPath `
28+
--collect:"XPlat Code Coverage" `
29+
--results-directory:$coverageOutputPath `
30+
31+
}
32+
33+
# Install required tools
34+
& dotnet tool install -g coverlet.console
35+
& dotnet tool install -g dotnet-reportgenerator-globaltool
36+
37+
# Generate HTML report
38+
if ($ProdPackagesOnly) {
39+
$assemblies = @(
40+
"+Microsoft.SemanticKernel.Abstractions",
41+
"+Microsoft.SemanticKernel.Core",
42+
"+Microsoft.SemanticKernel.PromptTemplates.Handlebars",
43+
"+Microsoft.SemanticKernel.Connectors.OpenAI",
44+
"+Microsoft.SemanticKernel.Yaml"
45+
)
46+
47+
$assemblyFilters = $assemblies -join ";"
48+
49+
# Generate report for production assemblies only
50+
& reportgenerator -reports:"$coverageOutputPath/**/coverage.cobertura.xml" -targetdir:$reportOutputPath -reporttypes:Html -assemblyfilters:$assemblyFilters
51+
}
52+
else {
53+
& reportgenerator -reports:"$coverageOutputPath/**/coverage.cobertura.xml" -targetdir:$reportOutputPath -reporttypes:Html
54+
}
55+
56+
Write-Host "Code coverage report generated at: $reportOutputPath"
57+
58+
# Open report
59+
$reportIndexHtml = Join-Path $reportOutputPath "index.html"
60+
Invoke-Item -Path $reportIndexHtml

‎dotnet/samples/.editorconfig‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ resharper_condition_is_always_true_or_false_according_to_nullable_api_contract_h
2121
resharper_inconsistent_naming_highlighting = none # InconsistentNaming
2222
resharper_equal_expression_comparison_highlighting = none # EqualExpressionComparison
2323
resharper_check_namespace_highlighting = none # CheckNamespace
24-
resharper_arrange_object_creation_when_type_not_evident_highlighting = none # Disable "Arrange object creation when type is not evident" highlighting
24+
resharper_arrange_object_creation_when_type_not_evident_highlighting = none # Disable "Arrange object creation when type is not evident" highlighting
25+
resharper_arrange_this_qualifier_highlighting = none # Disable "Arrange 'this.' qualifier" highlighting
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using Microsoft.Extensions.Configuration;
4+
using RepoUtils;
5+
using Xunit.Abstractions;
6+
7+
namespace Examples;
8+
9+
public abstract class BaseTest
10+
{
11+
protected ITestOutputHelper Output { get; }
12+
13+
protected BaseTest(ITestOutputHelper output)
14+
{
15+
this.Output = output;
16+
LoadUserSecrets();
17+
}
18+
19+
private static void LoadUserSecrets()
20+
{
21+
IConfigurationRoot configRoot = new ConfigurationBuilder()
22+
.AddJsonFile("appsettings.Development.json", true)
23+
.AddEnvironmentVariables()
24+
.AddUserSecrets<Env>()
25+
.Build();
26+
27+
TestConfiguration.Initialize(configRoot);
28+
}
29+
30+
/// <summary>
31+
/// This method can be substituted by Console.WriteLine when used in Console apps.
32+
/// </summary>
33+
/// <param name="target">Target object to write</param>
34+
protected void WriteLine(object? target = null)
35+
{
36+
this.Output.WriteLine(target ?? string.Empty);
37+
}
38+
39+
/// <summary>
40+
/// Current interface ITestOutputHelper does not have a Write method. This extension method adds it to make it analogous to Console.Write when used in Console apps.
41+
/// </summary>
42+
/// <param name="target">Target object to write</param>
43+
protected void Write(object? target = null)
44+
{
45+
this.Output.WriteLine(target ?? string.Empty);
46+
}
47+
}
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
// Copyright (c) Microsoft. All rights reserved.
22

3-
using System;
43
using System.Threading.Tasks;
54
using Microsoft.SemanticKernel.Plugins.Core;
5+
using Xunit;
6+
using Xunit.Abstractions;
67

7-
public static class Example01_MethodFunctions
8+
namespace Examples;
9+
10+
public class Example01_MethodFunctions : BaseTest
811
{
9-
public static Task RunAsync()
12+
[Fact]
13+
public Task RunAsync()
1014
{
11-
Console.WriteLine("======== Functions ========");
15+
this.WriteLine("======== Functions ========");
1216

1317
// Load native plugin
1418
var text = new TextPlugin();
1519

1620
// Use function without kernel
1721
var result = text.Uppercase("ciao!");
1822

19-
Console.WriteLine(result);
23+
this.WriteLine(result);
2024

2125
return Task.CompletedTask;
2226
}
27+
28+
public Example01_MethodFunctions(ITestOutputHelper output) : base(output)
29+
{
30+
}
2331
}

‎dotnet/samples/KernelSyntaxExamples/Example03_Arguments.cs‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
using System.Threading.Tasks;
66
using Microsoft.SemanticKernel;
77
using Plugins;
8+
using Xunit;
9+
using Xunit.Abstractions;
810

11+
namespace Examples;
912
// This example shows how to use kernel arguments when invoking functions.
10-
public static class Example03_Arguments
13+
public class Example03_Arguments : BaseTest
1114
{
12-
public static async Task RunAsync()
15+
[Fact]
16+
public async Task RunAsync()
1317
{
14-
Console.WriteLine("======== Arguments ========");
18+
this.WriteLine("======== Arguments ========");
1519

1620
Kernel kernel = new();
1721
var textPlugin = kernel.ImportPluginFromType<StaticTextPlugin>();
@@ -26,16 +30,20 @@ public static async Task RunAsync()
2630

2731
// Specify and get the value type as generic parameter
2832
string? resultValue = await kernel.InvokeAsync<string>(textPlugin["AppendDay"], arguments);
29-
Console.WriteLine($"string -> {resultValue}");
33+
this.WriteLine($"string -> {resultValue}");
3034

3135
// If you need to access the result metadata, you can use the non-generic version to get the FunctionResult
3236
FunctionResult functionResult = await kernel.InvokeAsync(textPlugin["AppendDay"], arguments);
3337
var metadata = functionResult.Metadata;
3438

3539
// Specify the type from the FunctionResult
36-
Console.WriteLine($"FunctionResult.GetValue<string>() -> {functionResult.GetValue<string>()}");
40+
this.WriteLine($"FunctionResult.GetValue<string>() -> {functionResult.GetValue<string>()}");
3741

3842
// FunctionResult.ToString() automatically converts the result to string
39-
Console.WriteLine($"FunctionResult.ToString() -> {functionResult}");
43+
this.WriteLine($"FunctionResult.ToString() -> {functionResult}");
44+
}
45+
46+
public Example03_Arguments(ITestOutputHelper output) : base(output)
47+
{
4048
}
4149
}

‎dotnet/samples/KernelSyntaxExamples/Example05_InlineFunctionDefinition.cs‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,24 @@
44
using System.Threading.Tasks;
55
using Microsoft.SemanticKernel;
66
using Microsoft.SemanticKernel.Connectors.OpenAI;
7+
using Xunit;
8+
using Xunit.Abstractions;
79

8-
public static class Example05_InlineFunctionDefinition
10+
namespace Examples;
11+
12+
public class Example05_InlineFunctionDefinition : BaseTest
913
{
10-
public static async Task RunAsync()
14+
[Fact]
15+
public async Task RunAsync()
1116
{
12-
Console.WriteLine("======== Inline Function Definition ========");
17+
this.WriteLine("======== Inline Function Definition ========");
1318

1419
string openAIModelId = TestConfiguration.OpenAI.ChatModelId;
1520
string openAIApiKey = TestConfiguration.OpenAI.ApiKey;
1621

1722
if (openAIModelId is null || openAIApiKey is null)
1823
{
19-
Console.WriteLine("OpenAI credentials not found. Skipping example.");
24+
this.WriteLine("OpenAI credentials not found. Skipping example.");
2025
return;
2126
}
2227

@@ -49,14 +54,18 @@ Be creative and be funny. Let your imagination run wild.
4954
var excuseFunction = kernel.CreateFunctionFromPrompt(promptTemplate, new OpenAIPromptExecutionSettings() { MaxTokens = 100, Temperature = 0.4, TopP = 1 });
5055

5156
var result = await kernel.InvokeAsync(excuseFunction, new() { ["input"] = "I missed the F1 final race" });
52-
Console.WriteLine(result.GetValue<string>());
57+
this.WriteLine(result.GetValue<string>());
5358

5459
result = await kernel.InvokeAsync(excuseFunction, new() { ["input"] = "sorry I forgot your birthday" });
55-
Console.WriteLine(result.GetValue<string>());
60+
this.WriteLine(result.GetValue<string>());
5661

5762
var fixedFunction = kernel.CreateFunctionFromPrompt($"Translate this date {DateTimeOffset.Now:f} to French format", new OpenAIPromptExecutionSettings() { MaxTokens = 100 });
5863

5964
result = await kernel.InvokeAsync(fixedFunction);
60-
Console.WriteLine(result.GetValue<string>());
65+
this.WriteLine(result.GetValue<string>());
66+
}
67+
68+
public Example05_InlineFunctionDefinition(ITestOutputHelper output) : base(output)
69+
{
6170
}
6271
}

0 commit comments

Comments
 (0)