openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
mailinhphan/clientOptions

Branches

Tags

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

Clone

HTTPS

Download ZIP

OpenAI/src/Generated/Models/Chat/AssistantChatMessage.Serialization.cs

304lines · modecode

1// <auto-generated/>
2
3#nullable disable
4
5using System;
6using System.ClientModel.Primitives;
7using System.Collections.Generic;
8using System.Diagnostics.CodeAnalysis;
9using System.Text;
10using System.Text.Json;
11using OpenAI;
12
13namespace OpenAI.Chat
14{
15 public partial class AssistantChatMessage : ChatMessage, IJsonModel<AssistantChatMessage>
16 {
17 [Experimental("OPENAI001")]
18 protected override ChatMessage PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
19 {
20 string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format;
21 switch (format)
22 {
23 case "J":
24 using (JsonDocument document = JsonDocument.Parse(data, ModelSerializationExtensions.JsonDocumentOptions))
25 {
26 return DeserializeAssistantChatMessage(document.RootElement, data, options);
27 }
28 default:
29 throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support reading '{options.Format}' format.");
30 }
31 }
32
33 [Experimental("OPENAI001")]
34 protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
35 {
36 string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format;
37 switch (format)
38 {
39 case "J":
40 return ModelReaderWriter.Write(this, options, OpenAIContext.Default);
41 default:
42 throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support writing '{options.Format}' format.");
43 }
44 }
45
46 BinaryData IPersistableModel<AssistantChatMessage>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);
47
48 AssistantChatMessage IPersistableModel<AssistantChatMessage>.Create(BinaryData data, ModelReaderWriterOptions options) => (AssistantChatMessage)PersistableModelCreateCore(data, options);
49
50 string IPersistableModel<AssistantChatMessage>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
51
52 [Experimental("OPENAI001")]
53 protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
54 {
55 string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format;
56 if (format != "J")
57 {
58 throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support writing '{format}' format.");
59 }
60 base.JsonModelWriteCore(writer, options);
61#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
62 if (Optional.IsDefined(Refusal) && !Patch.Contains("$.refusal"u8))
63 {
64 writer.WritePropertyName("refusal"u8);
65 writer.WriteStringValue(Refusal);
66 }
67 if (Optional.IsDefined(ParticipantName) && !Patch.Contains("$.name"u8))
68 {
69 writer.WritePropertyName("name"u8);
70 writer.WriteStringValue(ParticipantName);
71 }
72 if (Optional.IsDefined(OutputAudioReference) && !Patch.Contains("$.audio"u8))
73 {
74 writer.WritePropertyName("audio"u8);
75 writer.WriteObjectValue(OutputAudioReference, options);
76 }
77 if (Patch.Contains("$.tool_calls"u8))
78 {
79 if (!Patch.IsRemoved("$.tool_calls"u8))
80 {
81 writer.WritePropertyName("tool_calls"u8);
82 writer.WriteRawValue(Patch.GetJson("$.tool_calls"u8));
83 }
84 }
85 else if (Optional.IsCollectionDefined(ToolCalls))
86 {
87 writer.WritePropertyName("tool_calls"u8);
88 writer.WriteStartArray();
89 for (int i = 0; i < ToolCalls.Count; i++)
90 {
91 if (ToolCalls[i].Patch.IsRemoved("$"u8))
92 {
93 continue;
94 }
95 writer.WriteObjectValue(ToolCalls[i], options);
96 }
97 Patch.WriteTo(writer, "$.tool_calls"u8);
98 writer.WriteEndArray();
99 }
100 if (Optional.IsDefined(FunctionCall) && !Patch.Contains("$.function_call"u8))
101 {
102 writer.WritePropertyName("function_call"u8);
103 writer.WriteObjectValue(FunctionCall, options);
104 }
105
106 Patch.WriteTo(writer);
107#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
108 }
109
110 AssistantChatMessage IJsonModel<AssistantChatMessage>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (AssistantChatMessage)JsonModelCreateCore(ref reader, options);
111
112 [Experimental("OPENAI001")]
113 protected override ChatMessage JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
114 {
115 string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format;
116 if (format != "J")
117 {
118 throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support reading '{format}' format.");
119 }
120 using JsonDocument document = JsonDocument.ParseValue(ref reader);
121 return DeserializeAssistantChatMessage(document.RootElement, null, options);
122 }
123
124 internal static AssistantChatMessage DeserializeAssistantChatMessage(JsonElement element, BinaryData data, ModelReaderWriterOptions options)
125 {
126 if (element.ValueKind == JsonValueKind.Null)
127 {
128 return null;
129 }
130 ChatMessageRole role = default;
131 ChatMessageContent content = default;
132#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
133 JsonPatch patch = new JsonPatch(data is null ? ReadOnlyMemory<byte>.Empty : data.ToMemory());
134#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
135 string refusal = default;
136 string participantName = default;
137 ChatOutputAudioReference outputAudioReference = default;
138 IList<ChatToolCall> toolCalls = default;
139 ChatFunctionCall functionCall = default;
140 foreach (var prop in element.EnumerateObject())
141 {
142 if (prop.NameEquals("role"u8))
143 {
144 role = prop.Value.GetString().ToChatMessageRole();
145 continue;
146 }
147 if (prop.NameEquals("content"u8))
148 {
149 DeserializeContentValue(prop, ref content, options);
150 continue;
151 }
152 if (prop.NameEquals("refusal"u8))
153 {
154 if (prop.Value.ValueKind == JsonValueKind.Null)
155 {
156 refusal = null;
157 continue;
158 }
159 refusal = prop.Value.GetString();
160 continue;
161 }
162 if (prop.NameEquals("name"u8))
163 {
164 participantName = prop.Value.GetString();
165 continue;
166 }
167 if (prop.NameEquals("audio"u8))
168 {
169 if (prop.Value.ValueKind == JsonValueKind.Null)
170 {
171 outputAudioReference = null;
172 continue;
173 }
174 outputAudioReference = ChatOutputAudioReference.DeserializeChatOutputAudioReference(prop.Value, prop.Value.GetUtf8Bytes(), options);
175 continue;
176 }
177 if (prop.NameEquals("tool_calls"u8))
178 {
179 if (prop.Value.ValueKind == JsonValueKind.Null)
180 {
181 continue;
182 }
183 List<ChatToolCall> array = new List<ChatToolCall>();
184 foreach (var item in prop.Value.EnumerateArray())
185 {
186 array.Add(ChatToolCall.DeserializeChatToolCall(item, item.GetUtf8Bytes(), options));
187 }
188 toolCalls = array;
189 continue;
190 }
191 if (prop.NameEquals("function_call"u8))
192 {
193 if (prop.Value.ValueKind == JsonValueKind.Null)
194 {
195 functionCall = null;
196 continue;
197 }
198 functionCall = ChatFunctionCall.DeserializeChatFunctionCall(prop.Value, prop.Value.GetUtf8Bytes(), options);
199 continue;
200 }
201 patch.Set([.. "$."u8, .. Encoding.UTF8.GetBytes(prop.Name)], prop.Value.GetUtf8Bytes());
202 }
203 return new AssistantChatMessage(
204 role,
205 content,
206 patch,
207 refusal,
208 participantName,
209 outputAudioReference,
210 toolCalls ?? new ChangeTrackingList<ChatToolCall>(),
211 functionCall);
212 }
213
214#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
215 private bool PropagateGet(ReadOnlySpan<byte> jsonPath, out JsonPatch.EncodedValue value)
216 {
217 ReadOnlySpan<byte> local = jsonPath.SliceToStartOfPropertyName();
218 value = default;
219
220 if (local.StartsWith("audio"u8))
221 {
222 return OutputAudioReference.Patch.TryGetEncodedValue([.. "$"u8, .. local.Slice("audio"u8.Length)], out value);
223 }
224 if (local.StartsWith("function_call"u8))
225 {
226 return FunctionCall.Patch.TryGetEncodedValue([.. "$"u8, .. local.Slice("function_call"u8.Length)], out value);
227 }
228 if (local.StartsWith("tool_calls"u8))
229 {
230 int propertyLength = "tool_calls"u8.Length;
231 ReadOnlySpan<byte> currentSlice = local.Slice(propertyLength);
232 if (currentSlice.IsEmpty)
233 {
234 return TryResolveToolCallsArray(out value);
235 }
236 if (!currentSlice.TryGetIndex(out int index, out int bytesConsumed))
237 {
238 return false;
239 }
240 return ToolCalls[index].Patch.TryGetEncodedValue([.. "$"u8, .. currentSlice.Slice(bytesConsumed)], out value);
241 }
242 return false;
243 }
244#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
245
246#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
247 private bool PropagateSet(ReadOnlySpan<byte> jsonPath, JsonPatch.EncodedValue value)
248 {
249 ReadOnlySpan<byte> local = jsonPath.SliceToStartOfPropertyName();
250
251 if (local.StartsWith("audio"u8))
252 {
253 OutputAudioReference.Patch.Set([.. "$"u8, .. local.Slice("audio"u8.Length)], value);
254 return true;
255 }
256 if (local.StartsWith("function_call"u8))
257 {
258 FunctionCall.Patch.Set([.. "$"u8, .. local.Slice("function_call"u8.Length)], value);
259 return true;
260 }
261 if (local.StartsWith("tool_calls"u8))
262 {
263 int propertyLength = "tool_calls"u8.Length;
264 ReadOnlySpan<byte> currentSlice = local.Slice(propertyLength);
265 if (!currentSlice.TryGetIndex(out int index, out int bytesConsumed))
266 {
267 return false;
268 }
269 ToolCalls[index].Patch.Set([.. "$"u8, .. currentSlice.Slice(bytesConsumed)], value);
270 return true;
271 }
272 return false;
273 }
274#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
275
276#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
277 private bool TryResolveToolCallsArray(out JsonPatch.EncodedValue value)
278 {
279 value = default;
280 BinaryData data = ModelReaderWriter.Write(ActiveToolCalls(), ModelReaderWriterOptions.Json, OpenAIContext.Default);
281 JsonPatch tempPatch = new JsonPatch();
282 tempPatch.Set("$"u8, data.ToMemory().Span);
283 return tempPatch.TryGetEncodedValue("$"u8, out value);
284 }
285#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
286
287#pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
288 private IEnumerable<ChatToolCall> ActiveToolCalls()
289 {
290 if (!Optional.IsCollectionDefined(ToolCalls))
291 {
292 yield break;
293 }
294 for (int i = 0; i < ToolCalls.Count; i++)
295 {
296 if (!ToolCalls[i].Patch.IsRemoved("$"u8))
297 {
298 yield return ToolCalls[i];
299 }
300 }
301 }
302#pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
303 }
304}
305