openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Chat/ChatCompletionOptions.Serialization.cs
91lines · modecode
| 1 | using System.ClientModel.Primitives; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Globalization; |
| 4 | using System.Runtime.CompilerServices; |
| 5 | using System.Text.Json; |
| 6 | |
| 7 | namespace OpenAI.Chat; |
| 8 | |
| 9 | public partial class ChatCompletionOptions |
| 10 | { |
| 11 | // CUSTOM: Added custom serialization to circumvent serialization failure of required 'messages', which is moved |
| 12 | // to a method parameter and should not block object serialization validity. |
| 13 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 14 | private void SerializeMessagesValue(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 15 | { |
| 16 | writer.WriteStartArray(); |
| 17 | foreach (var item in Messages) |
| 18 | { |
| 19 | writer.WriteObjectValue<ChatMessage>(item, options); |
| 20 | } |
| 21 | writer.WriteEndArray(); |
| 22 | } |
| 23 | |
| 24 | // CUSTOM: Added custom serialization to treat a single string as a collection of strings with one item. |
| 25 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 26 | private void SerializeStopSequencesValue(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 27 | { |
| 28 | writer.WriteStartArray(); |
| 29 | foreach (var item in StopSequences) |
| 30 | { |
| 31 | writer.WriteStringValue(item); |
| 32 | } |
| 33 | writer.WriteEndArray(); |
| 34 | } |
| 35 | |
| 36 | // CUSTOM: Added custom serialization to treat a single string as a collection of strings with one item. |
| 37 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 38 | private static void DeserializeStopSequencesValue(JsonProperty property, ref IList<string> stop) |
| 39 | { |
| 40 | if (property.Value.ValueKind == JsonValueKind.Null) |
| 41 | { |
| 42 | stop = null; |
| 43 | } |
| 44 | else if (property.Value.ValueKind == JsonValueKind.String) |
| 45 | { |
| 46 | List<string> array = [property.Value.GetString()]; |
| 47 | stop = array; |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | List<string> array = []; |
| 52 | foreach (var item in property.Value.EnumerateArray()) |
| 53 | { |
| 54 | array.Add(item.GetString()); |
| 55 | } |
| 56 | stop = array; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // CUSTOM: Added custom serialization to represent tokens as integers instead of strings. |
| 61 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 62 | private void SerializeLogitBiasesValue(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 63 | { |
| 64 | writer.WriteStartObject(); |
| 65 | foreach (var item in LogitBiases) |
| 66 | { |
| 67 | writer.WritePropertyName(item.Key.ToString(CultureInfo.InvariantCulture)); |
| 68 | writer.WriteNumberValue(item.Value); |
| 69 | } |
| 70 | writer.WriteEndObject(); |
| 71 | } |
| 72 | |
| 73 | // CUSTOM: Added custom serialization to represent tokens as integers instead of strings. |
| 74 | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 75 | private static void DeserializeLogitBiasesValue(JsonProperty property, ref IDictionary<int, int> logitBias) |
| 76 | { |
| 77 | if (property.Value.ValueKind == JsonValueKind.Null) |
| 78 | { |
| 79 | logitBias = null; |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | Dictionary<int, int> dictionary = new Dictionary<int, int>(); |
| 84 | foreach (var property0 in property.Value.EnumerateObject()) |
| 85 | { |
| 86 | dictionary.Add(int.Parse(property0.Name, CultureInfo.InvariantCulture), property0.Value.GetInt32()); |
| 87 | } |
| 88 | logitBias = dictionary; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |