openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.12

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/Custom/Chat/ChatCompletionOptions.Serialization.cs

98lines · modeblame

58f93c8dShivangiReja2 years ago1using System.ClientModel.Primitives;
0c0bc808Jose Arriaga Maldonado2 years ago2using System.Collections.Generic;
22ab606bStephen Toub2 years ago3using System.Globalization;
9f9f2936Jose Arriaga Maldonado2 years ago4using System.Runtime.CompilerServices;
5using System.Text.Json;
6
7namespace OpenAI.Chat;
8
9public partial class ChatCompletionOptions
10{
2ab1a942Jose Arriaga Maldonado1 years ago11// 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)]
14private void SerializeMessagesValue(Utf8JsonWriter writer, ModelReaderWriterOptions options)
15{
16if (Messages is not null)
17{
18writer.WriteStartArray();
19foreach (var item in Messages)
20{
21writer.WriteObjectValue<ChatMessage>(item, options);
22}
23writer.WriteEndArray();
24}
25else
26{
27writer.WriteNullValue();
28}
29}
30
9f9f2936Jose Arriaga Maldonado2 years ago31// 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 ago33private void SerializeStopSequencesValue(Utf8JsonWriter writer, ModelReaderWriterOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago34{
35writer.WriteStartArray();
36foreach (var item in StopSequences)
37{
38writer.WriteStringValue(item);
39}
40writer.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)]
45private static void DeserializeStopSequencesValue(JsonProperty property, ref IList<string> stop)
46{
47if (property.Value.ValueKind == JsonValueKind.Null)
48{
49stop = null;
50}
51else if (property.Value.ValueKind == JsonValueKind.String)
52{
53List<string> array = [property.Value.GetString()];
54stop = array;
55}
56else
57{
58List<string> array = [];
59foreach (var item in property.Value.EnumerateArray())
60{
61array.Add(item.GetString());
62}
63stop = 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 ago69private void SerializeLogitBiasesValue(Utf8JsonWriter writer, ModelReaderWriterOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago70{
71writer.WriteStartObject();
72foreach (var item in LogitBiases)
73{
22ab606bStephen Toub2 years ago74writer.WritePropertyName(item.Key.ToString(CultureInfo.InvariantCulture));
9f9f2936Jose Arriaga Maldonado2 years ago75writer.WriteNumberValue(item.Value);
76}
77writer.WriteEndObject();
78}
79
80// CUSTOM: Added custom serialization to represent tokens as integers instead of strings.
81[MethodImpl(MethodImplOptions.AggressiveInlining)]
82private static void DeserializeLogitBiasesValue(JsonProperty property, ref IDictionary<int, int> logitBias)
83{
84if (property.Value.ValueKind == JsonValueKind.Null)
85{
86logitBias = null;
87}
88else
89{
90Dictionary<int, int> dictionary = new Dictionary<int, int>();
91foreach (var property0 in property.Value.EnumerateObject())
92{
22ab606bStephen Toub2 years ago93dictionary.Add(int.Parse(property0.Name, CultureInfo.InvariantCulture), property0.Value.GetInt32());
9f9f2936Jose Arriaga Maldonado2 years ago94}
95logitBias = dictionary;
96}
97}
98}