Skip to content

Commit 3a35b75

Browse files
committed
Add enums handling to GetStructuredResponse, add new tests
1 parent 834a791 commit 3a35b75

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

OpenAI.ChatGpt.Modules.StructuredResponse/OpenAiClientExtensions.GetAsObjectAsync.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Text.Json;
2+
using System.Text.Json.Serialization;
23
using Json.Schema;
34
using Json.Schema.Generation;
45
using OpenAI.ChatGpt.Models.ChatCompletion;
@@ -97,7 +98,11 @@ internal static async Task<TObject> GetStructuredResponse<TObject>(
9798
rawResponseGetter,
9899
cancellationToken);
99100

100-
jsonDeserializerOptions ??= new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
101+
jsonDeserializerOptions ??= new JsonSerializerOptions
102+
{
103+
PropertyNameCaseInsensitive = true,
104+
Converters = { new JsonStringEnumConverter() }
105+
};
101106
var deserialized = JsonSerializer.Deserialize<TObject>(response, jsonDeserializerOptions);
102107
if (deserialized is null)
103108
{

tests/OpenAI.ChatGpt.IntegrationTests/OpenAiClientTests/OpenAiClient_GetAsObjectTests.cs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public async void Get_simple_structured_response_from_ChatGPT()
2525
}
2626

2727
[Fact]
28-
public async void Get_structured_response_with_array_from_ChatGPT()
28+
public async void Get_structured_response_with_ARRAY_from_ChatGPT()
2929
{
3030
var message =
3131
Dialog.StartAsSystem("What did user input?")
@@ -43,16 +43,36 @@ public async void Get_structured_response_with_array_from_ChatGPT()
4343
response.Items[1].Quantity.Should().Be(3);
4444
}
4545

46+
[Fact]
47+
public async void Get_structured_response_with_ENUM_from_ChatGPT()
48+
{
49+
var message =
50+
Dialog.StartAsSystem("What did user input?")
51+
.ThenUser("Мой любимый цвет - красный");
52+
var response = await _client.GetStructuredResponse<Thing>(message);
53+
response.Should().NotBeNull();
54+
response.Color.Should().Be(Thing.Colors.Red);
55+
}
56+
57+
[Fact]
58+
public async void Get_structured_response_with_extra_data_from_ChatGPT()
59+
{
60+
var message =
61+
Dialog.StartAsSystem("Just answer the questions.")
62+
.ThenUser("In what year was the city of Almaty originally founded?");
63+
var response = await _client.GetStructuredResponse<City>(message);
64+
response.Should().NotBeNull();
65+
response.Name.Should().Be("Almaty");
66+
response.YearOfFoundation.Should().Be(1854);
67+
response.Country.Should().Be("Kazakhstan");
68+
}
69+
4670
private class Order
4771
{
4872
public UserInfo UserInfo { get; set; }
4973
public List<Item> Items { get; set; }
5074

51-
public class Item
52-
{
53-
public string Name { get; set; } = "";
54-
public int Quantity { get; set; }
55-
}
75+
public record Item(string Name, int Quantity);
5676
}
5777

5878
private class UserInfo
@@ -61,4 +81,18 @@ private class UserInfo
6181
public int Age { get; init; }
6282
public string Email { get; init; }
6383
}
84+
85+
private record Thing(Thing.Colors Color)
86+
{
87+
public enum Colors
88+
{
89+
None,
90+
Red,
91+
Green,
92+
Blue
93+
}
94+
}
95+
96+
private record City(string Name, int YearOfFoundation, string Country);
6497
}
98+

0 commit comments

Comments
 (0)