Skip to content

Commit 3094238

Browse files
Add internal OpenAIResponseClient.CreateResponse{Streaming}Async overloads that take RequestOptions (#687)
Co-authored-by: Jose Arriaga Maldonado <[email protected]>
1 parent d9f9e02 commit 3094238

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

src/Custom/Chat/ChatClient.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,19 @@ protected internal ChatClient(ClientPipeline pipeline, string model, OpenAIClien
136136
/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
137137
/// <exception cref="ArgumentNullException"> <paramref name="messages"/> is null. </exception>
138138
/// <exception cref="ArgumentException"> <paramref name="messages"/> is an empty collection, and was expected to be non-empty. </exception>
139-
public virtual async Task<ClientResult<ChatCompletion>> CompleteChatAsync(IEnumerable<ChatMessage> messages, ChatCompletionOptions options = null, CancellationToken cancellationToken = default)
139+
public virtual Task<ClientResult<ChatCompletion>> CompleteChatAsync(IEnumerable<ChatMessage> messages, ChatCompletionOptions options = null, CancellationToken cancellationToken = default)
140+
{
141+
return CompleteChatAsync(messages, options, cancellationToken.ToRequestOptions() ?? new RequestOptions());
142+
}
143+
144+
internal async Task<ClientResult<ChatCompletion>> CompleteChatAsync(IEnumerable<ChatMessage> messages, ChatCompletionOptions options, RequestOptions requestOptions)
140145
{
141146
Argument.AssertNotNullOrEmpty(messages, nameof(messages));
147+
Argument.AssertNotNull(requestOptions, nameof(requestOptions));
148+
if (requestOptions.BufferResponse is false)
149+
{
150+
throw new InvalidOperationException("'requestOptions.BufferResponse' must be 'true' when calling 'CompleteChatAsync'.");
151+
}
142152

143153
options ??= new();
144154
CreateChatCompletionOptions(messages, ref options);
@@ -148,7 +158,7 @@ public virtual async Task<ClientResult<ChatCompletion>> CompleteChatAsync(IEnume
148158
{
149159
using BinaryContent content = options.ToBinaryContent();
150160

151-
ClientResult result = await CompleteChatAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
161+
ClientResult result = await CompleteChatAsync(content, requestOptions).ConfigureAwait(false);
152162
ChatCompletion chatCompletion = (ChatCompletion)result;
153163
scope?.RecordChatCompletion(chatCompletion);
154164
return ClientResult.FromValue(chatCompletion, result.GetRawResponse());
@@ -218,17 +228,27 @@ public virtual ClientResult<ChatCompletion> CompleteChat(params ChatMessage[] me
218228
/// <exception cref="ArgumentNullException"> <paramref name="messages"/> is null. </exception>
219229
/// <exception cref="ArgumentException"> <paramref name="messages"/> is an empty collection, and was expected to be non-empty. </exception>
220230
public virtual AsyncCollectionResult<StreamingChatCompletionUpdate> CompleteChatStreamingAsync(IEnumerable<ChatMessage> messages, ChatCompletionOptions options = null, CancellationToken cancellationToken = default)
231+
{
232+
return CompleteChatStreamingAsync(messages, options, cancellationToken.ToRequestOptions(streaming: true));
233+
}
234+
235+
internal AsyncCollectionResult<StreamingChatCompletionUpdate> CompleteChatStreamingAsync(IEnumerable<ChatMessage> messages, ChatCompletionOptions options, RequestOptions requestOptions)
221236
{
222237
Argument.AssertNotNull(messages, nameof(messages));
238+
Argument.AssertNotNull(requestOptions, nameof(requestOptions));
239+
if (requestOptions.BufferResponse is true)
240+
{
241+
throw new InvalidOperationException("'requestOptions.BufferResponse' must be 'false' when calling 'CompleteChatStreamingAsync'.");
242+
}
223243

224244
options ??= new();
225245
CreateChatCompletionOptions(messages, ref options, stream: true);
226246

227247
using BinaryContent content = options.ToBinaryContent();
228248
return new AsyncSseUpdateCollection<StreamingChatCompletionUpdate>(
229-
async () => await CompleteChatAsync(content, cancellationToken.ToRequestOptions(streaming: true)).ConfigureAwait(false),
249+
async () => await CompleteChatAsync(content, requestOptions).ConfigureAwait(false),
230250
StreamingChatCompletionUpdate.DeserializeStreamingChatCompletionUpdate,
231-
cancellationToken);
251+
requestOptions.CancellationToken);
232252
}
233253

234254
/// <summary>

src/Custom/Responses/OpenAIResponseClient.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,22 @@ protected internal OpenAIResponseClient(ClientPipeline pipeline, string model, O
119119
[Experimental("OPENAI001")]
120120
public Uri Endpoint => _endpoint;
121121

122-
public virtual async Task<ClientResult<OpenAIResponse>> CreateResponseAsync(IEnumerable<ResponseItem> inputItems, ResponseCreationOptions options = null, CancellationToken cancellationToken = default)
122+
public virtual Task<ClientResult<OpenAIResponse>> CreateResponseAsync(IEnumerable<ResponseItem> inputItems, ResponseCreationOptions options = null, CancellationToken cancellationToken = default)
123+
{
124+
return CreateResponseAsync(inputItems, options, cancellationToken.ToRequestOptions() ?? new RequestOptions());
125+
}
126+
127+
internal async Task<ClientResult<OpenAIResponse>> CreateResponseAsync(IEnumerable<ResponseItem> inputItems, ResponseCreationOptions options, RequestOptions requestOptions)
123128
{
124129
Argument.AssertNotNullOrEmpty(inputItems, nameof(inputItems));
130+
Argument.AssertNotNull(requestOptions, nameof(requestOptions));
131+
if (requestOptions.BufferResponse is false)
132+
{
133+
throw new InvalidOperationException("'requestOptions.BufferResponse' must be 'true' when calling 'CreateResponseAsync'.");
134+
}
125135

126136
using BinaryContent content = CreatePerCallOptions(options, inputItems, stream: false).ToBinaryContent();
127-
ClientResult protocolResult = await CreateResponseAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
137+
ClientResult protocolResult = await CreateResponseAsync(content, requestOptions).ConfigureAwait(false);
128138
OpenAIResponse convenienceValue = (OpenAIResponse)protocolResult;
129139
return ClientResult.FromValue(convenienceValue, protocolResult.GetRawResponse());
130140
}
@@ -161,14 +171,24 @@ public virtual ClientResult<OpenAIResponse> CreateResponse(string userInputText,
161171
}
162172

163173
public virtual AsyncCollectionResult<StreamingResponseUpdate> CreateResponseStreamingAsync(IEnumerable<ResponseItem> inputItems, ResponseCreationOptions options = null, CancellationToken cancellationToken = default)
174+
{
175+
return CreateResponseStreamingAsync(inputItems, options, cancellationToken.ToRequestOptions(streaming: true));
176+
}
177+
178+
internal AsyncCollectionResult<StreamingResponseUpdate> CreateResponseStreamingAsync(IEnumerable<ResponseItem> inputItems, ResponseCreationOptions options, RequestOptions requestOptions)
164179
{
165180
Argument.AssertNotNullOrEmpty(inputItems, nameof(inputItems));
181+
Argument.AssertNotNull(requestOptions, nameof(requestOptions));
182+
if (requestOptions.BufferResponse is true)
183+
{
184+
throw new InvalidOperationException("'requestOptions.BufferResponse' must be 'false' when calling 'CreateResponseStreamingAsync'.");
185+
}
166186

167187
using BinaryContent content = CreatePerCallOptions(options, inputItems, stream: true).ToBinaryContent();
168188
return new AsyncSseUpdateCollection<StreamingResponseUpdate>(
169-
async () => await CreateResponseAsync(content, cancellationToken.ToRequestOptions(streaming: true)).ConfigureAwait(false),
189+
async () => await CreateResponseAsync(content, requestOptions).ConfigureAwait(false),
170190
StreamingResponseUpdate.DeserializeStreamingResponseUpdate,
171-
cancellationToken);
191+
requestOptions.CancellationToken);
172192
}
173193

174194
public virtual CollectionResult<StreamingResponseUpdate> CreateResponseStreaming(IEnumerable<ResponseItem> inputItems, ResponseCreationOptions options = null, CancellationToken cancellationToken = default)

0 commit comments

Comments
 (0)