openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/FineTuning/FineTuningTests.cs
127lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Files; |
| 3 | using OpenAI.FineTuning; |
| 4 | using OpenAI.Tests.Utility; |
| 5 | using System; |
| 6 | using System.ClientModel; |
| 7 | using System.Collections.Generic; |
| 8 | using System.Text.Json; |
| 9 | using System.Threading.Tasks; |
| 10 | using static OpenAI.Tests.TestHelpers; |
| 11 | |
| 12 | namespace OpenAI.Tests.FineTuning; |
| 13 | |
| 14 | [TestFixture(true)] |
| 15 | [TestFixture(false)] |
| 16 | [Parallelizable(ParallelScope.All)] |
| 17 | [Category("FineTuning")] |
| 18 | internal class FineTuningTests : SyncAsyncTestBase |
| 19 | { |
| 20 | public FineTuningTests(bool isAsync) |
| 21 | : base(isAsync) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | [Test] |
| 26 | public void CreateJobCanParseServiceError() |
| 27 | { |
| 28 | FineTuningClient client = GetTestClient<FineTuningClient>(TestScenario.FineTuning); |
| 29 | BinaryData data = BinaryData.FromString($$""" |
| 30 | { |
| 31 | "model": "gpt-3.5-turbo" |
| 32 | } |
| 33 | """); |
| 34 | ClientResultException ex = null; |
| 35 | |
| 36 | if (IsAsync) |
| 37 | { |
| 38 | ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.CreateFineTuningJobAsync(BinaryContent.Create(data), waitUntilCompleted: false)); |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | ex = Assert.Throws<ClientResultException>(() => client.CreateFineTuningJob(BinaryContent.Create(data), waitUntilCompleted: false)); |
| 43 | } |
| 44 | |
| 45 | Assert.That(ex.Status, Is.EqualTo(400)); |
| 46 | Assert.That(ex.Message, Contains.Substring("training_file")); |
| 47 | } |
| 48 | |
| 49 | [Test] |
| 50 | public void GetJobCanParseServiceError() |
| 51 | { |
| 52 | FineTuningClient client = GetTestClient<FineTuningClient>(TestScenario.FineTuning); |
| 53 | ClientResultException ex = null; |
| 54 | |
| 55 | if (IsAsync) |
| 56 | { |
| 57 | ex = Assert.ThrowsAsync<ClientResultException>(async () => await client.GetJobAsync("fakeJobId", options: null)); |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | ex = Assert.Throws<ClientResultException>(() => client.GetJob("fakeJobId", options: null)); |
| 62 | } |
| 63 | |
| 64 | Assert.That(ex.Status, Is.EqualTo(404)); |
| 65 | } |
| 66 | |
| 67 | [Test] |
| 68 | public async Task GetJobsAsyncWorks() |
| 69 | { |
| 70 | AssertAsyncOnly(); |
| 71 | |
| 72 | FineTuningClient client = GetTestClient<FineTuningClient>(TestScenario.FineTuning); |
| 73 | |
| 74 | await foreach (ClientResult result in client.GetJobsAsync(after: null, limit: null, options: null).GetRawPagesAsync()) |
| 75 | { |
| 76 | BinaryData response = result.GetRawResponse().Content; |
| 77 | JsonDocument jsonDocument = JsonDocument.Parse(response); |
| 78 | JsonElement jsonRoot = jsonDocument.RootElement; |
| 79 | |
| 80 | Assert.That(jsonRoot.TryGetProperty("data", out _), Is.True); |
| 81 | Assert.That(jsonRoot.TryGetProperty("has_more", out _), Is.True); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | [Test] |
| 86 | public void GetJobsWorks() |
| 87 | { |
| 88 | AssertSyncOnly(); |
| 89 | |
| 90 | FineTuningClient client = GetTestClient<FineTuningClient>(TestScenario.FineTuning); |
| 91 | |
| 92 | foreach (ClientResult result in client.GetJobs(after: null, limit: null, options: null).GetRawPages()) |
| 93 | { |
| 94 | BinaryData response = result.GetRawResponse().Content; |
| 95 | JsonDocument jsonDocument = JsonDocument.Parse(response); |
| 96 | JsonElement jsonRoot = jsonDocument.RootElement; |
| 97 | |
| 98 | Assert.That(jsonRoot.TryGetProperty("data", out _), Is.True); |
| 99 | Assert.That(jsonRoot.TryGetProperty("has_more", out _), Is.True); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // We need to add this test back once we have access to the test resources |
| 104 | // |
| 105 | //[Test] |
| 106 | //public void BasicFineTuningJobOperationsWork() |
| 107 | //{ |
| 108 | // // Upload training file first |
| 109 | // FileClient fileClient = GetTestClient<FileClient>(TestScenario.Files); |
| 110 | // string filename = "toy_chat.jsonl"; |
| 111 | // BinaryData fileContent = BinaryData.FromString(""" |
| 112 | // {"messages": [{"role": "user", "content": "I lost my book today."}, {"role": "assistant", "content": "You can read everything on ebooks these days!"}]} |
| 113 | // {"messages": [{"role": "system", "content": "You are a happy assistant that puts a positive spin on everything."}, {"role": "assistant", "content": "You're great!"}]} |
| 114 | // """); |
| 115 | // OpenAIFile uploadedFile = fileClient.UploadFile(fileContent, filename, FileUploadPurpose.FineTune); |
| 116 | // Assert.That(uploadedFile?.Filename, Is.EqualTo(filename)); |
| 117 | |
| 118 | // // Submit fine-tuning job |
| 119 | // FineTuningClient client = GetTestClient<FineTuningClient>(TestScenario.FineTuning); |
| 120 | |
| 121 | // string json = $"{{\"training_file\":\"{uploadedFile.Id}\",\"model\":\"gpt-3.5-turbo\"}}"; |
| 122 | // BinaryData input = BinaryData.FromString(json); |
| 123 | // using BinaryContent content = BinaryContent.Create(input); |
| 124 | |
| 125 | // FineTuningJobOperation operation = client.CreateJob(content, waitUntilCompleted: false); |
| 126 | //} |
| 127 | } |