Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 31 additions & 0 deletions api/OpenAI.net8.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,14 @@ public class ChatClient {
[Experimental("OPENAI001")]
public virtual Task<ClientResult<ChatCompletion>> GetChatCompletionAsync(string completionId, CancellationToken cancellationToken = default);
[Experimental("OPENAI001")]
public virtual CollectionResult<ChatCompletionMessageListDatum> GetChatCompletionMessages(string completionId, ChatCompletionCollectionOptions options = null, CancellationToken cancellationToken = default);
[Experimental("OPENAI001")]
public virtual CollectionResult GetChatCompletionMessages(string completionId, string after, int? limit, string order, RequestOptions options);
[Experimental("OPENAI001")]
public virtual AsyncCollectionResult<ChatCompletionMessageListDatum> GetChatCompletionMessagesAsync(string completionId, ChatCompletionMessageCollectionOptions options = null, CancellationToken cancellationToken = default);
[Experimental("OPENAI001")]
public virtual AsyncCollectionResult GetChatCompletionMessagesAsync(string completionId, string after, int? limit, string order, RequestOptions options);
[Experimental("OPENAI001")]
public virtual CollectionResult<ChatCompletion> GetChatCompletions(ChatCompletionCollectionOptions options = null, CancellationToken cancellationToken = default);
[Experimental("OPENAI001")]
public virtual CollectionResult GetChatCompletions(string after, int? limit, string order, IDictionary<string, string> metadata, string model, RequestOptions options);
Expand Down Expand Up @@ -1511,6 +1519,29 @@ public class ChatCompletionDeletionResult : IJsonModel<ChatCompletionDeletionRes
protected virtual ChatCompletionDeletionResult PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options);
protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options);
}
[Experimental("OPENAI001")]
public class ChatCompletionMessageCollectionOptions : IJsonModel<ChatCompletionMessageCollectionOptions>, IPersistableModel<ChatCompletionMessageCollectionOptions> {
public string AfterId { get; set; }
public ChatCompletionCollectionOrder? Order { get; set; }
public int? PageSizeLimit { get; set; }
protected virtual ChatCompletionMessageCollectionOptions JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
protected virtual ChatCompletionMessageCollectionOptions PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options);
protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options);
}
[Experimental("OPENAI001")]
public class ChatCompletionMessageListDatum : IJsonModel<ChatCompletionMessageListDatum>, IPersistableModel<ChatCompletionMessageListDatum> {
public IList<ChatMessageAnnotation> Annotations { get; }
public ChatOutputAudio Audio { get; }
public string Content { get; }
public string Id { get; }
public string Refusal { get; }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can be done in a separate PR: This class appears to be missing a few properties:

