openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/Audio/AudioTranscriptionOptions.Serialization.cs
304lines · 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.Diagnostics.CodeAnalysis; |
| 9 | using System.Text.Json; |
| 10 | using OpenAI; |
| 11 | using OpenAI.Realtime; |
| 12 | |
| 13 | namespace OpenAI.Audio |
| 14 | { |
| 15 | public partial class AudioTranscriptionOptions : IJsonModel<AudioTranscriptionOptions> |
| 16 | { |
| 17 | void IJsonModel<AudioTranscriptionOptions>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 18 | { |
| 19 | writer.WriteStartObject(); |
| 20 | JsonModelWriteCore(writer, options); |
| 21 | writer.WriteEndObject(); |
| 22 | } |
| 23 | |
| 24 | [Experimental("OPENAI001")] |
| 25 | protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 26 | { |
| 27 | string format = options.Format == "W" ? ((IPersistableModel<AudioTranscriptionOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 28 | if (format != "J") |
| 29 | { |
| 30 | throw new FormatException($"The model {nameof(AudioTranscriptionOptions)} does not support writing '{format}' format."); |
| 31 | } |
| 32 | if (Optional.IsDefined(Language) && _additionalBinaryDataProperties?.ContainsKey("language") != true) |
| 33 | { |
| 34 | writer.WritePropertyName("language"u8); |
| 35 | writer.WriteStringValue(Language); |
| 36 | } |
| 37 | if (Optional.IsDefined(Prompt) && _additionalBinaryDataProperties?.ContainsKey("prompt") != true) |
| 38 | { |
| 39 | writer.WritePropertyName("prompt"u8); |
| 40 | writer.WriteStringValue(Prompt); |
| 41 | } |
| 42 | if (Optional.IsDefined(ResponseFormat) && _additionalBinaryDataProperties?.ContainsKey("response_format") != true) |
| 43 | { |
| 44 | writer.WritePropertyName("response_format"u8); |
| 45 | writer.WriteStringValue(ResponseFormat.Value.ToString()); |
| 46 | } |
| 47 | if (Optional.IsDefined(Temperature) && _additionalBinaryDataProperties?.ContainsKey("temperature") != true) |
| 48 | { |
| 49 | writer.WritePropertyName("temperature"u8); |
| 50 | writer.WriteNumberValue(Temperature.Value); |
| 51 | } |
| 52 | if (Optional.IsDefined(ChunkingStrategy) && _additionalBinaryDataProperties?.ContainsKey("chunking_strategy") != true) |
| 53 | { |
| 54 | writer.WritePropertyName("chunking_strategy"u8); |
| 55 | writer.WriteObjectValue(ChunkingStrategy, options); |
| 56 | } |
| 57 | if (_additionalBinaryDataProperties?.ContainsKey("file") != true) |
| 58 | { |
| 59 | writer.WritePropertyName("file"u8); |
| 60 | writer.WriteBase64StringValue(File.ToArray(), "D"); |
| 61 | } |
| 62 | if (_additionalBinaryDataProperties?.ContainsKey("model") != true) |
| 63 | { |
| 64 | writer.WritePropertyName("model"u8); |
| 65 | writer.WriteStringValue(Model.ToString()); |
| 66 | } |
| 67 | if (Optional.IsCollectionDefined(InternalTimestampGranularities) && _additionalBinaryDataProperties?.ContainsKey("timestamp_granularities[]") != true) |
| 68 | { |
| 69 | writer.WritePropertyName("timestamp_granularities[]"u8); |
| 70 | writer.WriteStartArray(); |
| 71 | foreach (BinaryData item in InternalTimestampGranularities) |
| 72 | { |
| 73 | if (item == null) |
| 74 | { |
| 75 | writer.WriteNullValue(); |
| 76 | continue; |
| 77 | } |
| 78 | #if NET6_0_OR_GREATER |
| 79 | writer.WriteRawValue(item); |
| 80 | #else |
| 81 | using (JsonDocument document = JsonDocument.Parse(item)) |
| 82 | { |
| 83 | JsonSerializer.Serialize(writer, document.RootElement); |
| 84 | } |
| 85 | #endif |
| 86 | } |
| 87 | writer.WriteEndArray(); |
| 88 | } |
| 89 | if (Optional.IsDefined(Stream) && _additionalBinaryDataProperties?.ContainsKey("stream") != true) |
| 90 | { |
| 91 | writer.WritePropertyName("stream"u8); |
| 92 | writer.WriteBooleanValue(Stream.Value); |
| 93 | } |
| 94 | if (Optional.IsCollectionDefined(InternalInclude) && _additionalBinaryDataProperties?.ContainsKey("include[]") != true) |
| 95 | { |
| 96 | writer.WritePropertyName("include[]"u8); |
| 97 | writer.WriteStartArray(); |
| 98 | foreach (InternalTranscriptionInclude item in InternalInclude) |
| 99 | { |
| 100 | writer.WriteStringValue(item.ToString()); |
| 101 | } |
| 102 | writer.WriteEndArray(); |
| 103 | } |
| 104 | // Plugin customization: remove options.Format != "W" check |
| 105 | if (_additionalBinaryDataProperties != null) |
| 106 | { |
| 107 | foreach (var item in _additionalBinaryDataProperties) |
| 108 | { |
| 109 | if (ModelSerializationExtensions.IsSentinelValue(item.Value)) |
| 110 | { |
| 111 | continue; |
| 112 | } |
| 113 | writer.WritePropertyName(item.Key); |
| 114 | #if NET6_0_OR_GREATER |
| 115 | writer.WriteRawValue(item.Value); |
| 116 | #else |
| 117 | using (JsonDocument document = JsonDocument.Parse(item.Value)) |
| 118 | { |
| 119 | JsonSerializer.Serialize(writer, document.RootElement); |
| 120 | } |
| 121 | #endif |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | AudioTranscriptionOptions IJsonModel<AudioTranscriptionOptions>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 127 | |
| 128 | [Experimental("OPENAI001")] |
| 129 | protected virtual AudioTranscriptionOptions JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 130 | { |
| 131 | string format = options.Format == "W" ? ((IPersistableModel<AudioTranscriptionOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 132 | if (format != "J") |
| 133 | { |
| 134 | throw new FormatException($"The model {nameof(AudioTranscriptionOptions)} does not support reading '{format}' format."); |
| 135 | } |
| 136 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 137 | return DeserializeAudioTranscriptionOptions(document.RootElement, options); |
| 138 | } |
| 139 | |
| 140 | internal static AudioTranscriptionOptions DeserializeAudioTranscriptionOptions(JsonElement element, ModelReaderWriterOptions options) |
| 141 | { |
| 142 | if (element.ValueKind == JsonValueKind.Null) |
| 143 | { |
| 144 | return null; |
| 145 | } |
| 146 | string language = default; |
| 147 | string prompt = default; |
| 148 | AudioTranscriptionFormat? responseFormat = default; |
| 149 | float? temperature = default; |
| 150 | InternalVadConfig chunkingStrategy = default; |
| 151 | BinaryData @file = default; |
| 152 | InternalCreateTranscriptionRequestModel model = default; |
| 153 | IList<BinaryData> internalTimestampGranularities = default; |
| 154 | bool? stream = default; |
| 155 | IList<InternalTranscriptionInclude> internalInclude = default; |
| 156 | IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>(); |
| 157 | foreach (var prop in element.EnumerateObject()) |
| 158 | { |
| 159 | if (prop.NameEquals("language"u8)) |
| 160 | { |
| 161 | language = prop.Value.GetString(); |
| 162 | continue; |
| 163 | } |
| 164 | if (prop.NameEquals("prompt"u8)) |
| 165 | { |
| 166 | prompt = prop.Value.GetString(); |
| 167 | continue; |
| 168 | } |
| 169 | if (prop.NameEquals("response_format"u8)) |
| 170 | { |
| 171 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 172 | { |
| 173 | continue; |
| 174 | } |
| 175 | responseFormat = new AudioTranscriptionFormat(prop.Value.GetString()); |
| 176 | continue; |
| 177 | } |
| 178 | if (prop.NameEquals("temperature"u8)) |
| 179 | { |
| 180 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 181 | { |
| 182 | continue; |
| 183 | } |
| 184 | temperature = prop.Value.GetSingle(); |
| 185 | continue; |
| 186 | } |
| 187 | if (prop.NameEquals("chunking_strategy"u8)) |
| 188 | { |
| 189 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 190 | { |
| 191 | chunkingStrategy = null; |
| 192 | continue; |
| 193 | } |
| 194 | chunkingStrategy = InternalVadConfig.DeserializeInternalVadConfig(prop.Value, options); |
| 195 | continue; |
| 196 | } |
| 197 | if (prop.NameEquals("file"u8)) |
| 198 | { |
| 199 | @file = BinaryData.FromBytes(prop.Value.GetBytesFromBase64("D")); |
| 200 | continue; |
| 201 | } |
| 202 | if (prop.NameEquals("model"u8)) |
| 203 | { |
| 204 | model = new InternalCreateTranscriptionRequestModel(prop.Value.GetString()); |
| 205 | continue; |
| 206 | } |
| 207 | if (prop.NameEquals("timestamp_granularities[]"u8)) |
| 208 | { |
| 209 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 210 | { |
| 211 | continue; |
| 212 | } |
| 213 | List<BinaryData> array = new List<BinaryData>(); |
| 214 | foreach (var item in prop.Value.EnumerateArray()) |
| 215 | { |
| 216 | if (item.ValueKind == JsonValueKind.Null) |
| 217 | { |
| 218 | array.Add(null); |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | array.Add(BinaryData.FromString(item.GetRawText())); |
| 223 | } |
| 224 | } |
| 225 | internalTimestampGranularities = array; |
| 226 | continue; |
| 227 | } |
| 228 | if (prop.NameEquals("stream"u8)) |
| 229 | { |
| 230 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 231 | { |
| 232 | stream = null; |
| 233 | continue; |
| 234 | } |
| 235 | stream = prop.Value.GetBoolean(); |
| 236 | continue; |
| 237 | } |
| 238 | if (prop.NameEquals("include[]"u8)) |
| 239 | { |
| 240 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 241 | { |
| 242 | continue; |
| 243 | } |
| 244 | List<InternalTranscriptionInclude> array = new List<InternalTranscriptionInclude>(); |
| 245 | foreach (var item in prop.Value.EnumerateArray()) |
| 246 | { |
| 247 | array.Add(new InternalTranscriptionInclude(item.GetString())); |
| 248 | } |
| 249 | internalInclude = array; |
| 250 | continue; |
| 251 | } |
| 252 | // Plugin customization: remove options.Format != "W" check |
| 253 | additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText())); |
| 254 | } |
| 255 | return new AudioTranscriptionOptions( |
| 256 | language, |
| 257 | prompt, |
| 258 | responseFormat, |
| 259 | temperature, |
| 260 | chunkingStrategy, |
| 261 | @file, |
| 262 | model, |
| 263 | internalTimestampGranularities ?? new ChangeTrackingList<BinaryData>(), |
| 264 | stream, |
| 265 | internalInclude ?? new ChangeTrackingList<InternalTranscriptionInclude>(), |
| 266 | additionalBinaryDataProperties); |
| 267 | } |
| 268 | |
| 269 | BinaryData IPersistableModel<AudioTranscriptionOptions>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 270 | |
| 271 | [Experimental("OPENAI001")] |
| 272 | protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 273 | { |
| 274 | string format = options.Format == "W" ? ((IPersistableModel<AudioTranscriptionOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 275 | switch (format) |
| 276 | { |
| 277 | case "J": |
| 278 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 279 | default: |
| 280 | throw new FormatException($"The model {nameof(AudioTranscriptionOptions)} does not support writing '{options.Format}' format."); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | AudioTranscriptionOptions IPersistableModel<AudioTranscriptionOptions>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 285 | |
| 286 | [Experimental("OPENAI001")] |
| 287 | protected virtual AudioTranscriptionOptions PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 288 | { |
| 289 | string format = options.Format == "W" ? ((IPersistableModel<AudioTranscriptionOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 290 | switch (format) |
| 291 | { |
| 292 | case "J": |
| 293 | using (JsonDocument document = JsonDocument.Parse(data)) |
| 294 | { |
| 295 | return DeserializeAudioTranscriptionOptions(document.RootElement, options); |
| 296 | } |
| 297 | default: |
| 298 | throw new FormatException($"The model {nameof(AudioTranscriptionOptions)} does not support reading '{options.Format}' format."); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | string IPersistableModel<AudioTranscriptionOptions>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 303 | } |
| 304 | } |
| 305 | |