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/FineTuningClient.Protocol.cs

232lines · modeblame

58f93c8dShivangiReja2 years ago1using System;
9f9f2936Jose Arriaga Maldonado2 years ago2using System.ClientModel;
3using System.ClientModel.Primitives;
a330c2e7Jose Arriaga Maldonado1 years ago4using System.Text.Json;
9f9f2936Jose Arriaga Maldonado2 years ago5using System.Threading.Tasks;
6
7namespace OpenAI.FineTuning;
8
9[CodeGenSuppress("CreateFineTuningJobAsync", typeof(BinaryContent), typeof(RequestOptions))]
10[CodeGenSuppress("CreateFineTuningJob", typeof(BinaryContent), typeof(RequestOptions))]
e0fee603Jose Arriaga Maldonado1 years ago11[CodeGenSuppress("ListPaginatedFineTuningJobsAsync", typeof(string), typeof(int?), typeof(RequestOptions))]
12[CodeGenSuppress("ListPaginatedFineTuningJobs", typeof(string), typeof(int?), typeof(RequestOptions))]
9f9f2936Jose Arriaga Maldonado2 years ago13[CodeGenSuppress("RetrieveFineTuningJobAsync", typeof(string), typeof(RequestOptions))]
14[CodeGenSuppress("RetrieveFineTuningJob", typeof(string), typeof(RequestOptions))]
15[CodeGenSuppress("CancelFineTuningJobAsync", typeof(string), typeof(RequestOptions))]
16[CodeGenSuppress("CancelFineTuningJob", typeof(string), typeof(RequestOptions))]
e0fee603Jose Arriaga Maldonado1 years ago17[CodeGenSuppress("ListFineTuningEventsAsync", typeof(string), typeof(string), typeof(int?), typeof(RequestOptions))]
18[CodeGenSuppress("ListFineTuningEvents", typeof(string), typeof(string), typeof(int?), typeof(RequestOptions))]
19[CodeGenSuppress("ListFineTuningJobCheckpointsAsync", typeof(string), typeof(string), typeof(int?), typeof(RequestOptions))]
20[CodeGenSuppress("ListFineTuningJobCheckpoints", typeof(string), typeof(string), typeof(int?), typeof(RequestOptions))]
9f9f2936Jose Arriaga Maldonado2 years ago21public partial class FineTuningClient
22{
23// CUSTOM:
24// - Renamed.
25// - Edited doc comment.
26/// <summary>
27/// [Protocol Method] Creates a fine-tuning job which begins the process of creating a new model from a given dataset.
28///
29/// Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.
30///
31/// [Learn more about fine-tuning](/docs/guides/fine-tuning)
32/// </summary>
a330c2e7Jose Arriaga Maldonado1 years ago33/// <param name="waitUntilCompleted"> Value indicating whether the method
34/// should return after the operation has been started and is still running
35/// on the service, or wait until the operation has completed to return.
36/// </param>
9f9f2936Jose Arriaga Maldonado2 years ago37/// <param name="content"> The content to send as the body of the request. </param>
38/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
39/// <exception cref="ArgumentNullException"> <paramref name="content"/> is null. </exception>
40/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
a330c2e7Jose Arriaga Maldonado1 years ago41/// <returns> A <see cref="FineTuningJobOperation"/> that can be used to wait for
42/// the operation to complete, get information about the fine tuning job, or
43/// cancel the operation. </returns>
44public virtual async Task<FineTuningJobOperation> CreateFineTuningJobAsync(
45BinaryContent content,
46bool waitUntilCompleted,
47RequestOptions options = null)
9f9f2936Jose Arriaga Maldonado2 years ago48{
49Argument.AssertNotNull(content, nameof(content));
50
51using PipelineMessage message = CreateCreateFineTuningJobRequest(content, options);
e0fee603Jose Arriaga Maldonado1 years ago52PipelineResponse response = await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false);
a330c2e7Jose Arriaga Maldonado1 years ago53
54using JsonDocument doc = JsonDocument.Parse(response.Content);
55string jobId = doc.RootElement.GetProperty("id"u8).GetString();
56string status = doc.RootElement.GetProperty("status"u8).GetString();
57
58FineTuningJobOperation operation = this.CreateCreateJobOperation(jobId, status, response);
59return await operation.WaitUntilAsync(waitUntilCompleted, options).ConfigureAwait(false);
9f9f2936Jose Arriaga Maldonado2 years ago60}
61
62// CUSTOM:
63// - Renamed.
64// - Edited doc comment.
65/// <summary>
66/// [Protocol Method] Creates a fine-tuning job which begins the process of creating a new model from a given dataset.
67///
68/// Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.
69///
70/// [Learn more about fine-tuning](/docs/guides/fine-tuning)
71/// </summary>
a330c2e7Jose Arriaga Maldonado1 years ago72/// <param name="waitUntilCompleted"> Value indicating whether the method
73/// should return after the operation has been started and is still running
74/// on the service, or wait until the operation has completed to return.
75/// </param>
9f9f2936Jose Arriaga Maldonado2 years ago76/// <param name="content"> The content to send as the body of the request. </param>
77/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
78/// <exception cref="ArgumentNullException"> <paramref name="content"/> is null. </exception>
79/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
a330c2e7Jose Arriaga Maldonado1 years ago80/// <returns> A <see cref="FineTuningJobOperation"/> that can be used to wait for
81/// the operation to complete, get information about the fine tuning job, or
82/// cancel the operation. </returns>
83public virtual FineTuningJobOperation CreateFineTuningJob(
84BinaryContent content,
85bool waitUntilCompleted,
86RequestOptions options = null)
9f9f2936Jose Arriaga Maldonado2 years ago87{
88Argument.AssertNotNull(content, nameof(content));
89
90using PipelineMessage message = CreateCreateFineTuningJobRequest(content, options);
e0fee603Jose Arriaga Maldonado1 years ago91PipelineResponse response = Pipeline.ProcessMessage(message, options);
a330c2e7Jose Arriaga Maldonado1 years ago92
93using JsonDocument doc = JsonDocument.Parse(response.Content);
94string jobId = doc.RootElement.GetProperty("id"u8).GetString();
95string status = doc.RootElement.GetProperty("status"u8).GetString();
96
97FineTuningJobOperation operation = this.CreateCreateJobOperation(jobId, status, response);
98return operation.WaitUntil(waitUntilCompleted, options);
9f9f2936Jose Arriaga Maldonado2 years ago99}
100
101// CUSTOM:
102// - Renamed.
103// - Edited doc comment.
104/// <summary>
105/// [Protocol Method] List your organization's fine-tuning jobs
106/// </summary>
107/// <param name="after"> Identifier for the last job from the previous pagination request. </param>
108/// <param name="limit"> Number of fine-tuning jobs to retrieve. </param>
109/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
110/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
111/// <returns> The response returned from the service. </returns>
2ab1a942Jose Arriaga Maldonado1 years ago112public virtual AsyncCollectionResult GetJobsAsync(string after, int? limit, RequestOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago113{
e0fee603Jose Arriaga Maldonado1 years ago114return new AsyncFineTuningJobCollectionResult(this, Pipeline, options, limit, after);
9f9f2936Jose Arriaga Maldonado2 years ago115}
116
117// CUSTOM:
118// - Renamed.
119// - Edited doc comment.
120/// <summary>
121/// [Protocol Method] List your organization's fine-tuning jobs
122/// </summary>
123/// <param name="after"> Identifier for the last job from the previous pagination request. </param>
124/// <param name="limit"> Number of fine-tuning jobs to retrieve. </param>
125/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
126/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
127/// <returns> The response returned from the service. </returns>
2ab1a942Jose Arriaga Maldonado1 years ago128public virtual CollectionResult GetJobs(string after, int? limit, RequestOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago129{
e0fee603Jose Arriaga Maldonado1 years ago130return new FineTuningJobCollectionResult(this, Pipeline, options, limit, after);
9f9f2936Jose Arriaga Maldonado2 years ago131}
132
133// CUSTOM:
134// - Renamed.
135// - Edited doc comment.
136/// <summary>
137/// [Protocol Method] Get info about a fine-tuning job.
138///
139/// [Learn more about fine-tuning](/docs/guides/fine-tuning)
140/// </summary>
a330c2e7Jose Arriaga Maldonado1 years ago141/// <param name="fineTuningJobId"> The ID of the fine-tuning job. </param>
9f9f2936Jose Arriaga Maldonado2 years ago142/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
a330c2e7Jose Arriaga Maldonado1 years ago143/// <exception cref="ArgumentNullException"> <paramref name="fineTuningJobId"/> is null. </exception>
144/// <exception cref="ArgumentException"> <paramref name="fineTuningJobId"/> is an empty string, and was expected to be non-empty. </exception>
9f9f2936Jose Arriaga Maldonado2 years ago145/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
146/// <returns> The response returned from the service. </returns>
a330c2e7Jose Arriaga Maldonado1 years ago147public virtual async Task<ClientResult> GetJobAsync(string fineTuningJobId, RequestOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago148{
a330c2e7Jose Arriaga Maldonado1 years ago149Argument.AssertNotNullOrEmpty(fineTuningJobId, nameof(fineTuningJobId));
9f9f2936Jose Arriaga Maldonado2 years ago150
a330c2e7Jose Arriaga Maldonado1 years ago151using PipelineMessage message = CreateRetrieveFineTuningJobRequest(fineTuningJobId, options);
e0fee603Jose Arriaga Maldonado1 years ago152return ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
9f9f2936Jose Arriaga Maldonado2 years ago153}
154
155// CUSTOM:
156// - Renamed.
157// - Edited doc comment.
158/// <summary>
159/// [Protocol Method] Get info about a fine-tuning job.
160///
161/// [Learn more about fine-tuning](/docs/guides/fine-tuning)
162/// </summary>
a330c2e7Jose Arriaga Maldonado1 years ago163/// <param name="fineTuningJobId"> The ID of the fine-tuning job. </param>
9f9f2936Jose Arriaga Maldonado2 years ago164/// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
a330c2e7Jose Arriaga Maldonado1 years ago165/// <exception cref="ArgumentNullException"> <paramref name="fineTuningJobId"/> is null. </exception>
166/// <exception cref="ArgumentException"> <paramref name="fineTuningJobId"/> is an empty string, and was expected to be non-empty. </exception>
9f9f2936Jose Arriaga Maldonado2 years ago167/// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
168/// <returns> The response returned from the service. </returns>
a330c2e7Jose Arriaga Maldonado1 years ago169public virtual ClientResult GetJob(string fineTuningJobId, RequestOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago170{
a330c2e7Jose Arriaga Maldonado1 years ago171Argument.AssertNotNullOrEmpty(fineTuningJobId, nameof(fineTuningJobId));
9f9f2936Jose Arriaga Maldonado2 years ago172
a330c2e7Jose Arriaga Maldonado1 years ago173using PipelineMessage message = CreateRetrieveFineTuningJobRequest(fineTuningJobId, options);
e0fee603Jose Arriaga Maldonado1 years ago174return ClientResult.FromResponse(Pipeline.ProcessMessage(message, options));
9f9f2936Jose Arriaga Maldonado2 years ago175}
176
a330c2e7Jose Arriaga Maldonado1 years ago177internal virtual PipelineMessage CreateCreateFineTuningJobRequest(BinaryContent content, RequestOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago178{
e0fee603Jose Arriaga Maldonado1 years ago179var message = Pipeline.CreateMessage();
a330c2e7Jose Arriaga Maldonado1 years ago180message.ResponseClassifier = PipelineMessageClassifier200;
181var request = message.Request;
182request.Method = "POST";
183var uri = new ClientUriBuilder();
184uri.Reset(_endpoint);
185uri.AppendPath("/fine_tuning/jobs", false);
186request.Uri = uri.ToUri();
187request.Headers.Set("Accept", "application/json");
188request.Headers.Set("Content-Type", "application/json");
189request.Content = content;
190message.Apply(options);
191return message;
9f9f2936Jose Arriaga Maldonado2 years ago192}
193
a330c2e7Jose Arriaga Maldonado1 years ago194internal virtual PipelineMessage CreateGetPaginatedFineTuningJobsRequest(string after, int? limit, RequestOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago195{
e0fee603Jose Arriaga Maldonado1 years ago196var message = Pipeline.CreateMessage();
a330c2e7Jose Arriaga Maldonado1 years ago197message.ResponseClassifier = PipelineMessageClassifier200;
198var request = message.Request;
199request.Method = "GET";
200var uri = new ClientUriBuilder();
201uri.Reset(_endpoint);
202uri.AppendPath("/fine_tuning/jobs", false);
203if (after != null)
204{
205uri.AppendQuery("after", after, true);
206}
207if (limit != null)
208{
209uri.AppendQuery("limit", limit.Value, true);
210}
211request.Uri = uri.ToUri();
212request.Headers.Set("Accept", "application/json");
213message.Apply(options);
214return message;
9f9f2936Jose Arriaga Maldonado2 years ago215}
216
a330c2e7Jose Arriaga Maldonado1 years ago217internal virtual PipelineMessage CreateRetrieveFineTuningJobRequest(string fineTuningJobId, RequestOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago218{
e0fee603Jose Arriaga Maldonado1 years ago219var message = Pipeline.CreateMessage();
a330c2e7Jose Arriaga Maldonado1 years ago220message.ResponseClassifier = PipelineMessageClassifier200;
221var request = message.Request;
222request.Method = "GET";
223var uri = new ClientUriBuilder();
224uri.Reset(_endpoint);
225uri.AppendPath("/fine_tuning/jobs/", false);
226uri.AppendPath(fineTuningJobId, true);
227request.Uri = uri.ToUri();
228request.Headers.Set("Accept", "application/json");
229message.Apply(options);
230return message;
9f9f2936Jose Arriaga Maldonado2 years ago231}
232}