openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Chat/Internal/InternalChatCompletionStreamResponseDelta.Serialization.cs
133lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel.Primitives; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Runtime.CompilerServices; |
| 5 | using System.Text.Json; |
| 6 | |
| 7 | namespace OpenAI.Chat; |
| 8 | |
| 9 | [CodeGenSuppress("global::System.ClientModel.Primitives.IJsonModel<OpenAI.Chat.InternalChatCompletionStreamResponseDelta>.Write", typeof(Utf8JsonWriter), typeof(ModelReaderWriterOptions))] |
| 10 | internal partial class InternalChatCompletionStreamResponseDelta : IJsonModel<InternalChatCompletionStreamResponseDelta> |
| 11 | { |
| 12 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 13 | private void SerializeContentValue(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 14 | { |
| 15 | throw new NotImplementedException(); |
| 16 | } |
| 17 | |
| 18 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 19 | private static void DeserializeContentValue(JsonProperty property, ref IReadOnlyList<ChatMessageContentPart> content, ModelReaderWriterOptions options = null) |
| 20 | { |
| 21 | throw new NotImplementedException(); |
| 22 | } |
| 23 | |
| 24 | void IJsonModel<InternalChatCompletionStreamResponseDelta>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 25 | => CustomSerializationHelpers.SerializeInstance(this, SerializeInternalChatCompletionStreamResponseDelta, writer, options); |
| 26 | |
| 27 | internal static void SerializeInternalChatCompletionStreamResponseDelta(InternalChatCompletionStreamResponseDelta instance, Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 28 | { |
| 29 | writer.WriteStartObject(); |
| 30 | if (Optional.IsCollectionDefined(instance.Content)) |
| 31 | { |
| 32 | if (instance.Content[0] != null) |
| 33 | { |
| 34 | writer.WritePropertyName("content"u8); |
| 35 | writer.WriteStringValue(instance.Content[0].Text); |
| 36 | } |
| 37 | else |
| 38 | { |
| 39 | writer.WriteNull("content"); |
| 40 | } |
| 41 | } |
| 42 | if (Optional.IsDefined(instance.FunctionCall)) |
| 43 | { |
| 44 | writer.WritePropertyName("function_call"u8); |
| 45 | writer.WriteObjectValue(instance.FunctionCall, options); |
| 46 | } |
| 47 | if (Optional.IsCollectionDefined(instance.ToolCalls)) |
| 48 | { |
| 49 | writer.WritePropertyName("tool_calls"u8); |
| 50 | writer.WriteStartArray(); |
| 51 | foreach (var item in instance.ToolCalls) |
| 52 | { |
| 53 | writer.WriteObjectValue(item, options); |
| 54 | } |
| 55 | writer.WriteEndArray(); |
| 56 | } |
| 57 | if (Optional.IsDefined(instance.Role)) |
| 58 | { |
| 59 | writer.WritePropertyName("role"u8); |
| 60 | writer.WriteStringValue(instance.Role.Value.ToSerialString()); |
| 61 | } |
| 62 | writer.WriteSerializedAdditionalRawData(instance._serializedAdditionalRawData, options); |
| 63 | writer.WriteEndObject(); |
| 64 | } |
| 65 | |
| 66 | internal static InternalChatCompletionStreamResponseDelta DeserializeInternalChatCompletionStreamResponseDelta(JsonElement element, ModelReaderWriterOptions options = null) |
| 67 | { |
| 68 | options ??= ModelSerializationExtensions.WireOptions; |
| 69 | |
| 70 | if (element.ValueKind == JsonValueKind.Null) |
| 71 | { |
| 72 | return null; |
| 73 | } |
| 74 | IReadOnlyList<ChatMessageContentPart> content = default; |
| 75 | StreamingChatFunctionCallUpdate functionCall = default; |
| 76 | IReadOnlyList<StreamingChatToolCallUpdate> toolCalls = default; |
| 77 | ChatMessageRole? role = default; |
| 78 | IDictionary<string, BinaryData> serializedAdditionalRawData = default; |
| 79 | Dictionary<string, BinaryData> rawDataDictionary = new Dictionary<string, BinaryData>(); |
| 80 | foreach (var property in element.EnumerateObject()) |
| 81 | { |
| 82 | if (property.NameEquals("content"u8)) |
| 83 | { |
| 84 | if (property.Value.ValueKind == JsonValueKind.Null) |
| 85 | { |
| 86 | continue; |
| 87 | } |
| 88 | List<ChatMessageContentPart> array = new List<ChatMessageContentPart>(); |
| 89 | array.Add(ChatMessageContentPart.CreateTextMessageContentPart(property.Value.GetString())); |
| 90 | content = array; |
| 91 | continue; |
| 92 | } |
| 93 | if (property.NameEquals("function_call"u8)) |
| 94 | { |
| 95 | if (property.Value.ValueKind == JsonValueKind.Null) |
| 96 | { |
| 97 | continue; |
| 98 | } |
| 99 | functionCall = StreamingChatFunctionCallUpdate.DeserializeStreamingChatFunctionCallUpdate(property.Value, options); |
| 100 | continue; |
| 101 | } |
| 102 | if (property.NameEquals("tool_calls"u8)) |
| 103 | { |
| 104 | if (property.Value.ValueKind == JsonValueKind.Null) |
| 105 | { |
| 106 | continue; |
| 107 | } |
| 108 | List<StreamingChatToolCallUpdate> array = new List<StreamingChatToolCallUpdate>(); |
| 109 | foreach (var item in property.Value.EnumerateArray()) |
| 110 | { |
| 111 | array.Add(StreamingChatToolCallUpdate.DeserializeStreamingChatToolCallUpdate(item, options)); |
| 112 | } |
| 113 | toolCalls = array; |
| 114 | continue; |
| 115 | } |
| 116 | if (property.NameEquals("role"u8)) |
| 117 | { |
| 118 | if (property.Value.ValueKind == JsonValueKind.Null) |
| 119 | { |
| 120 | continue; |
| 121 | } |
| 122 | role = property.Value.GetString().ToChatMessageRole(); |
| 123 | continue; |
| 124 | } |
| 125 | if (true) |
| 126 | { |
| 127 | rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); |
| 128 | } |
| 129 | } |
| 130 | serializedAdditionalRawData = rawDataDictionary; |
| 131 | return new InternalChatCompletionStreamResponseDelta(content ?? new ChangeTrackingList<ChatMessageContentPart>(), functionCall, toolCalls ?? new ChangeTrackingList<StreamingChatToolCallUpdate>(), role, serializedAdditionalRawData); |
| 132 | } |
| 133 | } |
| 134 | |