openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.2.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/Chat/ChatMockTests.cs

284lines · modecode

1using NUnit.Framework;
2using OpenAI.Chat;
3using OpenAI.Tests.Utility;
4using System;
5using System.ClientModel;
6using System.Collections.Generic;
7using System.Linq;
8using System.Threading;
9using System.Threading.Tasks;
10
11namespace OpenAI.Tests.Chat;
12
13[TestFixture(true)]
14[TestFixture(false)]
15[Parallelizable(ParallelScope.All)]
16[Category("Chat")]
17[Category("Smoke")]
18public class ChatMockTests : SyncAsyncTestBase
19{
20 private static readonly ApiKeyCredential s_fakeCredential = new ApiKeyCredential("key");
21
22 public ChatMockTests(bool isAsync) : base(isAsync)
23 {
24 }
25
26 private static readonly List<ChatMessage> s_messages = new()
27 {
28 new UserChatMessage("Message content.")
29 };
30
31 [Test]
32 public async Task CompleteChatDeserializesId()
33 {
34 OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """
35 {
36 "id": "chat_id"
37 }
38 """);
39 ChatClient client = new ChatClient("model", s_fakeCredential, clientOptions);
40
41 ChatCompletion chatCompletion = IsAsync
42 ? await client.CompleteChatAsync(s_messages)
43 : client.CompleteChat(s_messages);
44
45 Assert.That(chatCompletion.Id, Is.EqualTo("chat_id"));
46 }
47
48 [Test]
49 public async Task CompleteChatDeserializesCreatedAt()
50 {
51 OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """
52 {
53 "created": 1704096000
54 }
55 """);
56 ChatClient client = new ChatClient("model", s_fakeCredential, clientOptions);
57
58 ChatCompletion chatCompletion = IsAsync
59 ? await client.CompleteChatAsync(s_messages)
60 : client.CompleteChat(s_messages);
61
62 Assert.That(chatCompletion.CreatedAt.ToUnixTimeSeconds(), Is.EqualTo(1704096000));
63 }
64
65 [Test]
66 public async Task CompleteChatDeserializesModel()
67 {
68 OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """
69 {
70 "model": "model_name"
71 }
72 """);
73 ChatClient client = new ChatClient("model", s_fakeCredential, clientOptions);
74
75 ChatCompletion chatCompletion = IsAsync
76 ? await client.CompleteChatAsync(s_messages)
77 : client.CompleteChat(s_messages);
78
79 Assert.That(chatCompletion.Model, Is.EqualTo("model_name"));
80 }
81
82 [Test]
83 public async Task CompleteChatDeserializesSystemFingerprint()
84 {
85 OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """
86 {
87 "system_fingerprint": "fingerprint_value"
88 }
89 """);
90 ChatClient client = new ChatClient("model", s_fakeCredential, clientOptions);
91
92 ChatCompletion chatCompletion = IsAsync
93 ? await client.CompleteChatAsync(s_messages)
94 : client.CompleteChat(s_messages);
95
96 Assert.That(chatCompletion.SystemFingerprint, Is.EqualTo("fingerprint_value"));
97 }
98
99 [Test]
100 public async Task CompleteChatDeserializesUsage()
101 {
102 OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """
103 {
104 "usage": {
105 "prompt_tokens": 10,
106 "completion_tokens": 20,
107 "total_tokens": 30
108 }
109 }
110 """);
111 ChatClient client = new ChatClient("model", s_fakeCredential, clientOptions);
112
113 ChatCompletion chatCompletion = IsAsync
114 ? await client.CompleteChatAsync(s_messages)
115 : client.CompleteChat(s_messages);
116
117 Assert.That(chatCompletion.Usage.InputTokenCount, Is.EqualTo(10));
118 Assert.That(chatCompletion.Usage.OutputTokenCount, Is.EqualTo(20));
119 Assert.That(chatCompletion.Usage.TotalTokenCount, Is.EqualTo(30));
120 }
121
122 [Test]
123 [TestCase("stop", ChatFinishReason.Stop)]
124 [TestCase("length", ChatFinishReason.Length)]
125 [TestCase("content_filter", ChatFinishReason.ContentFilter)]
126 [TestCase("tool_calls", ChatFinishReason.ToolCalls)]
127 [TestCase("function_call", ChatFinishReason.FunctionCall)]
128 public async Task CompleteChatDeserializesFinishReason(string stringReason, ChatFinishReason expectedReason)
129 {
130 OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, $$"""
131 {
132 "choices": [
133 {
134 "finish_reason": "{{stringReason}}"
135 }
136 ]
137 }
138 """);
139 ChatClient client = new ChatClient("model", s_fakeCredential, clientOptions);
140
141 ChatCompletion chatCompletion = IsAsync
142 ? await client.CompleteChatAsync(s_messages)
143 : client.CompleteChat(s_messages);
144
145 Assert.That(chatCompletion.FinishReason, Is.EqualTo(expectedReason));
146 }
147
148 [Test]
149 [TestCase("system", ChatMessageRole.System)]
150 [TestCase("user", ChatMessageRole.User)]
151 [TestCase("assistant", ChatMessageRole.Assistant)]
152 [TestCase("tool", ChatMessageRole.Tool)]
153 [TestCase("function", ChatMessageRole.Function)]
154 public async Task CompleteChatDeserializesRole(string stringRole, ChatMessageRole expectedRole)
155 {
156 OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, $$"""
157 {
158 "choices": [
159 {
160 "message": {
161 "role": "{{stringRole}}"
162 }
163 }
164 ]
165 }
166 """);
167 ChatClient client = new ChatClient("model", s_fakeCredential, clientOptions);
168
169 ChatCompletion chatCompletion = IsAsync
170 ? await client.CompleteChatAsync(s_messages)
171 : client.CompleteChat(s_messages);
172
173 Assert.That(chatCompletion.Role, Is.EqualTo(expectedRole));
174 }
175
176 [Test]
177 public async Task CompleteChatDeserializesTextContent()
178 {
179 OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """
180 {
181 "choices": [
182 {
183 "message": {
184 "content": "This is the content."
185 }
186 }
187 ]
188 }
189 """);
190 ChatClient client = new ChatClient("model", s_fakeCredential, clientOptions);
191
192 ChatCompletion chatCompletion = IsAsync
193 ? await client.CompleteChatAsync(s_messages)
194 : client.CompleteChat(s_messages);
195 ChatMessageContentPart contentPart = chatCompletion.Content.Single();
196
197 Assert.That(contentPart.Kind, Is.EqualTo(ChatMessageContentPartKind.Text));
198 Assert.That(contentPart.Text, Is.EqualTo("This is the content."));
199 }
200
201 [Test]
202 public void CompleteChatRespectsTheCancellationToken()
203 {
204 ChatClient client = new ChatClient("model", s_fakeCredential);
205 using CancellationTokenSource cancellationSource = new();
206 cancellationSource.Cancel();
207
208 if (IsAsync)
209 {
210 Assert.That(async () => await client.CompleteChatAsync(s_messages, cancellationToken: cancellationSource.Token),
211 Throws.InstanceOf<OperationCanceledException>());
212 }
213 else
214 {
215 Assert.That(() => client.CompleteChat(s_messages, cancellationToken: cancellationSource.Token),
216 Throws.InstanceOf<OperationCanceledException>());
217 }
218 }
219
220 [Test]
221 public void CompleteChatStreamingAsyncRespectsTheCancellationToken()
222 {
223 AssertAsyncOnly();
224
225 ChatClient client = new ChatClient("model", s_fakeCredential);
226 using CancellationTokenSource cancellationSource = new();
227 cancellationSource.Cancel();
228
229 IAsyncEnumerator<StreamingChatCompletionUpdate> enumerator = client
230 .CompleteChatStreamingAsync(s_messages, cancellationToken: cancellationSource.Token)
231 .GetAsyncEnumerator();
232
233 Assert.That(async () => await enumerator.MoveNextAsync(), Throws.InstanceOf<OperationCanceledException>());
234 }
235
236 [Test]
237 public void CompleteChatStreamingRespectsTheCancellationToken()
238 {
239 AssertSyncOnly();
240
241 ChatClient client = new ChatClient("model", s_fakeCredential);
242 using CancellationTokenSource cancellationSource = new();
243 cancellationSource.Cancel();
244
245 IEnumerator<StreamingChatCompletionUpdate> enumerator = client
246 .CompleteChatStreaming(s_messages, cancellationToken: cancellationSource.Token)
247 .GetEnumerator();
248
249 Assert.That(() => enumerator.MoveNext(), Throws.InstanceOf<OperationCanceledException>());
250 }
251
252 private async ValueTask<StreamingChatCompletionUpdate> InvokeCompleteChatStreamingAsync(ChatClient client)
253 {
254 if (IsAsync)
255 {
256 IAsyncEnumerator<StreamingChatCompletionUpdate> enumerator = client
257 .CompleteChatStreamingAsync(s_messages)
258 .GetAsyncEnumerator();
259
260 await enumerator.MoveNextAsync();
261 return enumerator.Current;
262 }
263 else
264 {
265 IEnumerator<StreamingChatCompletionUpdate> enumerator = client
266 .CompleteChatStreaming(s_messages)
267 .GetEnumerator();
268
269 enumerator.MoveNext();
270 return enumerator.Current;
271 }
272 }
273
274 private OpenAIClientOptions GetClientOptionsWithMockResponse(int status, string content)
275 {
276 MockPipelineResponse response = new MockPipelineResponse(status);
277 response.SetContent(content);
278
279 return new OpenAIClientOptions()
280 {
281 Transport = new MockPipelineTransport(response)
282 };
283 }
284}