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