openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Chat/Internal/InternalCreateChatCompletionStreamResponseChoice.Serialization.cs
105lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel.Primitives; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Text.Json; |
| 5 | |
| 6 | namespace OpenAI.Chat; |
| 7 | |
| 8 | [CodeGenSuppress("global::System.ClientModel.Primitives.IJsonModel<OpenAI.Chat.InternalCreateChatCompletionStreamResponseChoice>.Write", typeof(Utf8JsonWriter), typeof(ModelReaderWriterOptions))] |
| 9 | internal partial class InternalCreateChatCompletionStreamResponseChoice : IJsonModel<InternalCreateChatCompletionStreamResponseChoice> |
| 10 | { |
| 11 | // CUSTOM: |
| 12 | // - Made FinishReason nullable. |
| 13 | void IJsonModel<InternalCreateChatCompletionStreamResponseChoice>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 14 | => CustomSerializationHelpers.SerializeInstance(this, SerializeInternalCreateChatCompletionStreamResponseChoice, writer, options); |
| 15 | |
| 16 | internal static void SerializeInternalCreateChatCompletionStreamResponseChoice(InternalCreateChatCompletionStreamResponseChoice instance, Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 17 | { |
| 18 | writer.WriteStartObject(); |
| 19 | writer.WritePropertyName("delta"u8); |
| 20 | writer.WriteObjectValue(instance.Delta, options); |
| 21 | if (Optional.IsDefined(instance.Logprobs)) |
| 22 | { |
| 23 | if (instance.Logprobs != null) |
| 24 | { |
| 25 | writer.WritePropertyName("logprobs"u8); |
| 26 | writer.WriteObjectValue(instance.Logprobs, options); |
| 27 | } |
| 28 | else |
| 29 | { |
| 30 | writer.WriteNull("logprobs"); |
| 31 | } |
| 32 | } |
| 33 | if (Optional.IsDefined(instance.FinishReason)) |
| 34 | { |
| 35 | if (instance.FinishReason != null) |
| 36 | { |
| 37 | writer.WritePropertyName("finish_reason"u8); |
| 38 | writer.WriteStringValue(instance.FinishReason.Value.ToSerialString()); |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | writer.WriteNull("finish_reason"); |
| 43 | } |
| 44 | } |
| 45 | writer.WritePropertyName("index"u8); |
| 46 | writer.WriteNumberValue(instance.Index); |
| 47 | writer.WriteSerializedAdditionalRawData(instance._serializedAdditionalRawData, options); |
| 48 | writer.WriteEndObject(); |
| 49 | } |
| 50 | |
| 51 | internal static InternalCreateChatCompletionStreamResponseChoice DeserializeInternalCreateChatCompletionStreamResponseChoice(JsonElement element, ModelReaderWriterOptions options = null) |
| 52 | { |
| 53 | options ??= ModelSerializationExtensions.WireOptions; |
| 54 | |
| 55 | if (element.ValueKind == JsonValueKind.Null) |
| 56 | { |
| 57 | return null; |
| 58 | } |
| 59 | InternalChatCompletionStreamResponseDelta delta = default; |
| 60 | InternalCreateChatCompletionStreamResponseChoiceLogprobs logprobs = default; |
| 61 | ChatFinishReason? finishReason = default; |
| 62 | int index = default; |
| 63 | IDictionary<string, BinaryData> serializedAdditionalRawData = default; |
| 64 | Dictionary<string, BinaryData> rawDataDictionary = new Dictionary<string, BinaryData>(); |
| 65 | foreach (var property in element.EnumerateObject()) |
| 66 | { |
| 67 | if (property.NameEquals("delta"u8)) |
| 68 | { |
| 69 | delta = InternalChatCompletionStreamResponseDelta.DeserializeInternalChatCompletionStreamResponseDelta(property.Value, options); |
| 70 | continue; |
| 71 | } |
| 72 | if (property.NameEquals("logprobs"u8)) |
| 73 | { |
| 74 | if (property.Value.ValueKind == JsonValueKind.Null) |
| 75 | { |
| 76 | logprobs = null; |
| 77 | continue; |
| 78 | } |
| 79 | logprobs = InternalCreateChatCompletionStreamResponseChoiceLogprobs.DeserializeInternalCreateChatCompletionStreamResponseChoiceLogprobs(property.Value, options); |
| 80 | continue; |
| 81 | } |
| 82 | if (property.NameEquals("finish_reason"u8)) |
| 83 | { |
| 84 | if (property.Value.ValueKind == JsonValueKind.Null) |
| 85 | { |
| 86 | finishReason = null; |
| 87 | continue; |
| 88 | } |
| 89 | finishReason = property.Value.GetString().ToChatFinishReason(); |
| 90 | continue; |
| 91 | } |
| 92 | if (property.NameEquals("index"u8)) |
| 93 | { |
| 94 | index = property.Value.GetInt32(); |
| 95 | continue; |
| 96 | } |
| 97 | if (true) |
| 98 | { |
| 99 | rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); |
| 100 | } |
| 101 | } |
| 102 | serializedAdditionalRawData = rawDataDictionary; |
| 103 | return new InternalCreateChatCompletionStreamResponseChoice(delta, logprobs, finishReason, index, serializedAdditionalRawData); |
| 104 | } |
| 105 | } |
| 106 | |