Skip to content

Commit 1409d97

Browse files
committed
wip
1 parent 9a79c80 commit 1409d97

10 files changed

+477
-101
lines changed

src/Custom/Chat/ChatClient.cs

Lines changed: 125 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ namespace OpenAI.Chat;
2020
[CodeGenSuppress("ChatClient", typeof(ClientPipeline), typeof(Uri))]
2121
[CodeGenSuppress("CompleteChat", typeof(ChatCompletionOptions), typeof(CancellationToken))]
2222
[CodeGenSuppress("CompleteChatAsync", typeof(ChatCompletionOptions), typeof(CancellationToken))]
23+
[CodeGenSuppress("GetChatCompletionMessagesAsync", typeof(string), typeof(string), typeof(int?), typeof(string), typeof(RequestOptions))]
24+
[CodeGenSuppress("GetChatCompletionMessages", typeof(string), typeof(string), typeof(int?), typeof(string), typeof(RequestOptions))]
2325
public partial class ChatClient
2426
{
2527
private readonly string _model;
@@ -215,6 +217,43 @@ public virtual async Task<ClientResult<ChatCompletion>> CompleteChatAsync(params
215217
public virtual ClientResult<ChatCompletion> CompleteChat(params ChatMessage[] messages)
216218
=> CompleteChat(messages, default(ChatCompletionOptions));
217219

220+
[Experimental("OPENAI001")]
221+
[OverloadResolutionPriority(2)]
222+
public virtual ClientResult<ChatCompletionResult> CompleteChat(CreateChatCompletionOptions options, CancellationToken cancellationToken = default)
223+
{
224+
Argument.AssertNotNull(options, nameof(options));
225+
226+
ClientResult result = this.CompleteChat(options.Body, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
227+
return ClientResult.FromValue((ChatCompletionResult)result.GetRawResponse().Content, result.GetRawResponse());
228+
}
229+
230+
[Experimental("OPENAI001")]
231+
[OverloadResolutionPriority(2)]
232+
public virtual async Task<ClientResult<ChatCompletionResult>> CompleteChatAsync(CreateChatCompletionOptions options, CancellationToken cancellationToken = default)
233+
{
234+
Argument.AssertNotNull(options, nameof(options));
235+
236+
ClientResult result = await this.CompleteChatAsync(options, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null).ConfigureAwait(false);
237+
// this doesn't work for streaming responses because it will not serialize correctly. We need a CompleteChatStreaming.
238+
return ClientResult.FromValue((ChatCompletionResult)result.GetRawResponse().Content, result.GetRawResponse());
239+
}
240+
241+
[OverloadResolutionPriority(1)]
242+
public virtual ClientResult CompleteChat(CreateChatCompletionOptions options, RequestOptions requestOptions = default)
243+
{
244+
Argument.AssertNotNull(options, nameof(options));
245+
246+
return CompleteChat((BinaryContent)options, requestOptions);
247+
}
248+
249+
[OverloadResolutionPriority(1)]
250+
public virtual async Task<ClientResult> CompleteChatAsync(CreateChatCompletionOptions options, RequestOptions requestOptions = default)
251+
{
252+
Argument.AssertNotNull(options, nameof(options));
253+
254+
return await CompleteChatAsync((BinaryContent)options, requestOptions).ConfigureAwait(false);
255+
}
256+
218257
/// <summary>
219258
/// Generates a completion for the given chat. The completion is streamed back token by token as it is being
220259
/// generated by the model instead of waiting for it to be finished first.
@@ -368,6 +407,92 @@ public virtual ClientResult<ChatCompletion> GetChatCompletion(string completionI
368407
return ClientResult.FromValue((ChatCompletion)result, result.GetRawResponse());
369408
}
370409

410+
[OverloadResolutionPriority(1)]
411+
public virtual ClientResult<ChatCompletionResult> GetChatCompletion(GetChatCompletionOptions options, CancellationToken cancellationToken = default)
412+
{
413+
Argument.AssertNotNullOrEmpty(options.CompletionId, nameof(options.CompletionId));
414+
415+
ClientResult result = GetChatCompletion(options.CompletionId, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
416+
return ClientResult.FromValue((ChatCompletionResult)result, result.GetRawResponse());
417+
}
418+
419+
[OverloadResolutionPriority(1)]
420+
public virtual async Task<ClientResult<ChatCompletionResult>> GetChatCompletionAsync(GetChatCompletionOptions options, CancellationToken cancellationToken = default)
421+
{
422+
Argument.AssertNotNullOrEmpty(options.CompletionId, nameof(options.CompletionId));
423+
424+
ClientResult result = await GetChatCompletionAsync(options.CompletionId, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
425+
return ClientResult.FromValue((ChatCompletionResult)result, result.GetRawResponse());
426+
}
427+
428+
[Experimental("OPENAI001")]
429+
[OverloadResolutionPriority(2)]
430+
public virtual ClientResult GetChatCompletion(GetChatCompletionOptions options, RequestOptions requestOptions = null)
431+
{
432+
Argument.AssertNotNullOrEmpty(options.CompletionId, nameof(options.CompletionId));
433+
434+
using PipelineMessage message = CreateGetChatCompletionRequest(options.CompletionId, requestOptions);
435+
return ClientResult.FromResponse(Pipeline.ProcessMessage(message, requestOptions));
436+
}
437+
438+
[Experimental("OPENAI001")]
439+
[OverloadResolutionPriority(2)]
440+
public virtual async Task<ClientResult> GetChatCompletionAsync(GetChatCompletionOptions options, RequestOptions requestOptions = null)
441+
{
442+
Argument.AssertNotNullOrEmpty(options.CompletionId, nameof(options.CompletionId));
443+
444+
using PipelineMessage message = CreateGetChatCompletionRequest(options.CompletionId, requestOptions);
445+
return ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, requestOptions).ConfigureAwait(false));
446+
}
447+
448+
[Experimental("OPENAI001")]
449+
public virtual ClientResult<ChatCompletionList> GetChatCompletionMessages(GetChatCompletionMessageOptions options, CancellationToken cancellationToken = default)
450+
{
451+
Argument.AssertNotNullOrEmpty(options.CompletionId, nameof(options.CompletionId));
452+
453+
PipelineMessage message = CreateGetChatCompletionMessagesRequest(options.CompletionId, options.After, options.Limit, options.Order, cancellationToken.ToRequestOptions());
454+
ClientResult result = ClientResult.FromResponse(Pipeline.ProcessMessage(message, cancellationToken.ToRequestOptions()));
455+
return ClientResult.FromValue((ChatCompletionList)result, result.GetRawResponse());
456+
}
457+
458+
[Experimental("OPENAI001")]
459+
public virtual async Task<ClientResult<ChatCompletionList>> GetChatCompletionMessagesAsync(GetChatCompletionMessageOptions options, CancellationToken cancellationToken = default)
460+
{
461+
Argument.AssertNotNullOrEmpty(options.CompletionId, nameof(options.CompletionId));
462+
463+
PipelineMessage message = CreateGetChatCompletionMessagesRequest(options.CompletionId, options.After, options.Limit, options.Order, cancellationToken.ToRequestOptions());
464+
ClientResult result = ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, cancellationToken.ToRequestOptions()).ConfigureAwait(false));
465+
return ClientResult.FromValue((ChatCompletionList)result, result.GetRawResponse());
466+
}
467+
468+
[Experimental("OPENAI001")]
469+
public virtual CollectionResult GetChatCompletionMessages(GetChatCompletionMessageOptions options, RequestOptions requestOptions = null)
470+
{
471+
Argument.AssertNotNullOrEmpty(options.CompletionId, nameof(options.CompletionId));
472+
473+
return new ChatClientGetChatCompletionMessagesCollectionResult(
474+
this,
475+
options.CompletionId,
476+
options.After,
477+
options.Limit,
478+
options.Order,
479+
requestOptions);
480+
}
481+
482+
[Experimental("OPENAI001")]
483+
public virtual AsyncCollectionResult GetChatCompletionMessagesAsync(GetChatCompletionMessageOptions options, RequestOptions requestOptions = null)
484+
{
485+
Argument.AssertNotNullOrEmpty(options.CompletionId, nameof(options.CompletionId));
486+
487+
return new ChatClientGetChatCompletionMessagesAsyncCollectionResult(
488+
this,
489+
options.CompletionId,
490+
options.After,
491+
options.Limit,
492+
options.Order,
493+
requestOptions);
494+
}
495+
371496
// CUSTOM:
372497
// - Call FromClientResult.
373498
[Experimental("OPENAI001")]
@@ -444,39 +569,6 @@ public virtual AsyncCollectionResult<ChatCompletionResult> GetChatCompletionsAsy
444569
cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
445570
}
446571