  • content_parts
  • function_call
  • name

public IReadOnlyList<ChatToolCall> ToolCalls { get; }
protected virtual ChatCompletionMessageListDatum JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
protected virtual ChatCompletionMessageListDatum PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options);
protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options);
}
public class ChatCompletionOptions : IJsonModel<ChatCompletionOptions>, IPersistableModel<ChatCompletionOptions> {
public bool? AllowParallelToolCalls { get; set; }
[Experimental("OPENAI001")]
Expand Down
25 changes: 25 additions & 0 deletions api/OpenAI.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,10 @@ public class ChatClient {
public virtual ClientResult<ChatCompletion> GetChatCompletion(string completionId, CancellationToken cancellationToken = default);
public virtual Task<ClientResult> GetChatCompletionAsync(string completionId, RequestOptions options);
public virtual Task<ClientResult<ChatCompletion>> GetChatCompletionAsync(string completionId, CancellationToken cancellationToken = default);
public virtual CollectionResult<ChatCompletionMessageListDatum> GetChatCompletionMessages(string completionId, ChatCompletionCollectionOptions options = null, CancellationToken cancellationToken = default);
public virtual CollectionResult GetChatCompletionMessages(string completionId, string after, int? limit, string order, RequestOptions options);
public virtual AsyncCollectionResult<ChatCompletionMessageListDatum> GetChatCompletionMessagesAsync(string completionId, ChatCompletionMessageCollectionOptions options = null, CancellationToken cancellationToken = default);
public virtual AsyncCollectionResult GetChatCompletionMessagesAsync(string completionId, string after, int? limit, string order, RequestOptions options);
public virtual CollectionResult<ChatCompletion> GetChatCompletions(ChatCompletionCollectionOptions options = null, CancellationToken cancellationToken = default);
public virtual CollectionResult GetChatCompletions(string after, int? limit, string order, IDictionary<string, string> metadata, string model, RequestOptions options);
public virtual AsyncCollectionResult<ChatCompletion> GetChatCompletionsAsync(ChatCompletionCollectionOptions options = null, CancellationToken cancellationToken = default);
Expand Down Expand Up @@ -1349,6 +1353,27 @@ public class ChatCompletionDeletionResult : IJsonModel<ChatCompletionDeletionRes
protected virtual ChatCompletionDeletionResult PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options);
protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options);
}
public class ChatCompletionMessageCollectionOptions : IJsonModel<ChatCompletionMessageCollectionOptions>, IPersistableModel<ChatCompletionMessageCollectionOptions> {
public string AfterId { get; set; }
public ChatCompletionCollectionOrder? Order { get; set; }
public int? PageSizeLimit { get; set; }
protected virtual ChatCompletionMessageCollectionOptions JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
protected virtual ChatCompletionMessageCollectionOptions PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options);
protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options);
}
public class ChatCompletionMessageListDatum : IJsonModel<ChatCompletionMessageListDatum>, IPersistableModel<ChatCompletionMessageListDatum> {
public IList<ChatMessageAnnotation> Annotations { get; }
public ChatOutputAudio Audio { get; }
public string Content { get; }
public string Id { get; }
public string Refusal { get; }
public IReadOnlyList<ChatToolCall> ToolCalls { get; }
protected virtual ChatCompletionMessageListDatum JsonModelCreateCore(ref Utf8JsonReader reader, ModelReaderWriterOptions options);
protected virtual void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options);
protected virtual ChatCompletionMessageListDatum PersistableModelCreateCore(BinaryData data, ModelReaderWriterOptions options);
protected virtual BinaryData PersistableModelWriteCore(ModelReaderWriterOptions options);
}
public class ChatCompletionOptions : IJsonModel<ChatCompletionOptions>, IPersistableModel<ChatCompletionOptions> {
public bool? AllowParallelToolCalls { get; set; }
public ChatAudioOptions AudioOptions { get; set; }
Expand Down
29 changes: 18 additions & 11 deletions codegen/generator/src/Visitors/PaginationVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace OpenAILibraryPlugin.Visitors;

/// <summary>
/// This visitor modifies GetRawPagesAsync methods to consider HasMore in addition to LastId when deciding whether to continue pagination.
/// This visitor modifies GetRawPagesAsync and GetRawPages methods to consider HasMore in addition to LastId when deciding whether to continue pagination.
/// It also replaces specific parameters with an options type for pagination methods.
/// </summary>
public class PaginationVisitor : ScmLibraryVisitor
Expand All @@ -37,6 +37,14 @@ public class PaginationVisitor : ScmLibraryVisitor
{
"GetChatCompletionsAsync",
("ChatCompletion", "ChatCompletionCollectionOptions", _chatParamsToReplace)
},
{
"GetChatCompletionMessages",
("ChatCompletionMessageListDatum", "ChatCompletionCollectionOptions", _chatParamsToReplace)
},
{
"GetChatCompletionMessagesAsync",
("ChatCompletionMessageListDatum", "ChatCompletionMessageCollectionOptions", _chatParamsToReplace)
}
};

