openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/Graders/GraderLabelModel.Serialization.cs
220lines · 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 | using OpenAI.Evals; |
| 11 | |
| 12 | namespace OpenAI.Graders |
| 13 | { |
| 14 | public partial class GraderLabelModel : IJsonModel<GraderLabelModel> |
| 15 | { |
| 16 | internal GraderLabelModel() : this(GraderType.LabelModel, null, null, null, null, null, null) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | void IJsonModel<GraderLabelModel>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 21 | { |
| 22 | writer.WriteStartObject(); |
| 23 | JsonModelWriteCore(writer, options); |
| 24 | writer.WriteEndObject(); |
| 25 | } |
| 26 | |
| 27 | protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 28 | { |
| 29 | string format = options.Format == "W" ? ((IPersistableModel<GraderLabelModel>)this).GetFormatFromOptions(options) : options.Format; |
| 30 | if (format != "J") |
| 31 | { |
| 32 | throw new FormatException($"The model {nameof(GraderLabelModel)} does not support writing '{format}' format."); |
| 33 | } |
| 34 | base.JsonModelWriteCore(writer, options); |
| 35 | if (_additionalBinaryDataProperties?.ContainsKey("name") != true) |
| 36 | { |
| 37 | writer.WritePropertyName("name"u8); |
| 38 | writer.WriteStringValue(Name); |
| 39 | } |
| 40 | if (_additionalBinaryDataProperties?.ContainsKey("model") != true) |
| 41 | { |
| 42 | writer.WritePropertyName("model"u8); |
| 43 | writer.WriteStringValue(Model); |
| 44 | } |
| 45 | if (_additionalBinaryDataProperties?.ContainsKey("input") != true) |
| 46 | { |
| 47 | writer.WritePropertyName("input"u8); |
| 48 | writer.WriteStartArray(); |
| 49 | foreach (InternalEvalItem item in Input) |
| 50 | { |
| 51 | writer.WriteObjectValue(item, options); |
| 52 | } |
| 53 | writer.WriteEndArray(); |
| 54 | } |
| 55 | if (_additionalBinaryDataProperties?.ContainsKey("labels") != true) |
| 56 | { |
| 57 | writer.WritePropertyName("labels"u8); |
| 58 | writer.WriteStartArray(); |
| 59 | foreach (string item in Labels) |
| 60 | { |
| 61 | if (item == null) |
| 62 | { |
| 63 | writer.WriteNullValue(); |
| 64 | continue; |
| 65 | } |
| 66 | writer.WriteStringValue(item); |
| 67 | } |
| 68 | writer.WriteEndArray(); |
| 69 | } |
| 70 | if (_additionalBinaryDataProperties?.ContainsKey("passing_labels") != true) |
| 71 | { |
| 72 | writer.WritePropertyName("passing_labels"u8); |
| 73 | writer.WriteStartArray(); |
| 74 | foreach (string item in PassingLabels) |
| 75 | { |
| 76 | if (item == null) |
| 77 | { |
| 78 | writer.WriteNullValue(); |
| 79 | continue; |
| 80 | } |
| 81 | writer.WriteStringValue(item); |
| 82 | } |
| 83 | writer.WriteEndArray(); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | GraderLabelModel IJsonModel<GraderLabelModel>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (GraderLabelModel)JsonModelCreateCore(ref reader, options); |
| 88 | |
| 89 | protected override Grader JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 90 | { |
| 91 | string format = options.Format == "W" ? ((IPersistableModel<GraderLabelModel>)this).GetFormatFromOptions(options) : options.Format; |
| 92 | if (format != "J") |
| 93 | { |
| 94 | throw new FormatException($"The model {nameof(GraderLabelModel)} does not support reading '{format}' format."); |
| 95 | } |
| 96 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 97 | return DeserializeGraderLabelModel(document.RootElement, options); |
| 98 | } |
| 99 | |
| 100 | internal static GraderLabelModel DeserializeGraderLabelModel(JsonElement element, ModelReaderWriterOptions options) |
| 101 | { |
| 102 | if (element.ValueKind == JsonValueKind.Null) |
| 103 | { |
| 104 | return null; |
| 105 | } |
| 106 | GraderType kind = default; |
| 107 | IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>(); |
| 108 | string name = default; |
| 109 | string model = default; |
| 110 | IList<InternalEvalItem> input = default; |
| 111 | IList<string> labels = default; |
| 112 | IList<string> passingLabels = default; |
| 113 | foreach (var prop in element.EnumerateObject()) |
| 114 | { |
| 115 | if (prop.NameEquals("type"u8)) |
| 116 | { |
| 117 | kind = new GraderType(prop.Value.GetString()); |
| 118 | continue; |
| 119 | } |
| 120 | if (prop.NameEquals("name"u8)) |
| 121 | { |
| 122 | name = prop.Value.GetString(); |
| 123 | continue; |
| 124 | } |
| 125 | if (prop.NameEquals("model"u8)) |
| 126 | { |
| 127 | model = prop.Value.GetString(); |
| 128 | continue; |
| 129 | } |
| 130 | if (prop.NameEquals("input"u8)) |
| 131 | { |
| 132 | List<InternalEvalItem> array = new List<InternalEvalItem>(); |
| 133 | foreach (var item in prop.Value.EnumerateArray()) |
| 134 | { |
| 135 | array.Add(InternalEvalItem.DeserializeInternalEvalItem(item, options)); |
| 136 | } |
| 137 | input = array; |
| 138 | continue; |
| 139 | } |
| 140 | if (prop.NameEquals("labels"u8)) |
| 141 | { |
| 142 | List<string> array = new List<string>(); |
| 143 | foreach (var item in prop.Value.EnumerateArray()) |
| 144 | { |
| 145 | if (item.ValueKind == JsonValueKind.Null) |
| 146 | { |
| 147 | array.Add(null); |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | array.Add(item.GetString()); |
| 152 | } |
| 153 | } |
| 154 | labels = array; |
| 155 | continue; |
| 156 | } |
| 157 | if (prop.NameEquals("passing_labels"u8)) |
| 158 | { |
| 159 | List<string> array = new List<string>(); |
| 160 | foreach (var item in prop.Value.EnumerateArray()) |
| 161 | { |
| 162 | if (item.ValueKind == JsonValueKind.Null) |
| 163 | { |
| 164 | array.Add(null); |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | array.Add(item.GetString()); |
| 169 | } |
| 170 | } |
| 171 | passingLabels = array; |
| 172 | continue; |
| 173 | } |
| 174 | // Plugin customization: remove options.Format != "W" check |
| 175 | additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText())); |
| 176 | } |
| 177 | return new GraderLabelModel( |
| 178 | kind, |
| 179 | additionalBinaryDataProperties, |
| 180 | name, |
| 181 | model, |
| 182 | input, |
| 183 | labels, |
| 184 | passingLabels); |
| 185 | } |
| 186 | |
| 187 | BinaryData IPersistableModel<GraderLabelModel>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 188 | |
| 189 | protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 190 | { |
| 191 | string format = options.Format == "W" ? ((IPersistableModel<GraderLabelModel>)this).GetFormatFromOptions(options) : options.Format; |
| 192 | switch (format) |
| 193 | { |
| 194 | case "J": |
| 195 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 196 | default: |
| 197 | throw new FormatException($"The model {nameof(GraderLabelModel)} does not support writing '{options.Format}' format."); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | GraderLabelModel IPersistableModel<GraderLabelModel>.Create(BinaryData data, ModelReaderWriterOptions options) => (GraderLabelModel)PersistableModelCreateCore(data, options); |
| 202 | |
| 203 | protected override Grader PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 204 | { |
| 205 | string format = options.Format == "W" ? ((IPersistableModel<GraderLabelModel>)this).GetFormatFromOptions(options) : options.Format; |
| 206 | switch (format) |
| 207 | { |
| 208 | case "J": |
| 209 | using (JsonDocument document = JsonDocument.Parse(data)) |
| 210 | { |
| 211 | return DeserializeGraderLabelModel(document.RootElement, options); |
| 212 | } |
| 213 | default: |
| 214 | throw new FormatException($"The model {nameof(GraderLabelModel)} does not support reading '{options.Format}' format."); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | string IPersistableModel<GraderLabelModel>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 219 | } |
| 220 | } |
| 221 | |