447-
448-
[Experimental("OPENAI001")]
449-
public virtual ClientResult<ChatCompletionResult> CompleteChat(CreateChatCompletionOptions options, CancellationToken cancellationToken = default)
450-
{
451-
Argument.AssertNotNull(options, nameof(options));
452-
453-
ClientResult result = this.CompleteChat(options.Body, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
454-
return ClientResult.FromValue((ChatCompletionResult)result.GetRawResponse().Content, result.GetRawResponse());
455-
}
456-
457-
[Experimental("OPENAI001")]
458-
public virtual async Task<ClientResult<ChatCompletionResult>> CompleteChatAsync(CreateChatCompletionOptions options, CancellationToken cancellationToken = default)
459-
{
460-
Argument.AssertNotNull(options, nameof(options));
461-
462-
ClientResult result = await this.CompleteChatAsync(options, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null).ConfigureAwait(false);
463-
// this doesn't work for streaming responses because it will not serialize correctly. We need a CompleteChatStreaming.
464-
return ClientResult.FromValue((ChatCompletionResult)result.GetRawResponse().Content, result.GetRawResponse());
465-
}
466-
467-
[Experimental("OPENAI001")]
468-
public virtual ClientResult GetChatCompletion(GetChatCompletionOptions options, RequestOptions requestOptions = null)
469-
{
470-
Argument.AssertNotNull(options, nameof(options));
471-
if (string.IsNullOrEmpty(options.CompletionId))
472-
{
473-
throw new ArgumentException("Completion ID is required.", nameof(options.CompletionId));
474-
}
475-
476-
using PipelineMessage message = CreateGetChatCompletionRequest(options.CompletionId, requestOptions);
477-
return ClientResult.FromResponse(Pipeline.ProcessMessage(message, requestOptions));
478-
}
479-
480572
[Experimental("OPENAI001")]
481573
[OverloadResolutionPriority(2)]
482574
public virtual ClientResult UpdateChatCompletion(UpdateChatCompletionOptions options, RequestOptions requestOptions = null)

src/Generated/ChatClientGetChatCompletionMessagesAsyncCollectionResult.cs renamed to src/Custom/Chat/ChatClientGetChatCompletionMessagesAsyncCollectionResult.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// <auto-generated/>
2-
3-
#nullable disable
4-
51
using System;
62
using System.ClientModel;
73
using System.ClientModel.Primitives;

src/Generated/ChatClientGetChatCompletionMessagesCollectionResult.cs renamed to src/Custom/Chat/ChatClientGetChatCompletionMessagesCollectionResult.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// <auto-generated/>
2-
3-
#nullable disable
4-
51
using System;
62
using System.ClientModel;
73
using System.ClientModel.Primitives;

0 commit comments

Comments
 (0)