openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
achandmsft-patch-1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/FineTuning/FineTuningJobOperation.Protocol.cs

462lines · modecode

1using System;
2using System.ClientModel;
3using System.ClientModel.Primitives;
4using System.Diagnostics.CodeAnalysis;
5using System.Text.Json;
6using System.Threading;
7using System.Threading.Tasks;
8
9#nullable enable
10
11namespace OpenAI.FineTuning;
12
13/// <summary>
14/// A long-running operation for creating a new model from a given dataset.
15/// </summary>
16[Experimental("OPENAI001")]
17public class FineTuningJobOperation : OperationResult
18{
19 private readonly ClientPipeline _pipeline;
20 private readonly Uri _endpoint;
21
22 private readonly string _jobId;
23
24 internal FineTuningJobOperation(
25 ClientPipeline pipeline,
26 Uri endpoint,
27 string jobId,
28 string status,
29 PipelineResponse response) : base(response)
30 {
31 _pipeline = pipeline;
32 _endpoint = endpoint;
33 _jobId = jobId;
34
35 HasCompleted = GetHasCompleted(status);
36 RehydrationToken = new FineTuningJobOperationToken(jobId);
37 }
38
39 public string JobId => _jobId;
40
41 /// <inheritdoc/>
42 public override ContinuationToken? RehydrationToken { get; protected set; }
43
44 /// <summary>
45 /// Recreates a <see cref="FineTuningJobOperation"/> from a rehydration token.
46 /// </summary>
47 /// <param name="client"> The <see cref="FineTuningClient"/> used to obtain the
48 /// operation status from the service. </param>
49 /// <param name="rehydrationToken"> The rehydration token corresponding to
50 /// the operation to rehydrate. </param>
51 /// <param name="cancellationToken"> A token that can be used to cancel the
52 /// request. </param>
53 /// <returns> The rehydrated operation. </returns>
54 /// <exception cref="ArgumentNullException"> <paramref name="client"/> or <paramref name="rehydrationToken"/> is null. </exception>
55 public static async Task<FineTuningJobOperation> RehydrateAsync(FineTuningClient client, ContinuationToken rehydrationToken, CancellationToken cancellationToken = default)
56 {
57 Argument.AssertNotNull(client, nameof(client));
58 Argument.AssertNotNull(rehydrationToken, nameof(rehydrationToken));
59
60 FineTuningJobOperationToken token = FineTuningJobOperationToken.FromToken(rehydrationToken);
61
62 ClientResult result = await client.GetJobAsync(token.JobId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
63 PipelineResponse response = result.GetRawResponse();
64
65 using JsonDocument doc = JsonDocument.Parse(response.Content);
66 string status = doc.RootElement.GetProperty("status"u8).GetString()!;
67
68 return client.CreateCreateJobOperation(token.JobId, status, response);
69 }
70
71 /// <summary>
72 /// Recreates a <see cref="FineTuningJobOperation"/> from a rehydration token.
73 /// </summary>
74 /// <param name="client"> The <see cref="FineTuningClient"/> used to obtain the
75 /// operation status from the service. </param>
76 /// <param name="rehydrationToken"> The rehydration token corresponding to
77 /// the operation to rehydrate. </param>
78 /// <param name="cancellationToken"> A token that can be used to cancel the
79 /// request. </param>
80 /// <returns> The rehydrated operation. </returns>
81 /// <exception cref="ArgumentNullException"> <paramref name="client"/> or <paramref name="rehydrationToken"/> is null. </exception>
82 public static FineTuningJobOperation Rehydrate(FineTuningClient client, ContinuationToken rehydrationToken, CancellationToken cancellationToken = default)
83 {
84 Argument.AssertNotNull(client, nameof(client));
85 Argument.AssertNotNull(rehydrationToken, nameof(rehydrationToken));
86
87 FineTuningJobOperationToken token = FineTuningJobOperationToken.FromToken(rehydrationToken);
88
89 ClientResult result = client.GetJob(token.JobId, cancellationToken.ToRequestOptions());
90 PipelineResponse response = result.GetRawResponse();
91
92 using JsonDocument doc = JsonDocument.Parse(response.Content);
93 string status = doc.RootElement.GetProperty("status"u8).GetString()!;
94
95 return client.CreateCreateJobOperation(token.JobId, status, response);
96 }
97
98 /// <summary>
99 /// Recreates a <see cref="FineTuningJobOperation"/> from a rehydration token.
100 /// </summary>
101 /// <param name="client"> The <see cref="FineTuningClient"/> used to obtain the
102 /// operation status from the service. </param>
103 /// <param name="fineTuningJobId"> The id of the fine tuning job to rehydrate.</param>
104 /// <param name="cancellationToken"> A token that can be used to cancel the
105 /// request. </param>
106 /// <returns> The rehydrated operation. </returns>
107 /// <exception cref="ArgumentNullException"> <paramref name="client"/> or <paramref name="fineTuningJobId"/> is null. </exception>
108 public static async Task<FineTuningJobOperation> RehydrateAsync(FineTuningClient client, string fineTuningJobId, CancellationToken cancellationToken = default)
109 {
110 Argument.AssertNotNull(client, nameof(client));
111 Argument.AssertNotNull(fineTuningJobId, nameof(fineTuningJobId));
112
113 ClientResult result = await client.GetJobAsync(fineTuningJobId, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
114 PipelineResponse response = result.GetRawResponse();
115
116 using JsonDocument doc = JsonDocument.Parse(response.Content);
117 string status = doc.RootElement.GetProperty("status"u8).GetString()!;
118
119 return client.CreateCreateJobOperation(fineTuningJobId, status, response);
120 }
121
122 /// <summary>
123 /// Recreates a <see cref="FineTuningJobOperation"/> from a rehydration token.
124 /// </summary>
125 /// <param name="client"> The <see cref="FineTuningClient"/> used to obtain the
126 /// operation status from the service. </param>
127 /// <param name="fineTuningJobId"> The id of the fine tuning job to rehydrate.</param>
128 /// <param name="cancellationToken"> A token that can be used to cancel the
129 /// request. </param>
130 /// <returns> The rehydrated operation. </returns>
131 /// <exception cref="ArgumentNullException"> <paramref name="client"/> or <paramref name="fineTuningJobId"/> is null. </exception>
132 public static FineTuningJobOperation Rehydrate(FineTuningClient client, string fineTuningJobId, CancellationToken cancellationToken = default)
133 {
134 Argument.AssertNotNull(client, nameof(client));
135 Argument.AssertNotNull(fineTuningJobId, nameof(fineTuningJobId));
136
137 ClientResult result = client.GetJob(fineTuningJobId, cancellationToken.ToRequestOptions());
138 PipelineResponse response = result.GetRawResponse();
139
140 using JsonDocument doc = JsonDocument.Parse(response.Content);
141 string status = doc.RootElement.GetProperty("status"u8).GetString()!;
142
143 return client.CreateCreateJobOperation(fineTuningJobId, status, response);
144 }
145
146 /// <inheritdoc/>
147 public override async ValueTask<ClientResult> UpdateStatusAsync(RequestOptions? options = null)
148 {
149 ClientResult result = await GetJobAsync(options).ConfigureAwait(false);
150
151 ApplyUpdate(result);
152
153 return result;
154 }
155
156 /// <inheritdoc/>
157 public override ClientResult UpdateStatus(RequestOptions? options = null)
158 {
159 ClientResult result = GetJob(options);
160
161 ApplyUpdate(result);
162
163 return result;
164 }
165
166 internal async Task<FineTuningJobOperation> WaitUntilAsync(bool waitUntilCompleted, RequestOptions? options)
167 {
168 if (!waitUntilCompleted) return this;
169 await WaitForCompletionAsync(options?.CancellationToken ?? default).ConfigureAwait(false);
170 return this;
171 }
172
173 internal FineTuningJobOperation WaitUntil(bool waitUntilCompleted, RequestOptions? options)
174 {
175 if (!waitUntilCompleted) return this;
176 WaitForCompletion(options?.CancellationToken ?? default);
177 return this;
178 }
179
180 private void ApplyUpdate(ClientResult result)
181 {
182 PipelineResponse response = result.GetRawResponse();
183
184 using JsonDocument doc = JsonDocument.Parse(response.Content);
185 string? status = doc.RootElement.GetProperty("status"u8).GetString();
186
187 HasCompleted = GetHasCompleted(status);
188 SetRawResponse(response);
189 }
190
191 private static bool GetHasCompleted(string? status)
192 {
193 return status == FineTuningJobStatus.Succeeded ||
194 status == FineTuningJobStatus.Failed ||
195 status == FineTuningJobStatus.Cancelled;
196 }
197
198 // Generated protocol methods
199
200 // CUSTOM:
201 // - Renamed.
202 // - Edited doc comment.
203 /// <summary>
204 /// [Protocol Method] Get info about a fine-tuning job.
205 ///
206 /// [Learn more about fine-tuning](/docs/guides/fine-tuning)
207 /// </summary>
208 /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
209 /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
210 /// <returns> The response returned from the service. </returns>
211 public virtual async Task<ClientResult> GetJobAsync(RequestOptions? options)
212 {
213 using PipelineMessage message = CreateRetrieveFineTuningJobRequest(_jobId, options);
214 return ClientResult.FromResponse(await _pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
215 }
216
217 // CUSTOM:
218 // - Renamed.
219 // - Edited doc comment.
220 /// <summary>
221 /// [Protocol Method] Get info about a fine-tuning job.
222 ///
223 /// [Learn more about fine-tuning](/docs/guides/fine-tuning)
224 /// </summary>
225 /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
226 /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
227 /// <returns> The response returned from the service. </returns>
228 public virtual ClientResult GetJob(RequestOptions? options)
229 {
230 using PipelineMessage message = CreateRetrieveFineTuningJobRequest(_jobId, options);
231 return ClientResult.FromResponse(_pipeline.ProcessMessage(message, options));
232 }
233
234 // CUSTOM:
235 // - Renamed.
236 // - Edited doc comment.
237 /// <summary>
238 /// [Protocol Method] Immediately cancel a fine-tune job.
239 /// </summary>
240 /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
241 /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
242 /// <returns> The response returned from the service. </returns>
243 public virtual async Task<ClientResult> CancelAsync(RequestOptions? options)
244 {
245 using PipelineMessage message = CreateCancelFineTuningJobRequest(_jobId, options);
246 return ClientResult.FromResponse(await _pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
247 }
248
249 // CUSTOM:
250 // - Renamed.
251 // - Edited doc comment.
252 /// <summary>
253 /// [Protocol Method] Immediately cancel a fine-tune job.
254 /// </summary>
255 /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
256 /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
257 /// <returns> The response returned from the service. </returns>
258 public virtual ClientResult Cancel(RequestOptions? options)
259 {
260 using PipelineMessage message = CreateCancelFineTuningJobRequest(_jobId, options);
261 return ClientResult.FromResponse(_pipeline.ProcessMessage(message, options));
262 }
263
264 // CUSTOM:
265 // - Renamed.
266 // - Edited doc comment.
267 /// <summary>
268 /// [Protocol Method] Get status updates for a fine-tuning job.
269 /// </summary>
270 /// <param name="after"> Identifier for the last event from the previous pagination request. </param>
271 /// <param name="limit"> Number of events to retrieve. </param>
272 /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
273 /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
274 /// <returns> The response returned from the service. </returns>
275 public virtual AsyncCollectionResult GetJobEventsAsync(string? after, int? limit, RequestOptions options)
276 {
277 return new AsyncFineTuningJobEventCollectionResult(this, options, limit, after);
278 }
279
280 // CUSTOM:
281 // - Renamed.
282 // - Edited doc comment.
283 /// <summary>
284 /// [Protocol Method] Get status updates for a fine-tuning job.
285 /// </summary>
286 /// <param name="after"> Identifier for the last event from the previous pagination request. </param>
287 /// <param name="limit"> Number of events to retrieve. </param>
288 /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
289 /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
290 /// <returns> The response returned from the service. </returns>
291 public virtual CollectionResult GetJobEvents(string? after, int? limit, RequestOptions options)
292 {
293 return new FineTuningJobEventCollectionResult(this, options, limit, after);
294 }
295
296 /// <summary>
297 /// [Protocol Method] List the checkpoints for a fine-tuning job.
298 /// </summary>
299 /// <param name="after"> Identifier for the last checkpoint ID from the previous pagination request. </param>
300 /// <param name="limit"> Number of checkpoints to retrieve. </param>
301 /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
302 /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
303 /// <returns> The response returned from the service. </returns>
304 public virtual AsyncCollectionResult GetJobCheckpointsAsync(string? after, int? limit, RequestOptions? options)
305 {
306 return new AsyncFineTuningJobCheckpointCollectionResult(this, options, limit, after);
307 }
308
309 /// <summary>
310 /// [Protocol Method] List the checkpoints for a fine-tuning job.
311 /// </summary>
312 /// <param name="after"> Identifier for the last checkpoint ID from the previous pagination request. </param>
313 /// <param name="limit"> Number of checkpoints to retrieve. </param>
314 /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
315 /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
316 /// <returns> The response returned from the service. </returns>
317 public virtual CollectionResult GetJobCheckpoints(string? after, int? limit, RequestOptions? options)
318 {
319 return new FineTuningJobCheckpointCollectionResult(this, options, limit, after);
320 }
321
322 /// <summary>
323 /// [Protocol Method] List the checkpoints for a fine-tuning job.
324 /// </summary>
325 /// <param name="after"> Identifier for the last checkpoint ID from the previous pagination request. </param>
326 /// <param name="limit"> Number of checkpoints to retrieve. </param>
327 /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
328 /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
329 /// <returns> The response returned from the service. </returns>
330 internal virtual async Task<ClientResult> GetJobCheckpointsPageAsync(string? after, int? limit, RequestOptions? options)
331 {
332 using PipelineMessage message = CreateGetFineTuningJobCheckpointsRequest(_jobId, after, limit, options);
333 return ClientResult.FromResponse(await _pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
334 }
335
336 /// <summary>
337 /// [Protocol Method] List the checkpoints for a fine-tuning job.
338 /// </summary>
339 /// <param name="after"> Identifier for the last checkpoint ID from the previous pagination request. </param>
340 /// <param name="limit"> Number of checkpoints to retrieve. </param>
341 /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
342 /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
343 /// <returns> The response returned from the service. </returns>
344 internal virtual ClientResult GetJobPageCheckpoints(string? after, int? limit, RequestOptions? options)
345 {
346 using PipelineMessage message = CreateGetFineTuningJobCheckpointsRequest(_jobId, after, limit, options);
347 return ClientResult.FromResponse(_pipeline.ProcessMessage(message, options));
348 }
349
350 /// <summary>
351 /// [Protocol Method] List the checkpoints for a fine-tuning job.
352 /// </summary>
353 /// <param name="after"> Identifier for the last checkpoint ID from the previous pagination request. </param>
354 /// <param name="limit"> Number of checkpoints to retrieve. </param>
355 /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
356 /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
357 /// <returns> The response returned from the service. </returns>
358 internal virtual ClientResult GetJobCheckpointsPage(string? after, int? limit, RequestOptions? options)
359 {
360 using PipelineMessage message = CreateGetFineTuningJobCheckpointsRequest(_jobId, after, limit, options);
361 return ClientResult.FromResponse(_pipeline.ProcessMessage(message, options));
362 }
363
364 internal virtual async Task<ClientResult> GetJobEventsPageAsync(string? after, int? limit, RequestOptions? options)
365 {
366 using PipelineMessage message = CreateGetFineTuningEventsRequest(_jobId, after, limit, options);
367 return ClientResult.FromResponse(await _pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
368 }
369
370 internal virtual ClientResult GetJobEventsPage(string? after, int? limit, RequestOptions? options)
371 {
372 using PipelineMessage message = CreateGetFineTuningEventsRequest(_jobId, after, limit, options);
373 return ClientResult.FromResponse(_pipeline.ProcessMessage(message, options));
374
375 }
376
377 internal virtual PipelineMessage CreateRetrieveFineTuningJobRequest(string fineTuningJobId, RequestOptions? options)
378 {
379 var message = _pipeline.CreateMessage();
380 message.ResponseClassifier = PipelineMessageClassifier200;
381 var request = message.Request;
382 request.Method = "GET";
383 var uri = new ClientUriBuilder();
384 uri.Reset(_endpoint);
385 uri.AppendPath("/fine_tuning/jobs/", false);
386 uri.AppendPath(fineTuningJobId, true);
387 request.Uri = uri.ToUri();
388 request.Headers.Set("Accept", "application/json");
389 message.Apply(options);
390 return message;
391 }
392
393 internal virtual PipelineMessage CreateCancelFineTuningJobRequest(string fineTuningJobId, RequestOptions? options)
394 {
395 var message = _pipeline.CreateMessage();
396 message.ResponseClassifier = PipelineMessageClassifier200;
397 var request = message.Request;
398 request.Method = "POST";
399 var uri = new ClientUriBuilder();
400 uri.Reset(_endpoint);
401 uri.AppendPath("/fine_tuning/jobs/", false);
402 uri.AppendPath(fineTuningJobId, true);
403 uri.AppendPath("/cancel", false);
404 request.Uri = uri.ToUri();
405 request.Headers.Set("Accept", "application/json");
406 message.Apply(options);
407 return message;
408 }
409
410 internal virtual PipelineMessage CreateGetFineTuningJobCheckpointsRequest(string fineTuningJobId, string? after, int? limit, RequestOptions? options)
411 {
412 var message = _pipeline.CreateMessage();
413 message.ResponseClassifier = PipelineMessageClassifier200;
414 var request = message.Request;
415 request.Method = "GET";
416 var uri = new ClientUriBuilder();
417 uri.Reset(_endpoint);
418 uri.AppendPath("/fine_tuning/jobs/", false);
419 uri.AppendPath(fineTuningJobId, true);
420 uri.AppendPath("/checkpoints", false);
421 if (after != null)
422 {
423 uri.AppendQuery("after", after, true);
424 }
425 if (limit != null)
426 {
427 uri.AppendQuery("limit", limit.Value, true);
428 }
429 request.Uri = uri.ToUri();
430 request.Headers.Set("Accept", "application/json");
431 message.Apply(options);
432 return message;
433 }
434
435 internal virtual PipelineMessage CreateGetFineTuningEventsRequest(string jobId, string? after, int? limit, RequestOptions? options)
436 {
437 var message = _pipeline.CreateMessage();
438 message.ResponseClassifier = PipelineMessageClassifier200;
439 var request = message.Request;
440 request.Method = "GET";
441 var uri = new ClientUriBuilder();
442 uri.Reset(_endpoint);
443 uri.AppendPath("/fine_tuning/jobs/", false);
444 uri.AppendPath(jobId, true);
445 uri.AppendPath("/events", false);
446 if (after != null)
447 {
448 uri.AppendQuery("after", after, true);
449 }
450 if (limit != null)
451 {
452 uri.AppendQuery("limit", limit.Value, true);
453 }
454 request.Uri = uri.ToUri();
455 request.Headers.Set("Accept", "application/json");
456 message.Apply(options);
457 return message;
458 }
459
460 private static PipelineMessageClassifier? _pipelineMessageClassifier200;
461 private static PipelineMessageClassifier PipelineMessageClassifier200 => _pipelineMessageClassifier200 ??= PipelineMessageClassifier.Create(stackalloc ushort[] { 200 });
462}
463