openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/FineTuning/FineTuningClient.Protocol.cs
232lines · modecode
| 1 | using System; |
| 2 | using System.ClientModel; |
| 3 | using System.ClientModel.Primitives; |
| 4 | using System.Text.Json; |
| 5 | using System.Threading.Tasks; |
| 6 | |
| 7 | namespace OpenAI.FineTuning; |
| 8 | |
| 9 | [CodeGenSuppress("CreateFineTuningJobAsync", typeof(BinaryContent), typeof(RequestOptions))] |
| 10 | [CodeGenSuppress("CreateFineTuningJob", typeof(BinaryContent), typeof(RequestOptions))] |
| 11 | [CodeGenSuppress("ListPaginatedFineTuningJobsAsync", typeof(string), typeof(int?), typeof(RequestOptions))] |
| 12 | [CodeGenSuppress("ListPaginatedFineTuningJobs", typeof(string), typeof(int?), typeof(RequestOptions))] |
| 13 | [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))] |
| 17 | [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))] |
| 21 | public 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> |
| 33 | /// <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> |
| 37 | /// <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> |
| 41 | /// <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> |
| 44 | public virtual async Task<FineTuningJobOperation> CreateFineTuningJobAsync( |
| 45 | BinaryContent content, |
| 46 | bool waitUntilCompleted, |
| 47 | RequestOptions options = null) |
| 48 | { |
| 49 | Argument.AssertNotNull(content, nameof(content)); |
| 50 | |
| 51 | using PipelineMessage message = CreateCreateFineTuningJobRequest(content, options); |
| 52 | PipelineResponse response = await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false); |
| 53 | |
| 54 | using JsonDocument doc = JsonDocument.Parse(response.Content); |
| 55 | string jobId = doc.RootElement.GetProperty("id"u8).GetString(); |
| 56 | string status = doc.RootElement.GetProperty("status"u8).GetString(); |
| 57 | |
| 58 | FineTuningJobOperation operation = this.CreateCreateJobOperation(jobId, status, response); |
| 59 | return await operation.WaitUntilAsync(waitUntilCompleted, options).ConfigureAwait(false); |
| 60 | } |
| 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> |
| 72 | /// <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> |
| 76 | /// <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> |
| 80 | /// <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> |
| 83 | public virtual FineTuningJobOperation CreateFineTuningJob( |
| 84 | BinaryContent content, |
| 85 | bool waitUntilCompleted, |
| 86 | RequestOptions options = null) |
| 87 | { |
| 88 | Argument.AssertNotNull(content, nameof(content)); |
| 89 | |
| 90 | using PipelineMessage message = CreateCreateFineTuningJobRequest(content, options); |
| 91 | PipelineResponse response = Pipeline.ProcessMessage(message, options); |
| 92 | |
| 93 | using JsonDocument doc = JsonDocument.Parse(response.Content); |
| 94 | string jobId = doc.RootElement.GetProperty("id"u8).GetString(); |
| 95 | string status = doc.RootElement.GetProperty("status"u8).GetString(); |
| 96 | |
| 97 | FineTuningJobOperation operation = this.CreateCreateJobOperation(jobId, status, response); |
| 98 | return operation.WaitUntil(waitUntilCompleted, options); |
| 99 | } |
| 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> |
| 112 | public virtual AsyncCollectionResult GetJobsAsync(string after, int? limit, RequestOptions options) |
| 113 | { |
| 114 | return new AsyncFineTuningJobCollectionResult(this, Pipeline, options, limit, after); |
| 115 | } |
| 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> |
| 128 | public virtual CollectionResult GetJobs(string after, int? limit, RequestOptions options) |
| 129 | { |
| 130 | return new FineTuningJobCollectionResult(this, Pipeline, options, limit, after); |
| 131 | } |
| 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> |
| 141 | /// <param name="fineTuningJobId"> The ID of the fine-tuning job. </param> |
| 142 | /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param> |
| 143 | /// <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> |
| 145 | /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception> |
| 146 | /// <returns> The response returned from the service. </returns> |
| 147 | public virtual async Task<ClientResult> GetJobAsync(string fineTuningJobId, RequestOptions options) |
| 148 | { |
| 149 | Argument.AssertNotNullOrEmpty(fineTuningJobId, nameof(fineTuningJobId)); |
| 150 | |
| 151 | using PipelineMessage message = CreateRetrieveFineTuningJobRequest(fineTuningJobId, options); |
| 152 | return ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false)); |
| 153 | } |
| 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> |
| 163 | /// <param name="fineTuningJobId"> The ID of the fine-tuning job. </param> |
| 164 | /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param> |
| 165 | /// <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> |
| 167 | /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception> |
| 168 | /// <returns> The response returned from the service. </returns> |
| 169 | public virtual ClientResult GetJob(string fineTuningJobId, RequestOptions options) |
| 170 | { |
| 171 | Argument.AssertNotNullOrEmpty(fineTuningJobId, nameof(fineTuningJobId)); |
| 172 | |
| 173 | using PipelineMessage message = CreateRetrieveFineTuningJobRequest(fineTuningJobId, options); |
| 174 | return ClientResult.FromResponse(Pipeline.ProcessMessage(message, options)); |
| 175 | } |
| 176 | |
| 177 | internal virtual PipelineMessage CreateCreateFineTuningJobRequest(BinaryContent content, RequestOptions options) |
| 178 | { |
| 179 | var message = Pipeline.CreateMessage(); |
| 180 | message.ResponseClassifier = PipelineMessageClassifier200; |
| 181 | var request = message.Request; |
| 182 | request.Method = "POST"; |
| 183 | var uri = new ClientUriBuilder(); |
| 184 | uri.Reset(_endpoint); |
| 185 | uri.AppendPath("/fine_tuning/jobs", false); |
| 186 | request.Uri = uri.ToUri(); |
| 187 | request.Headers.Set("Accept", "application/json"); |
| 188 | request.Headers.Set("Content-Type", "application/json"); |
| 189 | request.Content = content; |
| 190 | message.Apply(options); |
| 191 | return message; |
| 192 | } |
| 193 | |
| 194 | internal virtual PipelineMessage CreateGetPaginatedFineTuningJobsRequest(string after, int? limit, RequestOptions options) |
| 195 | { |
| 196 | var message = Pipeline.CreateMessage(); |
| 197 | message.ResponseClassifier = PipelineMessageClassifier200; |
| 198 | var request = message.Request; |
| 199 | request.Method = "GET"; |
| 200 | var uri = new ClientUriBuilder(); |
| 201 | uri.Reset(_endpoint); |
| 202 | uri.AppendPath("/fine_tuning/jobs", false); |
| 203 | if (after != null) |
| 204 | { |
| 205 | uri.AppendQuery("after", after, true); |
| 206 | } |
| 207 | if (limit != null) |
| 208 | { |
| 209 | uri.AppendQuery("limit", limit.Value, true); |
| 210 | } |
| 211 | request.Uri = uri.ToUri(); |
| 212 | request.Headers.Set("Accept", "application/json"); |
| 213 | message.Apply(options); |
| 214 | return message; |
| 215 | } |
| 216 | |
| 217 | internal virtual PipelineMessage CreateRetrieveFineTuningJobRequest(string fineTuningJobId, RequestOptions options) |
| 218 | { |
| 219 | var message = Pipeline.CreateMessage(); |
| 220 | message.ResponseClassifier = PipelineMessageClassifier200; |
| 221 | var request = message.Request; |
| 222 | request.Method = "GET"; |
| 223 | var uri = new ClientUriBuilder(); |
| 224 | uri.Reset(_endpoint); |
| 225 | uri.AppendPath("/fine_tuning/jobs/", false); |
| 226 | uri.AppendPath(fineTuningJobId, true); |
| 227 | request.Uri = uri.ToUri(); |
| 228 | request.Headers.Set("Accept", "application/json"); |
| 229 | message.Apply(options); |
| 230 | return message; |
| 231 | } |
| 232 | } |
| 233 | |