openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.6.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/Chat/ChatMockTests.cs

223lines · modecode

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