openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Embeddings/OpenAIEmbeddingCollection.Serialization.cs
87lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel.Primitives; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Text.Json; |
| 5 | |
| 6 | namespace OpenAI.Embeddings; |
| 7 | |
| 8 | [CodeGenSuppress("global::System.ClientModel.Primitives.IJsonModel<OpenAI.Embeddings.OpenAIEmbeddingCollection>.Write", typeof(Utf8JsonWriter), typeof(ModelReaderWriterOptions))] |
| 9 | public partial class OpenAIEmbeddingCollection : IJsonModel<OpenAIEmbeddingCollection> |
| 10 | { |
| 11 | // CUSTOM: |
| 12 | // - Serialized the Items property. |
| 13 | // - Recovered the deserialization of SerializedAdditionalRawData. See https://github.com/Azure/autorest.csharp/issues/4636. |
| 14 | void IJsonModel<OpenAIEmbeddingCollection>.Write(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 15 | => CustomSerializationHelpers.SerializeInstance(this, SerializeOpenAIEmbeddingCollection, writer, options); |
| 16 | |
| 17 | internal static void SerializeOpenAIEmbeddingCollection(OpenAIEmbeddingCollection instance, Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 18 | { |
| 19 | writer.WriteStartObject(); |
| 20 | writer.WritePropertyName("data"u8); |
| 21 | writer.WriteStartArray(); |
| 22 | foreach (var item in instance.Items) |
| 23 | { |
| 24 | writer.WriteObjectValue<OpenAIEmbedding>(item, options); |
| 25 | } |
| 26 | writer.WriteEndArray(); |
| 27 | writer.WritePropertyName("model"u8); |
| 28 | writer.WriteStringValue(instance.Model); |
| 29 | writer.WritePropertyName("object"u8); |
| 30 | writer.WriteStringValue(instance.Object.ToString()); |
| 31 | writer.WritePropertyName("usage"u8); |
| 32 | writer.WriteObjectValue<EmbeddingTokenUsage>(instance.Usage, options); |
| 33 | writer.WriteSerializedAdditionalRawData(instance._additionalBinaryDataProperties, options); |
| 34 | writer.WriteEndObject(); |
| 35 | } |
| 36 | |
| 37 | // CUSTOM: Recovered the deserialization of SerializedAdditionalRawData. See https://github.com/Azure/autorest.csharp/issues/4636. |
| 38 | internal static OpenAIEmbeddingCollection DeserializeOpenAIEmbeddingCollection(JsonElement element, ModelReaderWriterOptions options = null) |
| 39 | { |
| 40 | options ??= new ModelReaderWriterOptions("W"); |
| 41 | |
| 42 | if (element.ValueKind == JsonValueKind.Null) |
| 43 | { |
| 44 | return null; |
| 45 | } |
| 46 | IReadOnlyList<OpenAIEmbedding> data = default; |
| 47 | string model = default; |
| 48 | InternalCreateEmbeddingResponseObject @object = default; |
| 49 | EmbeddingTokenUsage usage = default; |
| 50 | IDictionary<string, BinaryData> serializedAdditionalRawData = default; |
| 51 | Dictionary<string, BinaryData> rawDataDictionary = new Dictionary<string, BinaryData>(); |
| 52 | foreach (var property in element.EnumerateObject()) |
| 53 | { |
| 54 | if (property.NameEquals("data"u8)) |
| 55 | { |
| 56 | List<OpenAIEmbedding> array = new List<OpenAIEmbedding>(); |
| 57 | foreach (var item in property.Value.EnumerateArray()) |
| 58 | { |
| 59 | array.Add(OpenAIEmbedding.DeserializeOpenAIEmbedding(item, options)); |
| 60 | } |
| 61 | data = array; |
| 62 | continue; |
| 63 | } |
| 64 | if (property.NameEquals("model"u8)) |
| 65 | { |
| 66 | model = property.Value.GetString(); |
| 67 | continue; |
| 68 | } |
| 69 | if (property.NameEquals("object"u8)) |
| 70 | { |
| 71 | @object = new InternalCreateEmbeddingResponseObject(property.Value.GetString()); |
| 72 | continue; |
| 73 | } |
| 74 | if (property.NameEquals("usage"u8)) |
| 75 | { |
| 76 | usage = EmbeddingTokenUsage.DeserializeEmbeddingTokenUsage(property.Value, options); |
| 77 | continue; |
| 78 | } |
| 79 | if (true) |
| 80 | { |
| 81 | rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText())); |
| 82 | } |
| 83 | } |
| 84 | serializedAdditionalRawData = rawDataDictionary; |
| 85 | return new OpenAIEmbeddingCollection(data, model, @object, usage, serializedAdditionalRawData); |
| 86 | } |
| 87 | } |
| 88 | |