-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenAITests.cs
More file actions
123 lines (99 loc) · 4.45 KB
/
OpenAITests.cs
File metadata and controls
123 lines (99 loc) · 4.45 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
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 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 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.StartsWith("https://catedralaltapatagonia.com/tarifas/"));
}
}