openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Batch/BatchClient.Protocol.cs
204lines · modeblame
9f9f2936Jose Arriaga Maldonado2 years ago | 1 | using System; |
| 2 | using System.ClientModel; | |
| 3 | using System.ClientModel.Primitives; | |
a330c2e7Jose Arriaga Maldonado1 years ago | 4 | using System.Text.Json; |
9f9f2936Jose Arriaga Maldonado2 years ago | 5 | using System.Threading.Tasks; |
| 6 | | |
| 7 | namespace OpenAI.Batch; | |
| 8 | | |
13a9c686Jose Arriaga Maldonado1 years ago | 9 | [CodeGenSuppress("RetrieveBatch", typeof(string), typeof(RequestOptions))] |
| 10 | [CodeGenSuppress("RetrieveBatchAsync", typeof(string), typeof(RequestOptions))] | |
e0fee603Jose Arriaga Maldonado1 years ago | 11 | [CodeGenSuppress("ListBatches", typeof(string), typeof(int?), typeof(RequestOptions))] |
| 12 | [CodeGenSuppress("ListBatchesAsync", typeof(string), typeof(int?), typeof(RequestOptions))] | |
9f9f2936Jose Arriaga Maldonado2 years ago | 13 | public partial class BatchClient |
| 14 | { | |
| 15 | /// <summary> | |
| 16 | /// [Protocol Method] Creates and executes a batch from an uploaded file of requests | |
| 17 | /// </summary> | |
a330c2e7Jose Arriaga Maldonado1 years ago | 18 | /// <param name="waitUntilCompleted"> Value indicating whether the method |
| 19 | /// should return after the operation has been started and is still running | |
| 20 | /// on the service, or wait until the operation has completed to return. | |
| 21 | /// </param> | |
9f9f2936Jose Arriaga Maldonado2 years ago | 22 | /// <param name="content"> The content to send as the body of the request. </param> |
| 23 | /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param> | |
| 24 | /// <exception cref="ArgumentNullException"> <paramref name="content"/> is null. </exception> | |
| 25 | /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception> | |
a330c2e7Jose Arriaga Maldonado1 years ago | 26 | /// <returns> A <see cref="CreateBatchOperation"/> that can be used to wait for |
| 27 | /// the operation to complete, or cancel the operation. </returns> | |
| 28 | public virtual async Task<CreateBatchOperation> CreateBatchAsync(BinaryContent content, bool waitUntilCompleted, RequestOptions options = null) | |
9f9f2936Jose Arriaga Maldonado2 years ago | 29 | { |
| 30 | Argument.AssertNotNull(content, nameof(content)); | |
| 31 | | |
| 32 | using PipelineMessage message = CreateCreateBatchRequest(content, options); | |
a330c2e7Jose Arriaga Maldonado1 years ago | 33 | |
e0fee603Jose Arriaga Maldonado1 years ago | 34 | PipelineResponse response = await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false); |
a330c2e7Jose Arriaga Maldonado1 years ago | 35 | |
| 36 | using JsonDocument doc = JsonDocument.Parse(response.Content); | |
| 37 | string batchId = doc.RootElement.GetProperty("id"u8).GetString(); | |
| 38 | string status = doc.RootElement.GetProperty("status"u8).GetString(); | |
| 39 | | |
| 40 | CreateBatchOperation operation = this.CreateCreateBatchOperation(batchId, status, response); | |
| 41 | return await operation.WaitUntilAsync(waitUntilCompleted, options).ConfigureAwait(false); | |
9f9f2936Jose Arriaga Maldonado2 years ago | 42 | } |
| 43 | | |
| 44 | /// <summary> | |
| 45 | /// [Protocol Method] Creates and executes a batch from an uploaded file of requests | |
| 46 | /// </summary> | |
a330c2e7Jose Arriaga Maldonado1 years ago | 47 | /// <param name="waitUntilCompleted"> Value indicating whether the method |
| 48 | /// should return after the operation has been started and is still running | |
| 49 | /// on the service, or wait until the operation has completed to return. | |
| 50 | /// </param> | |
9f9f2936Jose Arriaga Maldonado2 years ago | 51 | /// <param name="content"> The content to send as the body of the request. </param> |
| 52 | /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param> | |
| 53 | /// <exception cref="ArgumentNullException"> <paramref name="content"/> is null. </exception> | |
| 54 | /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception> | |
a330c2e7Jose Arriaga Maldonado1 years ago | 55 | /// <returns> A <see cref="CreateBatchOperation"/> that can be used to wait for |
| 56 | /// the operation to complete, or cancel the operation. </returns> | |
| 57 | public virtual CreateBatchOperation CreateBatch(BinaryContent content, bool waitUntilCompleted, RequestOptions options = null) | |
9f9f2936Jose Arriaga Maldonado2 years ago | 58 | { |
| 59 | Argument.AssertNotNull(content, nameof(content)); | |
| 60 | | |
| 61 | using PipelineMessage message = CreateCreateBatchRequest(content, options); | |
e0fee603Jose Arriaga Maldonado1 years ago | 62 | PipelineResponse response = Pipeline.ProcessMessage(message, options); |
a330c2e7Jose Arriaga Maldonado1 years ago | 63 | |
| 64 | using JsonDocument doc = JsonDocument.Parse(response.Content); | |
| 65 | string batchId = doc.RootElement.GetProperty("id"u8).GetString(); | |
| 66 | string status = doc.RootElement.GetProperty("status"u8).GetString(); | |
| 67 | | |
| 68 | CreateBatchOperation operation = this.CreateCreateBatchOperation(batchId, status, response); | |
| 69 | return operation.WaitUntil(waitUntilCompleted, options); | |
9f9f2936Jose Arriaga Maldonado2 years ago | 70 | } |
| 71 | | |
| 72 | /// <summary> | |
| 73 | /// [Protocol Method] List your organization's batches. | |
| 74 | /// </summary> | |
| 75 | /// <param name="after"> A cursor for use in pagination. `after` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list. </param> | |
| 76 | /// <param name="limit"> A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20. </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="ClientResultException"> Service returned a non-success status code. </exception> | |
| 79 | /// <returns> The response returned from the service. </returns> | |
2ab1a942Jose Arriaga Maldonado1 years ago | 80 | public virtual AsyncCollectionResult GetBatchesAsync(string after, int? limit, RequestOptions options) |
9f9f2936Jose Arriaga Maldonado2 years ago | 81 | { |
e0fee603Jose Arriaga Maldonado1 years ago | 82 | return new AsyncBatchCollectionResult(this, Pipeline, options, limit, after); |
9f9f2936Jose Arriaga Maldonado2 years ago | 83 | } |
| 84 | | |
| 85 | /// <summary> | |
| 86 | /// [Protocol Method] List your organization's batches. | |
| 87 | /// </summary> | |
| 88 | /// <param name="after"> A cursor for use in pagination. `after` is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page of the list. </param> | |
| 89 | /// <param name="limit"> A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20. </param> | |
| 90 | /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param> | |
| 91 | /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception> | |
| 92 | /// <returns> The response returned from the service. </returns> | |
2ab1a942Jose Arriaga Maldonado1 years ago | 93 | public virtual CollectionResult GetBatches(string after, int? limit, RequestOptions options) |
9f9f2936Jose Arriaga Maldonado2 years ago | 94 | { |
e0fee603Jose Arriaga Maldonado1 years ago | 95 | return new BatchCollectionResult(this, Pipeline, options, limit, after); |
9f9f2936Jose Arriaga Maldonado2 years ago | 96 | } |
| 97 | | |
| 98 | /// <summary> | |
| 99 | /// [Protocol Method] Retrieves a batch. | |
| 100 | /// </summary> | |
| 101 | /// <param name="batchId"> The ID of the batch to retrieve. </param> | |
| 102 | /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param> | |
| 103 | /// <exception cref="ArgumentNullException"> <paramref name="batchId"/> is null. </exception> | |
| 104 | /// <exception cref="ArgumentException"> <paramref name="batchId"/> is an empty string, and was expected to be non-empty. </exception> | |
| 105 | /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception> | |
| 106 | /// <returns> The response returned from the service. </returns> | |
a330c2e7Jose Arriaga Maldonado1 years ago | 107 | internal virtual async Task<ClientResult> GetBatchAsync(string batchId, RequestOptions options) |
9f9f2936Jose Arriaga Maldonado2 years ago | 108 | { |
| 109 | Argument.AssertNotNullOrEmpty(batchId, nameof(batchId)); | |
| 110 | | |
| 111 | using PipelineMessage message = CreateRetrieveBatchRequest(batchId, options); | |
e0fee603Jose Arriaga Maldonado1 years ago | 112 | return ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false)); |
9f9f2936Jose Arriaga Maldonado2 years ago | 113 | } |
| 114 | | |
| 115 | /// <summary> | |
| 116 | /// [Protocol Method] Retrieves a batch. | |
| 117 | /// </summary> | |
| 118 | /// <param name="batchId"> The ID of the batch to retrieve. </param> | |
| 119 | /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param> | |
| 120 | /// <exception cref="ArgumentNullException"> <paramref name="batchId"/> is null. </exception> | |
| 121 | /// <exception cref="ArgumentException"> <paramref name="batchId"/> is an empty string, and was expected to be non-empty. </exception> | |
| 122 | /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception> | |
| 123 | /// <returns> The response returned from the service. </returns> | |
a330c2e7Jose Arriaga Maldonado1 years ago | 124 | internal virtual ClientResult GetBatch(string batchId, RequestOptions options) |
9f9f2936Jose Arriaga Maldonado2 years ago | 125 | { |
| 126 | Argument.AssertNotNullOrEmpty(batchId, nameof(batchId)); | |
| 127 | | |
| 128 | using PipelineMessage message = CreateRetrieveBatchRequest(batchId, options); | |
e0fee603Jose Arriaga Maldonado1 years ago | 129 | return ClientResult.FromResponse(Pipeline.ProcessMessage(message, options)); |
9f9f2936Jose Arriaga Maldonado2 years ago | 130 | } |
| 131 | | |
a330c2e7Jose Arriaga Maldonado1 years ago | 132 | internal virtual PipelineMessage CreateCreateBatchRequest(BinaryContent content, RequestOptions options) |
9f9f2936Jose Arriaga Maldonado2 years ago | 133 | { |
e0fee603Jose Arriaga Maldonado1 years ago | 134 | var message = Pipeline.CreateMessage(); |
a330c2e7Jose Arriaga Maldonado1 years ago | 135 | message.ResponseClassifier = PipelineMessageClassifier200; |
| 136 | var request = message.Request; | |
| 137 | request.Method = "POST"; | |
| 138 | var uri = new ClientUriBuilder(); | |
| 139 | uri.Reset(_endpoint); | |
| 140 | uri.AppendPath("/batches", false); | |
| 141 | request.Uri = uri.ToUri(); | |
| 142 | request.Headers.Set("Accept", "application/json"); | |
| 143 | request.Headers.Set("Content-Type", "application/json"); | |
| 144 | request.Content = content; | |
| 145 | message.Apply(options); | |
| 146 | return message; | |
| 147 | } | |
9f9f2936Jose Arriaga Maldonado2 years ago | 148 | |
a330c2e7Jose Arriaga Maldonado1 years ago | 149 | internal virtual PipelineMessage CreateGetBatchesRequest(string after, int? limit, RequestOptions options) |
| 150 | { | |
e0fee603Jose Arriaga Maldonado1 years ago | 151 | var message = Pipeline.CreateMessage(); |
a330c2e7Jose Arriaga Maldonado1 years ago | 152 | message.ResponseClassifier = PipelineMessageClassifier200; |
| 153 | var request = message.Request; | |
| 154 | request.Method = "GET"; | |
| 155 | var uri = new ClientUriBuilder(); | |
| 156 | uri.Reset(_endpoint); | |
| 157 | uri.AppendPath("/batches", false); | |
| 158 | if (after != null) | |
| 159 | { | |
| 160 | uri.AppendQuery("after", after, true); | |
| 161 | } | |
| 162 | if (limit != null) | |
| 163 | { | |
| 164 | uri.AppendQuery("limit", limit.Value, true); | |
| 165 | } | |
| 166 | request.Uri = uri.ToUri(); | |
| 167 | request.Headers.Set("Accept", "application/json"); | |
| 168 | message.Apply(options); | |
| 169 | return message; | |
9f9f2936Jose Arriaga Maldonado2 years ago | 170 | } |
| 171 | | |
a330c2e7Jose Arriaga Maldonado1 years ago | 172 | internal virtual PipelineMessage CreateRetrieveBatchRequest(string batchId, RequestOptions options) |
9f9f2936Jose Arriaga Maldonado2 years ago | 173 | { |
e0fee603Jose Arriaga Maldonado1 years ago | 174 | var message = Pipeline.CreateMessage(); |
a330c2e7Jose Arriaga Maldonado1 years ago | 175 | message.ResponseClassifier = PipelineMessageClassifier200; |
| 176 | var request = message.Request; | |
| 177 | request.Method = "GET"; | |
| 178 | var uri = new ClientUriBuilder(); | |
| 179 | uri.Reset(_endpoint); | |
| 180 | uri.AppendPath("/batches/", false); | |
| 181 | uri.AppendPath(batchId, true); | |
| 182 | request.Uri = uri.ToUri(); | |
| 183 | request.Headers.Set("Accept", "application/json"); | |
| 184 | message.Apply(options); | |
| 185 | return message; | |
| 186 | } | |
9f9f2936Jose Arriaga Maldonado2 years ago | 187 | |
a330c2e7Jose Arriaga Maldonado1 years ago | 188 | internal virtual PipelineMessage CreateCancelBatchRequest(string batchId, RequestOptions options) |
| 189 | { | |
e0fee603Jose Arriaga Maldonado1 years ago | 190 | var message = Pipeline.CreateMessage(); |
a330c2e7Jose Arriaga Maldonado1 years ago | 191 | message.ResponseClassifier = PipelineMessageClassifier200; |
| 192 | var request = message.Request; | |
| 193 | request.Method = "POST"; | |
| 194 | var uri = new ClientUriBuilder(); | |
| 195 | uri.Reset(_endpoint); | |
| 196 | uri.AppendPath("/batches/", false); | |
| 197 | uri.AppendPath(batchId, true); | |
| 198 | uri.AppendPath("/cancel", false); | |
| 199 | request.Uri = uri.ToUri(); | |
| 200 | request.Headers.Set("Accept", "application/json"); | |
| 201 | message.Apply(options); | |
| 202 | return message; | |
9f9f2936Jose Arriaga Maldonado2 years ago | 203 | } |
| 204 | } |