-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenAITests.cs
More file actions
301 lines (243 loc) · 11.4 KB
/
OpenAITests.cs
File metadata and controls
301 lines (243 loc) · 11.4 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
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, "gpt-5")] // Obsolete as of 5.1
[InlineData(ReasoningEffort.None)]
[InlineData(ReasoningEffort.Low)]
[InlineData(ReasoningEffort.Medium)]
[InlineData(ReasoningEffort.High)]
[InlineData(ReasoningEffort.XHigh)]
public async Task GPT5_ThinkingTime(ReasoningEffort effort, string modelId = "gpt-5.2")
{
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"]!, modelId,
OpenAIClientOptions.Observable(requests.Add).WriteTo(output));
var options = new ChatOptions
{
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 expected value
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 GPT5_NoReasoningTokens()
{
var requests = new List<JsonNode>();
//var chat = new OpenAIChatClient(Configuration["OPENAI_API_KEY"]!, "gpt-4o",
// OpenAIClientOptions.Observable(requests.Add).WriteTo(output));
var chat = new OpenAIClient(new ApiKeyCredential(Configuration["OPENAI_API_KEY"]!),
OpenAIClientOptions.Observable(requests.Add).WriteTo(output))
.GetOpenAIResponseClient("gpt-4o")
.AsIChatClient();
var reasoned = await chat.GetResponseAsync(
"How much gold would it take to coat the Statue of Liberty in a 1mm layer?",
new ChatOptions
{
ModelId = "gpt-5.1",
ReasoningEffort = ReasoningEffort.Low
});
Assert.StartsWith("gpt-5.1", reasoned.ModelId);
Assert.NotNull(reasoned.Usage?.AdditionalCounts);
Assert.True(reasoned.Usage.AdditionalCounts.ContainsKey("OutputTokenDetails.ReasoningTokenCount"));
Assert.True(reasoned.Usage.AdditionalCounts["OutputTokenDetails.ReasoningTokenCount"] > 0);
var nonreasoned = await chat.GetResponseAsync(
"How much gold would it take to coat the Statue of Liberty in a 1mm layer?",
new ChatOptions
{
ModelId = "gpt-5.1",
ReasoningEffort = ReasoningEffort.None
});
Assert.NotNull(nonreasoned.Usage?.AdditionalCounts);
Assert.True(nonreasoned.Usage.AdditionalCounts.ContainsKey("OutputTokenDetails.ReasoningTokenCount"));
Assert.True(nonreasoned.Usage.AdditionalCounts["OutputTokenDetails.ReasoningTokenCount"] == 0);
}
[SecretsTheory("OPENAI_API_KEY")]
[InlineData(Verbosity.Low)]
[InlineData(Verbosity.Medium)]
[InlineData(Verbosity.High)]
public async Task GPT5_Verbosity(Verbosity verbosity)
{
var messages = new Chat()
{
{ "system", "You are an intelligent AI assistant that's an expert on everything." },
{ "user", "What's the answer to the universe and everything?" },
};
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-mini",
Verbosity = verbosity
};
var watch = System.Diagnostics.Stopwatch.StartNew();
var response = await chat.GetResponseAsync(messages, options);
watch.Stop();
var text = response.Text;
output.WriteLine(text);
Assert.StartsWith("gpt-5", response.ModelId);
Assert.DoesNotContain("nano", response.ModelId);
// Verbosity should have been set to the expected value
Assert.All(requests, x =>
{
var text = Assert.IsType<JsonObject>(x["text"]);
Assert.Equal(verbosity.ToString().ToLowerInvariant(), text["verbosity"]?.GetValue<string>());
});
output.WriteLine($"Verbosity: {verbosity}, 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 Devlooped.Extensions.AI.OpenAI.WebSearchTool("AR")
{
Region = "Bariloche",
TimeZone = "America/Argentina/Buenos_Aires",
}]
};
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/"));
}
}