Skip to content

Commit

Permalink
Added more Assert methods. ContainsInformationSubset
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisM000 committed Nov 20, 2023
1 parent b4f2533 commit f3a2b3f
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,28 @@ public async Task not_throw_exception_when_texts_are_in_different_languages()

Assert.Null(exception);
}

[Theory]
[InlineData("El Teide es el más alto de España")]
[InlineData("El Teide, que se encuentra en la isla de Tenerife en España, tiene una altura de aproximadamente 3718 metros sobre el nivel del mar. Es el pico más alto de España y uno de los volcanes más altos del mundo si se mide desde su base en el lecho oceánico.")]
public async Task throw_exception_when_text_contains_subset_of_information_included_in_another_text(string actual)
{
var exception = await Record.ExceptionAsync(() => Async.Assert.Not.ContainsInformationSubset(
"El Teide, que se encuentra en la isla de Tenerife en España, tiene una altura de aproximadamente 3718 metros sobre el nivel del mar. Es el pico más alto de España y uno de los volcanes más altos del mundo si se mide desde su base en el lecho oceánico.",
actual));

Assert.IsType<SemanticAssertionsException>(exception);
}

[Theory]
[InlineData("El Teide, que se encuentra en la isla de Tenerife en España, tiene una altura de aproximadamente 3718 metros sobre el nivel del mar. Es el pico más alto de España y uno de los volcanes más altos del mundo si se mide desde su base en el lecho oceánico.")]
[InlineData("Nueva York está en USA")]
public async Task not_throw_exception_when_text_not_contains_subset_of_information_included_in_another_text(string actual)
{
var exception = await Record.ExceptionAsync(() => Async.Assert.Not.ContainsInformationSubset(
"El Teide es el más alto de España",
actual));

Assert.Null(exception);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,28 @@ public async Task throw_exception_when_texts_are_in_different_languages()

Assert.IsType<SemanticAssertionsException>(exception);
}

[Theory]
[InlineData("El Teide es el más alto de España")]
[InlineData("El Teide, que se encuentra en la isla de Tenerife en España, tiene una altura de aproximadamente 3718 metros sobre el nivel del mar. Es el pico más alto de España y uno de los volcanes más altos del mundo si se mide desde su base en el lecho oceánico.")]
public async Task not_throw_exception_when_text_contains_subset_of_information_included_in_another_text(string actual)
{
var exception = await Record.ExceptionAsync(() => Async.Assert.ContainsInformationSubset(
"El Teide, que se encuentra en la isla de Tenerife en España, tiene una altura de aproximadamente 3718 metros sobre el nivel del mar. Es el pico más alto de España y uno de los volcanes más altos del mundo si se mide desde su base en el lecho oceánico.",
actual));

Assert.Null(exception);
}

[Theory]
[InlineData("El Teide, que se encuentra en la isla de Tenerife en España, tiene una altura de aproximadamente 3718 metros sobre el nivel del mar. Es el pico más alto de España y uno de los volcanes más altos del mundo si se mide desde su base en el lecho oceánico.")]
[InlineData("Nueva York está en USA")]
public async Task throw_exception_when_text_not_contains_subset_of_information_included_in_another_text(string actual)
{
var exception = await Record.ExceptionAsync(() => Async.Assert.ContainsInformationSubset(
"El Teide es el más alto de España",
actual));

Assert.IsType<SemanticAssertionsException>(exception);
}
}
2 changes: 2 additions & 0 deletions src/SemanticAssertions/Abstractions/IAssertHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ public interface IAssertHandler

Task<string> CalculateSimilarityAsync(string expected, string actual);

Task<string> ContainsInformationSubsetAsync(string expected, string actual);

Task<string> AreInSameLanguage(string expected, string actual);
}
16 changes: 16 additions & 0 deletions src/SemanticAssertions/Async/Assert.Not.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ public static async Task AreSimilar(string expected, string actual, double simil

throw new SemanticAssertionsException($"Strings are similar. Max similarity: {similarityThreshold}.");
}

#pragma warning disable S3218
public static async Task ContainsInformationSubset(string expected, string actual)
#pragma warning restore S3218
{
try
{
await Assert.ContainsInformationSubset(expected, actual);
}
catch (SemanticAssertionsException)
{
return;
}

throw new SemanticAssertionsException($"The string {nameof(actual)} does contain information included in the string {nameof(expected)}");
}

#pragma warning disable S3218
public static async Task AreInSameLanguage(string expected, string actual)
Expand Down
19 changes: 19 additions & 0 deletions src/SemanticAssertions/Async/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ public static async Task AreSimilar(string expected, string actual, double simil

throw new SemanticAssertionsException($"Strings are not similar. Expected similarity: {similarityThreshold}. Actual similarity: {result}");
}

