openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Models/Embeddings/OpenAIEmbeddingCollection.Serialization.cs
109lines · modecode
| 1 | // <auto-generated/> |
| 2 | |
| 3 | #nullable disable |
| 4 | |
| 5 | using System; |
| 6 | using System.ClientModel.Primitives; |
| 7 | using System.Diagnostics.CodeAnalysis; |
| 8 | using System.Text.Json; |
| 9 | using OpenAI; |
| 10 | |
| 11 | namespace OpenAI.Embeddings |
| 12 | { |
| 13 | public partial class OpenAIEmbeddingCollection : IJsonModel<OpenAIEmbeddingCollection> |
| 14 | { |
| 15 | [Experimental("OPENAI001")] |
| 16 | protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 17 | { |
| 18 | string format = options.Format == "W" ? ((IPersistableModel<OpenAIEmbeddingCollection>)this).GetFormatFromOptions(options) : options.Format; |
| 19 | if (format != "J") |
| 20 | { |
| 21 | throw new FormatException($"The model {nameof(OpenAIEmbeddingCollection)} does not support writing '{format}' format."); |
| 22 | } |
| 23 | if (_additionalBinaryDataProperties?.ContainsKey("model") != true) |
| 24 | { |
| 25 | writer.WritePropertyName("model"u8); |
| 26 | writer.WriteStringValue(Model); |
| 27 | } |
| 28 | if (_additionalBinaryDataProperties?.ContainsKey("usage") != true) |
| 29 | { |
| 30 | writer.WritePropertyName("usage"u8); |
| 31 | writer.WriteObjectValue(Usage, options); |
| 32 | } |
| 33 | if (_additionalBinaryDataProperties?.ContainsKey("object") != true) |
| 34 | { |
| 35 | writer.WritePropertyName("object"u8); |
| 36 | writer.WriteStringValue(Object); |
| 37 | } |
| 38 | // Plugin customization: remove options.Format != "W" check |
| 39 | if (_additionalBinaryDataProperties != null) |
| 40 | { |
| 41 | foreach (var item in _additionalBinaryDataProperties) |
| 42 | { |
| 43 | if (ModelSerializationExtensions.IsSentinelValue(item.Value)) |
| 44 | { |
| 45 | continue; |
| 46 | } |
| 47 | writer.WritePropertyName(item.Key); |
| 48 | #if NET6_0_OR_GREATER |
| 49 | writer.WriteRawValue(item.Value); |
| 50 | #else |
| 51 | using (JsonDocument document = JsonDocument.Parse(item.Value)) |
| 52 | { |
| 53 | JsonSerializer.Serialize(writer, document.RootElement); |
| 54 | } |
| 55 | #endif |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | OpenAIEmbeddingCollection IJsonModel<OpenAIEmbeddingCollection>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => JsonModelCreateCore(ref reader, options); |
| 61 | |
| 62 | [Experimental("OPENAI001")] |
| 63 | protected virtual OpenAIEmbeddingCollection JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options) |
| 64 | { |
| 65 | string format = options.Format == "W" ? ((IPersistableModel<OpenAIEmbeddingCollection>)this).GetFormatFromOptions(options) : options.Format; |
| 66 | if (format != "J") |
| 67 | { |
| 68 | throw new FormatException($"The model {nameof(OpenAIEmbeddingCollection)} does not support reading '{format}' format."); |
| 69 | } |
| 70 | using JsonDocument document = JsonDocument.ParseValue(ref reader); |
| 71 | return DeserializeOpenAIEmbeddingCollection(document.RootElement, options); |
| 72 | } |
| 73 | |
| 74 | BinaryData IPersistableModel<OpenAIEmbeddingCollection>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options); |
| 75 | |
| 76 | [Experimental("OPENAI001")] |
| 77 | protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options) |
| 78 | { |
| 79 | string format = options.Format == "W" ? ((IPersistableModel<OpenAIEmbeddingCollection>)this).GetFormatFromOptions(options) : options.Format; |
| 80 | switch (format) |
| 81 | { |
| 82 | case "J": |
| 83 | return ModelReaderWriter.Write(this, options, OpenAIContext.Default); |
| 84 | default: |
| 85 | throw new FormatException($"The model {nameof(OpenAIEmbeddingCollection)} does not support writing '{options.Format}' format."); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | OpenAIEmbeddingCollection IPersistableModel<OpenAIEmbeddingCollection>.Create(BinaryData data, ModelReaderWriterOptions options) => PersistableModelCreateCore(data, options); |
| 90 | |
| 91 | [Experimental("OPENAI001")] |
| 92 | protected virtual OpenAIEmbeddingCollection PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options) |
| 93 | { |
| 94 | string format = options.Format == "W" ? ((IPersistableModel<OpenAIEmbeddingCollection>)this).GetFormatFromOptions(options) : options.Format; |
| 95 | switch (format) |
| 96 | { |
| 97 | case "J": |
| 98 | using (JsonDocument document = JsonDocument.Parse(data)) |
| 99 | { |
| 100 | return DeserializeOpenAIEmbeddingCollection(document.RootElement, options); |
| 101 | } |
| 102 | default: |
| 103 | throw new FormatException($"The model {nameof(OpenAIEmbeddingCollection)} does not support reading '{options.Format}' format."); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | string IPersistableModel<OpenAIEmbeddingCollection>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J"; |
| 108 | } |
| 109 | } |
| 110 | |