Expand Down Expand Up @@ -174,10 +182,11 @@ nullConditional.Inner is VariableExpression varExpr2 &&
/// <returns>True if the method was handled, false otherwise.</returns>
private bool TryHandleGetRawPagesAsyncMethod(MethodProvider method)
{
// If the method is GetRawPagesAsync and is internal, we will modify the body statements to add a check for hasMore == false.
// If the method is GetRawPagesAsync or GetRawPages and is internal, we will modify the body statements to add a check for hasMore == false.
// This is to ensure that pagination stops when hasMore is false, in addition to checking LastId.
if (method.Signature.Name == "GetRawPagesAsync" && method.EnclosingType.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Internal))
if ((method.Signature.Name == "GetRawPagesAsync" || method.Signature.Name == "GetRawPages") && method.EnclosingType.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Internal))
{
VariableExpression? hasMoreVariable = null;
var statements = method.BodyStatements?.ToList() ?? new List<MethodBodyStatement>();
VisitExplodedMethodBodyStatements(
statements!,
Expand All @@ -198,14 +207,12 @@ private bool TryHandleGetRawPagesAsyncMethod(MethodProvider method)
binaryExpr.Right is KeywordExpression rightKeyword &&
rightKeyword.Keyword == "null")
{
// Create "hasMore == null" condition
var hasMoreNullCheck = new BinaryOperatorExpression(
"==",
new MemberExpression(null, "hasMore"),
Snippet.False);
// Create "!hasMore" condition. Note the hasMoreVariable gets assigned earlier in the method statements
// in the WhileStatement handler below.
var hasMoreNullCheck = Snippet.Not(hasMoreVariable);

// Return "nextToken == null || hasMore == null"
return new BinaryOperatorExpression("||", binaryExpr, hasMoreNullCheck);
// Return "nextToken == null || !hasMore"
return BoolSnippets.Or(binaryExpr.As<bool>(), hasMoreNullCheck);
}
}
return expression;
Expand All @@ -230,7 +237,7 @@ assignmentExpression.Value is MemberExpression memberExpression &&
{
// Create a new assignment for hasMore
var hasMoreAssignment = new AssignmentExpression(
new DeclarationExpression(typeof(bool), "hasMore"),
Snippet.Declare("hasMore", typeof(bool), out hasMoreVariable),
new MemberExpression(memberExpression.Inner, "HasMore"));

// Insert the new assignment before the existing one
Expand Down
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");
22 changes: 22 additions & 0 deletions specification/client/models/chat.models.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,26 @@ model ChatCompletionCollectionOptions {
@query `model`?: string,
}

alias ChatCompletionMessageCollectionOrderQueryParameter = {
/**
* Sort order by the `created_at` timestamp of the objects. `asc` for ascending order and`desc`
* for descending order.
*/
@query order?: ChatCompletionMessageCollectionOrder;
};

union ChatCompletionMessageCollectionOrder {
string,
Ascending: "asc",
Descending: "desc",
}

@access(Access.public)
@usage(Usage.input)
model ChatCompletionMessageCollectionOptions {
...CollectionAfterQueryParameter,
...CollectionLimitQueryParameter,
...ChatCompletionMessageCollectionOrderQueryParameter,
}


2 changes: 0 additions & 2 deletions src/Custom/Chat/ChatClient.Protocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
namespace OpenAI.Chat;

/// <summary> The service client for the OpenAI Chat Completions endpoint. </summary>
[CodeGenSuppress("GetChatCompletionMessagesAsync", typeof(string), typeof(string), typeof(int?), typeof(string), typeof(RequestOptions))]
[CodeGenSuppress("GetChatCompletionMessages", typeof(string), typeof(string), typeof(int?), typeof(string), typeof(RequestOptions))]
[CodeGenSuppress("UpdateChatCompletionAsync", typeof(string), typeof(BinaryContent), typeof(RequestOptions))]
[CodeGenSuppress("UpdateChatCompletion", typeof(string), typeof(BinaryContent), typeof(RequestOptions))]
public partial class ChatClient
Expand Down
2 changes: 0 additions & 2 deletions src/Custom/Chat/ChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ namespace OpenAI.Chat;
[CodeGenSuppress("ChatClient", typeof(ClientPipeline), typeof(Uri))]
[CodeGenSuppress("CompleteChat", typeof(ChatCompletionOptions), typeof(CancellationToken))]
[CodeGenSuppress("CompleteChatAsync", typeof(ChatCompletionOptions), typeof(CancellationToken))]
[CodeGenSuppress("GetChatCompletionMessages", typeof(string), typeof(string), typeof(int?), typeof(OpenAI.VectorStores.VectorStoreCollectionOrder?), typeof(CancellationToken))]
[CodeGenSuppress("GetChatCompletionMessagesAsync", typeof(string), typeof(string), typeof(int?), typeof(OpenAI.VectorStores.VectorStoreCollectionOrder?), typeof(CancellationToken))]
[CodeGenSuppress("UpdateChatCompletion", typeof(string), typeof(IDictionary<string, string>), typeof(CancellationToken))]
[CodeGenSuppress("UpdateChatCompletionAsync", typeof(string), typeof(IDictionary<string, string>), typeof(CancellationToken))]
public partial class ChatClient
Expand Down
5 changes: 5 additions & 0 deletions src/Custom/Chat/ChatCompletionMessageCollectionOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace OpenAI.Chat;

// CUSTOM: Use the correct namespace.
[CodeGenType("ChatCompletionMessageCollectionOptions")]
public partial class ChatCompletionMessageCollectionOptions {}
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 {}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
namespace OpenAI.Chat;

[CodeGenType("ChatCompletionMessageListDatum")]
internal partial class InternalChatCompletionMessageListDatum
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; }
}
56 changes: 56 additions & 0 deletions src/Generated/ChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,61 @@ public virtual async Task<ClientResult> CompleteChatAsync(BinaryContent content,
using PipelineMessage message = CreateCompleteChatRequest(content, options);
return ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
}

[Experimental("OPENAI001")]
public virtual CollectionResult GetChatCompletionMessages(string completionId, string after, int? limit, string order, RequestOptions options)
{
Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));

return new ChatClientGetChatCompletionMessagesCollectionResult(
this,
completionId,
after,
limit,
order,
options);
}

[Experimental("OPENAI001")]
public virtual AsyncCollectionResult GetChatCompletionMessagesAsync(string completionId, string after, int? limit, string order, RequestOptions options)
{
Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));

return new ChatClientGetChatCompletionMessagesAsyncCollectionResult(
this,
completionId,
after,
limit,
order,
options);
}

[Experimental("OPENAI001")]
public virtual CollectionResult<ChatCompletionMessageListDatum> GetChatCompletionMessages(string completionId, ChatCompletionCollectionOptions options = default, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));

return new ChatClientGetChatCompletionMessagesCollectionResultOfT(
this,
completionId,
options?.AfterId,
options?.PageSizeLimit,
options?.Order?.ToString(),
cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
}

[Experimental("OPENAI001")]
public virtual AsyncCollectionResult<ChatCompletionMessageListDatum> GetChatCompletionMessagesAsync(string completionId, ChatCompletionMessageCollectionOptions options = default, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));

return new ChatClientGetChatCompletionMessagesAsyncCollectionResultOfT(
this,
completionId,
options?.AfterId,
options?.PageSizeLimit,
options?.Order?.ToString(),
cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
}
}
}
Loading