openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/FineTuning/Internal/Pagination/FineTuningJobCheckpointCollectionPageToken.cs
135lines · modeblame
2ab1a942Jose Arriaga Maldonado1 years ago | 1 | using System; |
7bdecfd8Anne Thompson2 years ago | 2 | using System.ClientModel; |
2ab1a942Jose Arriaga Maldonado1 years ago | 3 | using System.ClientModel.Primitives; |
7bdecfd8Anne Thompson2 years ago | 4 | using System.Diagnostics; |
| 5 | using System.IO; | |
| 6 | using System.Text.Json; | |
| 7 | | |
| 8 | #nullable enable | |
| 9 | | |
2ab1a942Jose Arriaga Maldonado1 years ago | 10 | namespace OpenAI.FineTuning; |
7bdecfd8Anne Thompson2 years ago | 11 | |
2ab1a942Jose Arriaga Maldonado1 years ago | 12 | internal class FineTuningJobCheckpointCollectionPageToken : ContinuationToken |
7bdecfd8Anne Thompson2 years ago | 13 | { |
2ab1a942Jose Arriaga Maldonado1 years ago | 14 | protected FineTuningJobCheckpointCollectionPageToken(string jobId, int? limit, string? after) |
7bdecfd8Anne Thompson2 years ago | 15 | { |
2ab1a942Jose Arriaga Maldonado1 years ago | 16 | JobId = jobId; |
7bdecfd8Anne Thompson2 years ago | 17 | Limit = limit; |
| 18 | After = after; | |
| 19 | } | |
| 20 | | |
2ab1a942Jose Arriaga Maldonado1 years ago | 21 | public string JobId { get; } |
7bdecfd8Anne Thompson2 years ago | 22 | |
2ab1a942Jose Arriaga Maldonado1 years ago | 23 | public int? Limit { get; } |
7bdecfd8Anne Thompson2 years ago | 24 | |
| 25 | public string? After { get; } | |
| 26 | | |
| 27 | public override BinaryData ToBytes() | |
| 28 | { | |
| 29 | using MemoryStream stream = new(); | |
| 30 | using Utf8JsonWriter writer = new(stream); | |
| 31 | | |
| 32 | writer.WriteStartObject(); | |
2ab1a942Jose Arriaga Maldonado1 years ago | 33 | writer.WriteString("jobId", JobId); |
79014abcShivangiReja1 years ago | 34 | |
7bdecfd8Anne Thompson2 years ago | 35 | if (Limit.HasValue) |
| 36 | { | |
| 37 | writer.WriteNumber("limit", Limit.Value); | |
| 38 | } | |
| 39 | | |
| 40 | if (After is not null) | |
| 41 | { | |
| 42 | writer.WriteString("after", After); | |
| 43 | } | |
| 44 | | |
| 45 | writer.WriteEndObject(); | |
| 46 | | |
| 47 | writer.Flush(); | |
| 48 | stream.Position = 0; | |
| 49 | | |
| 50 | return BinaryData.FromStream(stream); | |
| 51 | } | |
79014abcShivangiReja1 years ago | 52 | |
2ab1a942Jose Arriaga Maldonado1 years ago | 53 | public static FineTuningJobCheckpointCollectionPageToken FromToken(ContinuationToken pageToken) |
7bdecfd8Anne Thompson2 years ago | 54 | { |
2ab1a942Jose Arriaga Maldonado1 years ago | 55 | if (pageToken is FineTuningJobCheckpointCollectionPageToken token) |
7bdecfd8Anne Thompson2 years ago | 56 | { |
| 57 | return token; | |
| 58 | } | |
| 59 | | |
| 60 | BinaryData data = pageToken.ToBytes(); | |
| 61 | | |
| 62 | if (data.ToMemory().Length == 0) | |
| 63 | { | |
2ab1a942Jose Arriaga Maldonado1 years ago | 64 | throw new ArgumentException("Failed to create FineTuningJobCheckpointCollectionPageToken from provided pageToken.", nameof(pageToken)); |
7bdecfd8Anne Thompson2 years ago | 65 | } |
| 66 | | |
| 67 | Utf8JsonReader reader = new(data); | |
| 68 | | |
2ab1a942Jose Arriaga Maldonado1 years ago | 69 | string jobId = null!; |
7bdecfd8Anne Thompson2 years ago | 70 | int? limit = null; |
| 71 | string? after = null; | |
| 72 | | |
| 73 | reader.Read(); | |
| 74 | | |
| 75 | Debug.Assert(reader.TokenType == JsonTokenType.StartObject); | |
| 76 | | |
| 77 | while (reader.Read()) | |
| 78 | { | |
| 79 | if (reader.TokenType == JsonTokenType.EndObject) | |
| 80 | { | |
| 81 | break; | |
| 82 | } | |
| 83 | | |
| 84 | Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); | |
| 85 | | |
| 86 | string propertyName = reader.GetString()!; | |
| 87 | | |
| 88 | switch (propertyName) | |
| 89 | { | |
2ab1a942Jose Arriaga Maldonado1 years ago | 90 | case "jobId": |
| 91 | reader.Read(); | |
| 92 | Debug.Assert(reader.TokenType == JsonTokenType.String); | |
| 93 | jobId = reader.GetString()!; | |
| 94 | break; | |
7bdecfd8Anne Thompson2 years ago | 95 | case "limit": |
| 96 | reader.Read(); | |
| 97 | Debug.Assert(reader.TokenType == JsonTokenType.Number); | |
| 98 | limit = reader.GetInt32(); | |
| 99 | break; | |
| 100 | case "after": | |
| 101 | reader.Read(); | |
| 102 | Debug.Assert(reader.TokenType == JsonTokenType.String); | |
| 103 | after = reader.GetString(); | |
| 104 | break; | |
| 105 | default: | |
| 106 | throw new JsonException($"Unrecognized property '{propertyName}'."); | |
| 107 | } | |
| 108 | } | |
| 109 | | |
2ab1a942Jose Arriaga Maldonado1 years ago | 110 | if (jobId is null) |
| 111 | { | |
| 112 | throw new ArgumentException("Failed to create FineTuningJobCheckpointCollectionPageToken from provided pageToken.", nameof(pageToken)); | |
| 113 | } | |
| 114 | | |
| 115 | return new(jobId, limit, after); | |
7bdecfd8Anne Thompson2 years ago | 116 | } |
| 117 | | |
2ab1a942Jose Arriaga Maldonado1 years ago | 118 | public static FineTuningJobCheckpointCollectionPageToken FromOptions(string jobId, int? limit, string? after) |
| 119 | => new(jobId, limit, after); | |
| 120 | | |
| 121 | public static FineTuningJobCheckpointCollectionPageToken? FromResponse(ClientResult result, string jobId, int? limit) | |
| 122 | { | |
| 123 | PipelineResponse response = result.GetRawResponse(); | |
| 124 | using JsonDocument doc = JsonDocument.Parse(response.Content); | |
| 125 | string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!; | |
| 126 | bool hasMore = doc.RootElement.GetProperty("has_more"u8).GetBoolean(); | |
| 127 | | |
| 128 | if (!hasMore || lastId is null) | |
| 129 | { | |
| 130 | return null; | |
| 131 | } | |
| 132 | | |
| 133 | return new(jobId, limit, lastId); | |
| 134 | } | |
| 135 | } |