Skip to content

Commit af456ab

Browse files
committed
POC for adding content_parts to GetChatCompletions
1 parent 9320b2a commit af456ab

13 files changed

+8560
-8437
lines changed

specification/base/typespec/chat/models.tsp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,8 @@ model ChatCompletionResponseMessage {
719719
/** The contents of the message. */
720720
content: string | null;
721721

722+
content_parts?: ChatCompletionRequestMessageContentPart[] | null;
723+
722724
/** The refusal message generated by the model. */
723725
refusal: string | null;
724726

src/Custom/Chat/OpenAIChatModelFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ public static ChatCompletion ChatCompletion(
6969
refusalTokenLogProbabilities ??= new List<ChatTokenLogProbabilityDetails>();
7070
messageAnnotations ??= new List<ChatMessageAnnotation>();
7171

72-
InternalChatCompletionResponseMessage message = new(
72+
InternalChatCompletionResponseMessage message =
73+
new(
74+
null,
7375
refusal,
7476
toolCalls.ToList(),
7577
messageAnnotations.ToList(),

src/Generated/Models/Chat/ChatCompletionMessageListDatum.Serialization.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace OpenAI.Chat
1212
{
1313
public partial class ChatCompletionMessageListDatum : IJsonModel<ChatCompletionMessageListDatum>
1414
{
15-
internal ChatCompletionMessageListDatum() : this(null, null, null, null, null, null, default, null, null)
15+
internal ChatCompletionMessageListDatum() : this(null, null, null, null, null, null, null, default, null, null)
1616
{
1717
}
1818

@@ -42,6 +42,16 @@ protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWrit
4242
writer.WriteNull("content"u8);
4343
}
4444
}
45+
if (Optional.IsCollectionDefined(ContentParts) && _additionalBinaryDataProperties?.ContainsKey("content_parts") != true)
46+
{
47+
writer.WritePropertyName("content_parts"u8);
48+
writer.WriteStartArray();
49+
foreach (ChatMessageContentPart item in ContentParts)
50+
{
51+
writer.WriteObjectValue(item, options);
52+
}
53+
writer.WriteEndArray();
54+
}
4555
if (_additionalBinaryDataProperties?.ContainsKey("refusal") != true)
4656
{
4757
if (Optional.IsDefined(Refusal))
@@ -138,6 +148,7 @@ internal static ChatCompletionMessageListDatum DeserializeChatCompletionMessageL
138148
return null;
139149
}
140150
string content = default;
151+
IList<ChatMessageContentPart> contentParts = default;
141152
string refusal = default;
142153
IReadOnlyList<ChatToolCall> toolCalls = default;
143154
IReadOnlyList<ChatMessageAnnotation> annotations = default;
@@ -158,6 +169,20 @@ internal static ChatCompletionMessageListDatum DeserializeChatCompletionMessageL
158169
content = prop.Value.GetString();
159170
continue;
160171
}
172+
if (prop.NameEquals("content_parts"u8))
173+
{
174+
if (prop.Value.ValueKind == JsonValueKind.Null)
175+
{
176+
continue;
177+
}
178+
List<ChatMessageContentPart> array = new List<ChatMessageContentPart>();
179+
foreach (var item in prop.Value.EnumerateArray())
180+
{
181+
array.Add(ChatMessageContentPart.DeserializeChatMessageContentPart(item, options));
182+
}
183+
contentParts = array;
184+
continue;
185+
}
161186
if (prop.NameEquals("refusal"u8))
162187
{
163188
if (prop.Value.ValueKind == JsonValueKind.Null)
@@ -230,6 +255,7 @@ internal static ChatCompletionMessageListDatum DeserializeChatCompletionMessageL
230255
}
231256
return new ChatCompletionMessageListDatum(
232257
content,
258+
contentParts ?? new ChangeTrackingList<ChatMessageContentPart>(),
233259
refusal,
234260
toolCalls ?? new ChangeTrackingList<ChatToolCall>(),
235261
annotations ?? new ChangeTrackingList<ChatMessageAnnotation>(),

src/Generated/Models/Chat/ChatCompletionMessageListDatum.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ public partial class ChatCompletionMessageListDatum
1717
internal ChatCompletionMessageListDatum(string content, string refusal, string id, ChatMessageRole role)
1818
{
1919
Content = content;
20+
ContentParts = new ChangeTrackingList<ChatMessageContentPart>();
2021
Refusal = refusal;
2122
ToolCalls = new ChangeTrackingList<ChatToolCall>();
2223
Annotations = new ChangeTrackingList<ChatMessageAnnotation>();
2324
Id = id;
2425
Role = role;
2526
}
2627

27-
internal ChatCompletionMessageListDatum(string content, string refusal, IReadOnlyList<ChatToolCall> toolCalls, IReadOnlyList<ChatMessageAnnotation> annotations, InternalChatCompletionResponseMessageFunctionCall functionCall, string id, ChatMessageRole role, ChatOutputAudio outputAudio, IDictionary<string, BinaryData> additionalBinaryDataProperties)
28+
internal ChatCompletionMessageListDatum(string content, IList<ChatMessageContentPart> contentParts, string refusal, IReadOnlyList<ChatToolCall> toolCalls, IReadOnlyList<ChatMessageAnnotation> annotations, InternalChatCompletionResponseMessageFunctionCall functionCall, string id, ChatMessageRole role, ChatOutputAudio outputAudio, IDictionary<string, BinaryData> additionalBinaryDataProperties)
2829
{
2930
// Plugin customization: ensure initialization of collections
3031
Content = content;
32+
ContentParts = contentParts ?? new ChangeTrackingList<ChatMessageContentPart>();
3133
Refusal = refusal;
3234
ToolCalls = toolCalls ?? new ChangeTrackingList<ChatToolCall>();
3335
Annotations = annotations ?? new ChangeTrackingList<ChatMessageAnnotation>();
@@ -40,6 +42,8 @@ internal ChatCompletionMessageListDatum(string content, string refusal, IReadOnl
4042

4143
public string Content { get; }
4244

45+
public IList<ChatMessageContentPart> ContentParts { get; }
46+
4347
public string Refusal { get; }
4448

4549
public IReadOnlyList<ChatToolCall> ToolCalls { get; }

src/Generated/Models/Chat/InternalChatCompletionRequestMessageContentPartAudio.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ internal InternalChatCompletionRequestMessageContentPartAudio(IDictionary<string
2222
InputAudio = inputAudio;
2323
}
2424

25-
internal InternalChatCompletionRequestMessageContentPartAudioInputAudio InputAudio { get; }
25+
internal InternalChatCompletionRequestMessageContentPartAudioInputAudio InputAudio { get; set; }
2626
}
2727
}

src/Generated/Models/Chat/InternalChatCompletionRequestMessageContentPartAudioInputAudio.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ internal InternalChatCompletionRequestMessageContentPartAudioInputAudio(BinaryDa
2727
_additionalBinaryDataProperties = additionalBinaryDataProperties;
2828
}
2929

30-
public BinaryData Data { get; }
30+
public BinaryData Data { get; set; }
3131

32-
public ChatInputAudioFormat Format { get; }
32+
public ChatInputAudioFormat Format { get; set; }
3333

3434
internal IDictionary<string, BinaryData> SerializedAdditionalRawData
3535
{

src/Generated/Models/Chat/InternalChatCompletionRequestMessageContentPartFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ internal InternalChatCompletionRequestMessageContentPartFile(IDictionary<string,
2222
File = @file;
2323
}
2424

25-
internal InternalChatCompletionRequestMessageContentPartFileFile File { get; }
25+
internal InternalChatCompletionRequestMessageContentPartFileFile File { get; set; }
2626
}
2727
}

src/Generated/Models/Chat/InternalChatCompletionRequestMessageContentPartImage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ internal InternalChatCompletionRequestMessageContentPartImage(IDictionary<string
2222
ImageUrl = imageUrl;
2323
}
2424

25-
internal InternalChatCompletionRequestMessageContentPartImageImageUrl ImageUrl { get; }
25+
internal InternalChatCompletionRequestMessageContentPartImageImageUrl ImageUrl { get; set; }
2626
}
2727
}

src/Generated/Models/Chat/InternalChatCompletionResponseMessage.Serialization.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace OpenAI.Chat
1212
{
1313
internal partial class InternalChatCompletionResponseMessage : IJsonModel<InternalChatCompletionResponseMessage>
1414
{
15-
internal InternalChatCompletionResponseMessage() : this(null, null, null, null, default, null, null, null)
15+
internal InternalChatCompletionResponseMessage() : this(null, null, null, null, null, default, null, null, null)
1616
{
1717
}
1818

@@ -30,6 +30,16 @@ protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWrit
3030
{
3131
throw new FormatException($"The model {nameof(InternalChatCompletionResponseMessage)} does not support writing '{format}' format.");
3232
}
33+
if (Optional.IsCollectionDefined(ContentParts) && _additionalBinaryDataProperties?.ContainsKey("content_parts") != true)
34+
{
35+
writer.WritePropertyName("content_parts"u8);
36+
writer.WriteStartArray();
37+
foreach (ChatMessageContentPart item in ContentParts)
38+
{
39+
writer.WriteObjectValue(item, options);
40+
}
41+
writer.WriteEndArray();
42+
}
3343
if (_additionalBinaryDataProperties?.ContainsKey("refusal") != true)
3444
{
3545
if (Optional.IsDefined(Refusal))
@@ -132,6 +142,7 @@ internal static InternalChatCompletionResponseMessage DeserializeInternalChatCom
132142
{
133143
return null;
134144
}
145+
IList<ChatMessageContentPart> contentParts = default;
135146
string refusal = default;
136147
IReadOnlyList<ChatToolCall> toolCalls = default;
137148
IReadOnlyList<ChatMessageAnnotation> annotations = default;
@@ -142,6 +153,20 @@ internal static InternalChatCompletionResponseMessage DeserializeInternalChatCom
142153
IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
143154
foreach (var prop in element.EnumerateObject())
144155
{
156+
if (prop.NameEquals("content_parts"u8))
157+
{
158+
if (prop.Value.ValueKind == JsonValueKind.Null)
159+
{
160+
continue;
161+
}
162+
List<ChatMessageContentPart> array = new List<ChatMessageContentPart>();
163+
foreach (var item in prop.Value.EnumerateArray())
164+
{
165+
array.Add(ChatMessageContentPart.DeserializeChatMessageContentPart(item, options));
166+
}
167+
contentParts = array;
168+
continue;
169+
}
145170
if (prop.NameEquals("refusal"u8))
146171
{
147172
if (prop.Value.ValueKind == JsonValueKind.Null)
@@ -213,6 +238,7 @@ internal static InternalChatCompletionResponseMessage DeserializeInternalChatCom
213238
additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
214239
}
215240
return new InternalChatCompletionResponseMessage(
241+
contentParts ?? new ChangeTrackingList<ChatMessageContentPart>(),
216242
refusal,
217243
toolCalls ?? new ChangeTrackingList<ChatToolCall>(),
218244
annotations ?? new ChangeTrackingList<ChatMessageAnnotation>(),

src/Generated/Models/Chat/InternalChatCompletionResponseMessage.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@ internal partial class InternalChatCompletionResponseMessage
1515
internal InternalChatCompletionResponseMessage(string refusal, ChatMessageRole role, ChatMessageContent content)
1616
{
1717
// Plugin customization: ensure initialization of collections
18+
ContentParts = new ChangeTrackingList<ChatMessageContentPart>();
1819
Refusal = refusal;
1920
ToolCalls = new ChangeTrackingList<ChatToolCall>();
2021
Annotations = new ChangeTrackingList<ChatMessageAnnotation>();
2122
Role = role;
2223
Content = content ?? new ChatMessageContent();
2324
}
2425

25-
internal InternalChatCompletionResponseMessage(string refusal, IReadOnlyList<ChatToolCall> toolCalls, IReadOnlyList<ChatMessageAnnotation> annotations, ChatOutputAudio audio, ChatMessageRole role, ChatMessageContent content, ChatFunctionCall functionCall, IDictionary<string, BinaryData> additionalBinaryDataProperties)
26+
internal InternalChatCompletionResponseMessage(IList<ChatMessageContentPart> contentParts, string refusal, IReadOnlyList<ChatToolCall> toolCalls, IReadOnlyList<ChatMessageAnnotation> annotations, ChatOutputAudio audio, ChatMessageRole role, ChatMessageContent content, ChatFunctionCall functionCall, IDictionary<string, BinaryData> additionalBinaryDataProperties)
2627
{
2728
// Plugin customization: ensure initialization of collections
29+
ContentParts = contentParts ?? new ChangeTrackingList<ChatMessageContentPart>();
2830
Refusal = refusal;
2931
ToolCalls = toolCalls ?? new ChangeTrackingList<ChatToolCall>();
3032
Annotations = annotations ?? new ChangeTrackingList<ChatMessageAnnotation>();
@@ -35,6 +37,8 @@ internal InternalChatCompletionResponseMessage(string refusal, IReadOnlyList<Cha
3537
_additionalBinaryDataProperties = additionalBinaryDataProperties;
3638
}
3739

40+
public IList<ChatMessageContentPart> ContentParts { get; }
41+
3842
public string Refusal { get; }
3943

4044
public IReadOnlyList<ChatToolCall> ToolCalls { get; }

0 commit comments

Comments
 (0)