openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Chat/Internal/InternalChatCompletionResponseMessage.Serialization.cs
136lines · 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.InternalChatCompletionResponseMessage>.Write", typeof(Utf8JsonWriter), typeof(ModelReaderWriterOptions))] |
| 10 | internal partial class InternalChatCompletionResponseMessage : IJsonModel<InternalChatCompletionResponseMessage> |
| 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<InternalChatCompletionResponseMessage>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 25 | => CustomSerializationHelpers.SerializeInstance(this, SerializeInternalChatCompletionResponseMessage, writer, options); |
| 26 | |
| 27 | internal static void SerializeInternalChatCompletionResponseMessage(InternalChatCompletionResponseMessage 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.IsCollectionDefined(instance.ToolCalls)) |
| 43 | { |
| 44 | writer.WritePropertyName("tool_calls"u8); |
| 45 | writer.WriteStartArray(); |
| 46 | foreach (var item in instance.ToolCalls) |
| 47 | { |
| 48 | writer.WriteObjectValue(item, options); |
| 49 | } |
| 50 | writer.WriteEndArray(); |
| 51 | } |
| 52 | writer.WritePropertyName("role"u8); |
| 53 | writer.WriteStringValue(instance.Role.ToSerialString()); |
| 54 | if (Optional.IsDefined(instance.FunctionCall)) |
| 55 | { |
| 56 | writer.WritePropertyName("function_call"u8); |
| 57 | writer.WriteObjectValue<ChatFunctionCall>(instance.FunctionCall, options); |
| 58 | } |
| 59 | writer.WriteSerializedAdditionalRawData(instance.SerializedAdditionalRawData, options); |
| 60 | writer.WriteEndObject(); |
| 61 | } |
| 62 | |
| 63 | internal static InternalChatCompletionResponseMessage DeserializeInternalChatCompletionResponseMessage(JsonElement element, ModelReaderWriterOptions options = null) |
| 64 | { |
| 65 | options ??= ModelSerializationExtensions.WireOptions; |
| 66 | |
| 67 | if (element.ValueKind == JsonValueKind.Null) |
| 68 | { |
| 69 | return null; |
| 70 | } |
| 71 | IReadOnlyList<ChatMessageContentPart> content = default; |
| 72 | string refusal = default; |
| 73 | IReadOnlyList<ChatToolCall> toolCalls = default; |
| 74 | ChatMessageRole role = default; |
| 75 | ChatFunctionCall functionCall = default; |
| 76 | IDictionary<string, BinaryData> serializedAdditionalRawData = default; |
| 77 | Dictionary<string, BinaryData> rawDataDictionary = new Dictionary<string, BinaryData>(); |
| 78 | foreach (var property in element.EnumerateObject()) |
| 79 | { |
| 80 | if (property.NameEquals("content"u8)) |
| 81 | { |
| 82 | if (property.Value.ValueKind == JsonValueKind.Null) |
| 83 | { |
| 84 | continue; |
| 85 | } |
| 86 | List<ChatMessageContentPart> array = new List<ChatMessageContentPart>(); |
| 87 | array.Add(ChatMessageContentPart.CreateTextPart(property.Value.GetString())); |
| 88 | content = array; |
| 89 | continue; |
| 90 | } |
| 91 | if (property.NameEquals("refusal"u8)) |
| 92 | { |
| 93 | if (property.Value.ValueKind == JsonValueKind.Null) |
| 94 | { |
| 95 | continue; |
| 96 | } |
| 97 | refusal = property.Value.GetString(); |
| 98 | continue; |
| 99 | } |
| 100 | if (property.NameEquals("tool_calls"u8)) |
| 101 | { |
| 102 | if (property.Value.ValueKind == JsonValueKind.Null) |
| 103 | { |
| 104 | continue; |
| 105 | } |
| 106 | List<ChatToolCall> array = new List<ChatToolCall>(); |
| 107 | foreach (var item in property.Value.EnumerateArray()) |
| 108 | { |
| 109 | array.Add(ChatToolCall.DeserializeChatToolCall(item, options)); |
| 110 | } |
| 111 | toolCalls = array; |
| 112 | continue; |
| 113 | } |
| 114 | if (property.NameEquals("role"u8)) |
| 115 | { |
| 116 | role = property.Value.GetString().ToChatMessageRole(); |
| 117 | continue; |
| 118 | } |
| 119 | if (property.NameEquals("function_call"u8)) |
| 120 | { |
| 121 | if (property.Value.ValueKind == JsonValueKind.Null) |
| 122 | { |
| 123 | continue; |
| 124 | } |
| 125 | functionCall = ChatFunctionCall.DeserializeChatFunctionCall(property.Value, options); |
| 126 | continue; |
| 127 | } |
| 128 | if (true) |
| 129 | { |
| 130 | rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); |
| 131 | } |
| 132 | } |
| 133 | serializedAdditionalRawData = rawDataDictionary; |
| 134 | return new InternalChatCompletionResponseMessage(content ?? new ChangeTrackingList<ChatMessageContentPart>(), refusal, toolCalls ?? new ChangeTrackingList<ChatToolCall>(), role, functionCall, serializedAdditionalRawData); |
| 135 | } |
| 136 | } |
| 137 | |