openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
OpenAI/src/Generated/Models/Chat/ChatCompletionCollectionOptions.Serialization.cs
117lines · 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.Text; |
| 9 | using System.Text.Json; |
| 10 | using OpenAI; |
| 11 | |
| 12 | namespace OpenAI.Chat |
| 13 | { |
| 14 | public partial class ChatCompletionCollectionOptions : IJsonModel<ChatCompletionCollectionOptions> |
| 15 | { |
| 16 | protected virtual ChatCompletionCollectionOptions PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 17 | { |
| 18 | string format = options.Format == "W" ? ((IPersistableModel<ChatCompletionCollectionOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 19 | switch (format) |
| 20 | { |
| 21 | case "J": |
| 22 | using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions)) |
| 23 | { |
| 24 | return DeserializeChatCompletionCollectionOptions(document.RootElement, data, options); |
| 25 | } |
| 26 | default: |
| 27 | throw new FormatException($"The model {nameof(ChatCompletionCollectionOptions)} does not support reading '{options.Format}' format."); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 32 | { |
| 33 | string format = options.Format == "W" ? ((IPersistableModel<ChatCompletionCollectionOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 34 | switch (format) |
| 35 | { |
| 36 | case "J": |
| 37 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 38 | default: |
| 39 | throw new FormatException($"The model {nameof(ChatCompletionCollectionOptions)} does not support writing '{options.Format}' format."); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | BinaryData IPersistableModel<ChatCompletionCollectionOptions>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 44 | |
| 45 | ChatCompletionCollectionOptions IPersistableModel<ChatCompletionCollectionOptions>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 46 | |
| 47 | string IPersistableModel<ChatCompletionCollectionOptions>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 48 | |
| 49 | void IJsonModel<ChatCompletionCollectionOptions>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 50 | { |
| 51 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 52 | if (Patch.Contains("$"u8)) |
| 53 | { |
| 54 | writer.WriteRawValue(Patch.GetJson("$"u8)); |
| 55 | return; |
| 56 | } |
| 57 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 58 | |
| 59 | writer.WriteStartObject(); |
| 60 | JsonModelWriteCore(writer, options); |
| 61 | writer.WriteEndObject(); |
| 62 | } |
| 63 | |
| 64 | protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 65 | { |
| 66 | string format = options.Format == "W" ? ((IPersistableModel<ChatCompletionCollectionOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 67 | if (format != "J") |
| 68 | { |
| 69 | throw new FormatException($"The model {nameof(ChatCompletionCollectionOptions)} does not support writing '{format}' format."); |
| 70 | } |
| 71 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 72 | |
| 73 | Patch.WriteTo(writer); |
| 74 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 75 | } |
| 76 | |
| 77 | ChatCompletionCollectionOptions IJsonModel<ChatCompletionCollectionOptions>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 78 | |
| 79 | protected virtual ChatCompletionCollectionOptions JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 80 | { |
| 81 | string format = options.Format == "W" ? ((IPersistableModel<ChatCompletionCollectionOptions>)this).GetFormatFromOptions(options) : options.Format; |
| 82 | if (format != "J") |
| 83 | { |
| 84 | throw new FormatException($"The model {nameof(ChatCompletionCollectionOptions)} does not support reading '{format}' format."); |
| 85 | } |
| 86 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 87 | return DeserializeChatCompletionCollectionOptions(document.RootElement, null, options); |
| 88 | } |
| 89 | |
| 90 | internal static ChatCompletionCollectionOptions DeserializeChatCompletionCollectionOptions(JsonElement element, BinaryData data, ModelReaderWriterOptions options) |
| 91 | { |
| 92 | if (element.ValueKind == JsonValueKind.Null) |
| 93 | { |
| 94 | return null; |
| 95 | } |
| 96 | string afterId = default; |
| 97 | int? pageSizeLimit = default; |
| 98 | ChatCompletionCollectionOrder? order = default; |
| 99 | IDictionary<string, string> metadata = default; |
| 100 | string model = default; |
| 101 | #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 102 | JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory<byte>.Empty : data.ToMemory()); |
| 103 | #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. |
| 104 | foreach (var prop in element.EnumerateObject()) |
| 105 | { |
| 106 | patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(prop.Name)], prop.Value.GetUtf8Bytes()); |
| 107 | } |
| 108 | return new ChatCompletionCollectionOptions( |
| 109 | afterId, |
| 110 | pageSizeLimit, |
| 111 | order, |
| 112 | metadata ?? new ChangeTrackingDictionary<string, string>(), |
| 113 | model, |
| 114 | patch); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |