Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions specification/client/chat.client.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using Azure.ClientGenerator.Core;
@@usage(CreateChatCompletionStreamResponse, Usage.output);

@@visibility(ChatCompletionResponseMessage.tool_calls, Lifecycle.Read);
@@visibility(ChatCompletionResponseMessage.annotations, Lifecycle.Read);
@@visibility(ChatCompletionStreamResponseDelta.tool_calls, Lifecycle.Read);

@@clientName(Chat.createChatCompletion, "CompleteChat");
2 changes: 1 addition & 1 deletion specification/client/models/chat.models.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ alias ChatCompletionMessageCollectionOrderQueryParameter = {
* Sort order by the `created_at` timestamp of the objects. `asc` for ascending order and`desc`
* for descending order.
*/
@query order?: ChatCompletionCollectionOrder;
@query order?: ChatCompletionMessageCollectionOrder;
};

union ChatCompletionMessageCollectionOrder {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

namespace OpenAI.Chat;

// CUSTOM: Use the correct namespace.
Expand Down
5 changes: 5 additions & 0 deletions src/Custom/Chat/ChatCompletionMessageCollectionOrder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace OpenAI.Chat;

// CUSTOM: Use the correct namespace.
[CodeGenType("ChatCompletionMessageCollectionOrder")]
public readonly partial struct ChatCompletionMessageCollectionOrder {}
4 changes: 4 additions & 0 deletions src/Custom/Chat/ChatCompletionMessageListDatum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ public partial class ChatCompletionMessageListDatum
// CUSTOM: Ensure enumerated value is used.
[CodeGenMember("Role")]
internal ChatMessageRole Role { get; set; } = ChatMessageRole.Assistant;

// CUSTOM: Rename
[CodeGenMember("Audio")]
public ChatOutputAudio OutputAudio { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal static ChatCompletionMessageCollectionOptions DeserializeChatCompletion
}
string afterId = default;
int? pageSizeLimit = default;
ChatCompletionCollectionOrder? order = default;
ChatCompletionMessageCollectionOrder? order = default;
IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
foreach (var prop in element.EnumerateObject())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ChatCompletionMessageCollectionOptions()
{
}

internal ChatCompletionMessageCollectionOptions(string afterId, int? pageSizeLimit, ChatCompletionCollectionOrder? order, IDictionary<string, BinaryData> additionalBinaryDataProperties)
internal ChatCompletionMessageCollectionOptions(string afterId, int? pageSizeLimit, ChatCompletionMessageCollectionOrder? order, IDictionary<string, BinaryData> additionalBinaryDataProperties)
{
AfterId = afterId;
PageSizeLimit = pageSizeLimit;
Expand All @@ -29,7 +29,7 @@ internal ChatCompletionMessageCollectionOptions(string afterId, int? pageSizeLim

public int? PageSizeLimit { get; set; }

public ChatCompletionCollectionOrder? Order { get; set; }
public ChatCompletionMessageCollectionOrder? Order { get; set; }

internal IDictionary<string, BinaryData> SerializedAdditionalRawData
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// <auto-generated/>

#nullable disable

using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using OpenAI;

namespace OpenAI.Chat
{
[Experimental("OPENAI001")]
public readonly partial struct ChatCompletionMessageCollectionOrder : IEquatable<ChatCompletionMessageCollectionOrder>
{
private readonly string _value;
private const string AscendingValue = "asc";
private const string DescendingValue = "desc";

public ChatCompletionMessageCollectionOrder(string value)
{
Argument.AssertNotNull(value, nameof(value));

_value = value;
}

public static ChatCompletionMessageCollectionOrder Ascending { get; } = new ChatCompletionMessageCollectionOrder(AscendingValue);

public static ChatCompletionMessageCollectionOrder Descending { get; } = new ChatCompletionMessageCollectionOrder(DescendingValue);

public static bool operator ==(ChatCompletionMessageCollectionOrder left, ChatCompletionMessageCollectionOrder right) => left.Equals(right);

public static bool operator !=(ChatCompletionMessageCollectionOrder left, ChatCompletionMessageCollectionOrder right) => !left.Equals(right);

public static implicit operator ChatCompletionMessageCollectionOrder(string value) => new ChatCompletionMessageCollectionOrder(value);

[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object obj) => obj is ChatCompletionMessageCollectionOrder other && Equals(other);

public bool Equals(ChatCompletionMessageCollectionOrder other) => string.Equals(_value, other._value, StringComparison.InvariantCultureIgnoreCase);

[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode() => _value != null ? StringComparer.InvariantCultureIgnoreCase.GetHashCode(_value) : 0;

public override string ToString() => _value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace OpenAI.Chat
{
public partial class ChatCompletionMessageListDatum : IJsonModel<ChatCompletionMessageListDatum>
{
internal ChatCompletionMessageListDatum() : this(null, null, null, null, null, null, null, default, null)
internal ChatCompletionMessageListDatum() : this(null, null, null, null, null, null, default, null, null)
{
}

Expand Down Expand Up @@ -65,6 +65,7 @@ protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWrit
}
writer.WriteEndArray();
}
// Plugin customization: remove options.Format != "W" check
if (Optional.IsCollectionDefined(Annotations) && _additionalBinaryDataProperties?.ContainsKey("annotations") != true)
{
writer.WritePropertyName("annotations"u8);
Expand All @@ -80,11 +81,6 @@ protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWrit
writer.WritePropertyName("function_call"u8);
writer.WriteObjectValue(FunctionCall, options);
}
if (Optional.IsDefined(Audio) && _additionalBinaryDataProperties?.ContainsKey("audio") != true)
{
writer.WritePropertyName("audio"u8);
writer.WriteObjectValue(Audio, options);
}
if (_additionalBinaryDataProperties?.ContainsKey("id") != true)
{
writer.WritePropertyName("id"u8);
Expand All @@ -95,6 +91,11 @@ protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWrit
writer.WritePropertyName("role"u8);
writer.WriteStringValue(Role.ToSerialString());
}
if (Optional.IsDefined(OutputAudio) && _additionalBinaryDataProperties?.ContainsKey("audio") != true)
{
writer.WritePropertyName("audio"u8);
writer.WriteObjectValue(OutputAudio, options);
}
// Plugin customization: remove options.Format != "W" check
if (_additionalBinaryDataProperties != null)
{
Expand Down Expand Up @@ -139,11 +140,11 @@ internal static ChatCompletionMessageListDatum DeserializeChatCompletionMessageL
string content = default;
string refusal = default;
IReadOnlyList<ChatToolCall> toolCalls = default;
IList<ChatMessageAnnotation> annotations = default;
IReadOnlyList<ChatMessageAnnotation> annotations = default;
InternalChatCompletionResponseMessageFunctionCall functionCall = default;
ChatOutputAudio audio = default;
string id = default;
ChatMessageRole role = default;
ChatOutputAudio outputAudio = default;
IDictionary<string, BinaryData> additionalBinaryDataProperties = new ChangeTrackingDictionary<string, BinaryData>();
foreach (var prop in element.EnumerateObject())
{
Expand Down Expand Up @@ -204,16 +205,6 @@ internal static ChatCompletionMessageListDatum DeserializeChatCompletionMessageL
functionCall = InternalChatCompletionResponseMessageFunctionCall.DeserializeInternalChatCompletionResponseMessageFunctionCall(prop.Value, options);
continue;
}
if (prop.NameEquals("audio"u8))
{
if (prop.Value.ValueKind == JsonValueKind.Null)
{
audio = null;
continue;
}
audio = ChatOutputAudio.DeserializeChatOutputAudio(prop.Value, options);
continue;
}
if (prop.NameEquals("id"u8))
{
id = prop.Value.GetString();
Expand All @@ -224,6 +215,16 @@ internal static ChatCompletionMessageListDatum DeserializeChatCompletionMessageL
role = prop.Value.GetString().ToChatMessageRole();
continue;
}
if (prop.NameEquals("audio"u8))
{
if (prop.Value.ValueKind == JsonValueKind.Null)
{
outputAudio = null;
continue;
}
outputAudio = ChatOutputAudio.DeserializeChatOutputAudio(prop.Value, options);
continue;
}
// Plugin customization: remove options.Format != "W" check
additionalBinaryDataProperties.Add(prop.Name, BinaryData.FromString(prop.Value.GetRawText()));
}
Expand All @@ -233,9 +234,9 @@ internal static ChatCompletionMessageListDatum DeserializeChatCompletionMessageL
toolCalls ?? new ChangeTrackingList<ChatToolCall>(),
annotations ?? new ChangeTrackingList<ChatMessageAnnotation>(),
functionCall,
audio,
id,
role,
outputAudio,
additionalBinaryDataProperties);
}

Expand Down
8 changes: 3 additions & 5 deletions src/Generated/Models/Chat/ChatCompletionMessageListDatum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ internal ChatCompletionMessageListDatum(string content, string refusal, string i
Role = role;
}

internal ChatCompletionMessageListDatum(string content, string refusal, IReadOnlyList<ChatToolCall> toolCalls, IList<ChatMessageAnnotation> annotations, InternalChatCompletionResponseMessageFunctionCall functionCall, ChatOutputAudio audio, string id, ChatMessageRole role, IDictionary<string, BinaryData> additionalBinaryDataProperties)
internal ChatCompletionMessageListDatum(string content, string refusal, IReadOnlyList<ChatToolCall> toolCalls, IReadOnlyList<ChatMessageAnnotation> annotations, InternalChatCompletionResponseMessageFunctionCall functionCall, string id, ChatMessageRole role, ChatOutputAudio outputAudio, IDictionary<string, BinaryData> additionalBinaryDataProperties)
{
// Plugin customization: ensure initialization of collections
Content = content;
Refusal = refusal;
ToolCalls = toolCalls ?? new ChangeTrackingList<ChatToolCall>();
Annotations = annotations ?? new ChangeTrackingList<ChatMessageAnnotation>();
FunctionCall = functionCall;
Audio = audio;
Id = id;
Role = role;
OutputAudio = outputAudio;
_additionalBinaryDataProperties = additionalBinaryDataProperties;
}

Expand All @@ -44,12 +44,10 @@ internal ChatCompletionMessageListDatum(string content, string refusal, IReadOnl

public IReadOnlyList<ChatToolCall> ToolCalls { get; }

public IList<ChatMessageAnnotation> Annotations { get; }
public IReadOnlyList<ChatMessageAnnotation> Annotations { get; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this readonly? messages are input models.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a ChatMessage but it is not. It's a read-only representation of the chat messages corresponding to a stored chat completion. While they look very similar, they unfortunately have a few differences and hence we couldn't reuse the ChatMessage class. For now, we're introducing this class as experimental. I will share offline a few considerations that we have been discussing.


internal InternalChatCompletionResponseMessageFunctionCall FunctionCall { get; }

public ChatOutputAudio Audio { get; }

public string Id { get; }

internal IDictionary<string, BinaryData> SerializedAdditionalRawData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWrit
}
writer.WriteEndArray();
}
// Plugin customization: remove options.Format != "W" check
if (Optional.IsCollectionDefined(Annotations) && _additionalBinaryDataProperties?.ContainsKey("annotations") != true)
{
writer.WritePropertyName("annotations"u8);
Expand Down Expand Up @@ -133,7 +134,7 @@ internal static InternalChatCompletionResponseMessage DeserializeInternalChatCom
}
string refusal = default;
IReadOnlyList<ChatToolCall> toolCalls = default;
IList<ChatMessageAnnotation> annotations = default;
IReadOnlyList<ChatMessageAnnotation> annotations = default;
ChatOutputAudio audio = default;
ChatMessageRole role = default;
ChatMessageContent content = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal InternalChatCompletionResponseMessage(string refusal, ChatMessageRole r
Content = content ?? new ChatMessageContent();
}

internal InternalChatCompletionResponseMessage(string refusal, IReadOnlyList<ChatToolCall> toolCalls, IList<ChatMessageAnnotation> annotations, ChatOutputAudio audio, ChatMessageRole role, ChatMessageContent content, ChatFunctionCall functionCall, IDictionary<string, BinaryData> additionalBinaryDataProperties)
internal InternalChatCompletionResponseMessage(string refusal, IReadOnlyList<ChatToolCall> toolCalls, IReadOnlyList<ChatMessageAnnotation> annotations, ChatOutputAudio audio, ChatMessageRole role, ChatMessageContent content, ChatFunctionCall functionCall, IDictionary<string, BinaryData> additionalBinaryDataProperties)
{
// Plugin customization: ensure initialization of collections
Refusal = refusal;
Expand All @@ -39,7 +39,7 @@ internal InternalChatCompletionResponseMessage(string refusal, IReadOnlyList<Cha

public IReadOnlyList<ChatToolCall> ToolCalls { get; }

public IList<ChatMessageAnnotation> Annotations { get; }
public IReadOnlyList<ChatMessageAnnotation> Annotations { get; }

public ChatOutputAudio Audio { get; }

Expand Down
2 changes: 1 addition & 1 deletion src/Generated/OpenAIModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ public static ChatCompletionCollectionOptions ChatCompletionCollectionOptions(st
additionalBinaryDataProperties: null);
}

public static ChatCompletionMessageCollectionOptions ChatCompletionMessageCollectionOptions(string afterId = default, int? pageSizeLimit = default, ChatCompletionCollectionOrder? order = default)
public static ChatCompletionMessageCollectionOptions ChatCompletionMessageCollectionOptions(string afterId = default, int? pageSizeLimit = default, ChatCompletionMessageCollectionOrder? order = default)
{
return new ChatCompletionMessageCollectionOptions(afterId, pageSizeLimit, order, additionalBinaryDataProperties: null);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Chat/ChatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1887,7 +1887,7 @@ await RetryWithExponentialBackoffAsync(async () =>
List<ChatCompletionMessageListDatum> ascMessages = new();
var ascOptions = new ChatCompletionMessageCollectionOptions()
{
Order = ChatCompletionCollectionOrder.Ascending,
Order = ChatCompletionMessageCollectionOrder.Ascending,
PageSizeLimit = 5
};

Expand All @@ -1901,7 +1901,7 @@ await RetryWithExponentialBackoffAsync(async () =>
List<ChatCompletionMessageListDatum> descMessages = new();
var descOptions = new ChatCompletionMessageCollectionOptions()
{
Order = ChatCompletionCollectionOrder.Descending,
Order = ChatCompletionMessageCollectionOrder.Descending,
PageSizeLimit = 5
};

Expand Down Expand Up @@ -2004,7 +2004,7 @@ await RetryWithExponentialBackoffAsync(async () =>
new ChatCompletionMessageCollectionOptions()
{
PageSizeLimit = 3,
Order = ChatCompletionCollectionOrder.Descending
Order = ChatCompletionMessageCollectionOrder.Descending
},
cancellationToken: cts.Token))
{
Expand Down