openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/Chat/AssistantChatMessage.Serialization.cs
195lines · 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 | |
| 12 | namespace OpenAI.Chat |
| 13 | { |
| 14 | public partial class AssistantChatMessage : IJsonModel<AssistantChatMessage> |
| 15 | { |
| 16 | [Experimental("OPENAI001")] |
| 17 | protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 18 | { |
| 19 | string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format; |
| 20 | if (format != "J") |
| 21 | { |
| 22 | throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support writing '{format}' format."); |
| 23 | } |
| 24 | base.JsonModelWriteCore(writer, options); |
| 25 | if (Optional.IsDefined(Refusal) && _additionalBinaryDataProperties?.ContainsKey("refusal") != true) |
| 26 | { |
| 27 | writer.WritePropertyName("refusal"u8); |
| 28 | writer.WriteStringValue(Refusal); |
| 29 | } |
| 30 | if (Optional.IsDefined(ParticipantName) && _additionalBinaryDataProperties?.ContainsKey("name") != true) |
| 31 | { |
| 32 | writer.WritePropertyName("name"u8); |
| 33 | writer.WriteStringValue(ParticipantName); |
| 34 | } |
| 35 | if (Optional.IsCollectionDefined(ToolCalls) && _additionalBinaryDataProperties?.ContainsKey("tool_calls") != true) |
| 36 | { |
| 37 | writer.WritePropertyName("tool_calls"u8); |
| 38 | writer.WriteStartArray(); |
| 39 | foreach (ChatToolCall item in ToolCalls) |
| 40 | { |
| 41 | writer.WriteObjectValue(item, options); |
| 42 | } |
| 43 | writer.WriteEndArray(); |
| 44 | } |
| 45 | if (Optional.IsDefined(FunctionCall) && _additionalBinaryDataProperties?.ContainsKey("function_call") != true) |
| 46 | { |
| 47 | writer.WritePropertyName("function_call"u8); |
| 48 | writer.WriteObjectValue(FunctionCall, options); |
| 49 | } |
| 50 | if (Optional.IsDefined(OutputAudioReference) && _additionalBinaryDataProperties?.ContainsKey("audio") != true) |
| 51 | { |
| 52 | writer.WritePropertyName("audio"u8); |
| 53 | writer.WriteObjectValue(OutputAudioReference, options); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | AssistantChatMessage IJsonModel<AssistantChatMessage>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (AssistantChatMessage)JsonModelCreateCore(ref reader, options); |
| 58 | |
| 59 | [Experimental("OPENAI001")] |
| 60 | protected override ChatMessage JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 61 | { |
| 62 | string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format; |
| 63 | if (format != "J") |
| 64 | { |
| 65 | throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support reading '{format}' format."); |
| 66 | } |
| 67 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 68 | return DeserializeAssistantChatMessage(document.RootElement, options); |
| 69 | } |
| 70 | |
| 71 | internal static AssistantChatMessage DeserializeAssistantChatMessage(JsonElement element, ModelReaderWriterOptions options) |
| 72 | { |
| 73 | if (element.ValueKind == JsonValueKind.Null) |
| 74 | { |
| 75 | return null; |
| 76 | } |
| 77 | ChatMessageContent content = default; |
| 78 | ChatMessageRole role = default; |
| 79 | IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>(); |
| 80 | string refusal = default; |
| 81 | string participantName = default; |
| 82 | IList<ChatToolCall> toolCalls = default; |
| 83 | ChatFunctionCall functionCall = default; |
| 84 | ChatOutputAudioReference outputAudioReference = default; |
| 85 | foreach (var prop in element.EnumerateObject()) |
| 86 | { |
| 87 | if (prop.NameEquals("content"u8)) |
| 88 | { |
| 89 | DeserializeContentValue(prop, ref content); |
| 90 | continue; |
| 91 | } |
| 92 | if (prop.NameEquals("role"u8)) |
| 93 | { |
| 94 | role = prop.Value.GetString().ToChatMessageRole(); |
| 95 | continue; |
| 96 | } |
| 97 | if (prop.NameEquals("refusal"u8)) |
| 98 | { |
| 99 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 100 | { |
| 101 | refusal = null; |
| 102 | continue; |
| 103 | } |
| 104 | refusal = prop.Value.GetString(); |
| 105 | continue; |
| 106 | } |
| 107 | if (prop.NameEquals("name"u8)) |
| 108 | { |
| 109 | participantName = prop.Value.GetString(); |
| 110 | continue; |
| 111 | } |
| 112 | if (prop.NameEquals("tool_calls"u8)) |
| 113 | { |
| 114 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 115 | { |
| 116 | continue; |
| 117 | } |
| 118 | List<ChatToolCall> array = new List<ChatToolCall>(); |
| 119 | foreach (var item in prop.Value.EnumerateArray()) |
| 120 | { |
| 121 | array.Add(ChatToolCall.DeserializeChatToolCall(item, options)); |
| 122 | } |
| 123 | toolCalls = array; |
| 124 | continue; |
| 125 | } |
| 126 | if (prop.NameEquals("function_call"u8)) |
| 127 | { |
| 128 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 129 | { |
| 130 | functionCall = null; |
| 131 | continue; |
| 132 | } |
| 133 | functionCall = ChatFunctionCall.DeserializeChatFunctionCall(prop.Value, options); |
| 134 | continue; |
| 135 | } |
| 136 | if (prop.NameEquals("audio"u8)) |
| 137 | { |
| 138 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 139 | { |
| 140 | outputAudioReference = null; |
| 141 | continue; |
| 142 | } |
| 143 | outputAudioReference = ChatOutputAudioReference.DeserializeChatOutputAudioReference(prop.Value, options); |
| 144 | continue; |
| 145 | } |
| 146 | // Plugin customization: remove options.Format != "W" check |
| 147 | additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText())); |
| 148 | } |
| 149 | return new AssistantChatMessage( |
| 150 | content, |
| 151 | role, |
| 152 | additionalBinaryDataProperties, |
| 153 | refusal, |
| 154 | participantName, |
| 155 | toolCalls ?? new ChangeTrackingList<ChatToolCall>(), |
| 156 | functionCall, |
| 157 | outputAudioReference); |
| 158 | } |
| 159 | |
| 160 | BinaryData IPersistableModel<AssistantChatMessage>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 161 | |
| 162 | [Experimental("OPENAI001")] |
| 163 | protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 164 | { |
| 165 | string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format; |
| 166 | switch (format) |
| 167 | { |
| 168 | case "J": |
| 169 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 170 | default: |
| 171 | throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support writing '{options.Format}' format."); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | AssistantChatMessage IPersistableModel<AssistantChatMessage>.Create(BinaryData data, ModelReaderWriterOptions options) => (AssistantChatMessage)PersistableModelCreateCore(data, options); |
| 176 | |
| 177 | [Experimental("OPENAI001")] |
| 178 | protected override ChatMessage PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 179 | { |
| 180 | string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format; |
| 181 | switch (format) |
| 182 | { |
| 183 | case "J": |
| 184 | using (JsonDocument document = JsonDocument.Parse(data)) |
| 185 | { |
| 186 | return DeserializeAssistantChatMessage(document.RootElement, options); |
| 187 | } |
| 188 | default: |
| 189 | throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support reading '{options.Format}' format."); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | string IPersistableModel<AssistantChatMessage>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 194 | } |
| 195 | } |
| 196 | |