openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
achandmsft-patch-1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Chat/ChatCompletionOptions.Serialization.cs

91lines · 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 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