From 72aa76bd92c67e926524d7c9d2051f0a2fda2203 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 17 Oct 2024 23:36:00 -0400 Subject: [PATCH] Remove unnecessary ctors from FunctionResultContent --- .../Contents/FunctionResultContent.cs | 34 ------------------- .../FunctionInvokingChatClient.cs | 2 +- .../ChatCompletion/ChatMessageTests.cs | 4 +-- .../StreamingChatCompletionUpdateTests.cs | 2 +- .../Contents/FunctionResultContentTests.cs | 24 +++---------- 5 files changed, 8 insertions(+), 58 deletions(-) diff --git a/src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/FunctionResultContent.cs b/src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/FunctionResultContent.cs index f793e2ceceb..731716e5427 100644 --- a/src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/FunctionResultContent.cs +++ b/src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/FunctionResultContent.cs @@ -33,40 +33,6 @@ public FunctionResultContent(string callId, string name, object? result) Result = result; } - /// - /// Initializes a new instance of the class. - /// - /// The function call ID for which this is the result. - /// The function name that produced the result. - /// - /// This may be if the function returned , if the function was void-returning - /// and thus had no result, or if the function call failed. Typically, however, in order to provide meaningfully representative - /// information to an AI service, a human-readable representation of those conditions should be supplied. - /// - /// Any exception that occurred when invoking the function. - public FunctionResultContent(string callId, string name, object? result, Exception? exception) - { - CallId = Throw.IfNull(callId); - Name = Throw.IfNull(name); - Result = result; - Exception = exception; - } - - /// - /// Initializes a new instance of the class. - /// - /// The function call for which this is the result. - /// - /// This may be if the function returned , if the function was void-returning - /// and thus had no result, or if the function call failed. Typically, however, in order to provide meaningfully representative - /// information to an AI service, a human-readable representation of those conditions should be supplied. - /// - /// Any exception that occurred when invoking the function. - public FunctionResultContent(FunctionCallContent functionCall, object? result, Exception? exception = null) - : this(Throw.IfNull(functionCall).CallId, functionCall.Name, result, exception) - { - } - /// /// Gets or sets the ID of the function call for which this is the result. /// diff --git a/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs b/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs index 94b87c9a7b1..308480635d8 100644 --- a/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs +++ b/src/Libraries/Microsoft.Extensions.AI/ChatCompletion/FunctionInvokingChatClient.cs @@ -552,7 +552,7 @@ FunctionResultContent CreateFunctionResultContent(FunctionInvocationResult resul functionResult = message; } - return new FunctionResultContent(result.CallContent.CallId, result.CallContent.Name, functionResult, result.Exception); + return new FunctionResultContent(result.CallContent.CallId, result.CallContent.Name, functionResult) { Exception = result.Exception }; } } diff --git a/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatMessageTests.cs b/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatMessageTests.cs index dbef5f4088b..e05e0d0ef47 100644 --- a/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatMessageTests.cs +++ b/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/ChatMessageTests.cs @@ -128,7 +128,7 @@ public void Text_GetSet_UsesFirstTextContent() new FunctionCallContent("callId1", "fc1"), new TextContent("text-1"), new TextContent("text-2"), - new FunctionResultContent(new FunctionCallContent("callId1", "fc2"), "result"), + new FunctionResultContent("callId1", "fc2", "result"), ]); TextContent textContent = Assert.IsType(message.Contents[3]); @@ -291,7 +291,7 @@ public void ItCanBeSerializeAndDeserialized() AdditionalProperties = new() { ["metadata-key-6"] = "metadata-value-6" } }, new FunctionCallContent("function-id", "plugin-name-function-name", new Dictionary { ["parameter"] = "argument" }), - new FunctionResultContent(new FunctionCallContent("function-id", "plugin-name-function-name"), "function-result"), + new FunctionResultContent("function-id", "plugin-name-function-name", "function-result"), ]; // Act diff --git a/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/StreamingChatCompletionUpdateTests.cs b/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/StreamingChatCompletionUpdateTests.cs index 988727b1159..f90f799c6f9 100644 --- a/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/StreamingChatCompletionUpdateTests.cs +++ b/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/ChatCompletion/StreamingChatCompletionUpdateTests.cs @@ -96,7 +96,7 @@ public void Text_GetSet_UsesFirstTextContent() new FunctionCallContent("callId1", "fc1"), new TextContent("text-1"), new TextContent("text-2"), - new FunctionResultContent(new FunctionCallContent("callId1", "fc2"), "result"), + new FunctionResultContent("callId1", "fc2", "result"), ], }; diff --git a/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/FunctionResultContentTests.cs b/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/FunctionResultContentTests.cs index a70386e42c6..10a23c69596 100644 --- a/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/FunctionResultContentTests.cs +++ b/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/FunctionResultContentTests.cs @@ -25,30 +25,14 @@ public void Constructor_PropsDefault() [Fact] public void Constructor_String_PropsRoundtrip() { - Exception e = new(); - - FunctionResultContent c = new("id", "name", "result", e); + FunctionResultContent c = new("id", "name", "result"); Assert.Null(c.RawRepresentation); Assert.Null(c.ModelId); Assert.Null(c.AdditionalProperties); Assert.Equal("name", c.Name); Assert.Equal("id", c.CallId); Assert.Equal("result", c.Result); - Assert.Same(e, c.Exception); - } - - [Fact] - public void Constructor_FunctionCallContent_PropsRoundtrip() - { - Exception e = new(); - - FunctionResultContent c = new(new FunctionCallContent("id", "name"), "result", e); - Assert.Null(c.RawRepresentation); - Assert.Null(c.ModelId); - Assert.Null(c.AdditionalProperties); - Assert.Equal("id", c.CallId); - Assert.Equal("result", c.Result); - Assert.Same(e, c.Exception); + Assert.Null(c.Exception); } [Fact] @@ -88,7 +72,7 @@ public void Constructor_PropsRoundtrip() public void ItShouldBeSerializableAndDeserializable() { // Arrange - var sut = new FunctionResultContent(new FunctionCallContent("id", "p1-f1"), "result"); + var sut = new FunctionResultContent("id", "p1-f1", "result"); // Act var json = JsonSerializer.Serialize(sut, TestJsonSerializerContext.Default.Options); @@ -106,7 +90,7 @@ public void ItShouldBeSerializableAndDeserializable() public void ItShouldBeSerializableAndDeserializableWithException() { // Arrange - var sut = new FunctionResultContent("callId1", "functionName", null, new InvalidOperationException("hello")); + var sut = new FunctionResultContent("callId1", "functionName", null) { Exception = new InvalidOperationException("hello") }; // Act var json = JsonSerializer.Serialize(sut, TestJsonSerializerContext.Default.Options);