openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.1.0-beta.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Chat/ChatCompletionOptions.Serialization.cs

98lines · modecode

1using System.ClientModel.Primitives;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Runtime.CompilerServices;
5using System.Text.Json;
6
7namespace OpenAI.Chat;
8
9public 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 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
31 // CUSTOM: Added custom serialization to treat a single string as a collection of strings with one item.
32 [MethodImpl(MethodImplOptions.AggressiveInlining)]
33 private void SerializeStopSequencesValue(Utf8JsonWriter writer, ModelReaderWriterOptions options)
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)]
69 private void SerializeLogitBiasesValue(Utf8JsonWriter writer, ModelReaderWriterOptions options)
70 {
71 writer.WriteStartObject();
72 foreach (var item in LogitBiases)
73 {
74 writer.WritePropertyName(item.Key.ToString(CultureInfo.InvariantCulture));
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 {
93 dictionary.Add(int.Parse(property0.Name, CultureInfo.InvariantCulture), property0.Value.GetInt32());
94 }
95 logitBias = dictionary;
96 }
97 }
98}
99