Skip to content

Commit

Permalink
.Net: Fix bug to allow activation of JSON mode through execution_sett…
Browse files Browse the repository at this point in the history
…ings (#4774)

### Motivation and Context
Closes [#4719](#4719)

**Issue**

- When setting the `execution_settings` from YAML or `config.txt`, the
`response_format` field is not recognized, and thus the JSON mode cannot
be activated.

**Root Cause**
- The issue arises because the `ResponseFormat` property, which is of
type `object?`, stores values as `JsonElement` types when read from the
aforementioned files.
- The `CreateChatCompletionOptions` method does not account for this and
performs type checking directly against types such as `string`, leading
to improper conditional branching.
- As a result, the `ChatCompletionsOptions`'s `ResponseFormat` value
remains unassigned, preventing the activation of JSON mode.

<!-- 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.
-->

### Description

This PR addresses the problem by adjusting the type checks to properly
recognize the `JsonElement` objects.
This ensures that when `ResponseFormat` is set, the request sent to
OpenAI is correct, and JSON mode can be utilized.

Please let me know if any further changes or discussions are needed
regarding this pull request.
<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### 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 😄
  • Loading branch information
sugii-norichika-intern-fixer committed Feb 3, 2024
1 parent eed5193 commit cd852e0
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions dotnet/src/Connectors/Connectors.OpenAI/AzureSdk/ClientCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,25 @@ private static CompletionsOptions CreateCompletionsOptions(string text, OpenAIPr
break;
}
break;

case JsonElement formatElement:
// This is a workaround for a type mismatch when deserializing a JSON into an object? type property.
// Handling only string formatElement.
if (formatElement.ValueKind == JsonValueKind.String)
{
string formatString = formatElement.GetString() ?? "";
switch (formatString)
{
case "json_object":
options.ResponseFormat = ChatCompletionsResponseFormat.JsonObject;
break;

case "text":
options.ResponseFormat = ChatCompletionsResponseFormat.Text;
break;
}
}
break;
}

executionSettings.ToolCallBehavior?.ConfigureOptions(kernel, options);
Expand Down

0 comments on commit cd852e0

Please sign in to comment.