openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/Embeddings/EmbeddingTokenUsage.Serialization.cs
143lines · 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.Diagnostics.CodeAnalysis; |
| 9 | using System.Text.Json; |
| 10 | using OpenAI; |
| 11 | |
| 12 | namespace OpenAI.Embeddings |
| 13 | { |
| 14 | public partial class EmbeddingTokenUsage : IJsonModel<EmbeddingTokenUsage> |
| 15 | { |
| 16 | internal EmbeddingTokenUsage() |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | void IJsonModel<EmbeddingTokenUsage>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 21 | { |
| 22 | writer.WriteStartObject(); |
| 23 | JsonModelWriteCore(writer, options); |
| 24 | writer.WriteEndObject(); |
| 25 | } |
| 26 | |
| 27 | [Experimental("OPENAI001")] |
| 28 | protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 29 | { |
| 30 | string format = options.Format == "W" ? ((IPersistableModel<EmbeddingTokenUsage>)this).GetFormatFromOptions(options) : options.Format; |
| 31 | if (format != "J") |
| 32 | { |
| 33 | throw new FormatException($"The model {nameof(EmbeddingTokenUsage)} does not support writing '{format}' format."); |
| 34 | } |
| 35 | if (_additionalBinaryDataProperties?.ContainsKey("prompt_tokens") != true) |
| 36 | { |
| 37 | writer.WritePropertyName("prompt_tokens"u8); |
| 38 | writer.WriteNumberValue(InputTokenCount); |
| 39 | } |
| 40 | if (_additionalBinaryDataProperties?.ContainsKey("total_tokens") != true) |
| 41 | { |
| 42 | writer.WritePropertyName("total_tokens"u8); |
| 43 | writer.WriteNumberValue(TotalTokenCount); |
| 44 | } |
| 45 | // Plugin customization: remove options.Format != "W" check |
| 46 | if (_additionalBinaryDataProperties != null) |
| 47 | { |
| 48 | foreach (var item in _additionalBinaryDataProperties) |
| 49 | { |
| 50 | if (ModelSerializationExtensions.IsSentinelValue(item.Value)) |
| 51 | { |
| 52 | continue; |
| 53 | } |
| 54 | writer.WritePropertyName(item.Key); |
| 55 | #if NET6_0_OR_GREATER |
| 56 | writer.WriteRawValue(item.Value); |
| 57 | #else |
| 58 | using (JsonDocument document = JsonDocument.Parse(item.Value)) |
| 59 | { |
| 60 | JsonSerializer.Serialize(writer, document.RootElement); |
| 61 | } |
| 62 | #endif |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | EmbeddingTokenUsage IJsonModel<EmbeddingTokenUsage>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 68 | |
| 69 | [Experimental("OPENAI001")] |
| 70 | protected virtual EmbeddingTokenUsage JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 71 | { |
| 72 | string format = options.Format == "W" ? ((IPersistableModel<EmbeddingTokenUsage>)this).GetFormatFromOptions(options) : options.Format; |
| 73 | if (format != "J") |
| 74 | { |
| 75 | throw new FormatException($"The model {nameof(EmbeddingTokenUsage)} does not support reading '{format}' format."); |
| 76 | } |
| 77 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 78 | return DeserializeEmbeddingTokenUsage(document.RootElement, options); |
| 79 | } |
| 80 | |
| 81 | internal static EmbeddingTokenUsage DeserializeEmbeddingTokenUsage(JsonElement element, ModelReaderWriterOptions options) |
| 82 | { |
| 83 | if (element.ValueKind == JsonValueKind.Null) |
| 84 | { |
| 85 | return null; |
| 86 | } |
| 87 | int inputTokenCount = default; |
| 88 | int totalTokenCount = default; |
| 89 | IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>(); |
| 90 | foreach (var prop in element.EnumerateObject()) |
| 91 | { |
| 92 | if (prop.NameEquals("prompt_tokens"u8)) |
| 93 | { |
| 94 | inputTokenCount = prop.Value.GetInt32(); |
| 95 | continue; |
| 96 | } |
| 97 | if (prop.NameEquals("total_tokens"u8)) |
| 98 | { |
| 99 | totalTokenCount = prop.Value.GetInt32(); |
| 100 | continue; |
| 101 | } |
| 102 | // Plugin customization: remove options.Format != "W" check |
| 103 | additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText())); |
| 104 | } |
| 105 | return new EmbeddingTokenUsage(inputTokenCount, totalTokenCount, additionalBinaryDataProperties); |
| 106 | } |
| 107 | |
| 108 | BinaryData IPersistableModel<EmbeddingTokenUsage>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 109 | |
| 110 | [Experimental("OPENAI001")] |
| 111 | protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 112 | { |
| 113 | string format = options.Format == "W" ? ((IPersistableModel<EmbeddingTokenUsage>)this).GetFormatFromOptions(options) : options.Format; |
| 114 | switch (format) |
| 115 | { |
| 116 | case "J": |
| 117 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 118 | default: |
| 119 | throw new FormatException($"The model {nameof(EmbeddingTokenUsage)} does not support writing '{options.Format}' format."); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | EmbeddingTokenUsage IPersistableModel<EmbeddingTokenUsage>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 124 | |
| 125 | [Experimental("OPENAI001")] |
| 126 | protected virtual EmbeddingTokenUsage PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 127 | { |
| 128 | string format = options.Format == "W" ? ((IPersistableModel<EmbeddingTokenUsage>)this).GetFormatFromOptions(options) : options.Format; |
| 129 | switch (format) |
| 130 | { |
| 131 | case "J": |
| 132 | using (JsonDocument document = JsonDocument.Parse(data)) |
| 133 | { |
| 134 | return DeserializeEmbeddingTokenUsage(document.RootElement, options); |
| 135 | } |
| 136 | default: |
| 137 | throw new FormatException($"The model {nameof(EmbeddingTokenUsage)} does not support reading '{options.Format}' format."); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | string IPersistableModel<EmbeddingTokenUsage>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 142 | } |
| 143 | } |
| 144 | |