openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/FineTuning/FineTuningCheckpoint.Serialization.cs
201lines · modecode
| 1 | // <auto-generated/> |
| 2 | |
| 3 | #nullable disable |
| 4 | |
| 5 | using System; |
| 6 | using System.ClientModel.Primitives; |
| 7 | using System.Collections.Generic; |
| 8 | using System.Text.Json; |
| 9 | using OpenAI; |
| 10 | |
| 11 | namespace OpenAI.FineTuning |
| 12 | { |
| 13 | public partial class FineTuningCheckpoint : IJsonModel<FineTuningCheckpoint> |
| 14 | { |
| 15 | internal FineTuningCheckpoint() |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | void IJsonModel<FineTuningCheckpoint>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 20 | { |
| 21 | writer.WriteStartObject(); |
| 22 | JsonModelWriteCore(writer, options); |
| 23 | writer.WriteEndObject(); |
| 24 | } |
| 25 | |
| 26 | protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 27 | { |
| 28 | string format = options.Format == "W" ? ((IPersistableModel<FineTuningCheckpoint>)this).GetFormatFromOptions(options) : options.Format; |
| 29 | if (format != "J") |
| 30 | { |
| 31 | throw new FormatException($"The model {nameof(FineTuningCheckpoint)} does not support writing '{format}' format."); |
| 32 | } |
| 33 | if (_additionalBinaryDataProperties?.ContainsKey("id") != true) |
| 34 | { |
| 35 | writer.WritePropertyName("id"u8); |
| 36 | writer.WriteStringValue(Id); |
| 37 | } |
| 38 | if (_additionalBinaryDataProperties?.ContainsKey("created_at") != true) |
| 39 | { |
| 40 | writer.WritePropertyName("created_at"u8); |
| 41 | writer.WriteNumberValue(CreatedAt, "U"); |
| 42 | } |
| 43 | if (_additionalBinaryDataProperties?.ContainsKey("fine_tuned_model_checkpoint") != true) |
| 44 | { |
| 45 | writer.WritePropertyName("fine_tuned_model_checkpoint"u8); |
| 46 | writer.WriteStringValue(ModelId); |
| 47 | } |
| 48 | if (_additionalBinaryDataProperties?.ContainsKey("step_number") != true) |
| 49 | { |
| 50 | writer.WritePropertyName("step_number"u8); |
| 51 | writer.WriteNumberValue(StepNumber); |
| 52 | } |
| 53 | if (_additionalBinaryDataProperties?.ContainsKey("metrics") != true) |
| 54 | { |
| 55 | writer.WritePropertyName("metrics"u8); |
| 56 | writer.WriteObjectValue(Metrics, options); |
| 57 | } |
| 58 | if (_additionalBinaryDataProperties?.ContainsKey("fine_tuning_job_id") != true) |
| 59 | { |
| 60 | writer.WritePropertyName("fine_tuning_job_id"u8); |
| 61 | writer.WriteStringValue(JobId); |
| 62 | } |
| 63 | if (_additionalBinaryDataProperties?.ContainsKey("object") != true) |
| 64 | { |
| 65 | writer.WritePropertyName("object"u8); |
| 66 | writer.WriteStringValue(_object); |
| 67 | } |
| 68 | // Plugin customization: remove options.Format != "W" check |
| 69 | if (_additionalBinaryDataProperties != null) |
| 70 | { |
| 71 | foreach (var item in _additionalBinaryDataProperties) |
| 72 | { |
| 73 | if (ModelSerializationExtensions.IsSentinelValue(item.Value)) |
| 74 | { |
| 75 | continue; |
| 76 | } |
| 77 | writer.WritePropertyName(item.Key); |
| 78 | #if NET6_0_OR_GREATER |
| 79 | writer.WriteRawValue(item.Value); |
| 80 | #else |
| 81 | using (JsonDocument document = JsonDocument.Parse(item.Value)) |
| 82 | { |
| 83 | JsonSerializer.Serialize(writer, document.RootElement); |
| 84 | } |
| 85 | #endif |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | FineTuningCheckpoint IJsonModel<FineTuningCheckpoint>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 91 | |
| 92 | protected virtual FineTuningCheckpoint JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 93 | { |
| 94 | string format = options.Format == "W" ? ((IPersistableModel<FineTuningCheckpoint>)this).GetFormatFromOptions(options) : options.Format; |
| 95 | if (format != "J") |
| 96 | { |
| 97 | throw new FormatException($"The model {nameof(FineTuningCheckpoint)} does not support reading '{format}' format."); |
| 98 | } |
| 99 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 100 | return DeserializeFineTuningCheckpoint(document.RootElement, options); |
| 101 | } |
| 102 | |
| 103 | internal static FineTuningCheckpoint DeserializeFineTuningCheckpoint(JsonElement element, ModelReaderWriterOptions options) |
| 104 | { |
| 105 | if (element.ValueKind == JsonValueKind.Null) |
| 106 | { |
| 107 | return null; |
| 108 | } |
| 109 | string id = default; |
| 110 | DateTimeOffset createdAt = default; |
| 111 | string modelId = default; |
| 112 | int stepNumber = default; |
| 113 | FineTuningCheckpointMetrics metrics = default; |
| 114 | string jobId = default; |
| 115 | string @object = default; |
| 116 | IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>(); |
| 117 | foreach (var prop in element.EnumerateObject()) |
| 118 | { |
| 119 | if (prop.NameEquals("id"u8)) |
| 120 | { |
| 121 | id = prop.Value.GetString(); |
| 122 | continue; |
| 123 | } |
| 124 | if (prop.NameEquals("created_at"u8)) |
| 125 | { |
| 126 | createdAt = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64()); |
| 127 | continue; |
| 128 | } |
| 129 | if (prop.NameEquals("fine_tuned_model_checkpoint"u8)) |
| 130 | { |
| 131 | modelId = prop.Value.GetString(); |
| 132 | continue; |
| 133 | } |
| 134 | if (prop.NameEquals("step_number"u8)) |
| 135 | { |
| 136 | stepNumber = prop.Value.GetInt32(); |
| 137 | continue; |
| 138 | } |
| 139 | if (prop.NameEquals("metrics"u8)) |
| 140 | { |
| 141 | metrics = FineTuningCheckpointMetrics.DeserializeFineTuningCheckpointMetrics(prop.Value, options); |
| 142 | continue; |
| 143 | } |
| 144 | if (prop.NameEquals("fine_tuning_job_id"u8)) |
| 145 | { |
| 146 | jobId = prop.Value.GetString(); |
| 147 | continue; |
| 148 | } |
| 149 | if (prop.NameEquals("object"u8)) |
| 150 | { |
| 151 | @object = prop.Value.GetString(); |
| 152 | continue; |
| 153 | } |
| 154 | // Plugin customization: remove options.Format != "W" check |
| 155 | additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText())); |
| 156 | } |
| 157 | return new FineTuningCheckpoint( |
| 158 | id, |
| 159 | createdAt, |
| 160 | modelId, |
| 161 | stepNumber, |
| 162 | metrics, |
| 163 | jobId, |
| 164 | @object, |
| 165 | additionalBinaryDataProperties); |
| 166 | } |
| 167 | |
| 168 | BinaryData IPersistableModel<FineTuningCheckpoint>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 169 | |
| 170 | protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 171 | { |
| 172 | string format = options.Format == "W" ? ((IPersistableModel<FineTuningCheckpoint>)this).GetFormatFromOptions(options) : options.Format; |
| 173 | switch (format) |
| 174 | { |
| 175 | case "J": |
| 176 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 177 | default: |
| 178 | throw new FormatException($"The model {nameof(FineTuningCheckpoint)} does not support writing '{options.Format}' format."); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | FineTuningCheckpoint IPersistableModel<FineTuningCheckpoint>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 183 | |
| 184 | protected virtual FineTuningCheckpoint PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 185 | { |
| 186 | string format = options.Format == "W" ? ((IPersistableModel<FineTuningCheckpoint>)this).GetFormatFromOptions(options) : options.Format; |
| 187 | switch (format) |
| 188 | { |
| 189 | case "J": |
| 190 | using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions)) |
| 191 | { |
| 192 | return DeserializeFineTuningCheckpoint(document.RootElement, options); |
| 193 | } |
| 194 | default: |
| 195 | throw new FormatException($"The model {nameof(FineTuningCheckpoint)} does not support reading '{options.Format}' format."); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | string IPersistableModel<FineTuningCheckpoint>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 200 | } |
| 201 | } |
| 202 | |