openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/Batch/BatchTests.cs
135lines · 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.IO; |
| 8 | using System.Text.Json; |
| 9 | using System.Threading.Tasks; |
| 10 | using static OpenAI.Tests.TestHelpers; |
| 11 | |
| 12 | namespace OpenAI.Tests.Batch; |
| 13 | |
| 14 | [TestFixture(true)] |
| 15 | [TestFixture(false)] |
| 16 | [Parallelizable(ParallelScope.All)] |
| 17 | [Category("Batch")] |
| 18 | public partial class BatchTests : SyncAsyncTestBase |
| 19 | { |
| 20 | public BatchTests(bool isAsync) : base(isAsync) |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | [Test] |
| 25 | public async Task ListBatchesProtocol() |
| 26 | { |
| 27 | BatchClient client = GetTestClient(); |
| 28 | ClientResult result = IsAsync |
| 29 | ? await client.GetBatchesAsync(after: null, limit: null, options: null) |
| 30 | : client.GetBatches(after: null, limit: null, options: null); |
| 31 | |
| 32 | BinaryData response = result.GetRawResponse().Content; |
| 33 | JsonDocument jsonDocument = JsonDocument.Parse(response); |
| 34 | JsonElement dataElement = jsonDocument.RootElement.GetProperty("data"); |
| 35 | |
| 36 | Assert.That(dataElement.GetArrayLength(), Is.GreaterThan(0)); |
| 37 | |
| 38 | long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds(); |
| 39 | |
| 40 | foreach (JsonElement batchElement in dataElement.EnumerateArray()) |
| 41 | { |
| 42 | JsonElement createdAtElement = batchElement.GetProperty("created_at"); |
| 43 | long createdAt = createdAtElement.GetInt64(); |
| 44 | |
| 45 | Assert.That(createdAt, Is.GreaterThan(unixTime2024)); |
| 46 | } |
| 47 | |
| 48 | //var dynamicResult = result.GetRawResponse().Content.ToDynamicFromJson(); |
| 49 | //Assert.That(dynamicResult.data.Count, Is.GreaterThan(0)); |
| 50 | //Assert.That(dynamicResult.data[0].createdAt, Is.GreaterThan(new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero))); |
| 51 | } |
| 52 | |
| 53 | [Test] |
| 54 | public async Task CreateGetAndCancelBatchProtocol() |
| 55 | { |
| 56 | using MemoryStream testFileStream = new(); |
| 57 | using StreamWriter streamWriter = new (testFileStream); |
| 58 | 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?""}]}}"; |
| 59 | streamWriter.WriteLine(input); |
| 60 | streamWriter.Flush(); |
| 61 | testFileStream.Position = 0; |
| 62 | |
| 63 | FileClient fileClient = GetTestClient<FileClient>(TestScenario.Files); |
| 64 | OpenAIFileInfo inputFile = await fileClient.UploadFileAsync(testFileStream, "test-batch-file", FileUploadPurpose.Batch); |
| 65 | Assert.That(inputFile.Id, Is.Not.Null.And.Not.Empty); |
| 66 | |
| 67 | BatchClient client = GetTestClient(); |
| 68 | BinaryContent content = BinaryContent.Create(BinaryData.FromObjectAsJson(new |
| 69 | { |
| 70 | input_file_id = inputFile.Id, |
| 71 | endpoint = "/v1/chat/completions", |
| 72 | completion_window = "24h", |
| 73 | metadata = new |
| 74 | { |
| 75 | testMetadataKey = "test metadata value", |
| 76 | }, |
| 77 | })); |
| 78 | ClientResult batchResult = IsAsync |
| 79 | ? await client.CreateBatchAsync(content) |
| 80 | : client.CreateBatch(content); |
| 81 | |
| 82 | BinaryData response = batchResult.GetRawResponse().Content; |
| 83 | JsonDocument jsonDocument = JsonDocument.Parse(response); |
| 84 | |
| 85 | JsonElement idElement = jsonDocument.RootElement.GetProperty("id"); |
| 86 | JsonElement createdAtElement = jsonDocument.RootElement.GetProperty("created_at"); |
| 87 | JsonElement statusElement = jsonDocument.RootElement.GetProperty("status"); |
| 88 | JsonElement metadataElement = jsonDocument.RootElement.GetProperty("metadata"); |
| 89 | JsonElement testMetadataKeyElement = metadataElement.GetProperty("testMetadataKey"); |
| 90 | |
| 91 | string id = idElement.GetString(); |
| 92 | long createdAt = createdAtElement.GetInt64(); |
| 93 | string status = statusElement.GetString(); |
| 94 | string testMetadataKey = testMetadataKeyElement.GetString(); |
| 95 | |
| 96 | long unixTime2024 = (new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero)).ToUnixTimeSeconds(); |
| 97 | |
| 98 | Assert.That(id, Is.Not.Null.And.Not.Empty); |
| 99 | Assert.That(createdAt, Is.GreaterThan(unixTime2024)); |
| 100 | Assert.That(status, Is.EqualTo("validating")); |
| 101 | Assert.That(testMetadataKey, Is.EqualTo("test metadata value")); |
| 102 | |
| 103 | batchResult = IsAsync |
| 104 | ? await client.GetBatchAsync(id, options: null) |
| 105 | : client.GetBatch(id, options: null); |
| 106 | |
| 107 | JsonElement endpointElement = jsonDocument.RootElement.GetProperty("endpoint"); |
| 108 | string endpoint = endpointElement.GetString(); |
| 109 | |
| 110 | Assert.That(endpoint, Is.EqualTo("/v1/chat/completions")); |
| 111 | |
| 112 | batchResult = IsAsync |
| 113 | ? await client.CancelBatchAsync(id, options: null) |
| 114 | : client.CancelBatch(id, options: null); |
| 115 | |
| 116 | statusElement = jsonDocument.RootElement.GetProperty("status"); |
| 117 | status = statusElement.GetString(); |
| 118 | |
| 119 | Assert.That(status, Is.EqualTo("validating")); |
| 120 | |
| 121 | //var newBatchDynamic = batchResult.GetRawResponse().Content.ToDynamicFromJson(); |
| 122 | |
| 123 | //Assert.That(newBatchDynamic?.createdAt, Is.GreaterThan(new DateTimeOffset(2024, 01, 01, 0, 0, 0, TimeSpan.Zero))); |
| 124 | //Assert.That(newBatchDynamic.status, Is.EqualTo("validating")); |
| 125 | //Assert.That(newBatchDynamic.metadata["testMetadataKey"], Is.EqualTo("test metadata value")); |
| 126 | //batchResult = await client.GetBatchAsync(newBatchDynamic.id, options: null); |
| 127 | //newBatchDynamic = batchResult.GetRawResponse().Content.ToObjectFromJson<dynamic>(); |
| 128 | //Assert.That(newBatchDynamic.endpoint, Is.EqualTo("/v1/chat/completions")); |
| 129 | //batchResult = await client.CancelBatchAsync(newBatchDynamic.id, options: null); |
| 130 | //newBatchDynamic = batchResult.GetRawResponse().Content.ToObjectFromJson<dynamic>(); |
| 131 | //Assert.That(newBatchDynamic.status, Is.EqualTo("cancelling")); |
| 132 | } |
| 133 | |
| 134 | private static BatchClient GetTestClient() => GetTestClient<BatchClient>(TestScenario.Batch); |
| 135 | } |