public static async Task ContainsInformationSubset(string expected, string actual)
{
if (string.IsNullOrEmpty(expected) && string.IsNullOrEmpty(actual))
{
return;
}

var result = await AssertHandler.ContainsInformationSubsetAsync(expected, actual).ConfigureAwait(false);

var containsInformation = await ParserProvider.ParseBoolAsync(result).ConfigureAwait(false);

if (containsInformation)
{
return;
}

throw new SemanticAssertionsException($"The string {nameof(actual)} does not contain information included in the string {nameof(expected)}");
}

public static async Task AreInSameLanguage(string expected, string actual)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"schema": 1,
"type": "completion",
"description": "Compare subset text",
"completion": {
"max_tokens": 10,
"temperature": 0.0,
"top_p": 0.0,
"presence_penalty": 0.0,
"frequency_penalty": 0.0
},
"input": {
"parameters": [
{
"name": "actual",
"description": "The value to be compared against",
"defaultValue": ""
},
{
"name": "expected",
"description": "The expected value",
"defaultValue": ""
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Your task is to check if the [SUBSET] is a subset of the information provided by [TEXT] text.
If affirmative, return TRUE; if negative, return FALSE.
Analyze the [SUBSET] and [TEXT] and determine if [SUBSET] exclusively contains information included in [TEXT].
Remember, if [SUBSET] contains more information than [TEXT], return false.
Don't provide any explanation, just return TRUE or FALSE.

[TEXT]
{{$expected}}
[END TEXT]

[SUBSET]
{{$actual}}
[END SUBSET]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// ReSharper disable MemberHidesStaticFromOuterClass
namespace SemanticAssertions.Internals.SemanticKernel.Plugins.InformationPlugin;

internal static class InformationPluginInfo
{
public const string Name = "InformationPlugin";

public static class ContainsInformationSubset
{
#pragma warning disable S3218
public const string Name = nameof(ContainsInformationSubset);
#pragma warning restore S3218
}
}
23 changes: 21 additions & 2 deletions src/SemanticAssertions/Internals/SemanticKernel/SKAssertHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task<string> AreSimilar(string expected, string actual)

var result = await RunAsync(kernel, variables, areSimilarFunction).ConfigureAwait(false);

return result;
return result;
}

public virtual async Task<string> CalculateSimilarityAsync(string expected, string actual)
Expand All @@ -48,6 +48,24 @@ public virtual async Task<string> CalculateSimilarityAsync(string expected, stri
return result;
}

public async Task<string> ContainsInformationSubsetAsync(string expected, string actual)
{
var kernel = BuildKernel();

var containsInformationSubsetFunction = kernel.Functions.GetFunction(
Plugins.InformationPlugin.InformationPluginInfo.Name,
Plugins.InformationPlugin.InformationPluginInfo.ContainsInformationSubset.Name);
var variables = new ContextVariables
{
[Plugins.PluginsInfo.Parameters.Expected] = expected,
[Plugins.PluginsInfo.Parameters.Actual] = actual
};

var result = await RunAsync(kernel, variables, containsInformationSubsetFunction).ConfigureAwait(false);

return result;
}

public async Task<string> AreInSameLanguage(string expected, string actual)
{
var kernel = BuildKernel();
Expand Down Expand Up @@ -86,7 +104,8 @@ protected static IKernel BuildKernel()

kernel.ImportSemanticFunctionsFromDirectory(Plugins.PluginsInfo.Directory,
Plugins.SimilarityPlugin.SimilarityPluginInfo.Name,
Plugins.LanguagePlugin.LanguagePluginInfo.Name);
Plugins.LanguagePlugin.LanguagePluginInfo.Name,
Plugins.InformationPlugin.InformationPluginInfo.Name);

return kernel;
}
Expand Down
18 changes: 18 additions & 0 deletions src/SemanticAssertions/SemanticAssertions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@
<None Update="Internals\SemanticKernel\Plugins\SimilarityPlugin\AreSimilar\skprompt.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Internals\SemanticKernel\Plugins\InformationPlugin\AreInSameLanguage\config.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Internals\SemanticKernel\Plugins\InformationPlugin\AreInSameLanguage\skprompt.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Internals\SemanticKernel\Plugins\InformationPlugin\ContainsInformation\config.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Internals\SemanticKernel\Plugins\InformationPlugin\ContainsInformation\skprompt.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Internals\SemanticKernel\Plugins\InformationPlugin\ContainsInformationSubset\config.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Internals\SemanticKernel\Plugins\InformationPlugin\ContainsInformationSubset\skprompt.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>

</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit f3a2b3f

Please sign in to comment.