openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.3.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/Custom/Chat/ChatClient.cs

372lines · modeblame

d5b5c604Liudmila Molkova1 years ago1using OpenAI.Telemetry;
9f9f2936Jose Arriaga Maldonado2 years ago2using System;
3using System.ClientModel;
4using System.ClientModel.Primitives;
5using System.Collections.Generic;
5dce104aJose Arriaga Maldonado1 years ago6using System.Diagnostics.CodeAnalysis;
9f9f2936Jose Arriaga Maldonado2 years ago7using System.Linq;
19a65a0aKrzysztof Cwalina2 years ago8using System.Threading;
9f9f2936Jose Arriaga Maldonado2 years ago9using System.Threading.Tasks;
10
11namespace OpenAI.Chat;
12
13a9c686Jose Arriaga Maldonado1 years ago13// CUSTOM:
14// - Renamed.
15// - Suppressed constructor that takes endpoint parameter; endpoint is now a property in the options class.
16// - Suppressed methods that only take the options parameter.
17/// <summary> The service client for OpenAI chat operations. </summary>
0ca4c062Jose Arriaga Maldonado1 years ago18[CodeGenType("Chat")]
19[CodeGenSuppress("ChatClient", typeof(ClientPipeline), typeof(Uri))]
dff9bb58Jose Arriaga Maldonado1 years ago20[CodeGenSuppress("CompleteChat", typeof(ChatCompletionOptions), typeof(CancellationToken))]
21[CodeGenSuppress("CompleteChatAsync", typeof(ChatCompletionOptions), typeof(CancellationToken))]
9f9f2936Jose Arriaga Maldonado2 years ago22public partial class ChatClient
23{
24private readonly string _model;
d5b5c604Liudmila Molkova1 years ago25private readonly OpenTelemetrySource _telemetry;
5dce104aJose Arriaga Maldonado1 years ago26private static readonly InternalChatCompletionStreamOptions s_includeUsageStreamOptions = new(includeUsage: true, additionalBinaryDataProperties: null);
9f9f2936Jose Arriaga Maldonado2 years ago27
2ab1a942Jose Arriaga Maldonado1 years ago28// CUSTOM: Added as a convenience.
e0fee603Jose Arriaga Maldonado1 years ago29/// <summary> Initializes a new instance of <see cref="ChatClient"/>. </summary>
2ab1a942Jose Arriaga Maldonado1 years ago30/// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param>
31/// <param name="apiKey"> The API key to authenticate with the service. </param>
32/// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="apiKey"/> is null. </exception>
33/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
34public ChatClient(string model, string apiKey) : this(model, new ApiKeyCredential(apiKey), new OpenAIClientOptions())
35{
36}
37
13a9c686Jose Arriaga Maldonado1 years ago38// CUSTOM:
39// - Added `model` parameter.
40// - Used a custom pipeline.
41// - Demoted the endpoint parameter to be a property in the options class.
e0fee603Jose Arriaga Maldonado1 years ago42/// <summary> Initializes a new instance of <see cref="ChatClient"/>. </summary>
13a9c686Jose Arriaga Maldonado1 years ago43/// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param>
44/// <param name="credential"> The API key to authenticate with the service. </param>
45/// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="credential"/> is null. </exception>
46/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
47public ChatClient(string model, ApiKeyCredential credential) : this(model, credential, new OpenAIClientOptions())
48{
49}
9f9f2936Jose Arriaga Maldonado2 years ago50
13a9c686Jose Arriaga Maldonado1 years ago51// CUSTOM:
52// - Added `model` parameter.
53// - Used a custom pipeline.
54// - Demoted the endpoint parameter to be a property in the options class.
55// - Added telemetry support.
e0fee603Jose Arriaga Maldonado1 years ago56/// <summary> Initializes a new instance of <see cref="ChatClient"/>. </summary>
13a9c686Jose Arriaga Maldonado1 years ago57/// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param>
58/// <param name="credential"> The API key to authenticate with the service. </param>
59/// <param name="options"> The options to configure the client. </param>
60/// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="credential"/> is null. </exception>
61/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
fa73161cJose Arriaga Maldonado1 years ago62public ChatClient(string model, ApiKeyCredential credential, OpenAIClientOptions options) : this(model, OpenAIClient.CreateApiKeyAuthenticationPolicy(credential), options)
63{
64}
65
66// CUSTOM: Added as a convenience.
67/// <summary> Initializes a new instance of <see cref="ChatClient"/>. </summary>
68/// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param>
69/// <param name="authenticationPolicy"> The authentication policy used to authenticate with the service. </param>
70/// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="authenticationPolicy"/> is null. </exception>
71/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
72[Experimental("OPENAI001")]
73public ChatClient(string model, AuthenticationPolicy authenticationPolicy) : this(model, authenticationPolicy, new OpenAIClientOptions())
74{
75}
76
77// CUSTOM: Added as a convenience.
78/// <summary> Initializes a new instance of <see cref="ChatClient"/>. </summary>
79/// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param>
80/// <param name="authenticationPolicy"> The authentication policy used to authenticate with the service. </param>
81/// <param name="options"> The options to configure the client. </param>
82/// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="authenticationPolicy"/> is null. </exception>
83/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
84[Experimental("OPENAI001")]
85public ChatClient(string model, AuthenticationPolicy authenticationPolicy, OpenAIClientOptions options)
13a9c686Jose Arriaga Maldonado1 years ago86{
87Argument.AssertNotNullOrEmpty(model, nameof(model));
fa73161cJose Arriaga Maldonado1 years ago88Argument.AssertNotNull(authenticationPolicy, nameof(authenticationPolicy));
13a9c686Jose Arriaga Maldonado1 years ago89options ??= new OpenAIClientOptions();
9f9f2936Jose Arriaga Maldonado2 years ago90
13a9c686Jose Arriaga Maldonado1 years ago91_model = model;
fa73161cJose Arriaga Maldonado1 years ago92Pipeline = OpenAIClient.CreatePipeline(authenticationPolicy, options);
13a9c686Jose Arriaga Maldonado1 years ago93_endpoint = OpenAIClient.GetEndpoint(options);
94_telemetry = new OpenTelemetrySource(model, _endpoint);
95}
96
97// CUSTOM:
98// - Added `model` parameter.
99// - Used a custom pipeline.
100// - Demoted the endpoint parameter to be a property in the options class.
101// - Added telemetry support.
102// - Made protected.
e0fee603Jose Arriaga Maldonado1 years ago103/// <summary> Initializes a new instance of <see cref="ChatClient"/>. </summary>
13a9c686Jose Arriaga Maldonado1 years ago104/// <param name="pipeline"> The HTTP pipeline to send and receive REST requests and responses. </param>
105/// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param>
106/// <param name="options"> The options to configure the client. </param>
107/// <exception cref="ArgumentNullException"> <paramref name="pipeline"/> or <paramref name="model"/> is null. </exception>
108/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
109protected internal ChatClient(ClientPipeline pipeline, string model, OpenAIClientOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago110{
13a9c686Jose Arriaga Maldonado1 years ago111Argument.AssertNotNull(pipeline, nameof(pipeline));
9f9f2936Jose Arriaga Maldonado2 years ago112Argument.AssertNotNullOrEmpty(model, nameof(model));
13a9c686Jose Arriaga Maldonado1 years ago113options ??= new OpenAIClientOptions();
9f9f2936Jose Arriaga Maldonado2 years ago114
115_model = model;
e0fee603Jose Arriaga Maldonado1 years ago116Pipeline = pipeline;
13a9c686Jose Arriaga Maldonado1 years ago117_endpoint = OpenAIClient.GetEndpoint(options);
118_telemetry = new OpenTelemetrySource(model, _endpoint);
9f9f2936Jose Arriaga Maldonado2 years ago119}
120
00423023Krzysztof Cwalina1 years ago121/// <summary>
122/// Gets the name of the model used in requests sent to the service.
123/// </summary>
92dffc2dJose Arriaga Maldonado1 years ago124[Experimental("OPENAI001")]
00423023Krzysztof Cwalina1 years ago125public string Model => _model;
126
13a9c686Jose Arriaga Maldonado1 years ago127/// <summary> Generates a completion for the given chat. </summary>
128/// <param name="messages"> The messages comprising the chat so far. </param>
129/// <param name="options"> The options to configure the chat completion. </param>
130/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
131/// <exception cref="ArgumentNullException"> <paramref name="messages"/> is null. </exception>
132/// <exception cref="ArgumentException"> <paramref name="messages"/> is an empty collection, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago133public virtual async Task<ClientResult<ChatCompletion>> CompleteChatAsync(IEnumerable<ChatMessage> messages, ChatCompletionOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago134{
135Argument.AssertNotNullOrEmpty(messages, nameof(messages));
136
137options ??= new();
138CreateChatCompletionOptions(messages, ref options);
d5b5c604Liudmila Molkova1 years ago139using OpenTelemetryScope scope = _telemetry.StartChatScope(options);
140
141try
142{
5dce104aJose Arriaga Maldonado1 years ago143using BinaryContent content = options.ToBinaryContent();
d5b5c604Liudmila Molkova1 years ago144
145ClientResult result = await CompleteChatAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
5dce104aJose Arriaga Maldonado1 years ago146ChatCompletion chatCompletion = ChatCompletion.FromClientResult(result);
d5b5c604Liudmila Molkova1 years ago147scope?.RecordChatCompletion(chatCompletion);
148return ClientResult.FromValue(chatCompletion, result.GetRawResponse());
149}
150catch (Exception ex)
151{
152scope?.RecordException(ex);
153throw;
154}
9f9f2936Jose Arriaga Maldonado2 years ago155}
156
13a9c686Jose Arriaga Maldonado1 years ago157/// <summary> Generates a completion for the given chat. </summary>
158/// <param name="messages"> The messages comprising the chat so far. </param>
159/// <param name="options"> The options to configure the chat completion. </param>
160/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
161/// <exception cref="ArgumentNullException"> <paramref name="messages"/> is null. </exception>
162/// <exception cref="ArgumentException"> <paramref name="messages"/> is an empty collection, and was expected to be non-empty. </exception>
19a65a0aKrzysztof Cwalina2 years ago163public virtual ClientResult<ChatCompletion> CompleteChat(IEnumerable<ChatMessage> messages, ChatCompletionOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago164{
165Argument.AssertNotNullOrEmpty(messages, nameof(messages));
166
167options ??= new();
168CreateChatCompletionOptions(messages, ref options);
d5b5c604Liudmila Molkova1 years ago169using OpenTelemetryScope scope = _telemetry.StartChatScope(options);
170
171try
172{
5dce104aJose Arriaga Maldonado1 years ago173using BinaryContent content = options.ToBinaryContent();
d5b5c604Liudmila Molkova1 years ago174ClientResult result = CompleteChat(content, cancellationToken.ToRequestOptions());
5dce104aJose Arriaga Maldonado1 years ago175ChatCompletion chatCompletion = ChatCompletion.FromClientResult(result);
d5b5c604Liudmila Molkova1 years ago176
177scope?.RecordChatCompletion(chatCompletion);
178return ClientResult.FromValue(chatCompletion, result.GetRawResponse());
179}
180catch (Exception ex)
181{
182scope?.RecordException(ex);
183throw;
184}
9f9f2936Jose Arriaga Maldonado2 years ago185}
186
13a9c686Jose Arriaga Maldonado1 years ago187/// <summary> Generates a completion for the given chat. </summary>
188/// <param name="messages"> The messages comprising the chat so far. </param>
189/// <exception cref="ArgumentNullException"> <paramref name="messages"/> is null. </exception>
190/// <exception cref="ArgumentException"> <paramref name="messages"/> is an empty collection, and was expected to be non-empty. </exception>
191public virtual async Task<ClientResult<ChatCompletion>> CompleteChatAsync(params ChatMessage[] messages)
192=> await CompleteChatAsync(messages, default(ChatCompletionOptions)).ConfigureAwait(false);
193
194/// <summary> Generates a completion for the given chat. </summary>
195/// <param name="messages"> The messages comprising the chat so far. </param>
196/// <exception cref="ArgumentNullException"> <paramref name="messages"/> is null. </exception>
197/// <exception cref="ArgumentException"> <paramref name="messages"/> is an empty collection, and was expected to be non-empty. </exception>
1c40de67Krzysztof Cwalina2 years ago198public virtual ClientResult<ChatCompletion> CompleteChat(params ChatMessage[] messages)
199=> CompleteChat(messages, default(ChatCompletionOptions));
200
9f9f2936Jose Arriaga Maldonado2 years ago201/// <summary>
13a9c686Jose Arriaga Maldonado1 years ago202/// Generates a completion for the given chat. The completion is streamed back token by token as it is being
203/// generated by the model instead of waiting for it to be finished first.
9f9f2936Jose Arriaga Maldonado2 years ago204/// </summary>
205/// <remarks>
13a9c686Jose Arriaga Maldonado1 years ago206/// <see cref="AsyncCollectionResult{T}"/> implements the <see cref="IAsyncEnumerable{T}"/> interface and can be
207/// enumerated over using the <c>await foreach</c> pattern.
9f9f2936Jose Arriaga Maldonado2 years ago208/// </remarks>
13a9c686Jose Arriaga Maldonado1 years ago209/// <param name="messages"> The messages comprising the chat so far. </param>
210/// <param name="options"> The options to configure the chat completion. </param>
211/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
212/// <exception cref="ArgumentNullException"> <paramref name="messages"/> is null. </exception>
213/// <exception cref="ArgumentException"> <paramref name="messages"/> is an empty collection, and was expected to be non-empty. </exception>
7bdecfd8Anne Thompson2 years ago214public virtual AsyncCollectionResult<StreamingChatCompletionUpdate> CompleteChatStreamingAsync(IEnumerable<ChatMessage> messages, ChatCompletionOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago215{
216Argument.AssertNotNull(messages, nameof(messages));
217
218options ??= new();
219CreateChatCompletionOptions(messages, ref options, stream: true);
220
5dce104aJose Arriaga Maldonado1 years ago221using BinaryContent content = options.ToBinaryContent();
0ca4c062Jose Arriaga Maldonado1 years ago222return new AsyncSseUpdateCollection<StreamingChatCompletionUpdate>(
223async () => await CompleteChatAsync(content, cancellationToken.ToRequestOptions(streaming: true)).ConfigureAwait(false),
224StreamingChatCompletionUpdate.DeserializeStreamingChatCompletionUpdate,
225cancellationToken);
9f9f2936Jose Arriaga Maldonado2 years ago226}
227
1c40de67Krzysztof Cwalina2 years ago228/// <summary>
13a9c686Jose Arriaga Maldonado1 years ago229/// Generates a completion for the given chat. The completion is streamed back token by token as it is being
230/// generated by the model instead of waiting for it to be finished first.
1c40de67Krzysztof Cwalina2 years ago231/// </summary>
232/// <remarks>
a330c2e7Jose Arriaga Maldonado1 years ago233/// <see cref="CollectionResult{T}"/> implements the <see cref="IEnumerable{T}"/> interface and can be
13a9c686Jose Arriaga Maldonado1 years ago234/// enumerated over using the <c>await foreach</c> pattern.
1c40de67Krzysztof Cwalina2 years ago235/// </remarks>
13a9c686Jose Arriaga Maldonado1 years ago236/// <param name="messages"> The messages comprising the chat so far. </param>
237/// <param name="options"> The options to configure the chat completion. </param>
238/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
239/// <exception cref="ArgumentNullException"> <paramref name="messages"/> is null. </exception>
240/// <exception cref="ArgumentException"> <paramref name="messages"/> is an empty collection, and was expected to be non-empty. </exception>
7bdecfd8Anne Thompson2 years ago241public virtual CollectionResult<StreamingChatCompletionUpdate> CompleteChatStreaming(IEnumerable<ChatMessage> messages, ChatCompletionOptions options = null, CancellationToken cancellationToken = default)
9f9f2936Jose Arriaga Maldonado2 years ago242{
243Argument.AssertNotNull(messages, nameof(messages));
244
245options ??= new();
246CreateChatCompletionOptions(messages, ref options, stream: true);
247
5dce104aJose Arriaga Maldonado1 years ago248using BinaryContent content = options.ToBinaryContent();
0ca4c062Jose Arriaga Maldonado1 years ago249return new SseUpdateCollection<StreamingChatCompletionUpdate>(
250() => CompleteChat(content, cancellationToken.ToRequestOptions(streaming: true)),
251StreamingChatCompletionUpdate.DeserializeStreamingChatCompletionUpdate,
252cancellationToken);
9f9f2936Jose Arriaga Maldonado2 years ago253}
254
1c40de67Krzysztof Cwalina2 years ago255/// <summary>
13a9c686Jose Arriaga Maldonado1 years ago256/// Generates a completion for the given chat. The completion is streamed back token by token as it is being
257/// generated by the model instead of waiting for it to be finished first.
258/// </summary>
259/// <remarks>
260/// <see cref="AsyncCollectionResult{T}"/> implements the <see cref="IAsyncEnumerable{T}"/> interface and can be
261/// enumerated over using the <c>await foreach</c> pattern.
262/// </remarks>
263/// <param name="messages"> The messages comprising the chat so far. </param>
264/// <exception cref="ArgumentNullException"> <paramref name="messages"/> is null. </exception>
265/// <exception cref="ArgumentException"> <paramref name="messages"/> is an empty collection, and was expected to be non-empty. </exception>
266public virtual AsyncCollectionResult<StreamingChatCompletionUpdate> CompleteChatStreamingAsync(params ChatMessage[] messages)
267=> CompleteChatStreamingAsync(messages, default(ChatCompletionOptions));
268
269/// <summary>
270/// Generates a completion for the given chat. The completion is streamed back token by token as it is being
271/// generated by the model instead of waiting for it to be finished first.
1c40de67Krzysztof Cwalina2 years ago272/// </summary>
273/// <remarks>
a330c2e7Jose Arriaga Maldonado1 years ago274/// <see cref="CollectionResult{T}"/> implements the <see cref="IEnumerable{T}"/> interface and can be
13a9c686Jose Arriaga Maldonado1 years ago275/// enumerated over using the <c>await foreach</c> pattern.
1c40de67Krzysztof Cwalina2 years ago276/// </remarks>
13a9c686Jose Arriaga Maldonado1 years ago277/// <param name="messages"> The messages comprising the chat so far. </param>
278/// <exception cref="ArgumentNullException"> <paramref name="messages"/> is null. </exception>
279/// <exception cref="ArgumentException"> <paramref name="messages"/> is an empty collection, and was expected to be non-empty. </exception>
7bdecfd8Anne Thompson2 years ago280public virtual CollectionResult<StreamingChatCompletionUpdate> CompleteChatStreaming(params ChatMessage[] messages)
1c40de67Krzysztof Cwalina2 years ago281=> CompleteChatStreaming(messages, default(ChatCompletionOptions));
282
5dce104aJose Arriaga Maldonado1 years ago283// CUSTOM:
284// - Added Experimental attribute.
285// - Call FromClientResult.
286[Experimental("OPENAI001")]
287public virtual async Task<ClientResult<ChatCompletion>> GetChatCompletionAsync(string completionId, CancellationToken cancellationToken = default)
288{
289Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));
290
291ClientResult result = await GetChatCompletionAsync(completionId, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null).ConfigureAwait(false);
292return ClientResult.FromValue(ChatCompletion.FromClientResult(result), result.GetRawResponse());
293}
294
295// CUSTOM:
296// - Added Experimental attribute.
297// - Call FromClientResult.
298[Experimental("OPENAI001")]
299public virtual ClientResult<ChatCompletion> GetChatCompletion(string completionId, CancellationToken cancellationToken = default)
300{
301Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));
302
303ClientResult result = GetChatCompletion(completionId, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
304return ClientResult.FromValue(ChatCompletion.FromClientResult(result), result.GetRawResponse());
305}
306
7a281781ShivangiReja1 years ago307// CUSTOM:
308// - Call FromClientResult.
309[Experimental("OPENAI001")]
310public virtual ClientResult<ChatCompletion> UpdateChatCompletion(string completionId, IDictionary<string, string> metadata, CancellationToken cancellationToken = default)
311{
312Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));
313Argument.AssertNotNull(metadata, nameof(metadata));
314
315InternalUpdateChatCompletionRequest spreadModel = new InternalUpdateChatCompletionRequest(metadata, null);
316ClientResult result = this.UpdateChatCompletion(completionId, spreadModel, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
317return ClientResult.FromValue(ChatCompletion.FromClientResult(result), result.GetRawResponse());
318}
319
320// CUSTOM:
321// - Call FromClientResult.
322[Experimental("OPENAI001")]
323public virtual async Task<ClientResult<ChatCompletion>> UpdateChatCompletionAsync(string completionId, IDictionary<string, string> metadata, CancellationToken cancellationToken = default)
324{
325Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));
326Argument.AssertNotNull(metadata, nameof(metadata));
327
328InternalUpdateChatCompletionRequest spreadModel = new InternalUpdateChatCompletionRequest(metadata, null);
329ClientResult result = await this.UpdateChatCompletionAsync(completionId, spreadModel, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null).ConfigureAwait(false);
330return ClientResult.FromValue(ChatCompletion.FromClientResult(result), result.GetRawResponse());
331}
332
5dce104aJose Arriaga Maldonado1 years ago333// CUSTOM:
334// - Added Experimental attribute.
335// - Call FromClientResult.
336[Experimental("OPENAI001")]
337public virtual async Task<ClientResult<ChatCompletionDeletionResult>> DeleteChatCompletionAsync(string completionId, CancellationToken cancellationToken = default)
338{
339Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));
340
341ClientResult result = await DeleteChatCompletionAsync(completionId, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null).ConfigureAwait(false);
342return ClientResult.FromValue(ChatCompletionDeletionResult.FromClientResult(result), result.GetRawResponse());
343}
344
345// CUSTOM:
346// - Added Experimental attribute.
347// - Call FromClientResult.
348[Experimental("OPENAI001")]
349public virtual ClientResult<ChatCompletionDeletionResult> DeleteChatCompletion(string completionId, CancellationToken cancellationToken = default)
350{
351Argument.AssertNotNullOrEmpty(completionId, nameof(completionId));
352
353ClientResult result = DeleteChatCompletion(completionId, cancellationToken.CanBeCanceled ? new RequestOptions { CancellationToken = cancellationToken } : null);
354return ClientResult.FromValue(ChatCompletionDeletionResult.FromClientResult(result), result.GetRawResponse());
355}
356
9f9f2936Jose Arriaga Maldonado2 years ago357private void CreateChatCompletionOptions(IEnumerable<ChatMessage> messages, ref ChatCompletionOptions options, bool stream = false)
358{
359options.Messages = messages.ToList();
360options.Model = _model;
d6615abeJose Arriaga Maldonado1 years ago361if (stream)
362{
363options.Stream = true;
364options.StreamOptions = s_includeUsageStreamOptions;
365}
366else
367{
368options.Stream = null;
369options.StreamOptions = null;
370}
9f9f2936Jose Arriaga Maldonado2 years ago371}
372}