openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.2.0-beta.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/Custom/Batch/BatchClient.Protocol.cs

204lines · modecode

1using System;
2using System.ClientModel;
3using System.ClientModel.Primitives;
4using System.Text.Json;
5using System.Threading.Tasks;
6
7namespace OpenAI.Batch;
8
9[CodeGenSuppress("RetrieveBatch", typeof(string), typeof(RequestOptions))]
10[CodeGenSuppress("RetrieveBatchAsync", typeof(string), typeof(RequestOptions))]
11[CodeGenSuppress("ListBatches", typeof(string), typeof(int?), typeof(RequestOptions))]
12[CodeGenSuppress("ListBatchesAsync", typeof(string), typeof(int?), typeof(RequestOptions))]
13public partial class BatchClient
14{
15 /// <summary>
16 /// [Protocol Method] Creates and executes a batch from an uploaded file of requests
17 /// </summary>
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>
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>
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)
29 {
30 Argument.AssertNotNull(content, nameof(content));
31
32 using PipelineMessage message = CreateCreateBatchRequest(content, options);
33
34 PipelineResponse response = await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false);
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);
42 }
43
44 /// <summary>
45 /// [Protocol Method] Creates and executes a batch from an uploaded file of requests
46 /// </summary>
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>
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>
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)
58 {
59 Argument.AssertNotNull(content, nameof(content));
60
61 using PipelineMessage message = CreateCreateBatchRequest(content, options);
62 PipelineResponse response = Pipeline.ProcessMessage(message, options);
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);
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>
80 public virtual AsyncCollectionResult GetBatchesAsync(string after, int? limit, RequestOptions options)
81 {
82 return new AsyncBatchCollectionResult(this, Pipeline, options, limit, after);
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>
93 public virtual CollectionResult GetBatches(string after, int? limit, RequestOptions options)
94 {
95 return new BatchCollectionResult(this, Pipeline, options, limit, after);
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>
107 internal virtual async Task<ClientResult> GetBatchAsync(string batchId, RequestOptions options)
108 {
109 Argument.AssertNotNullOrEmpty(batchId, nameof(batchId));
110
111 using PipelineMessage message = CreateRetrieveBatchRequest(batchId, options);
112 return ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
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>
124 internal virtual ClientResult GetBatch(string batchId, RequestOptions options)
125 {
126 Argument.AssertNotNullOrEmpty(batchId, nameof(batchId));
127
128 using PipelineMessage message = CreateRetrieveBatchRequest(batchId, options);
129 return ClientResult.FromResponse(Pipeline.ProcessMessage(message, options));
130 }
131
132 internal virtual PipelineMessage CreateCreateBatchRequest(BinaryContent content, RequestOptions options)
133 {
134 var message = Pipeline.CreateMessage();
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 }
148
149 internal virtual PipelineMessage CreateGetBatchesRequest(string after, int? limit, RequestOptions options)
150 {
151 var message = Pipeline.CreateMessage();
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;
170 }
171
172 internal virtual PipelineMessage CreateRetrieveBatchRequest(string batchId, RequestOptions options)
173 {
174 var message = Pipeline.CreateMessage();
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 }
187
188 internal virtual PipelineMessage CreateCancelBatchRequest(string batchId, RequestOptions options)
189 {
190 var message = Pipeline.CreateMessage();
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;
203 }
204}
205