openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/Chat/ChatCompletion.Serialization.cs
238lines · 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.Internal; |
| 12 | |
| 13 | namespace OpenAI.Chat |
| 14 | { |
| 15 | public partial class ChatCompletion : IJsonModel<ChatCompletion> |
| 16 | { |
| 17 | internal ChatCompletion() : this(null, null, null, null, null, default, null, default, null) |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | void IJsonModel<ChatCompletion>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 22 | { |
| 23 | writer.WriteStartObject(); |
| 24 | JsonModelWriteCore(writer, options); |
| 25 | writer.WriteEndObject(); |
| 26 | } |
| 27 | |
| 28 | [Experimental("OPENAI001")] |
| 29 | protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 30 | { |
| 31 | string format = options.Format == "W" ? ((IPersistableModel<ChatCompletion>)this).GetFormatFromOptions(options) : options.Format; |
| 32 | if (format != "J") |
| 33 | { |
| 34 | throw new FormatException($"The model {nameof(ChatCompletion)} does not support writing '{format}' format."); |
| 35 | } |
| 36 | if (_additionalBinaryDataProperties?.ContainsKey("id") != true) |
| 37 | { |
| 38 | writer.WritePropertyName("id"u8); |
| 39 | writer.WriteStringValue(Id); |
| 40 | } |
| 41 | if (_additionalBinaryDataProperties?.ContainsKey("model") != true) |
| 42 | { |
| 43 | writer.WritePropertyName("model"u8); |
| 44 | writer.WriteStringValue(Model); |
| 45 | } |
| 46 | if (Optional.IsDefined(SystemFingerprint) && _additionalBinaryDataProperties?.ContainsKey("system_fingerprint") != true) |
| 47 | { |
| 48 | writer.WritePropertyName("system_fingerprint"u8); |
| 49 | writer.WriteStringValue(SystemFingerprint); |
| 50 | } |
| 51 | if (Optional.IsDefined(Usage) && _additionalBinaryDataProperties?.ContainsKey("usage") != true) |
| 52 | { |
| 53 | writer.WritePropertyName("usage"u8); |
| 54 | writer.WriteObjectValue(Usage, options); |
| 55 | } |
| 56 | if (_additionalBinaryDataProperties?.ContainsKey("object") != true) |
| 57 | { |
| 58 | writer.WritePropertyName("object"u8); |
| 59 | writer.WriteStringValue(Object); |
| 60 | } |
| 61 | if (Optional.IsDefined(ServiceTier) && _additionalBinaryDataProperties?.ContainsKey("service_tier") != true) |
| 62 | { |
| 63 | writer.WritePropertyName("service_tier"u8); |
| 64 | writer.WriteStringValue(ServiceTier.Value.ToString()); |
| 65 | } |
| 66 | if (_additionalBinaryDataProperties?.ContainsKey("choices") != true) |
| 67 | { |
| 68 | writer.WritePropertyName("choices"u8); |
| 69 | writer.WriteStartArray(); |
| 70 | foreach (InternalCreateChatCompletionResponseChoice item in Choices) |
| 71 | { |
| 72 | writer.WriteObjectValue(item, options); |
| 73 | } |
| 74 | writer.WriteEndArray(); |
| 75 | } |
| 76 | if (_additionalBinaryDataProperties?.ContainsKey("created") != true) |
| 77 | { |
| 78 | writer.WritePropertyName("created"u8); |
| 79 | writer.WriteNumberValue(CreatedAt, "U"); |
| 80 | } |
| 81 | // Plugin customization: remove options.Format != "W" check |
| 82 | if (_additionalBinaryDataProperties != null) |
| 83 | { |
| 84 | foreach (var item in _additionalBinaryDataProperties) |
| 85 | { |
| 86 | if (ModelSerializationExtensions.IsSentinelValue(item.Value)) |
| 87 | { |
| 88 | continue; |
| 89 | } |
| 90 | writer.WritePropertyName(item.Key); |
| 91 | #if NET6_0_OR_GREATER |
| 92 | writer.WriteRawValue(item.Value); |
| 93 | #else |
| 94 | using (JsonDocument document = JsonDocument.Parse(item.Value)) |
| 95 | { |
| 96 | JsonSerializer.Serialize(writer, document.RootElement); |
| 97 | } |
| 98 | #endif |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | ChatCompletion IJsonModel<ChatCompletion>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 104 | |
| 105 | [Experimental("OPENAI001")] |
| 106 | protected virtual ChatCompletion JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 107 | { |
| 108 | string format = options.Format == "W" ? ((IPersistableModel<ChatCompletion>)this).GetFormatFromOptions(options) : options.Format; |
| 109 | if (format != "J") |
| 110 | { |
| 111 | throw new FormatException($"The model {nameof(ChatCompletion)} does not support reading '{format}' format."); |
| 112 | } |
| 113 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 114 | return DeserializeChatCompletion(document.RootElement, options); |
| 115 | } |
| 116 | |
| 117 | internal static ChatCompletion DeserializeChatCompletion(JsonElement element, ModelReaderWriterOptions options) |
| 118 | { |
| 119 | if (element.ValueKind == JsonValueKind.Null) |
| 120 | { |
| 121 | return null; |
| 122 | } |
| 123 | string id = default; |
| 124 | string model = default; |
| 125 | string systemFingerprint = default; |
| 126 | ChatTokenUsage usage = default; |
| 127 | string @object = default; |
| 128 | InternalServiceTier? serviceTier = default; |
| 129 | IReadOnlyList<InternalCreateChatCompletionResponseChoice> choices = default; |
| 130 | DateTimeOffset createdAt = default; |
| 131 | IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>(); |
| 132 | foreach (var prop in element.EnumerateObject()) |
| 133 | { |
| 134 | if (prop.NameEquals("id"u8)) |
| 135 | { |
| 136 | id = prop.Value.GetString(); |
| 137 | continue; |
| 138 | } |
| 139 | if (prop.NameEquals("model"u8)) |
| 140 | { |
| 141 | model = prop.Value.GetString(); |
| 142 | continue; |
| 143 | } |
| 144 | if (prop.NameEquals("system_fingerprint"u8)) |
| 145 | { |
| 146 | systemFingerprint = prop.Value.GetString(); |
| 147 | continue; |
| 148 | } |
| 149 | if (prop.NameEquals("usage"u8)) |
| 150 | { |
| 151 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 152 | { |
| 153 | continue; |
| 154 | } |
| 155 | usage = ChatTokenUsage.DeserializeChatTokenUsage(prop.Value, options); |
| 156 | continue; |
| 157 | } |
| 158 | if (prop.NameEquals("object"u8)) |
| 159 | { |
| 160 | @object = prop.Value.GetString(); |
| 161 | continue; |
| 162 | } |
| 163 | if (prop.NameEquals("service_tier"u8)) |
| 164 | { |
| 165 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 166 | { |
| 167 | serviceTier = null; |
| 168 | continue; |
| 169 | } |
| 170 | serviceTier = new InternalServiceTier(prop.Value.GetString()); |
| 171 | continue; |
| 172 | } |
| 173 | if (prop.NameEquals("choices"u8)) |
| 174 | { |
| 175 | List<InternalCreateChatCompletionResponseChoice> array = new List<InternalCreateChatCompletionResponseChoice>(); |
| 176 | foreach (var item in prop.Value.EnumerateArray()) |
| 177 | { |
| 178 | array.Add(InternalCreateChatCompletionResponseChoice.DeserializeInternalCreateChatCompletionResponseChoice(item, options)); |
| 179 | } |
| 180 | choices = array; |
| 181 | continue; |
| 182 | } |
| 183 | if (prop.NameEquals("created"u8)) |
| 184 | { |
| 185 | createdAt = DateTimeOffset.FromUnixTimeSeconds(prop.Value.GetInt64()); |
| 186 | continue; |
| 187 | } |
| 188 | // Plugin customization: remove options.Format != "W" check |
| 189 | additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText())); |
| 190 | } |
| 191 | return new ChatCompletion( |
| 192 | id, |
| 193 | model, |
| 194 | systemFingerprint, |
| 195 | usage, |
| 196 | @object, |
| 197 | serviceTier, |
| 198 | choices, |
| 199 | createdAt, |
| 200 | additionalBinaryDataProperties); |
| 201 | } |
| 202 | |
| 203 | BinaryData IPersistableModel<ChatCompletion>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 204 | |
| 205 | [Experimental("OPENAI001")] |
| 206 | protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 207 | { |
| 208 | string format = options.Format == "W" ? ((IPersistableModel<ChatCompletion>)this).GetFormatFromOptions(options) : options.Format; |
| 209 | switch (format) |
| 210 | { |
| 211 | case "J": |
| 212 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 213 | default: |
| 214 | throw new FormatException($"The model {nameof(ChatCompletion)} does not support writing '{options.Format}' format."); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | ChatCompletion IPersistableModel<ChatCompletion>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 219 | |
| 220 | [Experimental("OPENAI001")] |
| 221 | protected virtual ChatCompletion PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 222 | { |
| 223 | string format = options.Format == "W" ? ((IPersistableModel<ChatCompletion>)this).GetFormatFromOptions(options) : options.Format; |
| 224 | switch (format) |
| 225 | { |
| 226 | case "J": |
| 227 | using (JsonDocument document = JsonDocument.Parse(data)) |
| 228 | { |
| 229 | return DeserializeChatCompletion(document.RootElement, options); |
| 230 | } |
| 231 | default: |
| 232 | throw new FormatException($"The model {nameof(ChatCompletion)} does not support reading '{options.Format}' format."); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | string IPersistableModel<ChatCompletion>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 237 | } |
| 238 | } |
| 239 | |