openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Batch/BatchClient.Protocol.cs
95lines · 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("GetBatches", typeof(string), typeof(int?), typeof(RequestOptions))] |
| 10 | [CodeGenSuppress("GetBatchesAsync", typeof(string), typeof(int?), 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 | |