Skip to content

Commit 68e6dd7

Browse files
authored
.Net: Small improvements in Structured Outputs (#9906)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Resolves: #9897 This PR addresses remaining comments from previous PR related to Structured Outputs: #9873. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [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 😄
1 parent 049cbbf commit 68e6dd7

File tree

4 files changed

+10
-13
lines changed

4 files changed

+10
-13
lines changed

dotnet/samples/Concepts/ChatCompletion/OpenAI_StructuredOutputs.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,7 @@ public async Task StructuredOutputsWithFunctionsFromYamlAsync()
299299
var functionPath = Path.Combine(Directory.GetCurrentDirectory(), "Resources", "Plugins", "MoviePlugins", "MoviePluginYaml", "TopMovies.yaml");
300300

301301
// Load YAML prompt.
302-
using Stream stream = File.OpenRead(functionPath);
303-
using StreamReader reader = new(stream);
304-
305-
var topMoviesYaml = reader.ReadToEnd();
302+
var topMoviesYaml = File.ReadAllText(functionPath);
306303

307304
// Import a function from YAML.
308305
var function = kernel.CreateFunctionFromPromptYaml(topMoviesYaml);

dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Helpers/OpenAIChatResponseFormatHelperTests.cs renamed to dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Helpers/OpenAIChatResponseFormatBuilderTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
namespace SemanticKernel.Connectors.OpenAI.UnitTests.Helpers;
1111

1212
/// <summary>
13-
/// Unit tests for <see cref="OpenAIChatResponseFormatHelper"/> class.
13+
/// Unit tests for <see cref="OpenAIChatResponseFormatBuilder"/> class.
1414
/// </summary>
15-
public sealed class OpenAIChatResponseFormatHelperTests
15+
public sealed class OpenAIChatResponseFormatBuilderTests
1616
{
1717
private readonly JsonSerializerOptions _options = new();
1818

19-
public OpenAIChatResponseFormatHelperTests()
19+
public OpenAIChatResponseFormatBuilderTests()
2020
{
2121
this._options.Converters.Add(new BinaryDataJsonConverter());
2222
}
@@ -33,7 +33,7 @@ public void GetJsonSchemaResponseFormatReturnsChatResponseFormatByDefault(
3333
var jsonElement = jsonDocument.RootElement;
3434

3535
// Act
36-
var chatResponseFormat = OpenAIChatResponseFormatHelper.GetJsonSchemaResponseFormat(jsonElement);
36+
var chatResponseFormat = OpenAIChatResponseFormatBuilder.GetJsonSchemaResponseFormat(jsonElement);
3737
var responseFormat = this.GetResponseFormat(chatResponseFormat);
3838

3939
// Assert
@@ -79,7 +79,7 @@ public void GetJsonSchemaResponseFormatThrowsExceptionWhenSchemaDoesNotExist()
7979
var jsonElement = jsonDocument.RootElement;
8080

8181
// Act & Assert
82-
Assert.Throws<ArgumentException>(() => OpenAIChatResponseFormatHelper.GetJsonSchemaResponseFormat(jsonElement));
82+
Assert.Throws<ArgumentException>(() => OpenAIChatResponseFormatBuilder.GetJsonSchemaResponseFormat(jsonElement));
8383
}
8484

8585
public static TheoryData<string, string, bool?> ChatResponseFormatJson => new()

dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.ChatCompletion.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,10 +540,10 @@ protected virtual ChatCompletionOptions CreateChatCompletionOptions(
540540
}
541541
}
542542

543-
return OpenAIChatResponseFormatHelper.GetJsonSchemaResponseFormat(formatElement);
543+
return OpenAIChatResponseFormatBuilder.GetJsonSchemaResponseFormat(formatElement);
544544

545545
case Type formatObjectType:
546-
return OpenAIChatResponseFormatHelper.GetJsonSchemaResponseFormat(formatObjectType);
546+
return OpenAIChatResponseFormatBuilder.GetJsonSchemaResponseFormat(formatObjectType);
547547
}
548548

549549
return null;

dotnet/src/Connectors/Connectors.OpenAI/Helpers/OpenAIChatResponseFormatHelper.cs renamed to dotnet/src/Connectors/Connectors.OpenAI/Helpers/OpenAIChatResponseFormatBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
namespace Microsoft.SemanticKernel.Connectors.OpenAI;
1010

1111
/// <summary>
12-
/// Helper class to process <see cref="ChatResponseFormat"/> object.
12+
/// Helper class to build <see cref="ChatResponseFormat"/> object.
1313
/// </summary>
14-
internal static class OpenAIChatResponseFormatHelper
14+
internal static class OpenAIChatResponseFormatBuilder
1515
{
1616
/// <summary>
1717
/// <see cref="JsonSchemaMapperConfiguration"/> for JSON schema format for structured outputs.

0 commit comments

Comments
 (0)