openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/AssistantChatMessage.Serialization.cs
140lines · modecode
| 1 | // <auto-generated/> |
| 2 | |
| 3 | #nullable disable |
| 4 | |
| 5 | using System; |
| 6 | using System.ClientModel; |
| 7 | using System.ClientModel.Primitives; |
| 8 | using System.Collections.Generic; |
| 9 | using System.Text.Json; |
| 10 | |
| 11 | namespace OpenAI.Chat |
| 12 | { |
| 13 | public partial class AssistantChatMessage : IJsonModel<AssistantChatMessage> |
| 14 | { |
| 15 | AssistantChatMessage IJsonModel<AssistantChatMessage>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 16 | { |
| 17 | var format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format; |
| 18 | if (format != "J") |
| 19 | { |
| 20 | throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support reading '{format}' format."); |
| 21 | } |
| 22 | |
| 23 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 24 | return DeserializeAssistantChatMessage(document.RootElement, options); |
| 25 | } |
| 26 | |
| 27 | internal static AssistantChatMessage DeserializeAssistantChatMessage(JsonElement element, ModelReaderWriterOptions options = null) |
| 28 | { |
| 29 | options ??= ModelSerializationExtensions.WireOptions; |
| 30 | |
| 31 | if (element.ValueKind == JsonValueKind.Null) |
| 32 | { |
| 33 | return null; |
| 34 | } |
| 35 | string name = default; |
| 36 | IList<ChatToolCall> toolCalls = default; |
| 37 | ChatFunctionCall functionCall = default; |
| 38 | string role = "assistant"; |
| 39 | IList<ChatMessageContentPart> content = default; |
| 40 | IDictionary<string, BinaryData> serializedAdditionalRawData = default; |
| 41 | Dictionary<string, BinaryData> rawDataDictionary = new Dictionary<string, BinaryData>(); |
| 42 | foreach (var property in element.EnumerateObject()) |
| 43 | { |
| 44 | if (property.NameEquals("name"u8)) |
| 45 | { |
| 46 | name = property.Value.GetString(); |
| 47 | continue; |
| 48 | } |
| 49 | if (property.NameEquals("tool_calls"u8)) |
| 50 | { |
| 51 | if (property.Value.ValueKind == JsonValueKind.Null) |
| 52 | { |
| 53 | continue; |
| 54 | } |
| 55 | List<ChatToolCall> array = new List<ChatToolCall>(); |
| 56 | foreach (var item in property.Value.EnumerateArray()) |
| 57 | { |
| 58 | array.Add(ChatToolCall.DeserializeChatToolCall(item, options)); |
| 59 | } |
| 60 | toolCalls = array; |
| 61 | continue; |
| 62 | } |
| 63 | if (property.NameEquals("function_call"u8)) |
| 64 | { |
| 65 | if (property.Value.ValueKind == JsonValueKind.Null) |
| 66 | { |
| 67 | functionCall = null; |
| 68 | continue; |
| 69 | } |
| 70 | functionCall = ChatFunctionCall.DeserializeChatFunctionCall(property.Value, options); |
| 71 | continue; |
| 72 | } |
| 73 | if (property.NameEquals("role"u8)) |
| 74 | { |
| 75 | role = property.Value.GetString(); |
| 76 | continue; |
| 77 | } |
| 78 | if (property.NameEquals("content"u8)) |
| 79 | { |
| 80 | DeserializeContentValue(property, ref content); |
| 81 | continue; |
| 82 | } |
| 83 | if (true) |
| 84 | { |
| 85 | rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); |
| 86 | } |
| 87 | } |
| 88 | serializedAdditionalRawData = rawDataDictionary; |
| 89 | return new AssistantChatMessage( |
| 90 | role, |
| 91 | content ?? new ChangeTrackingList<ChatMessageContentPart>(), |
| 92 | serializedAdditionalRawData, |
| 93 | name, |
| 94 | toolCalls ?? new ChangeTrackingList<ChatToolCall>(), |
| 95 | functionCall); |
| 96 | } |
| 97 | |
| 98 | BinaryData IPersistableModel<AssistantChatMessage>.Write(ModelReaderWriterOptions options) |
| 99 | { |
| 100 | var format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format; |
| 101 | |
| 102 | switch (format) |
| 103 | { |
| 104 | case "J": |
| 105 | return ModelReaderWriter.Write(this, options); |
| 106 | default: |
| 107 | throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support writing '{options.Format}' format."); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | AssistantChatMessage IPersistableModel<AssistantChatMessage>.Create(BinaryData data, ModelReaderWriterOptions options) |
| 112 | { |
| 113 | var format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format; |
| 114 | |
| 115 | switch (format) |
| 116 | { |
| 117 | case "J": |
| 118 | { |
| 119 | using JsonDocument document = JsonDocument.Parse(data); |
| 120 | return DeserializeAssistantChatMessage(document.RootElement, options); |
| 121 | } |
| 122 | default: |
| 123 | throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support reading '{options.Format}' format."); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | string IPersistableModel<AssistantChatMessage>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 128 | |
| 129 | internal static new AssistantChatMessage FromResponse(PipelineResponse response) |
| 130 | { |
| 131 | using var document = JsonDocument.Parse(response.Content); |
| 132 | return DeserializeAssistantChatMessage(document.RootElement); |
| 133 | } |
| 134 | |
| 135 | internal override BinaryContent ToBinaryContent() |
| 136 | { |
| 137 | return BinaryContent.Create(this, ModelSerializationExtensions.WireOptions); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |