openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.2.0-beta.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Generated/Models/AssistantChatMessage.Serialization.cs

231lines · modecode

1// <auto-generated/>
2
3#nullable disable
4
5using System;
6using System.ClientModel;
7using System.ClientModel.Primitives;
8using System.Collections.Generic;
9using System.Text.Json;
10using OpenAI;
11
12namespace OpenAI.Chat
13{
14 public partial class AssistantChatMessage : IJsonModel<AssistantChatMessage>
15 {
16 protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options)
17 {
18 string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format;
19 if (format != "J")
20 {
21 throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support writing '{format}' format.");
22 }
23 base.JsonModelWriteCore(writer, options);
24 if (Optional.IsDefined(Refusal) && _additionalBinaryDataProperties?.ContainsKey("refusal") != true)
25 {
26 if (Refusal != null)
27 {
28 writer.WritePropertyName("refusal"u8);
29 writer.WriteStringValue(Refusal);
30 }
31 else
32 {
33 writer.WriteNull("refusal"u8);
34 }
35 }
36 if (Optional.IsDefined(ParticipantName) && _additionalBinaryDataProperties?.ContainsKey("name") != true)
37 {
38 writer.WritePropertyName("name"u8);
39 writer.WriteStringValue(ParticipantName);
40 }
41 if (Optional.IsCollectionDefined(ToolCalls) && _additionalBinaryDataProperties?.ContainsKey("tool_calls") != true)
42 {
43 writer.WritePropertyName("tool_calls"u8);
44 writer.WriteStartArray();
45 foreach (ChatToolCall item in ToolCalls)
46 {
47 writer.WriteObjectValue(item, options);
48 }
49 writer.WriteEndArray();
50 }
51 if (Optional.IsDefined(FunctionCall) && _additionalBinaryDataProperties?.ContainsKey("function_call") != true)
52 {
53 if (FunctionCall != null)
54 {
55 writer.WritePropertyName("function_call"u8);
56 writer.WriteObjectValue<ChatFunctionCall>(FunctionCall, options);
57 }
58 else
59 {
60 writer.WriteNull("functionCall"u8);
61 }
62 }
63 if (Optional.IsDefined(OutputAudioReference) && _additionalBinaryDataProperties?.ContainsKey("audio") != true)
64 {
65 if (OutputAudioReference != null)
66 {
67 writer.WritePropertyName("audio"u8);
68 writer.WriteObjectValue<ChatOutputAudioReference>(OutputAudioReference, options);
69 }
70 else
71 {
72 writer.WriteNull("audio"u8);
73 }
74 }
75 }
76
77 AssistantChatMessage IJsonModel<AssistantChatMessage>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (AssistantChatMessage)JsonModelCreateCore(ref reader, options);
78
79 protected override ChatMessage JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
80 {
81 string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format;
82 if (format != "J")
83 {
84 throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support reading '{format}' format.");
85 }
86 using JsonDocument document = JsonDocument.ParseValue(ref reader);
87 return DeserializeAssistantChatMessage(document.RootElement, options);
88 }
89
90 internal static AssistantChatMessage DeserializeAssistantChatMessage(JsonElement element, ModelReaderWriterOptions options)
91 {
92 if (element.ValueKind == JsonValueKind.Null)
93 {
94 return null;
95 }
96 ChatMessageContent content = default;
97 Chat.ChatMessageRole role = default;
98 IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
99 string refusal = default;
100 string participantName = default;
101 IList<ChatToolCall> toolCalls = default;
102 ChatFunctionCall functionCall = default;
103 ChatOutputAudioReference outputAudioReference = default;
104 foreach (var prop in element.EnumerateObject())
105 {
106 if (prop.NameEquals("content"u8))
107 {
108 DeserializeContentValue(prop, ref content);
109 continue;
110 }
111 if (prop.NameEquals("role"u8))
112 {
113 role = prop.Value.GetString().ToChatMessageRole();
114 continue;
115 }
116 if (prop.NameEquals("refusal"u8))
117 {
118 if (prop.Value.ValueKind == JsonValueKind.Null)
119 {
120 refusal = null;
121 continue;
122 }
123 refusal = prop.Value.GetString();
124 continue;
125 }
126 if (prop.NameEquals("name"u8))
127 {
128 participantName = prop.Value.GetString();
129 continue;
130 }
131 if (prop.NameEquals("tool_calls"u8))
132 {
133 if (prop.Value.ValueKind == JsonValueKind.Null)
134 {
135 continue;
136 }
137 List<ChatToolCall> array = new List<ChatToolCall>();
138 foreach (var item in prop.Value.EnumerateArray())
139 {
140 array.Add(ChatToolCall.DeserializeChatToolCall(item, options));
141 }
142 toolCalls = array;
143 continue;
144 }
145 if (prop.NameEquals("function_call"u8))
146 {
147 if (prop.Value.ValueKind == JsonValueKind.Null)
148 {
149 functionCall = null;
150 continue;
151 }
152 functionCall = ChatFunctionCall.DeserializeChatFunctionCall(prop.Value, options);
153 continue;
154 }
155 if (prop.NameEquals("audio"u8))
156 {
157 if (prop.Value.ValueKind == JsonValueKind.Null)
158 {
159 outputAudioReference = null;
160 continue;
161 }
162 outputAudioReference = ChatOutputAudioReference.DeserializeChatOutputAudioReference(prop.Value, options);
163 continue;
164 }
165 if (true)
166 {
167 additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
168 }
169 }
170 // CUSTOM: Initialize Content collection property.
171 return new AssistantChatMessage(
172 content ?? new ChatMessageContent(),
173 role,
174 additionalBinaryDataProperties,
175 refusal,
176 participantName,
177 toolCalls ?? new ChangeTrackingList<ChatToolCall>(),
178 functionCall,
179 outputAudioReference);
180 }
181
182 BinaryData IPersistableModel<AssistantChatMessage>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);
183
184 protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
185 {
186 string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format;
187 switch (format)
188 {
189 case "J":
190 return ModelReaderWriter.Write(this, options);
191 default:
192 throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support writing '{options.Format}' format.");
193 }
194 }
195
196 AssistantChatMessage IPersistableModel<AssistantChatMessage>.Create(BinaryData data, ModelReaderWriterOptions options) => (AssistantChatMessage)PersistableModelCreateCore(data, options);
197
198 protected override ChatMessage PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
199 {
200 string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format;
201 switch (format)
202 {
203 case "J":
204 using (JsonDocument document = JsonDocument.Parse(data))
205 {
206 return DeserializeAssistantChatMessage(document.RootElement, options);
207 }
208 default:
209 throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support reading '{options.Format}' format.");
210 }
211 }
212
213 string IPersistableModel<AssistantChatMessage>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
214
215 public static implicit operator BinaryContent(AssistantChatMessage assistantChatMessage)
216 {
217 if (assistantChatMessage == null)
218 {
219 return null;
220 }
221 return BinaryContent.Create(assistantChatMessage, ModelSerializationExtensions.WireOptions);
222 }
223
224 public static explicit operator AssistantChatMessage(ClientResult result)
225 {
226 using PipelineResponse response = result.GetRawResponse();
227 using JsonDocument document = JsonDocument.Parse(response.Content);
228 return DeserializeAssistantChatMessage(document.RootElement, ModelSerializationExtensions.WireOptions);
229 }
230 }
231}
232