openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/Chat/AssistantChatMessage.Serialization.cs
272lines · 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; |
| 10 | using System.Text.Json; |
| 11 | using OpenAI; |
| 12 | |
| 13 | namespace OpenAI.Chat |
| 14 | { |
| 15 | public partial class AssistantChatMessage : ChatMessage, IJsonModel<AssistantChatMessage> |
| 16 | { |
| 17 | [Experimental("OPENAI001")] |
| 18 | protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 19 | { |
| 20 | string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format; |
| 21 | if (format != "J") |
| 22 | { |
| 23 | throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support writing '{format}' format."); |
| 24 | } |
| 25 | base.JsonModelWriteCore(writer, options); |
| 26 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 27 | if (Optional.IsDefined(Refusal) && !Patch.Contains("$.refusal"u8)) |
| 28 | { |
| 29 | writer.WritePropertyName("refusal"u8); |
| 30 | writer.WriteStringValue(Refusal); |
| 31 | } |
| 32 | if (Optional.IsDefined(ParticipantName) && !Patch.Contains("$.name"u8)) |
| 33 | { |
| 34 | writer.WritePropertyName("name"u8); |
| 35 | writer.WriteStringValue(ParticipantName); |
| 36 | } |
| 37 | if (Optional.IsDefined(OutputAudioReference) && !Patch.Contains("$.audio"u8)) |
| 38 | { |
| 39 | writer.WritePropertyName("audio"u8); |
| 40 | writer.WriteObjectValue(OutputAudioReference, options); |
| 41 | } |
| 42 | if (Patch.Contains("$.tool_calls"u8)) |
| 43 | { |
| 44 | if (!Patch.IsRemoved("$.tool_calls"u8)) |
| 45 | { |
| 46 | writer.WritePropertyName("tool_calls"u8); |
| 47 | writer.WriteRawValue(Patch.GetJson("$.tool_calls"u8)); |
| 48 | } |
| 49 | } |
| 50 | else if (Optional.IsCollectionDefined(ToolCalls)) |
| 51 | { |
| 52 | writer.WritePropertyName("tool_calls"u8); |
| 53 | writer.WriteStartArray(); |
| 54 | for (int i = 0; i < ToolCalls.Count; i++) |
| 55 | { |
| 56 | if (ToolCalls[i].Patch.IsRemoved("$"u8)) |
| 57 | { |
| 58 | continue; |
| 59 | } |
| 60 | writer.WriteObjectValue(ToolCalls[i], options); |
| 61 | } |
| 62 | Patch.WriteTo(writer, "$.tool_calls"u8); |
| 63 | writer.WriteEndArray(); |
| 64 | } |
| 65 | if (Optional.IsDefined(FunctionCall) && !Patch.Contains("$.function_call"u8)) |
| 66 | { |
| 67 | writer.WritePropertyName("function_call"u8); |
| 68 | writer.WriteObjectValue(FunctionCall, options); |
| 69 | } |
| 70 | |
| 71 | Patch.WriteTo(writer); |
| 72 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 73 | } |
| 74 | |
| 75 | AssistantChatMessage IJsonModel<AssistantChatMessage>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (AssistantChatMessage)JsonModelCreateCore(ref reader, options); |
| 76 | |
| 77 | [Experimental("OPENAI001")] |
| 78 | protected override ChatMessage JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 79 | { |
| 80 | string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format; |
| 81 | if (format != "J") |
| 82 | { |
| 83 | throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support reading '{format}' format."); |
| 84 | } |
| 85 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 86 | return DeserializeAssistantChatMessage(document.RootElement, null, options); |
| 87 | } |
| 88 | |
| 89 | internal static AssistantChatMessage DeserializeAssistantChatMessage(JsonElement element, BinaryData data, ModelReaderWriterOptions options) |
| 90 | { |
| 91 | if (element.ValueKind == JsonValueKind.Null) |
| 92 | { |
| 93 | return null; |
| 94 | } |
| 95 | ChatMessageRole role = default; |
| 96 | ChatMessageContent content = default; |
| 97 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 98 | JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory<byte>.Empty : data.ToMemory()); |
| 99 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 100 | string refusal = default; |
| 101 | string participantName = default; |
| 102 | ChatOutputAudioReference outputAudioReference = default; |
| 103 | IList<ChatToolCall> toolCalls = default; |
| 104 | ChatFunctionCall functionCall = default; |
| 105 | foreach (var prop in element.EnumerateObject()) |
| 106 | { |
| 107 | if (prop.NameEquals("role"u8)) |
| 108 | { |
| 109 | role = prop.Value.GetString().ToChatMessageRole(); |
| 110 | continue; |
| 111 | } |
| 112 | if (prop.NameEquals("content"u8)) |
| 113 | { |
| 114 | DeserializeContentValue(prop, ref content); |
| 115 | continue; |
| 116 | } |
| 117 | if (prop.NameEquals("refusal"u8)) |
| 118 | { |
| 119 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 120 | { |
| 121 | refusal = null; |
| 122 | continue; |
| 123 | } |
| 124 | refusal = prop.Value.GetString(); |
| 125 | continue; |
| 126 | } |
| 127 | if (prop.NameEquals("name"u8)) |
| 128 | { |
| 129 | participantName = prop.Value.GetString(); |
| 130 | continue; |
| 131 | } |
| 132 | if (prop.NameEquals("audio"u8)) |
| 133 | { |
| 134 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 135 | { |
| 136 | outputAudioReference = null; |
| 137 | continue; |
| 138 | } |
| 139 | outputAudioReference = ChatOutputAudioReference.DeserializeChatOutputAudioReference(prop.Value, prop.Value.GetUtf8Bytes(), options); |
| 140 | continue; |
| 141 | } |
| 142 | if (prop.NameEquals("tool_calls"u8)) |
| 143 | { |
| 144 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 145 | { |
| 146 | continue; |
| 147 | } |
| 148 | List<ChatToolCall> array = new List<ChatToolCall>(); |
| 149 | foreach (var item in prop.Value.EnumerateArray()) |
| 150 | { |
| 151 | array.Add(ChatToolCall.DeserializeChatToolCall(item, item.GetUtf8Bytes(), options)); |
| 152 | } |
| 153 | toolCalls = array; |
| 154 | continue; |
| 155 | } |
| 156 | if (prop.NameEquals("function_call"u8)) |
| 157 | { |
| 158 | if (prop.Value.ValueKind == JsonValueKind.Null) |
| 159 | { |
| 160 | functionCall = null; |
| 161 | continue; |
| 162 | } |
| 163 | functionCall = ChatFunctionCall.DeserializeChatFunctionCall(prop.Value, prop.Value.GetUtf8Bytes(), options); |
| 164 | continue; |
| 165 | } |
| 166 | patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(prop.Name)], prop.Value.GetUtf8Bytes()); |
| 167 | } |
| 168 | return new AssistantChatMessage( |
| 169 | role, |
| 170 | content, |
| 171 | patch, |
| 172 | refusal, |
| 173 | participantName, |
| 174 | outputAudioReference, |
| 175 | toolCalls ?? new ChangeTrackingList<ChatToolCall>(), |
| 176 | functionCall); |
| 177 | } |
| 178 | |
| 179 | BinaryData IPersistableModel<AssistantChatMessage>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 180 | |
| 181 | [Experimental("OPENAI001")] |
| 182 | protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 183 | { |
| 184 | string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format; |
| 185 | switch (format) |
| 186 | { |
| 187 | case "J": |
| 188 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 189 | default: |
| 190 | throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support writing '{options.Format}' format."); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | AssistantChatMessage IPersistableModel<AssistantChatMessage>.Create(BinaryData data, ModelReaderWriterOptions options) => (AssistantChatMessage)PersistableModelCreateCore(data, options); |
| 195 | |
| 196 | [Experimental("OPENAI001")] |
| 197 | protected override ChatMessage PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 198 | { |
| 199 | string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format; |
| 200 | switch (format) |
| 201 | { |
| 202 | case "J": |
| 203 | using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions)) |
| 204 | { |
| 205 | return DeserializeAssistantChatMessage(document.RootElement, data, options); |
| 206 | } |
| 207 | default: |
| 208 | throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support reading '{options.Format}' format."); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | string IPersistableModel<AssistantChatMessage>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 213 | |
| 214 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 215 | private bool PropagateGet(ReadOnlySpan<byte> jsonPath, out JsonPatch.EncodedValue value) |
| 216 | { |
| 217 | ReadOnlySpan<byte> local = jsonPath.SliceToStartOfPropertyName(); |
| 218 | value = default; |
| 219 | |
| 220 | if (local.StartsWith("audio"u8)) |
| 221 | { |
| 222 | return OutputAudioReference.Patch.TryGetEncodedValue([.. "$"u8, .. local.Slice("audio"u8.Length)], out value); |
| 223 | } |
| 224 | if (local.StartsWith("function_call"u8)) |
| 225 | { |
| 226 | return FunctionCall.Patch.TryGetEncodedValue([.. "$"u8, .. local.Slice("function_call"u8.Length)], out value); |
| 227 | } |
| 228 | if (local.StartsWith("tool_calls"u8)) |
| 229 | { |
| 230 | int propertyLength = "tool_calls"u8.Length; |
| 231 | ReadOnlySpan<byte> currentSlice = local.Slice(propertyLength); |
| 232 | if (!currentSlice.TryGetIndex(out int index, out int bytesConsumed)) |
| 233 | { |
| 234 | return false; |
| 235 | } |
| 236 | return ToolCalls[index].Patch.TryGetEncodedValue([.. "$"u8, .. currentSlice.Slice(bytesConsumed)], out value); |
| 237 | } |
| 238 | return false; |
| 239 | } |
| 240 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 241 | |
| 242 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 243 | private bool PropagateSet(ReadOnlySpan<byte> jsonPath, JsonPatch.EncodedValue value) |
| 244 | { |
| 245 | ReadOnlySpan<byte> local = jsonPath.SliceToStartOfPropertyName(); |
| 246 | |
| 247 | if (local.StartsWith("audio"u8)) |
| 248 | { |
| 249 | OutputAudioReference.Patch.Set([.. "$"u8, .. local.Slice("audio"u8.Length)], value); |
| 250 | return true; |
| 251 | } |
| 252 | if (local.StartsWith("function_call"u8)) |
| 253 | { |
| 254 | FunctionCall.Patch.Set([.. "$"u8, .. local.Slice("function_call"u8.Length)], value); |
| 255 | return true; |
| 256 | } |
| 257 | if (local.StartsWith("tool_calls"u8)) |
| 258 | { |
| 259 | int propertyLength = "tool_calls"u8.Length; |
| 260 | ReadOnlySpan<byte> currentSlice = local.Slice(propertyLength); |
| 261 | if (!currentSlice.TryGetIndex(out int index, out int bytesConsumed)) |
| 262 | { |
| 263 | return false; |
| 264 | } |
| 265 | ToolCalls[index].Patch.Set([.. "$"u8, .. currentSlice.Slice(bytesConsumed)], value); |
| 266 | return true; |
| 267 | } |
| 268 | return false; |
| 269 | } |
| 270 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 271 | } |
| 272 | } |