From 68e6dd78de6e3b9fd7ef33fbd6fdda897fdf19b3 Mon Sep 17 00:00:00 2001
From: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>
Date: Mon, 9 Dec 2024 03:53:05 -0800
Subject: [PATCH] .Net: Small improvements in Structured Outputs (#9906)
### Motivation and Context
Resolves: https://github.com/microsoft/semantic-kernel/issues/9897
This PR addresses remaining comments from previous PR related to
Structured Outputs:
https://github.com/microsoft/semantic-kernel/pull/9873.
### Contribution Checklist
- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone :smile:
---
.../ChatCompletion/OpenAI_StructuredOutputs.cs | 5 +----
...ests.cs => OpenAIChatResponseFormatBuilderTests.cs} | 10 +++++-----
.../Core/ClientCore.ChatCompletion.cs | 4 ++--
...matHelper.cs => OpenAIChatResponseFormatBuilder.cs} | 4 ++--
4 files changed, 10 insertions(+), 13 deletions(-)
rename dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Helpers/{OpenAIChatResponseFormatHelperTests.cs => OpenAIChatResponseFormatBuilderTests.cs} (93%)
rename dotnet/src/Connectors/Connectors.OpenAI/Helpers/{OpenAIChatResponseFormatHelper.cs => OpenAIChatResponseFormatBuilder.cs} (96%)
diff --git a/dotnet/samples/Concepts/ChatCompletion/OpenAI_StructuredOutputs.cs b/dotnet/samples/Concepts/ChatCompletion/OpenAI_StructuredOutputs.cs
index 426a191944a0..9aed38a8a56c 100644
--- a/dotnet/samples/Concepts/ChatCompletion/OpenAI_StructuredOutputs.cs
+++ b/dotnet/samples/Concepts/ChatCompletion/OpenAI_StructuredOutputs.cs
@@ -299,10 +299,7 @@ public async Task StructuredOutputsWithFunctionsFromYamlAsync()
var functionPath = Path.Combine(Directory.GetCurrentDirectory(), "Resources", "Plugins", "MoviePlugins", "MoviePluginYaml", "TopMovies.yaml");
// Load YAML prompt.
- using Stream stream = File.OpenRead(functionPath);
- using StreamReader reader = new(stream);
-
- var topMoviesYaml = reader.ReadToEnd();
+ var topMoviesYaml = File.ReadAllText(functionPath);
// Import a function from YAML.
var function = kernel.CreateFunctionFromPromptYaml(topMoviesYaml);
diff --git a/dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Helpers/OpenAIChatResponseFormatHelperTests.cs b/dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Helpers/OpenAIChatResponseFormatBuilderTests.cs
similarity index 93%
rename from dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Helpers/OpenAIChatResponseFormatHelperTests.cs
rename to dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Helpers/OpenAIChatResponseFormatBuilderTests.cs
index 7530cbabe7df..13a5862b19b7 100644
--- a/dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Helpers/OpenAIChatResponseFormatHelperTests.cs
+++ b/dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Helpers/OpenAIChatResponseFormatBuilderTests.cs
@@ -10,13 +10,13 @@
namespace SemanticKernel.Connectors.OpenAI.UnitTests.Helpers;
///
-/// Unit tests for class.
+/// Unit tests for class.
///
-public sealed class OpenAIChatResponseFormatHelperTests
+public sealed class OpenAIChatResponseFormatBuilderTests
{
private readonly JsonSerializerOptions _options = new();
- public OpenAIChatResponseFormatHelperTests()
+ public OpenAIChatResponseFormatBuilderTests()
{
this._options.Converters.Add(new BinaryDataJsonConverter());
}
@@ -33,7 +33,7 @@ public void GetJsonSchemaResponseFormatReturnsChatResponseFormatByDefault(
var jsonElement = jsonDocument.RootElement;
// Act
- var chatResponseFormat = OpenAIChatResponseFormatHelper.GetJsonSchemaResponseFormat(jsonElement);
+ var chatResponseFormat = OpenAIChatResponseFormatBuilder.GetJsonSchemaResponseFormat(jsonElement);
var responseFormat = this.GetResponseFormat(chatResponseFormat);
// Assert
@@ -79,7 +79,7 @@ public void GetJsonSchemaResponseFormatThrowsExceptionWhenSchemaDoesNotExist()
var jsonElement = jsonDocument.RootElement;
// Act & Assert
- Assert.Throws(() => OpenAIChatResponseFormatHelper.GetJsonSchemaResponseFormat(jsonElement));
+ Assert.Throws(() => OpenAIChatResponseFormatBuilder.GetJsonSchemaResponseFormat(jsonElement));
}
public static TheoryData ChatResponseFormatJson => new()
diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.ChatCompletion.cs b/dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.ChatCompletion.cs
index 8800ef8bf306..9d03c3322964 100644
--- a/dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.ChatCompletion.cs
+++ b/dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.ChatCompletion.cs
@@ -540,10 +540,10 @@ protected virtual ChatCompletionOptions CreateChatCompletionOptions(
}
}
- return OpenAIChatResponseFormatHelper.GetJsonSchemaResponseFormat(formatElement);
+ return OpenAIChatResponseFormatBuilder.GetJsonSchemaResponseFormat(formatElement);
case Type formatObjectType:
- return OpenAIChatResponseFormatHelper.GetJsonSchemaResponseFormat(formatObjectType);
+ return OpenAIChatResponseFormatBuilder.GetJsonSchemaResponseFormat(formatObjectType);
}
return null;
diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Helpers/OpenAIChatResponseFormatHelper.cs b/dotnet/src/Connectors/Connectors.OpenAI/Helpers/OpenAIChatResponseFormatBuilder.cs
similarity index 96%
rename from dotnet/src/Connectors/Connectors.OpenAI/Helpers/OpenAIChatResponseFormatHelper.cs
rename to dotnet/src/Connectors/Connectors.OpenAI/Helpers/OpenAIChatResponseFormatBuilder.cs
index e6415c4f606b..835d4c19ae32 100644
--- a/dotnet/src/Connectors/Connectors.OpenAI/Helpers/OpenAIChatResponseFormatHelper.cs
+++ b/dotnet/src/Connectors/Connectors.OpenAI/Helpers/OpenAIChatResponseFormatBuilder.cs
@@ -9,9 +9,9 @@
namespace Microsoft.SemanticKernel.Connectors.OpenAI;
///
-/// Helper class to process object.
+/// Helper class to build object.
///
-internal static class OpenAIChatResponseFormatHelper
+internal static class OpenAIChatResponseFormatBuilder
{
///
/// for JSON schema format for structured outputs.