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