-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenAITests.cs
More file actions
221 lines (177 loc) · 8.12 KB
/
Copy pathOpenAITests.cs
File metadata and controls
221 lines (177 loc) · 8.12 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System.ClientModel;
using System.Text.Json.Nodes;
using Devlooped.Extensions.AI.OpenAI;
using Microsoft.Extensions.AI;
using OpenAI;
using OpenAI.Responses;
using static ConfigurationExtensions;
namespace Devlooped.Extensions.AI;
public class OpenAITests(ITestOutputHelper output)
{
[SecretsFact("OPENAI_API_KEY")]
public void CanGetAsIChatClient()
{
var inner = new OpenAIClient(new ApiKeyCredential(Configuration["OPENAI_API_KEY"]!),
new OpenAIClientOptions
{
//Endpoint = new Uri("https://api.x.ai/v1"),
}).GetChatClient("grok-4").AsIChatClient();
Assert.NotNull(inner);
}
[SecretsFact("OPENAI_API_KEY")]
public async Task OpenAISwitchesModel()
{
var messages = new Chat()
{
{ "user", "What products does Tesla make?" },
};
var chat = new OpenAIChatClient(Configuration["OPENAI_API_KEY"]!, "gpt-4.1-nano",
OpenAIClientOptions.WriteTo(output));
var options = new ChatOptions
{
ModelId = "gpt-4.1-mini",
};
var response = await chat.GetResponseAsync(messages, options);
// NOTE: the chat client was requested as grok-3 but the chat options wanted a
// different model and the grok client honors that choice.
Assert.StartsWith("gpt-4.1-mini", response.ModelId);
}
[SecretsFact("OPENAI_API_KEY")]
public async Task OpenAIThinks()
{
var messages = new Chat()
{
{ "system", "You are an intelligent AI assistant that's an expert on financial matters." },
{ "user", "If you have a debt of 100k and accumulate a compounding 5% debt on top of it every year, how long before you are a negative millonaire? (round up to full integer value)" },
};
var requests = new List<JsonNode>();
var chat = new OpenAIChatClient(Configuration["OPENAI_API_KEY"]!, "o3-mini",
OpenAIClientOptions.Observable(requests.Add).WriteTo(output));
var options = new ChatOptions
{
ModelId = "o4-mini",
ReasoningEffort = ReasoningEffort.Medium
};
var response = await chat.GetResponseAsync(messages, options);
var text = response.Text;
Assert.Contains("48 years", text);
// NOTE: the chat client was requested as grok-3 but the chat options wanted a
// different model and the grok client honors that choice.
Assert.StartsWith("o4-mini", response.ModelId);
// Reasoning should have been set to medium
Assert.All(requests, x =>
{
var search = Assert.IsType<JsonObject>(x["reasoning"]);
Assert.Equal("medium", search["effort"]?.GetValue<string>());
});
}
[SecretsFact("OPENAI_API_KEY")]
public async Task GPT5_ThinksFast()
{
var messages = new Chat()
{
{ "system", "You are an intelligent AI assistant that's an expert on financial matters." },
{ "user", "If you have a debt of 100k and accumulate a compounding 5% debt on top of it every year, how long before you are a negative millonaire? (round up to full integer value)" },
};
var requests = new List<JsonNode>();
var chat = new OpenAIChatClient(Configuration["OPENAI_API_KEY"]!, "gpt-5-nano",
OpenAIClientOptions.Observable(requests.Add).WriteTo(output));
var options = new ChatOptions
{
ModelId = "gpt-5",
ReasoningEffort = ReasoningEffort.Minimal
};
var response = await chat.GetResponseAsync(messages, options);
var text = response.Text;
Assert.Contains("48 years", text);
// NOTE: the chat client was requested as grok-3 but the chat options wanted a
// different model and the grok client honors that choice.
Assert.StartsWith("gpt-5", response.ModelId);
Assert.DoesNotContain("nano", response.ModelId);
// Reasoning should have been set to medium
Assert.All(requests, x =>
{
var search = Assert.IsType<JsonObject>(x["reasoning"]);
Assert.Equal("minimal", search["effort"]?.GetValue<string>());
});
}
[SecretsTheory("OPENAI_API_KEY")]
[InlineData(ReasoningEffort.Minimal)]
[InlineData(ReasoningEffort.Low)]
[InlineData(ReasoningEffort.Medium)]
[InlineData(ReasoningEffort.High)]
public async Task GPT5_ThinkingTime(ReasoningEffort effort)
{
var messages = new Chat()
{
{ "system", "You are an intelligent AI assistant that's an expert on financial matters." },
{ "user", "If you have a debt of 100k and accumulate a compounding 5% debt on top of it every year, how long before you are a negative millonaire? (round up to full integer value)" },
};
var requests = new List<JsonNode>();
var chat = new OpenAIChatClient(Configuration["OPENAI_API_KEY"]!, "gpt-5-nano",
OpenAIClientOptions.Observable(requests.Add).WriteTo(output));
var options = new ChatOptions
{
ModelId = "gpt-5",
ReasoningEffort = effort
};
var watch = System.Diagnostics.Stopwatch.StartNew();
var response = await chat.GetResponseAsync(messages, options);
watch.Stop();
var text = response.Text;
Assert.Contains("48 years", text);
// NOTE: the chat client was requested as grok-3 but the chat options wanted a
// different model and the grok client honors that choice.
Assert.StartsWith("gpt-5", response.ModelId);
Assert.DoesNotContain("nano", response.ModelId);
// Reasoning should have been set to medium
Assert.All(requests, x =>
{
var search = Assert.IsType<JsonObject>(x["reasoning"]);
Assert.Equal(effort.ToString().ToLowerInvariant(), search["effort"]?.GetValue<string>());
});
output.WriteLine($"Effort: {effort}, Time: {watch.ElapsedMilliseconds}ms, Tokens: {response.Usage?.TotalTokenCount}");
}
[SecretsFact("OPENAI_API_KEY")]
public async Task WebSearchCountryHighContext()
{
var messages = new Chat()
{
{ "system", "Sos un asistente del Cerro Catedral, usas la funcionalidad de Live Search en el sitio oficial." },
{ "system", $"Hoy es {DateTime.Now.ToString("o")}." },
{ "system",
"""
Web search sources:
https://catedralaltapatagonia.com/parte-de-nieve/
https://catedralaltapatagonia.com/tarifas/
https://catedralaltapatagonia.com/
DO NOT USE https://partediario.catedralaltapatagonia.com/partediario for web search, it's **OBSOLETE**.
"""},
{ "user", "Cuanto cuesta el pase diario en el Catedral hoy?" },
};
var requests = new List<JsonNode>();
var responses = new List<JsonNode>();
var chat = new OpenAIChatClient(Configuration["OPENAI_API_KEY"]!, "gpt-4.1",
OpenAIClientOptions.Observable(requests.Add, responses.Add).WriteTo(output));
var options = new ChatOptions
{
Tools = [new WebSearchTool("AR")
{
Region = "Bariloche",
TimeZone = "America/Argentina/Buenos_Aires",
ContextSize = WebSearchContextSize.High
}]
};
var response = await chat.GetResponseAsync(messages, options);
var text = response.Text;
var raw = Assert.IsType<OpenAIResponse>(response.RawRepresentation);
Assert.NotEmpty(raw.OutputItems.OfType<WebSearchCallResponseItem>());
var assistant = raw.OutputItems.OfType<MessageResponseItem>().Where(x => x.Role == MessageRole.Assistant).FirstOrDefault();
Assert.NotNull(assistant);
var content = Assert.Single(assistant.Content);
Assert.NotEmpty(content.OutputTextAnnotations);
Assert.Contains(content.OutputTextAnnotations,
x => x.Kind == ResponseMessageAnnotationKind.UriCitation &&
x.UriCitationUri.AbsoluteUri.StartsWith("https://catedralaltapatagonia.com/tarifas/"));
}
}