diff --git a/src/Custom/Assistants/AssistantClient.cs b/src/Custom/Assistants/AssistantClient.cs index 15838592..542c0e51 100644 --- a/src/Custom/Assistants/AssistantClient.cs +++ b/src/Custom/Assistants/AssistantClient.cs @@ -5,6 +5,7 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Runtime.CompilerServices; +using System.Threading; using System.Threading.Tasks; using static OpenAI.InternalListHelpers; @@ -72,28 +73,30 @@ protected internal AssistantClient(ClientPipeline pipeline, Uri endpoint, OpenAI /// Creates a new assistant. /// The default model that the assistant should use. /// The additional to use. + /// A token that can be used to cancel this method call. /// is null or empty. - public virtual async Task> CreateAssistantAsync(string model, AssistantCreationOptions options = null) + public virtual async Task> CreateAssistantAsync(string model, AssistantCreationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(model, nameof(model)); options ??= new(); options.Model = model; - ClientResult protocolResult = await CreateAssistantAsync(options?.ToBinaryContent(), null).ConfigureAwait(false); + ClientResult protocolResult = await CreateAssistantAsync(options?.ToBinaryContent(), cancellationToken.ToRequestOptions()).ConfigureAwait(false); return CreateResultFromProtocol(protocolResult, Assistant.FromResponse); } /// Creates a new assistant. /// The default model that the assistant should use. /// The additional to use. + /// A token that can be used to cancel this method call. /// is null or empty. - public virtual ClientResult CreateAssistant(string model, AssistantCreationOptions options = null) + public virtual ClientResult CreateAssistant(string model, AssistantCreationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(model, nameof(model)); options ??= new(); options.Model = model; - ClientResult protocolResult = CreateAssistant(options?.ToBinaryContent(), null); + ClientResult protocolResult = CreateAssistant(options?.ToBinaryContent(), cancellationToken.ToRequestOptions()); return CreateResultFromProtocol(protocolResult, Assistant.FromResponse); } @@ -104,11 +107,12 @@ public virtual ClientResult CreateAssistant(string model, AssistantCr /// The order that results should appear in the list according to their created_at /// timestamp. /// + /// A token that can be used to cancel this method call. /// A collection of assistants that can be enumerated using await foreach. - public virtual AsyncPageableCollection GetAssistantsAsync(ListOrder? resultOrder = null) + public virtual AsyncPageableCollection GetAssistantsAsync(ListOrder? resultOrder = null, CancellationToken cancellationToken = default) { return CreateAsyncPageable((continuationToken, pageSize) - => GetAssistantsAsync(pageSize, resultOrder?.ToString(), continuationToken, null, null)); + => GetAssistantsAsync(pageSize, resultOrder?.ToString(), continuationToken, null, cancellationToken.ToRequestOptions())); } /// @@ -118,23 +122,25 @@ public virtual AsyncPageableCollection GetAssistantsAsync(ListOrder? /// The order that results should appear in the list according to their created_at /// timestamp. /// + /// A token that can be used to cancel this method call. /// A collection of assistants that can be enumerated using foreach. - public virtual PageableCollection GetAssistants(ListOrder? resultOrder = null) + public virtual PageableCollection GetAssistants(ListOrder? resultOrder = null, CancellationToken cancellationToken = default) { return CreatePageable((continuationToken, pageSize) - => GetAssistants(pageSize, resultOrder?.ToString(), continuationToken, null, null)); + => GetAssistants(pageSize, resultOrder?.ToString(), continuationToken, null, cancellationToken.ToRequestOptions())); } /// /// Deletes an existing . /// /// The ID of the assistant to delete. + /// A token that can be used to cancel this method call. /// A value indicating whether the deletion was successful. - public virtual async Task> DeleteAssistantAsync(string assistantId) + public virtual async Task> DeleteAssistantAsync(string assistantId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId)); - ClientResult protocolResult = await DeleteAssistantAsync(assistantId, null).ConfigureAwait(false); + ClientResult protocolResult = await DeleteAssistantAsync(assistantId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return CreateResultFromProtocol(protocolResult, response => InternalDeleteAssistantResponse.FromResponse(response).Deleted); } @@ -143,12 +149,13 @@ public virtual async Task> DeleteAssistantAsync(string assist /// Deletes an existing . /// /// The ID of the assistant to delete. + /// A token that can be used to cancel this method call. /// A value indicating whether the deletion was successful. - public virtual ClientResult DeleteAssistant(string assistantId) + public virtual ClientResult DeleteAssistant(string assistantId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId)); - ClientResult protocolResult = DeleteAssistant(assistantId, (RequestOptions)null); + ClientResult protocolResult = DeleteAssistant(assistantId, cancellationToken.ToRequestOptions()); return CreateResultFromProtocol(protocolResult, response => InternalDeleteAssistantResponse.FromResponse(response).Deleted); } @@ -157,10 +164,11 @@ public virtual ClientResult DeleteAssistant(string assistantId) /// Creates a new . /// /// Additional options to use when creating the thread. + /// A token that can be used to cancel this method call. /// A new thread. - public virtual async Task> CreateThreadAsync(ThreadCreationOptions options = null) + public virtual async Task> CreateThreadAsync(ThreadCreationOptions options = null, CancellationToken cancellationToken = default) { - ClientResult protocolResult = await CreateThreadAsync(options?.ToBinaryContent(), null).ConfigureAwait(false); + ClientResult protocolResult = await CreateThreadAsync(options?.ToBinaryContent(), cancellationToken.ToRequestOptions()).ConfigureAwait(false); return CreateResultFromProtocol(protocolResult, AssistantThread.FromResponse); } @@ -168,10 +176,11 @@ public virtual async Task> CreateThreadAsync(Threa /// Creates a new . /// /// Additional options to use when creating the thread. + /// A token that can be used to cancel this method call. /// A new thread. - public virtual ClientResult CreateThread(ThreadCreationOptions options = null) + public virtual ClientResult CreateThread(ThreadCreationOptions options = null, CancellationToken cancellationToken = default) { - ClientResult protocolResult = CreateThread(options?.ToBinaryContent(), null); + ClientResult protocolResult = CreateThread(options?.ToBinaryContent(), cancellationToken.ToRequestOptions()); return CreateResultFromProtocol(protocolResult, AssistantThread.FromResponse); } @@ -179,12 +188,13 @@ public virtual ClientResult CreateThread(ThreadCreationOptions /// Gets an existing , retrieved via a known ID. /// /// The ID of the thread to retrieve. + /// A token that can be used to cancel this method call. /// The existing thread instance. - public virtual async Task> GetThreadAsync(string threadId) + public virtual async Task> GetThreadAsync(string threadId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); - ClientResult protocolResult = await GetThreadAsync(threadId, null).ConfigureAwait(false); + ClientResult protocolResult = await GetThreadAsync(threadId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return CreateResultFromProtocol(protocolResult, AssistantThread.FromResponse); } @@ -192,12 +202,13 @@ public virtual async Task> GetThreadAsync(string t /// Gets an existing , retrieved via a known ID. /// /// The ID of the thread to retrieve. + /// A token that can be used to cancel this method call. /// The existing thread instance. - public virtual ClientResult GetThread(string threadId) + public virtual ClientResult GetThread(string threadId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); - ClientResult protocolResult = GetThread(threadId, null); + ClientResult protocolResult = GetThread(threadId, cancellationToken.ToRequestOptions()); return CreateResultFromProtocol(protocolResult, AssistantThread.FromResponse); } @@ -206,13 +217,14 @@ public virtual ClientResult GetThread(string threadId) /// /// The ID of the thread to modify. /// The modifications to apply to the thread. + /// A token that can be used to cancel this method call. /// The updated instance. - public virtual async Task> ModifyThreadAsync(string threadId, ThreadModificationOptions options) + public virtual async Task> ModifyThreadAsync(string threadId, ThreadModificationOptions options, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNull(options, nameof(options)); - ClientResult protocolResult = await ModifyThreadAsync(threadId, options?.ToBinaryContent(), null).ConfigureAwait(false); + ClientResult protocolResult = await ModifyThreadAsync(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions()).ConfigureAwait(false); return CreateResultFromProtocol(protocolResult, AssistantThread.FromResponse); } @@ -221,13 +233,14 @@ public virtual async Task> ModifyThreadAsync(strin /// /// The ID of the thread to modify. /// The modifications to apply to the thread. + /// A token that can be used to cancel this method call. /// The updated instance. - public virtual ClientResult ModifyThread(string threadId, ThreadModificationOptions options) + public virtual ClientResult ModifyThread(string threadId, ThreadModificationOptions options, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNull(options, nameof(options)); - ClientResult protocolResult = ModifyThread(threadId, options?.ToBinaryContent(), null); + ClientResult protocolResult = ModifyThread(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions()); return CreateResultFromProtocol(protocolResult, AssistantThread.FromResponse); } @@ -235,12 +248,13 @@ public virtual ClientResult ModifyThread(string threadId, Threa /// Deletes an existing . /// /// The ID of the thread to delete. + /// A token that can be used to cancel this method call. /// A value indicating whether the deletion was successful. - public virtual async Task> DeleteThreadAsync(string threadId) + public virtual async Task> DeleteThreadAsync(string threadId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); - ClientResult protocolResult = await DeleteThreadAsync(threadId, null).ConfigureAwait(false); + ClientResult protocolResult = await DeleteThreadAsync(threadId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return CreateResultFromProtocol(protocolResult, response => InternalDeleteThreadResponse.FromResponse(response).Deleted); } @@ -249,12 +263,13 @@ public virtual async Task> DeleteThreadAsync(string threadId) /// Deletes an existing . /// /// The ID of the thread to delete. + /// A token that can be used to cancel this method call. /// A value indicating whether the deletion was successful. - public virtual ClientResult DeleteThread(string threadId) + public virtual ClientResult DeleteThread(string threadId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); - ClientResult protocolResult = DeleteThread(threadId, null); + ClientResult protocolResult = DeleteThread(threadId, cancellationToken.ToRequestOptions()); return CreateResultFromProtocol(protocolResult, response => InternalDeleteThreadResponse.FromResponse(response).Deleted); } @@ -265,11 +280,13 @@ public virtual ClientResult DeleteThread(string threadId) /// The ID of the thread to associate the new message with. /// The collection of items for the message. /// Additional options to apply to the new message. + /// A token that can be used to cancel this method call. /// A new . public virtual async Task> CreateMessageAsync( string threadId, IEnumerable content, - MessageCreationOptions options = null) + MessageCreationOptions options = null, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); options ??= new(); @@ -279,7 +296,7 @@ public virtual async Task> CreateMessageAsync( options.Content.Add(contentItem); } - ClientResult protocolResult = await CreateMessageAsync(threadId, options?.ToBinaryContent(), null) + ClientResult protocolResult = await CreateMessageAsync(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions()) .ConfigureAwait(false); return CreateResultFromProtocol(protocolResult, ThreadMessage.FromResponse); } @@ -290,11 +307,13 @@ public virtual async Task> CreateMessageAsync( /// The ID of the thread to associate the new message with. /// The collection of items for the message. /// Additional options to apply to the new message. + /// A token that can be used to cancel this method call. /// A new . public virtual ClientResult CreateMessage( string threadId, IEnumerable content, - MessageCreationOptions options = null) + MessageCreationOptions options = null, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); options ??= new(); @@ -304,7 +323,7 @@ public virtual ClientResult CreateMessage( options.Content.Add(contentItem); } - ClientResult protocolResult = CreateMessage(threadId, options?.ToBinaryContent(), null); + ClientResult protocolResult = CreateMessage(threadId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions()); return CreateResultFromProtocol(protocolResult, ThreadMessage.FromResponse); } @@ -316,15 +335,17 @@ public virtual ClientResult CreateMessage( /// The order that results should appear in the list according to their created_at /// timestamp. /// + /// A token that can be used to cancel this method call. /// A collection of messages that can be enumerated using await foreach. public virtual AsyncPageableCollection GetMessagesAsync( string threadId, - ListOrder? resultOrder = null) + ListOrder? resultOrder = null, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); return CreateAsyncPageable((continuationToken, pageSize) - => GetMessagesAsync(threadId, pageSize, resultOrder?.ToString(), continuationToken, null, null)); + => GetMessagesAsync(threadId, pageSize, resultOrder?.ToString(), continuationToken, null, cancellationToken.ToRequestOptions())); } /// @@ -335,15 +356,17 @@ public virtual AsyncPageableCollection GetMessagesAsync( /// The order that results should appear in the list according to their created_at /// timestamp. /// + /// A token that can be used to cancel this method call. /// A collection of messages that can be enumerated using foreach. public virtual PageableCollection GetMessages( string threadId, - ListOrder? resultOrder = null) + ListOrder? resultOrder = null, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); return CreatePageable((continuationToken, pageSize) - => GetMessages(threadId, pageSize, resultOrder?.ToString(), continuationToken, null, null)); + => GetMessages(threadId, pageSize, resultOrder?.ToString(), continuationToken, null, cancellationToken.ToRequestOptions())); } /// @@ -351,13 +374,14 @@ public virtual PageableCollection GetMessages( /// /// The ID of the thread to retrieve the message from. /// The ID of the message to retrieve. + /// A token that can be used to cancel this method call. /// The existing instance. - public virtual async Task> GetMessageAsync(string threadId, string messageId) + public virtual async Task> GetMessageAsync(string threadId, string messageId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(messageId, nameof(messageId)); - ClientResult protocolResult = await GetMessageAsync(threadId, messageId, null).ConfigureAwait(false); + ClientResult protocolResult = await GetMessageAsync(threadId, messageId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return CreateResultFromProtocol(protocolResult, ThreadMessage.FromResponse); } @@ -366,13 +390,14 @@ public virtual async Task> GetMessageAsync(string th /// /// The ID of the thread to retrieve the message from. /// The ID of the message to retrieve. + /// A token that can be used to cancel this method call. /// The existing instance. - public virtual ClientResult GetMessage(string threadId, string messageId) + public virtual ClientResult GetMessage(string threadId, string messageId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(messageId, nameof(messageId)); - ClientResult protocolResult = GetMessage(threadId, messageId, null); + ClientResult protocolResult = GetMessage(threadId, messageId, cancellationToken.ToRequestOptions()); return CreateResultFromProtocol(protocolResult, ThreadMessage.FromResponse); } @@ -382,14 +407,15 @@ public virtual ClientResult GetMessage(string threadId, string me /// The ID of the thread associated with the message to modify. /// The ID of the message to modify. /// The changes to apply to the message. + /// A token that can be used to cancel this method call. /// The updated . - public virtual async Task> ModifyMessageAsync(string threadId, string messageId, MessageModificationOptions options) + public virtual async Task> ModifyMessageAsync(string threadId, string messageId, MessageModificationOptions options, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(messageId, nameof(messageId)); Argument.AssertNotNull(options, nameof(options)); - ClientResult protocolResult = await ModifyMessageAsync(threadId, messageId, options?.ToBinaryContent(), null) + ClientResult protocolResult = await ModifyMessageAsync(threadId, messageId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions()) .ConfigureAwait(false); return CreateResultFromProtocol(protocolResult, ThreadMessage.FromResponse); } @@ -400,14 +426,15 @@ public virtual async Task> ModifyMessageAsync(string /// The ID of the thread associated with the message to modify. /// The ID of the message to modify. /// The changes to apply to the message. + /// A token that can be used to cancel this method call. /// The updated . - public virtual ClientResult ModifyMessage(string threadId, string messageId, MessageModificationOptions options) + public virtual ClientResult ModifyMessage(string threadId, string messageId, MessageModificationOptions options, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(messageId, nameof(messageId)); Argument.AssertNotNull(options, nameof(options)); - ClientResult protocolResult = ModifyMessage(threadId, messageId, options?.ToBinaryContent(), null); + ClientResult protocolResult = ModifyMessage(threadId, messageId, options?.ToBinaryContent(), cancellationToken.ToRequestOptions()); return CreateResultFromProtocol(protocolResult, ThreadMessage.FromResponse); } @@ -416,13 +443,14 @@ public virtual ClientResult ModifyMessage(string threadId, string /// /// The ID of the thread associated with the message. /// The ID of the message. + /// A token that can be used to cancel this method call. /// A value indicating whether the deletion was successful. - public virtual async Task> DeleteMessageAsync(string threadId, string messageId) + public virtual async Task> DeleteMessageAsync(string threadId, string messageId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(messageId, nameof(messageId)); - ClientResult protocolResult = await DeleteMessageAsync(threadId, messageId, null).ConfigureAwait(false); + ClientResult protocolResult = await DeleteMessageAsync(threadId, messageId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return CreateResultFromProtocol(protocolResult, response => InternalDeleteMessageResponse.FromResponse(response).Deleted); } @@ -432,13 +460,14 @@ public virtual async Task> DeleteMessageAsync(string threadId /// /// The ID of the thread associated with the message. /// The ID of the message. + /// A token that can be used to cancel this method call. /// A value indicating whether the deletion was successful. - public virtual ClientResult DeleteMessage(string threadId, string messageId) + public virtual ClientResult DeleteMessage(string threadId, string messageId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(messageId, nameof(messageId)); - ClientResult protocolResult = DeleteMessage(threadId, messageId, null); + ClientResult protocolResult = DeleteMessage(threadId, messageId, cancellationToken.ToRequestOptions()); return CreateResultFromProtocol(protocolResult, response => InternalDeleteMessageResponse.FromResponse(response).Deleted); } @@ -450,8 +479,9 @@ public virtual ClientResult DeleteMessage(string threadId, string messageI /// The ID of the thread that the run should evaluate. /// The ID of the assistant that should be used when evaluating the thread. /// Additional options for the run. + /// A token that can be used to cancel this method call. /// A new instance. - public virtual async Task> CreateRunAsync(string threadId, string assistantId, RunCreationOptions options = null) + public virtual async Task> CreateRunAsync(string threadId, string assistantId, RunCreationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId)); @@ -459,7 +489,7 @@ public virtual async Task> CreateRunAsync(string threadI options.AssistantId = assistantId; options.Stream = null; - ClientResult protocolResult = await CreateRunAsync(threadId, options.ToBinaryContent(), null) + ClientResult protocolResult = await CreateRunAsync(threadId, options.ToBinaryContent(), cancellationToken.ToRequestOptions()) .ConfigureAwait(false); return CreateResultFromProtocol(protocolResult, ThreadRun.FromResponse); } @@ -471,8 +501,9 @@ public virtual async Task> CreateRunAsync(string threadI /// The ID of the thread that the run should evaluate. /// The ID of the assistant that should be used when evaluating the thread. /// Additional options for the run. + /// A token that can be used to cancel this method call. /// A new instance. - public virtual ClientResult CreateRun(string threadId, string assistantId, RunCreationOptions options = null) + public virtual ClientResult CreateRun(string threadId, string assistantId, RunCreationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId)); @@ -480,7 +511,7 @@ public virtual ClientResult CreateRun(string threadId, string assista options.AssistantId = assistantId; options.Stream = null; - ClientResult protocolResult = CreateRun(threadId, options.ToBinaryContent(), null); + ClientResult protocolResult = CreateRun(threadId, options.ToBinaryContent(), cancellationToken.ToRequestOptions()); return CreateResultFromProtocol(protocolResult, ThreadRun.FromResponse); } @@ -491,10 +522,12 @@ public virtual ClientResult CreateRun(string threadId, string assista /// The ID of the thread that the run should evaluate. /// The ID of the assistant that should be used when evaluating the thread. /// Additional options for the run. + /// A token that can be used to cancel this method call. public virtual AsyncResultCollection CreateRunStreamingAsync( string threadId, string assistantId, - RunCreationOptions options = null) + RunCreationOptions options = null, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId)); @@ -504,7 +537,7 @@ public virtual AsyncResultCollection CreateRunStreamingAsync( options.Stream = true; async Task getResultAsync() => - await CreateRunAsync(threadId, options.ToBinaryContent(), StreamRequestOptions) + await CreateRunAsync(threadId, options.ToBinaryContent(), cancellationToken.ToRequestOptions(streaming: true)) .ConfigureAwait(false); return new AsyncStreamingUpdateCollection(getResultAsync); @@ -517,10 +550,12 @@ await CreateRunAsync(threadId, options.ToBinaryContent(), StreamRequestOptions) /// The ID of the thread that the run should evaluate. /// The ID of the assistant that should be used when evaluating the thread. /// Additional options for the run. + /// A token that can be used to cancel this method call. public virtual ResultCollection CreateRunStreaming( string threadId, string assistantId, - RunCreationOptions options = null) + RunCreationOptions options = null, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId)); @@ -529,7 +564,7 @@ public virtual ResultCollection CreateRunStreaming( options.AssistantId = assistantId; options.Stream = true; - ClientResult getResult() => CreateRun(threadId, options.ToBinaryContent(), StreamRequestOptions); + ClientResult getResult() => CreateRun(threadId, options.ToBinaryContent(), cancellationToken.ToRequestOptions(streaming: true)); return new StreamingUpdateCollection(getResult); } @@ -540,16 +575,18 @@ public virtual ResultCollection CreateRunStreaming( /// The ID of the assistant that the new run should use. /// Options for the new thread that will be created. /// Additional options to apply to the run that will begin. + /// A token that can be used to cancel this method call. /// A new . public virtual async Task> CreateThreadAndRunAsync( string assistantId, ThreadCreationOptions threadOptions = null, - RunCreationOptions runOptions = null) + RunCreationOptions runOptions = null, + CancellationToken cancellationToken = default) { runOptions ??= new(); runOptions.Stream = null; BinaryContent protocolContent = CreateThreadAndRunProtocolContent(assistantId, threadOptions, runOptions); - ClientResult protocolResult = await CreateThreadAndRunAsync(protocolContent, null).ConfigureAwait(false); + ClientResult protocolResult = await CreateThreadAndRunAsync(protocolContent, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return CreateResultFromProtocol(protocolResult, ThreadRun.FromResponse); } @@ -559,16 +596,18 @@ public virtual async Task> CreateThreadAndRunAsync( /// The ID of the assistant that the new run should use. /// Options for the new thread that will be created. /// Additional options to apply to the run that will begin. + /// A token that can be used to cancel this method call. /// A new . public virtual ClientResult CreateThreadAndRun( string assistantId, ThreadCreationOptions threadOptions = null, - RunCreationOptions runOptions = null) + RunCreationOptions runOptions = null, + CancellationToken cancellationToken = default) { runOptions ??= new(); runOptions.Stream = null; BinaryContent protocolContent = CreateThreadAndRunProtocolContent(assistantId, threadOptions, runOptions); - ClientResult protocolResult = CreateThreadAndRun(protocolContent, null); + ClientResult protocolResult = CreateThreadAndRun(protocolContent, cancellationToken.ToRequestOptions()); return CreateResultFromProtocol(protocolResult, ThreadRun.FromResponse); } @@ -578,10 +617,12 @@ public virtual ClientResult CreateThreadAndRun( /// The ID of the assistant that the new run should use. /// Options for the new thread that will be created. /// Additional options to apply to the run that will begin. + /// A token that can be used to cancel this method call. public virtual AsyncResultCollection CreateThreadAndRunStreamingAsync( string assistantId, ThreadCreationOptions threadOptions = null, - RunCreationOptions runOptions = null) + RunCreationOptions runOptions = null, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId)); @@ -590,7 +631,7 @@ public virtual AsyncResultCollection CreateThreadAndRunStreamin BinaryContent protocolContent = CreateThreadAndRunProtocolContent(assistantId, threadOptions, runOptions); async Task getResultAsync() => - await CreateThreadAndRunAsync(protocolContent, StreamRequestOptions) + await CreateThreadAndRunAsync(protocolContent, cancellationToken.ToRequestOptions(streaming: true)) .ConfigureAwait(false); return new AsyncStreamingUpdateCollection(getResultAsync); @@ -602,10 +643,12 @@ await CreateThreadAndRunAsync(protocolContent, StreamRequestOptions) /// The ID of the assistant that the new run should use. /// Options for the new thread that will be created. /// Additional options to apply to the run that will begin. + /// A token that can be used to cancel this method call. public virtual ResultCollection CreateThreadAndRunStreaming( string assistantId, ThreadCreationOptions threadOptions = null, - RunCreationOptions runOptions = null) + RunCreationOptions runOptions = null, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(assistantId, nameof(assistantId)); @@ -613,7 +656,7 @@ public virtual ResultCollection CreateThreadAndRunStreaming( runOptions.Stream = true; BinaryContent protocolContent = CreateThreadAndRunProtocolContent(assistantId, threadOptions, runOptions); - ClientResult getResult() => CreateThreadAndRun(protocolContent, StreamRequestOptions); + ClientResult getResult() => CreateThreadAndRun(protocolContent, cancellationToken.ToRequestOptions(streaming: true)); return new StreamingUpdateCollection(getResult); } @@ -626,15 +669,17 @@ public virtual ResultCollection CreateThreadAndRunStreaming( /// The order that results should appear in the list according to their created_at /// timestamp. /// + /// A token that can be used to cancel this method call. /// A collection of runs that can be enumerated using await foreach. public virtual AsyncPageableCollection GetRunsAsync( string threadId, - ListOrder? resultOrder = default) + ListOrder? resultOrder = default, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); return CreateAsyncPageable((continuationToken, pageSize) - => GetRunsAsync(threadId, pageSize, resultOrder?.ToString(), continuationToken, null, null)); + => GetRunsAsync(threadId, pageSize, resultOrder?.ToString(), continuationToken, null, cancellationToken.ToRequestOptions())); } /// @@ -645,15 +690,17 @@ public virtual AsyncPageableCollection GetRunsAsync( /// The order that results should appear in the list according to their created_at /// timestamp. /// + /// A token that can be used to cancel this method call. /// A collection of runs that can be enumerated using foreach. public virtual PageableCollection GetRuns( string threadId, - ListOrder? resultOrder = default) + ListOrder? resultOrder = default, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); return CreatePageable((continuationToken, pageSize) - => GetRuns(threadId, pageSize, resultOrder?.ToString(), continuationToken, null, null)); + => GetRuns(threadId, pageSize, resultOrder?.ToString(), continuationToken, null, cancellationToken.ToRequestOptions())); } /// @@ -661,13 +708,14 @@ public virtual PageableCollection GetRuns( /// /// The ID of the thread to retrieve the run from. /// The ID of the run to retrieve. + /// A token that can be used to cancel this method call. /// The existing instance. - public virtual async Task> GetRunAsync(string threadId, string runId) + public virtual async Task> GetRunAsync(string threadId, string runId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(runId, nameof(runId)); - ClientResult protocolResult = await GetRunAsync(threadId, runId, null).ConfigureAwait(false); + ClientResult protocolResult = await GetRunAsync(threadId, runId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return CreateResultFromProtocol(protocolResult, ThreadRun.FromResponse); } @@ -676,13 +724,14 @@ public virtual async Task> GetRunAsync(string threadId, /// /// The ID of the thread to retrieve the run from. /// The ID of the run to retrieve. + /// A token that can be used to cancel this method call. /// The existing instance. - public virtual ClientResult GetRun(string threadId, string runId) + public virtual ClientResult GetRun(string threadId, string runId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(runId, nameof(runId)); - ClientResult protocolResult = GetRun(threadId, runId, null); + ClientResult protocolResult = GetRun(threadId, runId, cancellationToken.ToRequestOptions()); return CreateResultFromProtocol(protocolResult, ThreadRun.FromResponse); } @@ -694,17 +743,19 @@ public virtual ClientResult GetRun(string threadId, string runId) /// /// The tool outputs, corresponding to instances from the run. /// + /// A token that can be used to cancel this method call. /// The , updated after the submission was processed. public virtual async Task> SubmitToolOutputsToRunAsync( string threadId, string runId, - IEnumerable toolOutputs) + IEnumerable toolOutputs, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(runId, nameof(runId)); BinaryContent content = new InternalSubmitToolOutputsRunRequest(toolOutputs).ToBinaryContent(); - ClientResult protocolResult = await SubmitToolOutputsToRunAsync(threadId, runId, content, null) + ClientResult protocolResult = await SubmitToolOutputsToRunAsync(threadId, runId, content, cancellationToken.ToRequestOptions()) .ConfigureAwait(false); return CreateResultFromProtocol(protocolResult, ThreadRun.FromResponse); } @@ -717,17 +768,19 @@ public virtual async Task> SubmitToolOutputsToRunAsync( /// /// The tool outputs, corresponding to instances from the run. /// + /// A token that can be used to cancel this method call. /// The , updated after the submission was processed. public virtual ClientResult SubmitToolOutputsToRun( string threadId, string runId, - IEnumerable toolOutputs) + IEnumerable toolOutputs, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(runId, nameof(runId)); BinaryContent content = new InternalSubmitToolOutputsRunRequest(toolOutputs).ToBinaryContent(); - ClientResult protocolResult = SubmitToolOutputsToRun(threadId, runId, content, null); + ClientResult protocolResult = SubmitToolOutputsToRun(threadId, runId, content, cancellationToken.ToRequestOptions()); return CreateResultFromProtocol(protocolResult, ThreadRun.FromResponse); } @@ -739,10 +792,12 @@ public virtual ClientResult SubmitToolOutputsToRun( /// /// The tool outputs, corresponding to instances from the run. /// + /// A token that can be used to cancel this method call. public virtual AsyncResultCollection SubmitToolOutputsToRunStreamingAsync( string threadId, string runId, - IEnumerable toolOutputs) + IEnumerable toolOutputs, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(runId, nameof(runId)); @@ -751,7 +806,7 @@ public virtual AsyncResultCollection SubmitToolOutputsToRunStre .ToBinaryContent(); async Task getResultAsync() => - await SubmitToolOutputsToRunAsync(threadId, runId, content, StreamRequestOptions) + await SubmitToolOutputsToRunAsync(threadId, runId, content, cancellationToken.ToRequestOptions(streaming: true)) .ConfigureAwait(false); return new AsyncStreamingUpdateCollection(getResultAsync); @@ -765,10 +820,12 @@ await SubmitToolOutputsToRunAsync(threadId, runId, content, StreamRequestOptions /// /// The tool outputs, corresponding to instances from the run. /// + /// A token that can be used to cancel this method call. public virtual ResultCollection SubmitToolOutputsToRunStreaming( string threadId, string runId, - IEnumerable toolOutputs) + IEnumerable toolOutputs, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(runId, nameof(runId)); @@ -776,7 +833,7 @@ public virtual ResultCollection SubmitToolOutputsToRunStreaming BinaryContent content = new InternalSubmitToolOutputsRunRequest(toolOutputs.ToList(), stream: true, null) .ToBinaryContent(); - ClientResult getResult() => SubmitToolOutputsToRun(threadId, runId, content, StreamRequestOptions); + ClientResult getResult() => SubmitToolOutputsToRun(threadId, runId, content, cancellationToken.ToRequestOptions(streaming: true)); return new StreamingUpdateCollection(getResult); } @@ -786,13 +843,14 @@ public virtual ResultCollection SubmitToolOutputsToRunStreaming /// /// The ID of the thread associated with the run. /// The ID of the run to cancel. + /// A token that can be used to cancel this method call. /// An updated instance, reflecting the new status of the run. - public virtual async Task> CancelRunAsync(string threadId, string runId) + public virtual async Task> CancelRunAsync(string threadId, string runId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(runId, nameof(runId)); - ClientResult protocolResult = await CancelRunAsync(threadId, runId, null).ConfigureAwait(false); + ClientResult protocolResult = await CancelRunAsync(threadId, runId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return CreateResultFromProtocol(protocolResult, ThreadRun.FromResponse); } @@ -801,13 +859,14 @@ public virtual async Task> CancelRunAsync(string threadI /// /// The ID of the thread associated with the run. /// The ID of the run to cancel. + /// A token that can be used to cancel this method call. /// An updated instance, reflecting the new status of the run. - public virtual ClientResult CancelRun(string threadId, string runId) + public virtual ClientResult CancelRun(string threadId, string runId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(runId, nameof(runId)); - ClientResult protocolResult = CancelRun(threadId, runId, null); + ClientResult protocolResult = CancelRun(threadId, runId, cancellationToken.ToRequestOptions()); return CreateResultFromProtocol(protocolResult, ThreadRun.FromResponse); } @@ -820,17 +879,19 @@ public virtual ClientResult CancelRun(string threadId, string runId) /// The order that results should appear in the list according to their created_at /// timestamp. /// + /// A token that can be used to cancel this method call. /// A collection of run steps that can be enumerated using await foreach. public virtual AsyncPageableCollection GetRunStepsAsync( string threadId, string runId, - ListOrder? resultOrder = default) + ListOrder? resultOrder = default, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(runId, nameof(runId)); return CreateAsyncPageable((continuationToken, pageSize) - => GetRunStepsAsync(threadId, runId, pageSize, resultOrder?.ToString(), continuationToken, null, null)); + => GetRunStepsAsync(threadId, runId, pageSize, resultOrder?.ToString(), continuationToken, null, cancellationToken.ToRequestOptions())); } /// @@ -842,17 +903,19 @@ public virtual AsyncPageableCollection GetRunStepsAsync( /// The order that results should appear in the list according to their created_at /// timestamp. /// + /// A token that can be used to cancel this method call. /// A collection of run steps that can be enumerated using foreach. public virtual PageableCollection GetRunSteps( string threadId, string runId, - ListOrder? resultOrder = default) + ListOrder? resultOrder = default, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(threadId, nameof(threadId)); Argument.AssertNotNullOrEmpty(runId, nameof(runId)); return CreatePageable((continuationToken, pageSize) - => GetRunSteps(threadId, runId, pageSize, resultOrder?.ToString(), continuationToken, null, null)); + => GetRunSteps(threadId, runId, pageSize, resultOrder?.ToString(), continuationToken, null, cancellationToken.ToRequestOptions())); } /// @@ -861,10 +924,11 @@ public virtual PageableCollection GetRunSteps( /// The ID of the thread associated with the run. /// The ID of the run. /// The ID of the run step. + /// A token that can be used to cancel this method call. /// A instance corresponding to the specified step. - public virtual async Task> GetRunStepAsync(string threadId, string runId, string stepId) + public virtual async Task> GetRunStepAsync(string threadId, string runId, string stepId, CancellationToken cancellationToken = default) { - ClientResult protocolResult = await GetRunStepAsync(threadId, runId, stepId, null).ConfigureAwait(false); + ClientResult protocolResult = await GetRunStepAsync(threadId, runId, stepId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return CreateResultFromProtocol(protocolResult, RunStep.FromResponse); } @@ -874,10 +938,11 @@ public virtual async Task> GetRunStepAsync(string threadId /// The ID of the thread associated with the run. /// The ID of the run. /// The ID of the run step. + /// A token that can be used to cancel this method call. /// A instance corresponding to the specified step. - public virtual ClientResult GetRunStep(string threadId, string runId, string stepId) + public virtual ClientResult GetRunStep(string threadId, string runId, string stepId, CancellationToken cancellationToken = default) { - ClientResult protocolResult = GetRunStep(threadId, runId, stepId, null); + ClientResult protocolResult = GetRunStep(threadId, runId, stepId, cancellationToken.ToRequestOptions()); return CreateResultFromProtocol(protocolResult, RunStep.FromResponse); } @@ -915,7 +980,4 @@ private static ClientResult CreateResultFromProtocol(ClientResult protocol T deserializedResultValue = responseDeserializer.Invoke(pipelineResponse); return ClientResult.FromValue(deserializedResultValue, pipelineResponse); } - - private static RequestOptions StreamRequestOptions => _streamRequestOptions ??= new() { BufferResponse = false }; - private static RequestOptions _streamRequestOptions; } diff --git a/src/Custom/Audio/AudioClient.cs b/src/Custom/Audio/AudioClient.cs index e5adcd6c..bf5ab688 100644 --- a/src/Custom/Audio/AudioClient.cs +++ b/src/Custom/Audio/AudioClient.cs @@ -2,6 +2,7 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.IO; +using System.Threading; using System.Threading.Tasks; namespace OpenAI.Audio; @@ -85,8 +86,9 @@ protected internal AudioClient(ClientPipeline pipeline, string model, Uri endpoi /// The text for the voice to speak. /// The voice to use. /// Additional options to tailor the text-to-speech request. + /// A token that can be used to cancel this method call. /// The generated audio in the specified output format. - public virtual async Task> GenerateSpeechFromTextAsync(string text, GeneratedSpeechVoice voice, SpeechGenerationOptions options = null) + public virtual async Task> GenerateSpeechFromTextAsync(string text, GeneratedSpeechVoice voice, SpeechGenerationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(text, nameof(text)); @@ -94,7 +96,7 @@ public virtual async Task> GenerateSpeechFromTextAsync( CreateSpeechGenerationOptions(text, voice, ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = await GenerateSpeechFromTextAsync(content, null).ConfigureAwait(false); + ClientResult result = await GenerateSpeechFromTextAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse()); } @@ -108,8 +110,9 @@ public virtual async Task> GenerateSpeechFromTextAsync( /// The text for the voice to speak. /// The voice to use. /// Additional options to tailor the text-to-speech request. + /// A token that can be used to cancel this method call. /// The generated audio in the specified output format. - public virtual ClientResult GenerateSpeechFromText(string text, GeneratedSpeechVoice voice, SpeechGenerationOptions options = null) + public virtual ClientResult GenerateSpeechFromText(string text, GeneratedSpeechVoice voice, SpeechGenerationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(text, nameof(text)); @@ -117,7 +120,7 @@ public virtual ClientResult GenerateSpeechFromText(string text, Gene CreateSpeechGenerationOptions(text, voice, ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = GenerateSpeechFromText(content, (RequestOptions)null); + ClientResult result = GenerateSpeechFromText(content, cancellationToken.ToRequestOptions()); ; return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse()); } @@ -135,10 +138,11 @@ public virtual ClientResult GenerateSpeechFromText(string text, Gene /// not match. /// /// Additional options to tailor the audio transcription request. + /// A token that can be used to cancel this method call. /// or is null. /// is an empty string, and was expected to be non-empty. /// The audio transcription. - public virtual async Task> TranscribeAudioAsync(Stream audio, string audioFilename, AudioTranscriptionOptions options = null) + public virtual async Task> TranscribeAudioAsync(Stream audio, string audioFilename, AudioTranscriptionOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(audio, nameof(audio)); Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename)); @@ -147,7 +151,7 @@ public virtual async Task> TranscribeAudioAsync CreateAudioTranscriptionOptions(audio, audioFilename, ref options); using MultipartFormDataBinaryContent content = options.ToMultipartContent(audio, audioFilename); - ClientResult result = await TranscribeAudioAsync(content, content.ContentType).ConfigureAwait(false); + ClientResult result = await TranscribeAudioAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(AudioTranscription.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -161,10 +165,11 @@ public virtual async Task> TranscribeAudioAsync /// not match. /// /// Additional options to tailor the audio transcription request. + /// A token that can be used to cancel this method call. /// or is null. /// is an empty string, and was expected to be non-empty. /// The audio transcription. - public virtual ClientResult TranscribeAudio(Stream audio, string audioFilename, AudioTranscriptionOptions options = null) + public virtual ClientResult TranscribeAudio(Stream audio, string audioFilename, AudioTranscriptionOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(audio, nameof(audio)); Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename)); @@ -173,7 +178,7 @@ public virtual ClientResult TranscribeAudio(Stream audio, st CreateAudioTranscriptionOptions(audio, audioFilename, ref options); using MultipartFormDataBinaryContent content = options.ToMultipartContent(audio, audioFilename); - ClientResult result = TranscribeAudio(content, content.ContentType); + ClientResult result = TranscribeAudio(content, content.ContentType, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(AudioTranscription.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -229,10 +234,11 @@ public virtual ClientResult TranscribeAudio(string audioFile /// not match. /// /// Additional options to tailor the audio translation request. + /// A token that can be used to cancel this method call. /// or is null. /// is an empty string, and was expected to be non-empty. /// The audio translation. - public virtual async Task> TranslateAudioAsync(Stream audio, string audioFilename, AudioTranslationOptions options = null) + public virtual async Task> TranslateAudioAsync(Stream audio, string audioFilename, AudioTranslationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(audio, nameof(audio)); Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename)); @@ -241,7 +247,7 @@ public virtual async Task> TranslateAudioAsync(St CreateAudioTranslationOptions(audio, audioFilename, ref options); using MultipartFormDataBinaryContent content = options.ToMultipartContent(audio, audioFilename); - ClientResult result = await TranslateAudioAsync(content, content.ContentType).ConfigureAwait(false); + ClientResult result = await TranslateAudioAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(AudioTranslation.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -253,10 +259,11 @@ public virtual async Task> TranslateAudioAsync(St /// not match. /// /// Additional options to tailor the audio translation request. + /// A token that can be used to cancel this method call. /// or is null. /// is an empty string, and was expected to be non-empty. /// The audio translation. - public virtual ClientResult TranslateAudio(Stream audio, string audioFilename, AudioTranslationOptions options = null) + public virtual ClientResult TranslateAudio(Stream audio, string audioFilename, AudioTranslationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(audio, nameof(audio)); Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename)); @@ -265,7 +272,7 @@ public virtual ClientResult TranslateAudio(Stream audio, strin CreateAudioTranslationOptions(audio, audioFilename, ref options); using MultipartFormDataBinaryContent content = options.ToMultipartContent(audio, audioFilename); - ClientResult result = TranslateAudio(content, content.ContentType); + ClientResult result = TranslateAudio(content, content.ContentType, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(AudioTranslation.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } diff --git a/src/Custom/Chat/ChatClient.cs b/src/Custom/Chat/ChatClient.cs index 68e5e70b..0c509b39 100644 --- a/src/Custom/Chat/ChatClient.cs +++ b/src/Custom/Chat/ChatClient.cs @@ -3,6 +3,7 @@ using System.ClientModel.Primitives; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; namespace OpenAI.Chat; @@ -68,8 +69,9 @@ protected internal ChatClient(ClientPipeline pipeline, string model, Uri endpoin /// /// The messages to provide as input and history for chat completion. /// Additional options for the chat completion request. + /// A token that can be used to cancel this method call. /// A result for a single chat completion. - public virtual async Task> CompleteChatAsync(IEnumerable messages, ChatCompletionOptions options = null) + public virtual async Task> CompleteChatAsync(IEnumerable messages, ChatCompletionOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(messages, nameof(messages)); @@ -77,7 +79,8 @@ public virtual async Task> CompleteChatAsync(IEnume CreateChatCompletionOptions(messages, ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = await CompleteChatAsync(content, null).ConfigureAwait(false); + + ClientResult result = await CompleteChatAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(ChatCompletion.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -94,8 +97,9 @@ public virtual async Task> CompleteChatAsync(params /// /// The messages to provide as input and history for chat completion. /// Additional options for the chat completion request. + /// A token that can be used to cancel this method call. /// A result for a single chat completion. - public virtual ClientResult CompleteChat(IEnumerable messages, ChatCompletionOptions options = null) + public virtual ClientResult CompleteChat(IEnumerable messages, ChatCompletionOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(messages, nameof(messages)); @@ -103,7 +107,7 @@ public virtual ClientResult CompleteChat(IEnumerable CompleteChat(params ChatMessage[] me /// /// The messages to provide as input for chat completion. /// Additional options for the chat completion request. + /// A token that can be used to cancel this method call. /// A streaming result with incremental chat completion updates. - public virtual AsyncResultCollection CompleteChatStreamingAsync(IEnumerable messages, ChatCompletionOptions options = null) + public virtual AsyncResultCollection CompleteChatStreamingAsync(IEnumerable messages, ChatCompletionOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(messages, nameof(messages)); @@ -135,9 +140,9 @@ public virtual AsyncResultCollection CompleteChat CreateChatCompletionOptions(messages, ref options, stream: true); using BinaryContent content = options.ToBinaryContent(); - RequestOptions requestOptions = new() { BufferResponse = false }; + async Task getResultAsync() => - await CompleteChatAsync(content, requestOptions).ConfigureAwait(false); + await CompleteChatAsync(content, cancellationToken.ToRequestOptions(streaming: true)).ConfigureAwait(false); return new AsyncStreamingChatCompletionUpdateCollection(getResultAsync); } @@ -164,8 +169,9 @@ public virtual AsyncResultCollection CompleteChat /// /// The messages to provide as input for chat completion. /// Additional options for the chat completion request. + /// A token that can be used to cancel this method call. /// A streaming result with incremental chat completion updates. - public virtual ResultCollection CompleteChatStreaming(IEnumerable messages, ChatCompletionOptions options = null) + public virtual ResultCollection CompleteChatStreaming(IEnumerable messages, ChatCompletionOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(messages, nameof(messages)); @@ -173,8 +179,7 @@ public virtual ResultCollection CompleteChatStrea CreateChatCompletionOptions(messages, ref options, stream: true); using BinaryContent content = options.ToBinaryContent(); - RequestOptions requestOptions = new() { BufferResponse = false }; - ClientResult getResult() => CompleteChat(content, requestOptions); + ClientResult getResult() => CompleteChat(content, cancellationToken.ToRequestOptions(streaming: true)); return new StreamingChatCompletionUpdateCollection(getResult); } diff --git a/src/Custom/Common/CancellationTokenExtensions.cs b/src/Custom/Common/CancellationTokenExtensions.cs new file mode 100644 index 00000000..500a1fdc --- /dev/null +++ b/src/Custom/Common/CancellationTokenExtensions.cs @@ -0,0 +1,22 @@ +using System.ClientModel.Primitives; +using System.Threading; + +internal static class CancellationTokenExtensions +{ + public static RequestOptions ToRequestOptions(this CancellationToken cancellationToken, bool streaming = false) + { + if (cancellationToken == default) + { + if (!streaming) return null; + return StreamRequestOptions; + } + + return new RequestOptions() { + CancellationToken = cancellationToken, + BufferResponse = !streaming, + }; + } + + private static RequestOptions StreamRequestOptions => _streamRequestOptions ??= new() { BufferResponse = false }; + private static RequestOptions _streamRequestOptions; +} diff --git a/src/Custom/Embeddings/EmbeddingClient.cs b/src/Custom/Embeddings/EmbeddingClient.cs index f4c47b98..351b29e6 100644 --- a/src/Custom/Embeddings/EmbeddingClient.cs +++ b/src/Custom/Embeddings/EmbeddingClient.cs @@ -3,6 +3,7 @@ using System.ClientModel.Primitives; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; namespace OpenAI.Embeddings; @@ -74,9 +75,10 @@ protected internal EmbeddingClient(ClientPipeline pipeline, string model, Uri en /// Creates an embedding vector representing the input text. /// The string that will be turned into an embedding. /// The to use. + /// A token that can be used to cancel this method call. /// is null. /// is an empty string, and was expected to be non-empty. - public virtual async Task> GenerateEmbeddingAsync(string input, EmbeddingGenerationOptions options = null) + public virtual async Task> GenerateEmbeddingAsync(string input, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(input, nameof(input)); @@ -84,7 +86,7 @@ public virtual async Task> GenerateEmbeddingAsync(string CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(input), ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = await GenerateEmbeddingsAsync(content, (RequestOptions)null).ConfigureAwait(false); + ClientResult result = await GenerateEmbeddingsAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(EmbeddingCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse()); } @@ -92,9 +94,10 @@ public virtual async Task> GenerateEmbeddingAsync(string /// Creates an embedding vector representing the input text. /// The string that will be turned into an embedding. /// The to use. + /// A token that can be used to cancel this method call. /// is null. /// is an empty string, and was expected to be non-empty. - public virtual ClientResult GenerateEmbedding(string input, EmbeddingGenerationOptions options = null) + public virtual ClientResult GenerateEmbedding(string input, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(input, nameof(input)); @@ -102,7 +105,7 @@ public virtual ClientResult GenerateEmbedding(string input, Embedding CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(input), ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = GenerateEmbeddings(content, (RequestOptions)null); + ClientResult result = GenerateEmbeddings(content, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(EmbeddingCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse()); } @@ -110,9 +113,10 @@ public virtual ClientResult GenerateEmbedding(string input, Embedding /// Creates an embedding vector representing the input text. /// The strings that will be turned into embeddings. /// The to use. + /// A token that can be used to cancel this method call. /// is null. /// is an empty collection, and was expected to be non-empty. - public virtual async Task> GenerateEmbeddingsAsync(IEnumerable inputs, EmbeddingGenerationOptions options = null) + public virtual async Task> GenerateEmbeddingsAsync(IEnumerable inputs, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(inputs, nameof(inputs)); @@ -120,7 +124,7 @@ public virtual async Task> GenerateEmbeddingsA CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs), ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = await GenerateEmbeddingsAsync(content, (RequestOptions)null).ConfigureAwait(false); + ClientResult result = await GenerateEmbeddingsAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(EmbeddingCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -129,9 +133,10 @@ public virtual async Task> GenerateEmbeddingsA /// Creates an embedding vector representing the input text. /// The strings that will be turned into embeddings. /// The to use. + /// A token that can be used to cancel this method call. /// is null. /// is an empty collection, and was expected to be non-empty. - public virtual ClientResult GenerateEmbeddings(IEnumerable inputs, EmbeddingGenerationOptions options = null) + public virtual ClientResult GenerateEmbeddings(IEnumerable inputs, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(inputs, nameof(inputs)); @@ -139,7 +144,7 @@ public virtual ClientResult GenerateEmbeddings(IEnumerable< CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs), ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = GenerateEmbeddings(content, (RequestOptions)null); + ClientResult result = GenerateEmbeddings(content, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(EmbeddingCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -147,9 +152,10 @@ public virtual ClientResult GenerateEmbeddings(IEnumerable< /// Creates an embedding vector representing the input text. /// The strings that will be turned into embeddings. /// The to use. + /// A token that can be used to cancel this method call. /// is null. /// is an empty collection, and was expected to be non-empty. - public virtual async Task> GenerateEmbeddingsAsync(IEnumerable> inputs, EmbeddingGenerationOptions options = null) + public virtual async Task> GenerateEmbeddingsAsync(IEnumerable> inputs, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(inputs, nameof(inputs)); @@ -157,7 +163,7 @@ public virtual async Task> GenerateEmbeddingsA CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs), ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = await GenerateEmbeddingsAsync(content, (RequestOptions)null).ConfigureAwait(false); + ClientResult result = await GenerateEmbeddingsAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(EmbeddingCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -165,9 +171,10 @@ public virtual async Task> GenerateEmbeddingsA /// Creates an embedding vector representing the input text. /// The strings that will be turned into embeddings. /// The to use. + /// A token that can be used to cancel this method call. /// is null. /// is an empty collection, and was expected to be non-empty. - public virtual ClientResult GenerateEmbeddings(IEnumerable> inputs, EmbeddingGenerationOptions options = null) + public virtual ClientResult GenerateEmbeddings(IEnumerable> inputs, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(inputs, nameof(inputs)); @@ -175,7 +182,7 @@ public virtual ClientResult GenerateEmbeddings(IEnumerable< CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs), ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = GenerateEmbeddings(content, (RequestOptions)null); + ClientResult result = GenerateEmbeddings(content, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(EmbeddingCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } diff --git a/src/Custom/Files/FileClient.cs b/src/Custom/Files/FileClient.cs index 2b42a54a..3b8f7c33 100644 --- a/src/Custom/Files/FileClient.cs +++ b/src/Custom/Files/FileClient.cs @@ -2,6 +2,7 @@ using System.ClientModel; using System.ClientModel.Primitives; using System.IO; +using System.Threading; using System.Threading.Tasks; namespace OpenAI.Files; @@ -83,10 +84,11 @@ protected internal FileClient(ClientPipeline pipeline, Uri endpoint, OpenAIClien /// validate the file format. The request may fail if the file extension and file format do not match. /// /// The intended purpose of the uploaded file. + /// A token that can be used to cancel this method call. /// or is null. /// is an empty string, and was expected to be non-empty. /// Information about the uploaded file. - public virtual async Task> UploadFileAsync(Stream file, string filename, FileUploadPurpose purpose) + public virtual async Task> UploadFileAsync(Stream file, string filename, FileUploadPurpose purpose, CancellationToken cancellationToken = default) { Argument.AssertNotNull(file, nameof(file)); Argument.AssertNotNullOrEmpty(filename, nameof(filename)); @@ -97,7 +99,7 @@ public virtual async Task> UploadFileAsync(Stream f }; using MultipartFormDataBinaryContent content = options.ToMultipartContent(file, filename); - ClientResult result = await UploadFileAsync(content, content.ContentType).ConfigureAwait(false); + ClientResult result = await UploadFileAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(OpenAIFileInfo.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -118,10 +120,11 @@ public virtual async Task> UploadFileAsync(Stream f /// validate the file format. The request may fail if the file extension and file format do not match. /// /// The intended purpose of the uploaded file. + /// A token that can be used to cancel this method call. /// or is null. /// is an empty string, and was expected to be non-empty. /// Information about the uploaded file. - public virtual ClientResult UploadFile(Stream file, string filename, FileUploadPurpose purpose) + public virtual ClientResult UploadFile(Stream file, string filename, FileUploadPurpose purpose, CancellationToken cancellationToken = default) { Argument.AssertNotNull(file, nameof(file)); Argument.AssertNotNullOrEmpty(filename, nameof(filename)); @@ -132,7 +135,7 @@ public virtual ClientResult UploadFile(Stream file, string filen }; using MultipartFormDataBinaryContent content = options.ToMultipartContent(file, filename); - ClientResult result = UploadFile(content, content.ContentType); + ClientResult result = UploadFile(content, content.ContentType, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(OpenAIFileInfo.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -248,72 +251,78 @@ public virtual ClientResult UploadFile(string filePath, FileUplo /// Retrieves a list of files that belong to the user's organization. /// Only return files with the given purpose. + /// A token that can be used to cancel this method call. /// Information about the files in the user's organization. - public virtual async Task> GetFilesAsync(OpenAIFilePurpose? purpose = null) + public virtual async Task> GetFilesAsync(OpenAIFilePurpose? purpose = null, CancellationToken cancellationToken = default) { - ClientResult result = await GetFilesAsync(purpose?.ToString(), null).ConfigureAwait(false); + ClientResult result = await GetFilesAsync(purpose?.ToString(), cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(OpenAIFileInfoCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } /// Retrieves a list of files that belong to the user's organization. /// Only return files with the given purpose. + /// A token that can be used to cancel this method call. /// Information about the files in the user's organization. - public virtual ClientResult GetFiles(OpenAIFilePurpose? purpose = null) + public virtual ClientResult GetFiles(OpenAIFilePurpose? purpose = null, CancellationToken cancellationToken = default) { - ClientResult result = GetFiles(purpose?.ToString(), null); + ClientResult result = GetFiles(purpose?.ToString(), cancellationToken.ToRequestOptions()); return ClientResult.FromValue(OpenAIFileInfoCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } /// Retrieves information about a specified file. /// The ID of the file to retrieve. + /// A token that can be used to cancel this method call. /// is null. /// is an empty string, and was expected to be non-empty. /// Information about the specified file. - public virtual async Task> GetFileAsync(string fileId) + public virtual async Task> GetFileAsync(string fileId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(fileId, nameof(fileId)); - ClientResult result = await GetFileAsync(fileId, (RequestOptions)null).ConfigureAwait(false); + ClientResult result = await GetFileAsync(fileId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(OpenAIFileInfo.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } /// Retrieves information about a specified file. /// The ID of the file to retrieve. + /// A token that can be used to cancel this method call. /// is null. /// is an empty string, and was expected to be non-empty. /// Information about the specified file. - public virtual ClientResult GetFile(string fileId) + public virtual ClientResult GetFile(string fileId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(fileId, nameof(fileId)); - ClientResult result = GetFile(fileId, (RequestOptions)null); + ClientResult result = GetFile(fileId, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(OpenAIFileInfo.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } /// Deletes a previously uploaded file. /// The ID of the file to delete. + /// A token that can be used to cancel this method call. /// is null. /// is an empty string, and was expected to be non-empty. /// A boolean value indicating whether the deletion request was successful. - public virtual async Task> DeleteFileAsync(string fileId) + public virtual async Task> DeleteFileAsync(string fileId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(fileId, nameof(fileId)); - ClientResult result = await DeleteFileAsync(fileId, null).ConfigureAwait(false); + ClientResult result = await DeleteFileAsync(fileId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); InternalDeleteFileResponse internalDeletion = InternalDeleteFileResponse.FromResponse(result.GetRawResponse()); return ClientResult.FromValue(internalDeletion.Deleted, result.GetRawResponse()); } /// Deletes a previously uploaded file. /// The ID of the file to delete. + /// A token that can be used to cancel this method call. /// is null. /// is an empty string, and was expected to be non-empty. /// A boolean value indicating whether the deletion request was successful. - public virtual ClientResult DeleteFile(string fileId) + public virtual ClientResult DeleteFile(string fileId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(fileId, nameof(fileId)); - ClientResult result = DeleteFile(fileId, null); + ClientResult result = DeleteFile(fileId, cancellationToken.ToRequestOptions()); InternalDeleteFileResponse internalDeletion = InternalDeleteFileResponse.FromResponse(result.GetRawResponse()); return ClientResult.FromValue(internalDeletion.Deleted, result.GetRawResponse()); } @@ -340,27 +349,29 @@ public virtual ClientResult DeleteFile(OpenAIFileInfo file) /// Downloads the binary content of the specified file. /// The ID of the file to download. + /// A token that can be used to cancel this method call. /// is null. /// is an empty string, and was expected to be non-empty. /// The contents of the specified file. - public virtual async Task> DownloadFileAsync(string fileId) + public virtual async Task> DownloadFileAsync(string fileId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(fileId, nameof(fileId)); - ClientResult result = await DownloadFileAsync(fileId, null).ConfigureAwait(false); + ClientResult result = await DownloadFileAsync(fileId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse()); } /// Downloads the binary content of the specified file. /// The ID of the file to download. + /// A token that can be used to cancel this method call. /// is null. /// is an empty string, and was expected to be non-empty. /// The bionary content of the specified file. - public virtual ClientResult DownloadFile(string fileId) + public virtual ClientResult DownloadFile(string fileId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(fileId, nameof(fileId)); - ClientResult result = DownloadFile(fileId, null); + ClientResult result = DownloadFile(fileId, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse()); } diff --git a/src/Custom/Images/ImageClient.cs b/src/Custom/Images/ImageClient.cs index 7d2445e4..7f57dd88 100644 --- a/src/Custom/Images/ImageClient.cs +++ b/src/Custom/Images/ImageClient.cs @@ -3,6 +3,7 @@ using System.ClientModel.Primitives; using System.IO; using System.Linq; +using System.Threading; using System.Threading.Tasks; namespace OpenAI.Images; @@ -81,10 +82,11 @@ protected internal ImageClient(ClientPipeline pipeline, string model, Uri endpoi /// /// A text description of the desired image. /// Additional options to tailor the image generation request. + /// A token that can be used to cancel this method call. /// is null. /// is an empty string, and was expected to be non-empty. /// The generated image. - public virtual async Task> GenerateImageAsync(string prompt, ImageGenerationOptions options = null) + public virtual async Task> GenerateImageAsync(string prompt, ImageGenerationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(prompt, nameof(prompt)); @@ -92,7 +94,7 @@ public virtual async Task> GenerateImageAsync(strin CreateImageGenerationOptions(prompt, null, ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = await GenerateImagesAsync(content, (RequestOptions)null).ConfigureAwait(false); + ClientResult result = await GenerateImagesAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse()); } @@ -101,10 +103,11 @@ public virtual async Task> GenerateImageAsync(strin /// /// A text description of the desired image. /// Additional options to tailor the image generation request. + /// A token that can be used to cancel this method call. /// is null. /// is an empty string, and was expected to be non-empty. /// The generated image. - public virtual ClientResult GenerateImage(string prompt, ImageGenerationOptions options = null) + public virtual ClientResult GenerateImage(string prompt, ImageGenerationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(prompt, nameof(prompt)); @@ -112,7 +115,7 @@ public virtual ClientResult GenerateImage(string prompt, ImageGe CreateImageGenerationOptions(prompt, null, ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = GenerateImages(content, (RequestOptions)null); + ClientResult result = GenerateImages(content, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse()); } @@ -122,10 +125,11 @@ public virtual ClientResult GenerateImage(string prompt, ImageGe /// A text description of the desired images. /// The number of images to generate. /// Additional options to tailor the image generation request. + /// A token that can be used to cancel this method call. /// is null. /// is an empty string, and was expected to be non-empty. /// The generated images. - public virtual async Task> GenerateImagesAsync(string prompt, int imageCount, ImageGenerationOptions options = null) + public virtual async Task> GenerateImagesAsync(string prompt, int imageCount, ImageGenerationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(prompt, nameof(prompt)); @@ -133,7 +137,7 @@ public virtual async Task> GenerateImages CreateImageGenerationOptions(prompt, imageCount, ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = await GenerateImagesAsync(content, (RequestOptions)null).ConfigureAwait(false); + ClientResult result = await GenerateImagesAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -143,10 +147,11 @@ public virtual async Task> GenerateImages /// A text description of the desired images. /// The number of images to generate. /// Additional options to tailor the image generation request. + /// A token that can be used to cancel this method call. /// is null. /// is an empty string, and was expected to be non-empty. /// The generated images. - public virtual ClientResult GenerateImages(string prompt, int imageCount, ImageGenerationOptions options = null) + public virtual ClientResult GenerateImages(string prompt, int imageCount, ImageGenerationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(prompt, nameof(prompt)); @@ -154,7 +159,7 @@ public virtual ClientResult GenerateImages(string prom CreateImageGenerationOptions(prompt, imageCount, ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = GenerateImages(content, (RequestOptions)null); + ClientResult result = GenerateImages(content, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -174,10 +179,11 @@ public virtual ClientResult GenerateImages(string prom /// /// A text description of the desired image. /// Additional options to tailor the image edit request. + /// A token that can be used to cancel this method call. /// , , or is null. /// or is an empty string, and was expected to be non-empty. /// The edited or extended image. - public virtual async Task> GenerateImageEditAsync(Stream image, string imageFilename, string prompt, ImageEditOptions options = null) + public virtual async Task> GenerateImageEditAsync(Stream image, string imageFilename, string prompt, ImageEditOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(image, nameof(image)); Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename)); @@ -187,7 +193,7 @@ public virtual async Task> GenerateImageEditAsync(S CreateImageEditOptions(image, imageFilename, prompt, null, null, null, ref options); using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, null, null); - ClientResult result = await GenerateImageEditsAsync(content, content.ContentType).ConfigureAwait(false); + ClientResult result = await GenerateImageEditsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse()); } @@ -203,10 +209,11 @@ public virtual async Task> GenerateImageEditAsync(S /// /// A text description of the desired image. /// Additional options to tailor the image edit request. + /// A token that can be used to cancel this method call. /// , , or is null. /// or is an empty string, and was expected to be non-empty. /// The edited or extended image. - public virtual ClientResult GenerateImageEdit(Stream image, string imageFilename, string prompt, ImageEditOptions options = null) + public virtual ClientResult GenerateImageEdit(Stream image, string imageFilename, string prompt, ImageEditOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(image, nameof(image)); Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename)); @@ -216,7 +223,7 @@ public virtual ClientResult GenerateImageEdit(Stream image, stri CreateImageEditOptions(image, imageFilename, prompt, null, null, null, ref options); using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, null, null); - ClientResult result = GenerateImageEdits(content, content.ContentType); + ClientResult result = GenerateImageEdits(content, content.ContentType, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse()); } @@ -282,10 +289,11 @@ public virtual ClientResult GenerateImageEdit(string imageFilePa /// do not match. /// /// Additional options to tailor the image edit request. + /// A token that can be used to cancel this method call. /// , , , , or is null. /// , , or is an empty string, and was expected to be non-empty. /// The edited or extended image. - public virtual async Task> GenerateImageEditAsync(Stream image, string imageFilename, string prompt, Stream mask, string maskFilename, ImageEditOptions options = null) + public virtual async Task> GenerateImageEditAsync(Stream image, string imageFilename, string prompt, Stream mask, string maskFilename, ImageEditOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(image, nameof(image)); Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename)); @@ -297,7 +305,7 @@ public virtual async Task> GenerateImageEditAsync(S CreateImageEditOptions(image, imageFilename, prompt, mask, maskFilename, null, ref options); using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, mask, maskFilename); - ClientResult result = await GenerateImageEditsAsync(content, content.ContentType).ConfigureAwait(false); + ClientResult result = await GenerateImageEditsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse()); } @@ -321,10 +329,11 @@ public virtual async Task> GenerateImageEditAsync(S /// do not match. /// /// Additional options to tailor the image edit request. + /// A token that can be used to cancel this method call. /// , , , , or is null. /// , , or is an empty string, and was expected to be non-empty. /// The edited or extended image. - public virtual ClientResult GenerateImageEdit(Stream image, string imageFilename, string prompt, Stream mask, string maskFilename, ImageEditOptions options = null) + public virtual ClientResult GenerateImageEdit(Stream image, string imageFilename, string prompt, Stream mask, string maskFilename, ImageEditOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(image, nameof(image)); Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename)); @@ -336,7 +345,7 @@ public virtual ClientResult GenerateImageEdit(Stream image, stri CreateImageEditOptions(image, imageFilename, prompt, mask, maskFilename, null, ref options); using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, mask, maskFilename); - ClientResult result = GenerateImageEdits(content, content.ContentType); + ClientResult result = GenerateImageEdits(content, content.ContentType, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse()); } @@ -409,10 +418,11 @@ public virtual ClientResult GenerateImageEdit(string imageFilePa /// A text description of the desired image. /// The number of edit or extended images to generate. /// Additional options to tailor the image edit request. + /// A token that can be used to cancel this method call. /// , , or is null. /// or is an empty string, and was expected to be non-empty. /// The edited or extended images. - public virtual async Task> GenerateImageEditsAsync(Stream image, string imageFilename, string prompt, int imageCount, ImageEditOptions options = null) + public virtual async Task> GenerateImageEditsAsync(Stream image, string imageFilename, string prompt, int imageCount, ImageEditOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(image, nameof(image)); Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename)); @@ -422,7 +432,7 @@ public virtual async Task> GenerateImageE CreateImageEditOptions(image, imageFilename, prompt, null, null, imageCount, ref options); using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, null, null); - ClientResult result = await GenerateImageEditsAsync(content, content.ContentType).ConfigureAwait(false); + ClientResult result = await GenerateImageEditsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -439,10 +449,11 @@ public virtual async Task> GenerateImageE /// A text description of the desired image. /// The number of edit or extended images to generate. /// Additional options to tailor the image edit request. + /// A token that can be used to cancel this method call. /// , , or is null. /// or is an empty string, and was expected to be non-empty. /// The edited or extended images. - public virtual ClientResult GenerateImageEdits(Stream image, string imageFilename, string prompt, int imageCount, ImageEditOptions options = null) + public virtual ClientResult GenerateImageEdits(Stream image, string imageFilename, string prompt, int imageCount, ImageEditOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(image, nameof(image)); Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename)); @@ -452,7 +463,7 @@ public virtual ClientResult GenerateImageEdits(Stream CreateImageEditOptions(image, imageFilename, prompt, null, null, imageCount, ref options); using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, null, null); - ClientResult result = GenerateImageEdits(content, content.ContentType); + ClientResult result = GenerateImageEdits(content, content.ContentType, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -521,10 +532,11 @@ public virtual ClientResult GenerateImageEdits(string /// /// The number of edit or extended images to generate. /// Additional options to tailor the image edit request. + /// A token that can be used to cancel this method call. /// , , , , or is null. /// , , or is an empty string, and was expected to be non-empty. /// The edited or extended images. - public virtual async Task> GenerateImageEditsAsync(Stream image, string imageFilename, string prompt, Stream mask, string maskFilename, int imageCount, ImageEditOptions options = null) + public virtual async Task> GenerateImageEditsAsync(Stream image, string imageFilename, string prompt, Stream mask, string maskFilename, int imageCount, ImageEditOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(image, nameof(image)); Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename)); @@ -536,7 +548,7 @@ public virtual async Task> GenerateImageE CreateImageEditOptions(image, imageFilename, prompt, mask, maskFilename, imageCount, ref options); using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, mask, maskFilename); - ClientResult result = await GenerateImageEditsAsync(content, content.ContentType).ConfigureAwait(false); + ClientResult result = await GenerateImageEditsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -561,10 +573,11 @@ public virtual async Task> GenerateImageE /// /// The number of edit or extended images to generate. /// Additional options to tailor the image edit request. + /// A token that can be used to cancel this method call. /// , , , , or is null. /// , , or is an empty string, and was expected to be non-empty. /// The edited or extended images. - public virtual ClientResult GenerateImageEdits(Stream image, string imageFilename, string prompt, Stream mask, string maskFilename, int imageCount, ImageEditOptions options = null) + public virtual ClientResult GenerateImageEdits(Stream image, string imageFilename, string prompt, Stream mask, string maskFilename, int imageCount, ImageEditOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(image, nameof(image)); Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename)); @@ -576,7 +589,7 @@ public virtual ClientResult GenerateImageEdits(Stream CreateImageEditOptions(image, imageFilename, prompt, mask, maskFilename, imageCount, ref options); using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename, mask, maskFilename); - ClientResult result = GenerateImageEdits(content, content.ContentType); + ClientResult result = GenerateImageEdits(content, content.ContentType, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -652,10 +665,11 @@ public virtual ClientResult GenerateImageEdits(string /// not match. /// /// Additional options to tailor the image variation request. + /// A token that can be used to cancel this method call. /// or is null. /// is an empty string, and was expected to be non-empty. /// The generated image variation. - public virtual async Task> GenerateImageVariationAsync(Stream image, string imageFilename, ImageVariationOptions options = null) + public virtual async Task> GenerateImageVariationAsync(Stream image, string imageFilename, ImageVariationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(image, nameof(image)); Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename)); @@ -664,7 +678,7 @@ public virtual async Task> GenerateImageVariationAs CreateImageVariationOptions(image, imageFilename, null, ref options); using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename); - ClientResult result = await GenerateImageVariationsAsync(content, content.ContentType).ConfigureAwait(false); + ClientResult result = await GenerateImageVariationsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse()); } @@ -678,10 +692,11 @@ public virtual async Task> GenerateImageVariationAs /// not match. /// /// Additional options to tailor the image variation request. + /// A token that can be used to cancel this method call. /// or is null. /// is an empty string, and was expected to be non-empty. /// The generated image variation. - public virtual ClientResult GenerateImageVariation(Stream image, string imageFilename, ImageVariationOptions options = null) + public virtual ClientResult GenerateImageVariation(Stream image, string imageFilename, ImageVariationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(image, nameof(image)); Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename)); @@ -690,7 +705,7 @@ public virtual ClientResult GenerateImageVariation(Stream image, CreateImageVariationOptions(image, imageFilename, null, ref options); using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename); - ClientResult result = GenerateImageVariations(content, content.ContentType); + ClientResult result = GenerateImageVariations(content, content.ContentType, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse()); } @@ -741,10 +756,11 @@ public virtual ClientResult GenerateImageVariation(string imageF /// /// The number of image variations to generate. /// Additional options to tailor the image variation request. + /// A token that can be used to cancel this method call. /// or is null. /// is an empty string, and was expected to be non-empty. /// The generated image variations. - public virtual async Task> GenerateImageVariationsAsync(Stream image, string imageFilename, int imageCount, ImageVariationOptions options = null) + public virtual async Task> GenerateImageVariationsAsync(Stream image, string imageFilename, int imageCount, ImageVariationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(image, nameof(image)); Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename)); @@ -753,7 +769,7 @@ public virtual async Task> GenerateImageV CreateImageVariationOptions(image, imageFilename, imageCount, ref options); using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename); - ClientResult result = await GenerateImageVariationsAsync(content, content.ContentType).ConfigureAwait(false); + ClientResult result = await GenerateImageVariationsAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -768,10 +784,11 @@ public virtual async Task> GenerateImageV /// /// The number of image variations to generate. /// Additional options to tailor the image variation request. + /// A token that can be used to cancel this method call. /// or is null. /// is an empty string, and was expected to be non-empty. /// The generated image variations. - public virtual ClientResult GenerateImageVariations(Stream image, string imageFilename, int imageCount, ImageVariationOptions options = null) + public virtual ClientResult GenerateImageVariations(Stream image, string imageFilename, int imageCount, ImageVariationOptions options = null, CancellationToken cancellationToken = default) { Argument.AssertNotNull(image, nameof(image)); Argument.AssertNotNullOrEmpty(imageFilename, nameof(imageFilename)); @@ -780,7 +797,7 @@ public virtual ClientResult GenerateImageVariations(St CreateImageVariationOptions(image, imageFilename, imageCount, ref options); using MultipartFormDataBinaryContent content = options.ToMultipartContent(image, imageFilename); - ClientResult result = GenerateImageVariations(content, content.ContentType); + ClientResult result = GenerateImageVariations(content, content.ContentType, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(GeneratedImageCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } diff --git a/src/Custom/Moderations/ModerationClient.cs b/src/Custom/Moderations/ModerationClient.cs index bf27406f..44cfde54 100644 --- a/src/Custom/Moderations/ModerationClient.cs +++ b/src/Custom/Moderations/ModerationClient.cs @@ -3,6 +3,7 @@ using System.ClientModel.Primitives; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; namespace OpenAI.Moderations; @@ -67,9 +68,10 @@ protected internal ModerationClient(ClientPipeline pipeline, string model, Uri e /// Classifies if text is potentially harmful. /// The text to classify. + /// A token that can be used to cancel this method call. /// is null. /// is an empty string, and was expected to be non-empty. - public virtual async Task> ClassifyTextInputAsync(string input) + public virtual async Task> ClassifyTextInputAsync(string input, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(input, nameof(input)); @@ -77,15 +79,16 @@ public virtual async Task> ClassifyTextInputAsync CreateModerationOptions(BinaryData.FromObjectAsJson(input), ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = await ClassifyTextInputsAsync(content, (RequestOptions)null).ConfigureAwait(false); + ClientResult result = await ClassifyTextInputsAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(ModerationCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse()); } /// Classifies if text is potentially harmful. /// The text to classify. + /// A token that can be used to cancel this method call. /// is null. /// is an empty string, and was expected to be non-empty. - public virtual ClientResult ClassifyTextInput(string input) + public virtual ClientResult ClassifyTextInput(string input, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(input, nameof(input)); @@ -93,16 +96,17 @@ public virtual ClientResult ClassifyTextInput(string input) CreateModerationOptions(BinaryData.FromObjectAsJson(input), ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = ClassifyTextInputs(content, (RequestOptions)null); + ClientResult result = ClassifyTextInputs(content, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(ModerationCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse()); } /// Classifies if text is potentially harmful. /// The text to classify. + /// A token that can be used to cancel this method call. /// is null. /// is an empty collection, and was expected to be non-empty. - public virtual async Task> ClassifyTextInputsAsync(IEnumerable inputs) + public virtual async Task> ClassifyTextInputsAsync(IEnumerable inputs, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(inputs, nameof(inputs)); @@ -110,15 +114,16 @@ public virtual async Task> ClassifyTextInputs CreateModerationOptions(BinaryData.FromObjectAsJson(inputs), ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = await ClassifyTextInputsAsync(content, (RequestOptions)null).ConfigureAwait(false); + ClientResult result = await ClassifyTextInputsAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(ModerationCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } /// Classifies if text is potentially harmful. /// The text to classify. + /// A token that can be used to cancel this method call. /// is null. /// is an empty collection, and was expected to be non-empty. - public virtual ClientResult ClassifyTextInputs(IEnumerable inputs) + public virtual ClientResult ClassifyTextInputs(IEnumerable inputs, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(inputs, nameof(inputs)); @@ -126,7 +131,7 @@ public virtual ClientResult ClassifyTextInputs(IEnumerable CreateModerationOptions(BinaryData.FromObjectAsJson(inputs), ref options); using BinaryContent content = options.ToBinaryContent(); - ClientResult result = ClassifyTextInputs(content, (RequestOptions)null); + ClientResult result = ClassifyTextInputs(content, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(ModerationCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } diff --git a/src/Custom/VectorStores/VectorStoreClient.cs b/src/Custom/VectorStores/VectorStoreClient.cs index 50107611..02ea27b2 100644 --- a/src/Custom/VectorStores/VectorStoreClient.cs +++ b/src/Custom/VectorStores/VectorStoreClient.cs @@ -6,6 +6,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Threading; using System.Threading.Tasks; using static OpenAI.InternalListHelpers; @@ -77,23 +78,25 @@ protected internal VectorStoreClient(ClientPipeline pipeline, Uri endpoint, Open /// Creates a vector store. /// The to use. + /// A token that can be used to cancel this method call. /// is null. /// Create vector store. - public virtual async Task> CreateVectorStoreAsync(VectorStoreCreationOptions vectorStore = null) + public virtual async Task> CreateVectorStoreAsync(VectorStoreCreationOptions vectorStore = null, CancellationToken cancellationToken = default) { using BinaryContent content = vectorStore?.ToBinaryContent(); - ClientResult result = await CreateVectorStoreAsync(content, null).ConfigureAwait(false); + ClientResult result = await CreateVectorStoreAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); return ClientResult.FromValue(VectorStore.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } /// Creates a vector store. /// The to use. + /// A token that can be used to cancel this method call. /// is null. /// Create vector store. - public virtual ClientResult CreateVectorStore(VectorStoreCreationOptions vectorStore = null) + public virtual ClientResult CreateVectorStore(VectorStoreCreationOptions vectorStore = null, CancellationToken cancellationToken = default) { using BinaryContent content = vectorStore?.ToBinaryContent(); - ClientResult result = CreateVectorStore(content, null); + ClientResult result = CreateVectorStore(content, cancellationToken.ToRequestOptions()); return ClientResult.FromValue(VectorStore.FromResponse(result.GetRawResponse()), result.GetRawResponse()); } @@ -101,12 +104,13 @@ public virtual ClientResult CreateVectorStore(VectorStoreCreationOp /// Deletes a vector store. /// /// The ID of the vector store to delete. + /// A token that can be used to cancel this method call. /// A value indicating whether the deletion operation was successful. - public virtual async Task> DeleteVectorStoreAsync(string vectorStoreId) + public virtual async Task> DeleteVectorStoreAsync(string vectorStoreId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(vectorStoreId, nameof(vectorStoreId)); - ClientResult protocolResult = await DeleteVectorStoreAsync(vectorStoreId, null).ConfigureAwait(false); + ClientResult protocolResult = await DeleteVectorStoreAsync(vectorStoreId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); PipelineResponse rawProtocolResponse = protocolResult?.GetRawResponse(); InternalDeleteVectorStoreResponse internalResponse = InternalDeleteVectorStoreResponse.FromResponse(rawProtocolResponse); return ClientResult.FromValue(internalResponse.Deleted, rawProtocolResponse); @@ -116,12 +120,13 @@ public virtual async Task> DeleteVectorStoreAsync(string vect /// Deletes a vector store. /// /// The ID of the vector store to delete. + /// A token that can be used to cancel this method call. /// A value indicating whether the deletion operation was successful. - public virtual ClientResult DeleteVectorStore(string vectorStoreId) + public virtual ClientResult DeleteVectorStore(string vectorStoreId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(vectorStoreId, nameof(vectorStoreId)); - ClientResult protocolResult = DeleteVectorStore(vectorStoreId, null); + ClientResult protocolResult = DeleteVectorStore(vectorStoreId, cancellationToken.ToRequestOptions()); PipelineResponse rawProtocolResponse = protocolResult?.GetRawResponse(); InternalDeleteVectorStoreResponse internalResponse = InternalDeleteVectorStoreResponse.FromResponse(rawProtocolResponse); return ClientResult.FromValue(internalResponse.Deleted, rawProtocolResponse); @@ -134,14 +139,15 @@ public virtual ClientResult DeleteVectorStore(string vectorStoreId) /// The order that results should appear in the list according to their created_at /// timestamp. /// + /// A token that can be used to cancel this method call. /// /// A collection of instances that can be asynchronously enumerated via /// await foreach. /// - public virtual AsyncPageableCollection GetVectorStoresAsync(ListOrder? resultOrder = null) + public virtual AsyncPageableCollection GetVectorStoresAsync(ListOrder? resultOrder = null, CancellationToken cancellationToken = default) { return CreateAsyncPageable((continuationToken, pageSize) - => GetVectorStoresAsync(pageSize, resultOrder?.ToString(), continuationToken, null, null)); + => GetVectorStoresAsync(pageSize, resultOrder?.ToString(), continuationToken, null, cancellationToken.ToRequestOptions())); } /// @@ -151,13 +157,14 @@ public virtual AsyncPageableCollection GetVectorStoresAsync(ListOrd /// The order that results should appear in the list according to their created_at /// timestamp. /// + /// A token that can be used to cancel this method call. /// /// A collection of instances that can be synchronously enumerated via foreach. /// - public virtual PageableCollection GetVectorStores(ListOrder? resultOrder = null) + public virtual PageableCollection GetVectorStores(ListOrder? resultOrder = null, CancellationToken cancellationToken = default) { return CreatePageable((continuationToken, pageSize) - => GetVectorStores(pageSize, resultOrder?.ToString(), continuationToken, null, null)); + => GetVectorStores(pageSize, resultOrder?.ToString(), continuationToken, null, cancellationToken.ToRequestOptions())); } /// @@ -165,13 +172,14 @@ public virtual PageableCollection GetVectorStores(ListOrder? result /// /// The ID of the vector store to associate the file with. /// The ID of the file to associate with the vector store. + /// A token that can be used to cancel this method call. /// /// A instance that represents the new association. /// - public virtual async Task> AddFileToVectorStoreAsync(string vectorStoreId, string fileId) + public virtual async Task> AddFileToVectorStoreAsync(string vectorStoreId, string fileId, CancellationToken cancellationToken = default) { InternalCreateVectorStoreFileRequest internalRequest = new(fileId); - ClientResult protocolResult = await AddFileToVectorStoreAsync(vectorStoreId, internalRequest.ToBinaryContent(), null).ConfigureAwait(false); + ClientResult protocolResult = await AddFileToVectorStoreAsync(vectorStoreId, internalRequest.ToBinaryContent(), cancellationToken.ToRequestOptions()).ConfigureAwait(false); PipelineResponse protocolResponse = protocolResult?.GetRawResponse(); VectorStoreFileAssociation fileAssociation = VectorStoreFileAssociation.FromResponse(protocolResponse); return ClientResult.FromValue(fileAssociation, protocolResponse); @@ -182,13 +190,14 @@ public virtual async Task> AddFileToVec /// /// The ID of the vector store to associate the file with. /// The ID of the file to associate with the vector store. + /// A token that can be used to cancel this method call. /// /// A instance that represents the new association. /// - public virtual ClientResult AddFileToVectorStore(string vectorStoreId, string fileId) + public virtual ClientResult AddFileToVectorStore(string vectorStoreId, string fileId, CancellationToken cancellationToken = default) { InternalCreateVectorStoreFileRequest internalRequest = new(fileId); - ClientResult protocolResult = AddFileToVectorStore(vectorStoreId, internalRequest.ToBinaryContent(), null); + ClientResult protocolResult = AddFileToVectorStore(vectorStoreId, internalRequest.ToBinaryContent(), cancellationToken.ToRequestOptions()); PipelineResponse protocolResponse = protocolResult?.GetRawResponse(); VectorStoreFileAssociation fileAssociation = VectorStoreFileAssociation.FromResponse(protocolResponse); return ClientResult.FromValue(fileAssociation, protocolResponse); @@ -208,6 +217,7 @@ public virtual ClientResult AddFileToVectorStore(str /// /// A status filter that file associations must match to be included in the collection. /// + /// A token that can be used to cancel this method call. /// /// A collection of instances that can be asynchronously enumerated via /// await foreach. @@ -215,12 +225,13 @@ public virtual ClientResult AddFileToVectorStore(str public virtual AsyncPageableCollection GetFileAssociationsAsync( string vectorStoreId, ListOrder? resultOrder = null, - VectorStoreFileStatusFilter? filter = null) + VectorStoreFileStatusFilter? filter = null, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(vectorStoreId, nameof(vectorStoreId)); return CreateAsyncPageable( (continuationToken, pageSize) => GetFileAssociationsAsync( - vectorStoreId, pageSize, resultOrder?.ToString(), continuationToken, null, filter?.ToString(), null)); + vectorStoreId, pageSize, resultOrder?.ToString(), continuationToken, null, filter?.ToString(), cancellationToken.ToRequestOptions())); } /// @@ -237,16 +248,17 @@ public virtual AsyncPageableCollection GetFileAssoci /// /// A status filter that file associations must match to be included in the collection. /// + /// A token that can be used to cancel this method call. /// /// A collection of instances that can be synchronously enumerated via /// foreach. /// - public virtual PageableCollection GetFileAssociations(string vectorStoreId, ListOrder? resultOrder = null, VectorStoreFileStatusFilter? filter = null) + public virtual PageableCollection GetFileAssociations(string vectorStoreId, ListOrder? resultOrder = null, VectorStoreFileStatusFilter? filter = null, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(vectorStoreId, nameof(vectorStoreId)); return CreatePageable( (continuationToken, pageSize) => GetFileAssociations( - vectorStoreId, pageSize, resultOrder?.ToString(), continuationToken, null, filter?.ToString(), null)); + vectorStoreId, pageSize, resultOrder?.ToString(), continuationToken, null, filter?.ToString(), cancellationToken.ToRequestOptions())); } /// @@ -255,15 +267,17 @@ public virtual PageableCollection GetFileAssociation /// /// The ID of the vector store associated with the file. /// The ID of the file associated with the vector store. + /// A token that can be used to cancel this method call. /// A instance. public virtual async Task> GetFileAssociationAsync( string vectorStoreId, - string fileId) + string fileId, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(vectorStoreId, nameof(vectorStoreId)); Argument.AssertNotNullOrEmpty(fileId, nameof(fileId)); - ClientResult result = await GetFileAssociationAsync(vectorStoreId, fileId, null).ConfigureAwait(false); + ClientResult result = await GetFileAssociationAsync(vectorStoreId, fileId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); PipelineResponse response = result?.GetRawResponse(); VectorStoreFileAssociation value = VectorStoreFileAssociation.FromResponse(response); return ClientResult.FromValue(value, response); @@ -275,15 +289,17 @@ public virtual async Task> GetFileAssoc /// /// The ID of the vector store associated with the file. /// The ID of the file associated with the vector store. + /// A token that can be used to cancel this method call. /// A instance. public virtual ClientResult GetFileAssociation( string vectorStoreId, - string fileId) + string fileId, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(vectorStoreId, nameof(vectorStoreId)); Argument.AssertNotNullOrEmpty(fileId, nameof(fileId)); - ClientResult result = GetFileAssociation(vectorStoreId, fileId, null); + ClientResult result = GetFileAssociation(vectorStoreId, fileId, cancellationToken.ToRequestOptions()); PipelineResponse response = result?.GetRawResponse(); VectorStoreFileAssociation value = VectorStoreFileAssociation.FromResponse(response); return ClientResult.FromValue(value, response); @@ -298,10 +314,11 @@ public virtual ClientResult GetFileAssociation( /// /// The ID of the vector store that the file should be removed from. /// The ID of the file to remove from the vector store. + /// A token that can be used to cancel this method call. /// A value indicating whether the removal operation was successful. - public virtual async Task> RemoveFileFromStoreAsync(string vectorStoreId, string fileId) + public virtual async Task> RemoveFileFromStoreAsync(string vectorStoreId, string fileId, CancellationToken cancellationToken = default) { - ClientResult protocolResult = await RemoveFileFromStoreAsync(vectorStoreId, fileId, null).ConfigureAwait(false); + ClientResult protocolResult = await RemoveFileFromStoreAsync(vectorStoreId, fileId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); PipelineResponse protocolResponse = protocolResult?.GetRawResponse(); InternalDeleteVectorStoreFileResponse internalDeletion = InternalDeleteVectorStoreFileResponse.FromResponse(protocolResponse); return ClientResult.FromValue(internalDeletion.Deleted, protocolResponse); @@ -316,10 +333,11 @@ public virtual async Task> RemoveFileFromStoreAsync(string ve /// /// The ID of the vector store that the file should be removed from. /// The ID of the file to remove from the vector store. + /// A token that can be used to cancel this method call. /// A value indicating whether the removal operation was successful. - public virtual ClientResult RemoveFileFromStore(string vectorStoreId, string fileId) + public virtual ClientResult RemoveFileFromStore(string vectorStoreId, string fileId, CancellationToken cancellationToken = default) { - ClientResult protocolResult = RemoveFileFromStore(vectorStoreId, fileId, null); + ClientResult protocolResult = RemoveFileFromStore(vectorStoreId, fileId, cancellationToken.ToRequestOptions()); PipelineResponse protocolResponse = protocolResult?.GetRawResponse(); InternalDeleteVectorStoreFileResponse internalDeletion = InternalDeleteVectorStoreFileResponse.FromResponse(protocolResponse); return ClientResult.FromValue(internalDeletion.Deleted, protocolResponse); @@ -330,14 +348,15 @@ public virtual ClientResult RemoveFileFromStore(string vectorStoreId, stri /// /// The ID of the vector store to associate files with. /// The IDs of the files to associate with the vector store. + /// A token that can be used to cancel this method call. /// A instance representing the batch operation. - public virtual async Task> CreateBatchFileJobAsync(string vectorStoreId, IEnumerable fileIds) + public virtual async Task> CreateBatchFileJobAsync(string vectorStoreId, IEnumerable fileIds, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(vectorStoreId, nameof(vectorStoreId)); Argument.AssertNotNullOrEmpty(fileIds, nameof(fileIds)); BinaryContent content = new InternalCreateVectorStoreFileBatchRequest(fileIds).ToBinaryContent(); - ClientResult result = await CreateBatchFileJobAsync(vectorStoreId, content, null).ConfigureAwait(false); + ClientResult result = await CreateBatchFileJobAsync(vectorStoreId, content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); PipelineResponse response = result?.GetRawResponse(); VectorStoreBatchFileJob value = VectorStoreBatchFileJob.FromResponse(response); return ClientResult.FromValue(value, response); @@ -348,14 +367,15 @@ public virtual async Task> CreateBatchFile /// /// The ID of the vector store to associate files with. /// The IDs of the files to associate with the vector store. + /// A token that can be used to cancel this method call. /// A instance representing the batch operation. - public virtual ClientResult CreateBatchFileJob(string vectorStoreId, IEnumerable fileIds) + public virtual ClientResult CreateBatchFileJob(string vectorStoreId, IEnumerable fileIds, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(vectorStoreId, nameof(vectorStoreId)); Argument.AssertNotNullOrEmpty(fileIds, nameof(fileIds)); BinaryContent content = new InternalCreateVectorStoreFileBatchRequest(fileIds).ToBinaryContent(); - ClientResult result = CreateBatchFileJob(vectorStoreId, content, null); + ClientResult result = CreateBatchFileJob(vectorStoreId, content, cancellationToken.ToRequestOptions()); PipelineResponse response = result?.GetRawResponse(); VectorStoreBatchFileJob value = VectorStoreBatchFileJob.FromResponse(response); return ClientResult.FromValue(value, response); @@ -366,13 +386,14 @@ public virtual ClientResult CreateBatchFileJob(string v /// /// The ID of the vector store into which the batch of files was started. /// The ID of the batch operation adding files to the vector store. + /// A token that can be used to cancel this method call. /// A instance representing the ingestion operation. - public virtual async Task> GetBatchFileJobAsync(string vectorStoreId, string batchJobId) + public virtual async Task> GetBatchFileJobAsync(string vectorStoreId, string batchJobId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(vectorStoreId, nameof(vectorStoreId)); Argument.AssertNotNullOrEmpty(batchJobId, nameof(batchJobId)); - ClientResult result = await GetBatchFileJobAsync(vectorStoreId, batchJobId, null).ConfigureAwait(false); + ClientResult result = await GetBatchFileJobAsync(vectorStoreId, batchJobId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); PipelineResponse response = result?.GetRawResponse(); VectorStoreBatchFileJob value = VectorStoreBatchFileJob.FromResponse(response); return ClientResult.FromValue(value, response); @@ -383,13 +404,14 @@ public virtual async Task> GetBatchFileJob /// /// The ID of the vector store into which the batch of files was started. /// The ID of the batch operation adding files to the vector store. + /// A token that can be used to cancel this method call. /// A instance representing the ingestion operation. - public virtual ClientResult GetBatchFileJob(string vectorStoreId, string batchJobId) + public virtual ClientResult GetBatchFileJob(string vectorStoreId, string batchJobId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(vectorStoreId, nameof(vectorStoreId)); Argument.AssertNotNullOrEmpty(batchJobId, nameof(batchJobId)); - ClientResult result = GetBatchFileJob(vectorStoreId, batchJobId, null); + ClientResult result = GetBatchFileJob(vectorStoreId, batchJobId, cancellationToken.ToRequestOptions()); PipelineResponse response = result?.GetRawResponse(); VectorStoreBatchFileJob value = VectorStoreBatchFileJob.FromResponse(response); return ClientResult.FromValue(value, response); @@ -404,13 +426,14 @@ public virtual ClientResult GetBatchFileJob(string vect /// /// The ID of the that should be canceled. /// + /// A token that can be used to cancel this method call. /// An updated instance. - public virtual async Task> CancelBatchFileJobAsync(string vectorStoreId, string batchJobId) + public virtual async Task> CancelBatchFileJobAsync(string vectorStoreId, string batchJobId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(vectorStoreId, nameof(vectorStoreId)); Argument.AssertNotNullOrEmpty(batchJobId, nameof(batchJobId)); - ClientResult result = await CancelBatchFileJobAsync(vectorStoreId, batchJobId, null).ConfigureAwait(false); + ClientResult result = await CancelBatchFileJobAsync(vectorStoreId, batchJobId, cancellationToken.ToRequestOptions()).ConfigureAwait(false); PipelineResponse response = result?.GetRawResponse(); VectorStoreBatchFileJob value = VectorStoreBatchFileJob.FromResponse(response); return ClientResult.FromValue(value, response); @@ -425,13 +448,14 @@ public virtual async Task> CancelBatchFile /// /// The ID of the that should be canceled. /// + /// A token that can be used to cancel this method call. /// An updated instance. - public virtual ClientResult CancelBatchFileJob(string vectorStoreId, string batchJobId) + public virtual ClientResult CancelBatchFileJob(string vectorStoreId, string batchJobId, CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(vectorStoreId, nameof(vectorStoreId)); Argument.AssertNotNullOrEmpty(batchJobId, nameof(batchJobId)); - ClientResult result = CancelBatchFileJob(vectorStoreId, batchJobId, null); + ClientResult result = CancelBatchFileJob(vectorStoreId, batchJobId, cancellationToken.ToRequestOptions()); PipelineResponse response = result?.GetRawResponse(); VectorStoreBatchFileJob value = VectorStoreBatchFileJob.FromResponse(response); return ClientResult.FromValue(value, response); @@ -454,6 +478,7 @@ public virtual ClientResult CancelBatchFileJob(string v /// /// A status filter that file associations must match to be included in the collection. /// + /// A token that can be used to cancel this method call. /// /// A collection of instances that can be asynchronously enumerated via /// await foreach. @@ -462,14 +487,15 @@ public virtual AsyncPageableCollection GetFileAssoci string vectorStoreId, string batchJobId, ListOrder? resultOrder = null, - VectorStoreFileStatusFilter? filter = null) + VectorStoreFileStatusFilter? filter = null, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(vectorStoreId, nameof(vectorStoreId)); Argument.AssertNotNullOrEmpty(batchJobId, nameof(batchJobId)); return CreateAsyncPageable( (continuationToken, pageSize) => GetFileAssociationsAsync - (vectorStoreId, batchJobId, pageSize, resultOrder?.ToString(), continuationToken, null, filter?.ToString(), null)); + (vectorStoreId, batchJobId, pageSize, resultOrder?.ToString(), continuationToken, null, filter?.ToString(), cancellationToken.ToRequestOptions())); } /// @@ -489,6 +515,7 @@ public virtual AsyncPageableCollection GetFileAssoci /// /// A status filter that file associations must match to be included in the collection. /// + /// A token that can be used to cancel this method call. /// /// A collection of instances that can be synchronously enumerated via /// foreach. @@ -497,13 +524,14 @@ public virtual PageableCollection GetFileAssociation string vectorStoreId, string batchJobId, ListOrder? resultOrder = null, - VectorStoreFileStatusFilter? filter = null) + VectorStoreFileStatusFilter? filter = null, + CancellationToken cancellationToken = default) { Argument.AssertNotNullOrEmpty(vectorStoreId, nameof(vectorStoreId)); Argument.AssertNotNullOrEmpty(batchJobId, nameof(batchJobId)); return CreatePageable( (continuationToken, pageSize) => GetFileAssociations - (vectorStoreId, batchJobId, pageSize, resultOrder?.ToString(), continuationToken, null, filter?.ToString(), null)); + (vectorStoreId, batchJobId, pageSize, resultOrder?.ToString(), continuationToken, null, filter?.ToString(), cancellationToken.ToRequestOptions())); } } diff --git a/tests/Assistants/AssistantTests.cs b/tests/Assistants/AssistantTests.cs index 8e1ab351..6eeb4926 100644 --- a/tests/Assistants/AssistantTests.cs +++ b/tests/Assistants/AssistantTests.cs @@ -225,7 +225,7 @@ public void BasicRunStepFunctionalityWorks() }); Validate(assistant); - AssistantThread thread = client.CreateThread(new() + AssistantThread thread = client.CreateThread(new ThreadCreationOptions() { InitialMessages = { new(["Please graph the equation y = 3x + 4"]), }, }); @@ -375,7 +375,7 @@ public async Task StreamingRunWorks() Assistant assistant = await client.CreateAssistantAsync("gpt-3.5-turbo"); Validate(assistant); - AssistantThread thread = await client.CreateThreadAsync(new() + AssistantThread thread = await client.CreateThreadAsync(new ThreadCreationOptions() { InitialMessages = { new(["Hello there, assistant! How are you today?"]), }, }); diff --git a/tests/Assistants/VectorStoreTests.cs b/tests/Assistants/VectorStoreTests.cs index bd4e4f64..b151d6bb 100644 --- a/tests/Assistants/VectorStoreTests.cs +++ b/tests/Assistants/VectorStoreTests.cs @@ -31,7 +31,7 @@ public void CanCreateGetAndDeleteVectorStores() IReadOnlyList testFiles = GetNewTestFiles(5); - vectorStore = client.CreateVectorStore(new() + vectorStore = client.CreateVectorStore(new VectorStoreCreationOptions() { FileIds = { testFiles[0].Id }, Name = "test vector store", @@ -73,7 +73,7 @@ public void CanCreateGetAndDeleteVectorStores() Assert.That(deleted, Is.True); _vectorStoresToDelete.RemoveAt(_vectorStoresToDelete.Count - 1); - vectorStore = client.CreateVectorStore(new() + vectorStore = client.CreateVectorStore(new VectorStoreCreationOptions () { FileIds = testFiles.Select(file => file.Id).ToList() });