openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
OpenAI/src/Custom/Chat/Streaming/InternalCreateChatCompletionStreamResponseChoice.Serialization.cs
116lines · modecode
| 1 | using Microsoft.TypeSpec.Generator.Customizations; |
| 2 | using System; |
| 3 | using System.ClientModel.Primitives; |
| 4 | using System.Text; |
| 5 | using System.Text.Json; |
| 6 | |
| 7 | namespace OpenAI.Chat; |
| 8 | |
| 9 | [CodeGenSuppress("global::System.ClientModel.Primitives.IJsonModel<OpenAI.Chat.InternalCreateChatCompletionStreamResponseChoice>.Write", typeof(Utf8JsonWriter), typeof(ModelReaderWriterOptions))] |
| 10 | internal partial class InternalCreateChatCompletionStreamResponseChoice : IJsonModel<InternalCreateChatCompletionStreamResponseChoice> |
| 11 | { |
| 12 | // CUSTOM: |
| 13 | // - Made FinishReason nullable. |
| 14 | void IJsonModel<InternalCreateChatCompletionStreamResponseChoice>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 15 | => CustomSerializationHelpers.SerializeInstance(this, SerializeInternalCreateChatCompletionStreamResponseChoice, writer, options); |
| 16 | |
| 17 | internal static void SerializeInternalCreateChatCompletionStreamResponseChoice(InternalCreateChatCompletionStreamResponseChoice instance, Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 18 | { |
| 19 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 20 | if (instance.Patch.Contains("$"u8)) |
| 21 | { |
| 22 | writer.WriteRawValue(instance.Patch.GetJson("$"u8)); |
| 23 | return; |
| 24 | } |
| 25 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 26 | |
| 27 | writer.WriteStartObject(); |
| 28 | writer.WritePropertyName("delta"u8); |
| 29 | writer.WriteObjectValue(instance.Delta, options); |
| 30 | if (Optional.IsDefined(instance.Logprobs)) |
| 31 | { |
| 32 | if (instance.Logprobs != null) |
| 33 | { |
| 34 | writer.WritePropertyName("logprobs"u8); |
| 35 | writer.WriteObjectValue(instance.Logprobs, options); |
| 36 | } |
| 37 | else |
| 38 | { |
| 39 | writer.WriteNull("logprobs"); |
| 40 | } |
| 41 | } |
| 42 | if (Optional.IsDefined(instance.FinishReason)) |
| 43 | { |
| 44 | if (instance.FinishReason != null) |
| 45 | { |
| 46 | writer.WritePropertyName("finish_reason"u8); |
| 47 | writer.WriteStringValue(instance.FinishReason.Value.ToSerialString()); |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | writer.WriteNull("finish_reason"); |
| 52 | } |
| 53 | } |
| 54 | writer.WritePropertyName("index"u8); |
| 55 | writer.WriteNumberValue(instance.Index); |
| 56 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 57 | instance.Patch.WriteTo(writer); |
| 58 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 59 | writer.WriteEndObject(); |
| 60 | } |
| 61 | |
| 62 | internal static InternalCreateChatCompletionStreamResponseChoice DeserializeInternalCreateChatCompletionStreamResponseChoice(JsonElement element, BinaryData data, ModelReaderWriterOptions options = null) |
| 63 | { |
| 64 | options ??= ModelSerializationExtensions.WireOptions; |
| 65 | |
| 66 | if (element.ValueKind == JsonValueKind.Null) |
| 67 | { |
| 68 | return null; |
| 69 | } |
| 70 | InternalChatCompletionStreamResponseDelta delta = default; |
| 71 | InternalCreateChatCompletionStreamResponseChoiceLogprobs logprobs = default; |
| 72 | ChatFinishReason? finishReason = default; |
| 73 | int index = default; |
| 74 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 75 | JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory<byte>.Empty : data.ToMemory()); |
| 76 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 77 | foreach (var property in element.EnumerateObject()) |
| 78 | { |
| 79 | if (property.NameEquals("delta"u8)) |
| 80 | { |
| 81 | delta = InternalChatCompletionStreamResponseDelta.DeserializeInternalChatCompletionStreamResponseDelta(property.Value, property.Value.GetUtf8Bytes(), options); |
| 82 | continue; |
| 83 | } |
| 84 | if (property.NameEquals("logprobs"u8)) |
| 85 | { |
| 86 | if (property.Value.ValueKind == JsonValueKind.Null) |
| 87 | { |
| 88 | logprobs = null; |
| 89 | continue; |
| 90 | } |
| 91 | logprobs = InternalCreateChatCompletionStreamResponseChoiceLogprobs.DeserializeInternalCreateChatCompletionStreamResponseChoiceLogprobs(property.Value, property.Value.GetUtf8Bytes(), options); |
| 92 | continue; |
| 93 | } |
| 94 | if (property.NameEquals("finish_reason"u8)) |
| 95 | { |
| 96 | if (property.Value.ValueKind == JsonValueKind.Null) |
| 97 | { |
| 98 | finishReason = null; |
| 99 | continue; |
| 100 | } |
| 101 | finishReason = property.Value.GetString().ToChatFinishReason(); |
| 102 | continue; |
| 103 | } |
| 104 | if (property.NameEquals("index"u8)) |
| 105 | { |
| 106 | index = property.Value.GetInt32(); |
| 107 | continue; |
| 108 | } |
| 109 | if (true) |
| 110 | { |
| 111 | patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(property.Name)], property.Value.GetUtf8Bytes()); |
| 112 | } |
| 113 | } |
| 114 | return new InternalCreateChatCompletionStreamResponseChoice(delta: delta, logprobs: logprobs, index: index, finishReason: finishReason, patch: patch); |
| 115 | } |
| 116 | } |
| 117 | |