openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/Batch/BatchTests.cs
245lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Batch; |
| 3 | using OpenAI.Files; |
| 4 | using OpenAI.Tests.Utility; |
| 5 | using System; |
| 6 | using System.ClientModel; |
| 7 | using System.ClientModel.Primitives; |
| 8 | using System.IO; |
| 9 | using System.Text.Json; |
| 10 | using System.Threading.Tasks; |
| 11 | using static OpenAI.Tests.TestHelpers; |
| 12 | |
| 13 | namespace OpenAI.Tests.Batch; |
| 14 | |
| 15 | [TestFixture(true)] |
| 16 | [TestFixture(false)] |
| 17 | [Parallelizable(ParallelScope.All)] |
| 18 | [Category("Batch")] |
| 19 | public class BatchTests : SyncAsyncTestBase |
| 20 | { |
| 21 | private static BatchClient GetTestClient() => GetTestClient<BatchClient>(TestScenario.Batch); |
| 22 | |
| 23 | public BatchTests(bool isAsync) : base(isAsync) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | [Test] |
| 28 | public void ListBatchesProtocol() |
| 29 | { |
| 30 | AssertSyncOnly(); |
| 31 | |
| 32 | BatchClient client = GetTestClient(); |
| 33 | CollectionResult batches = client.GetBatches(after: null, limit: null, options: null); |
| 34 | |
| 35 | int pageCount = 0; |
| 36 | foreach (ClientResult pageResult in batches.GetRawPages()) |
| 37 | { |
| 38 | BinaryData response = pageResult.GetRawResponse().Content; |
| 39 | using JsonDocument jsonDocument = JsonDocument.Parse(response); |
| 40 | JsonElement dataElement = jsonDocument.RootElement.GetProperty("data"); |
| 41 | |
| 42 | Assert.That(dataElement.GetArrayLength(), Is.GreaterThan(0)); |
| 43 | |
| 44 | long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds(); |
| 45 | |
| 46 | foreach (JsonElement batchElement in dataElement.EnumerateArray()) |
| 47 | { |
| 48 | JsonElement createdAtElement = batchElement.GetProperty("created_at"); |
| 49 | long createdAt = createdAtElement.GetInt64(); |
| 50 | |
| 51 | Assert.That(createdAt, Is.GreaterThan(unixTime2024)); |
| 52 | } |
| 53 | pageCount++; |
| 54 | } |
| 55 | |
| 56 | Assert.GreaterOrEqual(pageCount, 1); |
| 57 | } |
| 58 | |
| 59 | [Test] |
| 60 | public async Task ListBatchesProtocolAsync() |
| 61 | { |
| 62 | AssertAsyncOnly(); |
| 63 | |
| 64 | BatchClient client = GetTestClient(); |
| 65 | AsyncCollectionResult batches = client.GetBatchesAsync(after: null, limit: null, options: null); |
| 66 | |
| 67 | int pageCount = 0; |
| 68 | await foreach (ClientResult pageResult in batches.GetRawPagesAsync()) |
| 69 | { |
| 70 | BinaryData response = pageResult.GetRawResponse().Content; |
| 71 | using JsonDocument jsonDocument = JsonDocument.Parse(response); |
| 72 | JsonElement dataElement = jsonDocument.RootElement.GetProperty("data"); |
| 73 | |
| 74 | Assert.That(dataElement.GetArrayLength(), Is.GreaterThan(0)); |
| 75 | |
| 76 | long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds(); |
| 77 | |
| 78 | foreach (JsonElement batchElement in dataElement.EnumerateArray()) |
| 79 | { |
| 80 | JsonElement createdAtElement = batchElement.GetProperty("created_at"); |
| 81 | long createdAt = createdAtElement.GetInt64(); |
| 82 | |
| 83 | Assert.That(createdAt, Is.GreaterThan(unixTime2024)); |
| 84 | } |
| 85 | pageCount++; |
| 86 | } |
| 87 | |
| 88 | Assert.GreaterOrEqual(pageCount, 1); |
| 89 | } |
| 90 | |
| 91 | [Test] |
| 92 | public async Task CreateGetAndCancelBatchProtocol() |
| 93 | { |
| 94 | using MemoryStream testFileStream = new(); |
| 95 | using StreamWriter streamWriter = new(testFileStream); |
| 96 | string input = @"{""custom_id"": ""request-1"", ""method"": ""POST"", ""url"": ""/v1/chat/completions"", ""body"": {""model"": ""gpt-4o-mini"", ""messages"": [{""role"": ""system"", ""content"": ""You are a helpful assistant.""}, {""role"": ""user"", ""content"": ""What is 2+2?""}]}}"; |
| 97 | streamWriter.WriteLine(input); |
| 98 | streamWriter.Flush(); |
| 99 | testFileStream.Position = 0; |
| 100 | |
| 101 | OpenAIFileClient fileClient = GetTestClient<OpenAIFileClient>(TestScenario.Files); |
| 102 | OpenAIFile inputFile = await fileClient.UploadFileAsync(testFileStream, "test-batch-file", FileUploadPurpose.Batch); |
| 103 | Assert.That(inputFile.Id, Is.Not.Null.And.Not.Empty); |
| 104 | |
| 105 | BatchClient client = GetTestClient(); |
| 106 | BinaryContent content = BinaryContent.Create(BinaryData.FromObjectAsJson(new |
| 107 | { |
| 108 | input_file_id = inputFile.Id, |
| 109 | endpoint = "/v1/chat/completions", |
| 110 | completion_window = "24h", |
| 111 | metadata = new |
| 112 | { |
| 113 | testMetadataKey = "test metadata value", |
| 114 | }, |
| 115 | })); |
| 116 | CreateBatchOperation batchOperation = IsAsync |
| 117 | ? await client.CreateBatchAsync(content, waitUntilCompleted: false) |
| 118 | : client.CreateBatch(content, waitUntilCompleted: false); |
| 119 | |
| 120 | BinaryData response = batchOperation.GetRawResponse().Content; |
| 121 | JsonDocument jsonDocument = JsonDocument.Parse(response); |
| 122 | |
| 123 | JsonElement idElement = jsonDocument.RootElement.GetProperty("id"); |
| 124 | JsonElement createdAtElement = jsonDocument.RootElement.GetProperty("created_at"); |
| 125 | JsonElement statusElement = jsonDocument.RootElement.GetProperty("status"); |
| 126 | JsonElement metadataElement = jsonDocument.RootElement.GetProperty("metadata"); |
| 127 | JsonElement testMetadataKeyElement = metadataElement.GetProperty("testMetadataKey"); |
| 128 | |
| 129 | string id = idElement.GetString(); |
| 130 | long createdAt = createdAtElement.GetInt64(); |
| 131 | string status = statusElement.GetString(); |
| 132 | string testMetadataKey = testMetadataKeyElement.GetString(); |
| 133 | |
| 134 | long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds(); |
| 135 | |
| 136 | Assert.That(id, Is.Not.Null.And.Not.Empty); |
| 137 | Assert.That(createdAt, Is.GreaterThan(unixTime2024)); |
| 138 | Assert.That(status, Is.EqualTo("validating")); |
| 139 | Assert.That(testMetadataKey, Is.EqualTo("test metadata value")); |
| 140 | |
| 141 | JsonElement endpointElement = jsonDocument.RootElement.GetProperty("endpoint"); |
| 142 | string endpoint = endpointElement.GetString(); |
| 143 | |
| 144 | Assert.That(endpoint, Is.EqualTo("/v1/chat/completions")); |
| 145 | |
| 146 | ClientResult clientResult = IsAsync |
| 147 | ? await batchOperation.CancelAsync(options: null) |
| 148 | : batchOperation.Cancel(options: null); |
| 149 | |
| 150 | statusElement = jsonDocument.RootElement.GetProperty("status"); |
| 151 | status = statusElement.GetString(); |
| 152 | |
| 153 | Assert.That(status, Is.EqualTo("validating")); |
| 154 | } |
| 155 | |
| 156 | [TestCase(true)] |
| 157 | [TestCase(false)] |
| 158 | public async Task CanRehydrateBatchOperation(bool useBatchId) |
| 159 | { |
| 160 | using MemoryStream testFileStream = new(); |
| 161 | using StreamWriter streamWriter = new(testFileStream); |
| 162 | string input = @"{""custom_id"": ""request-1"", ""method"": ""POST"", ""url"": ""/v1/chat/completions"", ""body"": {""model"": ""gpt-4o-mini"", ""messages"": [{""role"": ""system"", ""content"": ""You are a helpful assistant.""}, {""role"": ""user"", ""content"": ""What is 2+2?""}]}}"; |
| 163 | streamWriter.WriteLine(input); |
| 164 | streamWriter.Flush(); |
| 165 | testFileStream.Position = 0; |
| 166 | |
| 167 | OpenAIFileClient fileClient = GetTestClient<OpenAIFileClient>(TestScenario.Files); |
| 168 | OpenAIFile inputFile = await fileClient.UploadFileAsync(testFileStream, "test-batch-file", FileUploadPurpose.Batch); |
| 169 | Assert.That(inputFile.Id, Is.Not.Null.And.Not.Empty); |
| 170 | |
| 171 | BatchClient client = GetTestClient(); |
| 172 | BinaryContent content = BinaryContent.Create(BinaryData.FromObjectAsJson(new |
| 173 | { |
| 174 | input_file_id = inputFile.Id, |
| 175 | endpoint = "/v1/chat/completions", |
| 176 | completion_window = "24h", |
| 177 | metadata = new |
| 178 | { |
| 179 | testMetadataKey = "test metadata value", |
| 180 | }, |
| 181 | })); |
| 182 | |
| 183 | CreateBatchOperation batchOperation = IsAsync |
| 184 | ? await client.CreateBatchAsync(content, waitUntilCompleted: false) |
| 185 | : client.CreateBatch(content, waitUntilCompleted: false); |
| 186 | |
| 187 | CreateBatchOperation rehydratedOperation; |
| 188 | if (useBatchId) |
| 189 | { |
| 190 | rehydratedOperation = IsAsync ? |
| 191 | await CreateBatchOperation.RehydrateAsync(client, batchOperation.BatchId) : |
| 192 | CreateBatchOperation.Rehydrate(client, batchOperation.BatchId); |
| 193 | } |
| 194 | else { |
| 195 | // Simulate rehydration of the operation |
| 196 | BinaryData rehydrationBytes = batchOperation.RehydrationToken.ToBytes(); |
| 197 | ContinuationToken rehydrationToken = ContinuationToken.FromBytes(rehydrationBytes); |
| 198 | |
| 199 | rehydratedOperation = IsAsync ? |
| 200 | await CreateBatchOperation.RehydrateAsync(client, rehydrationToken) : |
| 201 | CreateBatchOperation.Rehydrate(client, rehydrationToken); |
| 202 | } |
| 203 | |
| 204 | static bool Validate(CreateBatchOperation operation) |
| 205 | { |
| 206 | BinaryData response = operation.GetRawResponse().Content; |
| 207 | using JsonDocument jsonDocument = JsonDocument.Parse(response); |
| 208 | |
| 209 | JsonElement idElement = jsonDocument.RootElement.GetProperty("id"); |
| 210 | JsonElement createdAtElement = jsonDocument.RootElement.GetProperty("created_at"); |
| 211 | JsonElement statusElement = jsonDocument.RootElement.GetProperty("status"); |
| 212 | JsonElement metadataElement = jsonDocument.RootElement.GetProperty("metadata"); |
| 213 | JsonElement testMetadataKeyElement = metadataElement.GetProperty("testMetadataKey"); |
| 214 | |
| 215 | string id = idElement.GetString(); |
| 216 | long createdAt = createdAtElement.GetInt64(); |
| 217 | string status = statusElement.GetString(); |
| 218 | string testMetadataKey = testMetadataKeyElement.GetString(); |
| 219 | |
| 220 | long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds(); |
| 221 | |
| 222 | Assert.That(id, Is.Not.Null.And.Not.Empty); |
| 223 | Assert.That(createdAt, Is.GreaterThan(unixTime2024)); |
| 224 | Assert.That(status, Is.EqualTo("validating")); |
| 225 | Assert.That(testMetadataKey, Is.EqualTo("test metadata value")); |
| 226 | |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | Assert.IsTrue(Validate(batchOperation)); |
| 231 | Assert.IsTrue(Validate(rehydratedOperation)); |
| 232 | |
| 233 | // We don't test wait for completion live because this is documented to |
| 234 | // sometimes take 24 hours. |
| 235 | |
| 236 | Assert.AreEqual(batchOperation.HasCompleted, rehydratedOperation.HasCompleted); |
| 237 | |
| 238 | using JsonDocument originalOperationJson = JsonDocument.Parse(batchOperation.GetRawResponse().Content); |
| 239 | using JsonDocument rehydratedOperationJson = JsonDocument.Parse(rehydratedOperation.GetRawResponse().Content); |
| 240 | |
| 241 | Assert.AreEqual(originalOperationJson.RootElement.GetProperty("id").GetString(), rehydratedOperationJson.RootElement.GetProperty("id").GetString()); |
| 242 | Assert.AreEqual(originalOperationJson.RootElement.GetProperty("created_at").GetInt64(), rehydratedOperationJson.RootElement.GetProperty("created_at").GetInt64()); |
| 243 | Assert.AreEqual(originalOperationJson.RootElement.GetProperty("status").GetString(), rehydratedOperationJson.RootElement.GetProperty("status").GetString()); |
| 244 | } |
| 245 | } |