/
OpenAiClient_GetStructuredResponse.cs
99 lines (86 loc) · 3.21 KB
/
OpenAiClient_GetStructuredResponse.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using OpenAI.ChatGpt.Modules.StructuredResponse;
namespace OpenAI.ChatGpt.IntegrationTests.OpenAiClientTests;
public class OpenAiClientGetStructuredResponseTests
{
private readonly OpenAiClient _client;
public OpenAiClientGetStructuredResponseTests()
{
_client = new OpenAiClient(Helpers.GetOpenAiKey());
}
[Fact]
public async void Get_simple_structured_response_from_ChatGPT()
{
var message =
Dialog.StartAsSystem("What did user input?")
.ThenUser("My name is John, my age is 30, my email is john@gmail.com");
var response = await _client.GetStructuredResponse<UserInfo>(message);
response.Should().NotBeNull();
response.Name.Should().Be("John");
response.Age.Should().Be(30);
response.Email.Should().Be("john@gmail.com");
}
[Fact]
public async void Get_structured_response_with_ARRAY_from_ChatGPT()
{
var message = Dialog
.StartAsSystem("What did user input?")
.ThenUser("My name is John, my age is 30, my email is john@gmail.com. " +
"I want to buy 2 apple and 3 orange.");
var response = await _client.GetStructuredResponse<Order>(message);
response.Should().NotBeNull();
response.UserInfo.Should().NotBeNull();
response.UserInfo!.Name.Should().Be("John");
response.UserInfo.Age.Should().Be(30);
response.UserInfo.Email.Should().Be("john@gmail.com");
response.Items.Should().HaveCount(2);
response.Items![0].Name.Should().Be("apple");
response.Items[0].Quantity.Should().Be(2);
response.Items[1].Name.Should().Be("orange");
response.Items[1].Quantity.Should().Be(3);
}
[Fact]
public async void Get_structured_response_with_ENUM_from_ChatGPT()
{
var message = Dialog
.StartAsSystem("What did user input?")
.ThenUser("Мой любимый цвет - красный");
var response = await _client.GetStructuredResponse<Thing>(message);
response.Should().NotBeNull();
response.Color.Should().Be(Thing.Colors.Red);
}
[Fact]
public async void Get_structured_response_with_extra_data_from_ChatGPT()
{
var message = Dialog
.StartAsSystem("Return requested data.")
.ThenUser("I need info about Almaty city");
var response = await _client.GetStructuredResponse<City>(message);
response.Should().NotBeNull();
response.Name.Should().Be("Almaty");
response.Country.Should().Be("Kazakhstan");
response.YearOfFoundation.Should().Be(1854);
}
private class Order
{
public UserInfo? UserInfo { get; set; }
public List<Item>? Items { get; set; }
public record Item(string Name, int Quantity);
}
private class UserInfo
{
public string? Name { get; init; }
public int Age { get; init; }
public string? Email { get; init; }
}
private record Thing(Thing.Colors Color)
{
public enum Colors
{
None,
Red,
Green,
Blue
}
}
private record City(string Name, int YearOfFoundation, string Country);
}