-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Fix function result logging exception (#5745)
Fix for the issues: - #5631 - #5264 ### Description Instead of serializing function result of kernel content type, we try to get it as a string first, and only if the attempt fails we fallback to serialization. Note: Later, the serializer should use kernel serializer options when they are available. Co-authored-by: Mark Wallace <127216156+markwallace-microsoft@users.noreply.github.com>
- Loading branch information
1 parent
ca9e3ae
commit 2321a45
Showing
3 changed files
with
69 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
dotnet/src/SemanticKernel.UnitTests/Functions/KernelFunctionLogMessagesTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Text.Json.Serialization; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.ChatCompletion; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace SemanticKernel.UnitTests.Functions; | ||
public class KernelFunctionLogMessagesTests | ||
{ | ||
[Theory] | ||
[InlineData(typeof(string))] | ||
[InlineData(typeof(int))] | ||
[InlineData(typeof(bool))] | ||
[InlineData(typeof(ChatMessageContent))] | ||
[InlineData(typeof(User))] | ||
public void ItShouldLogFunctionResultOfAnyType(Type resultType) | ||
{ | ||
// Arrange | ||
(object FunctionResult, string LogMessage) testData = resultType switch | ||
{ | ||
Type t when t == typeof(string) => ("test-string", "Function result: test-string"), | ||
Type t when t == typeof(int) => (6, "Function result: 6"), | ||
Type t when t == typeof(bool) => (true, "Function result: true"), | ||
Type t when t == typeof(ChatMessageContent) => (new ChatMessageContent(AuthorRole.Assistant, "test-content"), "Function result: test-content"), | ||
Type t when t == typeof(User) => (new User { Name = "test-user-name" }, "Function result: {\"name\":\"test-user-name\"}"), | ||
_ => throw new ArgumentException("Invalid type") | ||
}; | ||
|
||
var logger = new Mock<ILogger>(); | ||
logger.Setup(l => l.IsEnabled(It.IsAny<LogLevel>())).Returns(true); | ||
|
||
var functionResult = new FunctionResult(KernelFunctionFactory.CreateFromMethod(() => { }), testData.FunctionResult); | ||
|
||
// Act | ||
logger.Object.LogFunctionResultValue(functionResult); | ||
|
||
// Assert | ||
logger.Verify(l => l.Log( | ||
LogLevel.Trace, | ||
0, | ||
It.Is<It.IsAnyType>((o, _) => o.ToString() == testData.LogMessage), | ||
null, | ||
It.IsAny<Func<It.IsAnyType, Exception?, string>>())); | ||
} | ||
|
||
private sealed class User | ||
{ | ||
[JsonPropertyName("name")] | ||
public string? Name { get; set; } | ||
} | ||
} |