openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
joseharriaga/README-version

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Generated/Models/AssistantChatMessage.Serialization.cs

154lines · 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;
10
11namespace OpenAI.Chat
12{
13 public partial class AssistantChatMessage : IJsonModel<AssistantChatMessage>
14 {
15 AssistantChatMessage IJsonModel<AssistantChatMessage>.Create(ref Utf8JsonReader reader, ModelReaderWriterOptions options)
16 {
17 var format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format;
18 if (format != "J")
19 {
20 throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support reading '{format}' format.");
21 }
22
23 using JsonDocument document = JsonDocument.ParseValue(ref reader);
24 return DeserializeAssistantChatMessage(document.RootElement, options);
25 }
26
27 internal static AssistantChatMessage DeserializeAssistantChatMessage(JsonElement element, ModelReaderWriterOptions options = null)
28 {
29 options ??= ModelSerializationExtensions.WireOptions;
30
31 if (element.ValueKind == JsonValueKind.Null)
32 {
33 return null;
34 }
35 string refusal = default;
36 string name = default;
37 IList<ChatToolCall> toolCalls = default;
38 ChatFunctionCall functionCall = default;
39 ChatMessageRole role = default;
40 ChatMessageContent content = default;
41 IDictionary<string, BinaryData> serializedAdditionalRawData = default;
42 Dictionary<string, BinaryData> rawDataDictionary = new Dictionary<string, BinaryData>();
43 foreach (var property in element.EnumerateObject())
44 {
45 if (property.NameEquals("refusal"u8))
46 {
47 if (property.Value.ValueKind == JsonValueKind.Null)
48 {
49 refusal = null;
50 continue;
51 }
52 refusal = property.Value.GetString();
53 continue;
54 }
55 if (property.NameEquals("name"u8))
56 {
57 name = property.Value.GetString();
58 continue;
59 }
60 if (property.NameEquals("tool_calls"u8))
61 {
62 if (property.Value.ValueKind == JsonValueKind.Null)
63 {
64 continue;
65 }
66 List<ChatToolCall> array = new List<ChatToolCall>();
67 foreach (var item in property.Value.EnumerateArray())
68 {
69 array.Add(ChatToolCall.DeserializeChatToolCall(item, options));
70 }
71 toolCalls = array;
72 continue;
73 }
74 if (property.NameEquals("function_call"u8))
75 {
76 if (property.Value.ValueKind == JsonValueKind.Null)
77 {
78 functionCall = null;
79 continue;
80 }
81 functionCall = ChatFunctionCall.DeserializeChatFunctionCall(property.Value, options);
82 continue;
83 }
84 if (property.NameEquals("role"u8))
85 {
86 role = property.Value.GetString().ToChatMessageRole();
87 continue;
88 }
89 if (property.NameEquals("content"u8))
90 {
91 DeserializeContentValue(property, ref content);
92 continue;
93 }
94 if (true)
95 {
96 rawDataDictionary ??= new Dictionary<string, BinaryData>();
97 rawDataDictionary.Add(property.Name, BinaryData.FromString(property.Value.GetRawText()));
98 }
99 }
100 serializedAdditionalRawData = rawDataDictionary;
101 // CUSTOM: Initialize Content collection property.
102 return new AssistantChatMessage(
103 role,
104 content ?? new ChatMessageContent(),
105 serializedAdditionalRawData,
106 refusal,
107 name,
108 toolCalls ?? new ChangeTrackingList<ChatToolCall>(),
109 functionCall);
110 }
111
112 BinaryData IPersistableModel<AssistantChatMessage>.Write(ModelReaderWriterOptions options)
113 {
114 var format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format;
115
116 switch (format)
117 {
118 case "J":
119 return ModelReaderWriter.Write(this, options);
120 default:
121 throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support writing '{options.Format}' format.");
122 }
123 }
124
125 AssistantChatMessage IPersistableModel<AssistantChatMessage>.Create(BinaryData data, ModelReaderWriterOptions options)
126 {
127 var format = options.Format == "W" ? ((IPersistableModel<AssistantChatMessage>)this).GetFormatFromOptions(options) : options.Format;
128
129 switch (format)
130 {
131 case "J":
132 {
133 using JsonDocument document = JsonDocument.Parse(data);
134 return DeserializeAssistantChatMessage(document.RootElement, options);
135 }
136 default:
137 throw new FormatException($"The model {nameof(AssistantChatMessage)} does not support reading '{options.Format}' format.");
138 }
139 }
140
141 string IPersistableModel<AssistantChatMessage>.GetFormatFromOptions(ModelReaderWriterOptions options) => "J";
142
143 internal static new AssistantChatMessage FromResponse(PipelineResponse response)
144 {
145 using var document = JsonDocument.Parse(response.Content);
146 return DeserializeAssistantChatMessage(document.RootElement);
147 }
148
149 internal override BinaryContent ToBinaryContent()
150 {
151 return BinaryContent.Create(this, ModelSerializationExtensions.WireOptions);
152 }
153 }
154}
155