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