openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.2.0-beta.4

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Generated/Models/AssistantChatMessage.Serialization.cs

206lines · 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 writer.WritePropertyName("refusal"u8);
27 writer.WriteStringValue(Refusal);
28 }
29 if (Optional.IsDefined(ParticipantName) && _additionalBinaryDataProperties?.ContainsKey("name") != true)
30 {
31 writer.WritePropertyName("name"u8);
32 writer.WriteStringValue(ParticipantName);
33 }
34 if (Optional.IsCollectionDefined(ToolCalls) && _additionalBinaryDataProperties?.ContainsKey("tool_calls") != true)
35 {
36 writer.WritePropertyName("tool_calls"u8);
37 writer.WriteStartArray();
38 foreach (ChatToolCall item in ToolCalls)
39 {
40 writer.WriteObjectValue(item, options);
41 }
42 writer.WriteEndArray();
43 }
44 if (Optional.IsDefined(FunctionCall) && _additionalBinaryDataProperties?.ContainsKey("function_call") != true)
45 {
46 writer.WritePropertyName("function_call"u8);
47 writer.WriteObjectValue(FunctionCall, options);
48 }
49 if (Optional.IsDefined(OutputAudioReference) && _additionalBinaryDataProperties?.ContainsKey("audio") != true)
50 {
51 writer.WritePropertyName("audio"u8);
52 writer.WriteObjectValue(OutputAudioReference, options);
53 }
54 }
55
56 AssistantChatMessage IJsonModel<AssistantChatMessage>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options) => (AssistantChatMessage)JsonModelCreateCore(ref reader, options);
57
58 protected override ChatMessage JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
59 {
60 string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format;
61 if (format != "J")
62 {
63 throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support reading '{format}' format.");
64 }
65 using JsonDocument document = JsonDocument.ParseValue(ref reader);
66 return DeserializeAssistantChatMessage(document.RootElement, options);
67 }
68
69 internal static AssistantChatMessage DeserializeAssistantChatMessage(JsonElement element, ModelReaderWriterOptions options)
70 {
71 if (element.ValueKind == JsonValueKind.Null)
72 {
73 return null;
74 }
75 ChatMessageContent content = default;
76 ChatMessageRole role = default;
77 IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
78 string refusal = default;
79 string participantName = default;
80 IList<ChatToolCall> toolCalls = default;
81 ChatFunctionCall functionCall = default;
82 ChatOutputAudioReference outputAudioReference = default;
83 foreach (var prop in element.EnumerateObject())
84 {
85 if (prop.NameEquals("content"u8))
86 {
87 DeserializeContentValue(prop, ref content);
88 continue;
89 }
90 if (prop.NameEquals("role"u8))
91 {
92 role = prop.Value.GetString().ToChatMessageRole();
93 continue;
94 }
95 if (prop.NameEquals("refusal"u8))
96 {
97 if (prop.Value.ValueKind == JsonValueKind.Null)
98 {
99 refusal = null;
100 continue;
101 }
102 refusal = prop.Value.GetString();
103 continue;
104 }
105 if (prop.NameEquals("name"u8))
106 {
107 participantName = prop.Value.GetString();
108 continue;
109 }
110 if (prop.NameEquals("tool_calls"u8))
111 {
112 if (prop.Value.ValueKind == JsonValueKind.Null)
113 {
114 continue;
115 }
116 List<ChatToolCall> array = new List<ChatToolCall>();
117 foreach (var item in prop.Value.EnumerateArray())
118 {
119 array.Add(ChatToolCall.DeserializeChatToolCall(item, options));
120 }
121 toolCalls = array;
122 continue;
123 }
124 if (prop.NameEquals("function_call"u8))
125 {
126 if (prop.Value.ValueKind == JsonValueKind.Null)
127 {
128 functionCall = null;
129 continue;
130 }
131 functionCall = ChatFunctionCall.DeserializeChatFunctionCall(prop.Value, options);
132 continue;
133 }
134 if (prop.NameEquals("audio"u8))
135 {
136 if (prop.Value.ValueKind == JsonValueKind.Null)
137 {
138 outputAudioReference = null;
139 continue;
140 }
141 outputAudioReference = ChatOutputAudioReference.DeserializeChatOutputAudioReference(prop.Value, options);
142 continue;
143 }
144 additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
145 }
146 return new AssistantChatMessage(
147 content,
148 role,
149 additionalBinaryDataProperties,
150 refusal,
151 participantName,
152 toolCalls ?? new ChangeTrackingList<ChatToolCall>(),
153 functionCall,
154 outputAudioReference);
155 }
156
157 BinaryData IPersistableModel<AssistantChatMessage>.Write(ModelReaderWriterOptions options) => PersistableModelWriteCore(options);
158
159 protected override BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options)
160 {
161 string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format;
162 switch (format)
163 {
164 case "J":
165 return ModelReaderWriter.Write(this, options);
166 default:
167 throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support writing '{options.Format}' format.");
168 }
169 }
170
171 AssistantChatMessage IPersistableModel<AssistantChatMessage>.Create(BinaryData data, ModelReaderWriterOptions options) => (AssistantChatMessage)PersistableModelCreateCore(data, options);
172
173 protected override ChatMessage PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options)
174 {
175 string format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format;
176 switch (format)
177 {
178 case "J":
179 using (JsonDocument document = JsonDocument.Parse(data))
180 {
181 return DeserializeAssistantChatMessage(document.RootElement, options);
182 }
183 default:
184 throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support reading '{options.Format}' format.");
185 }
186 }
187
188 string IPersistableModel<AssistantChatMessage>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
189
190 public static implicit operator BinaryContent(AssistantChatMessage assistantChatMessage)
191 {
192 if (assistantChatMessage == null)
193 {
194 return null;
195 }
196 return BinaryContent.Create(assistantChatMessage, ModelSerializationExtensions.WireOptions);
197 }
198
199 public static explicit operator AssistantChatMessage(ClientResult result)
200 {
201 using PipelineResponse response = result.GetRawResponse();
202 using JsonDocument document = JsonDocument.Parse(response.Content);
203 return DeserializeAssistantChatMessage(document.RootElement, ModelSerializationExtensions.WireOptions);
204 }
205 }
206}
207