openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
achandmsft-patch-1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Batch/BatchClient.Protocol.cs

204lines · modeblame

9f9f2936Jose Arriaga Maldonado2 years ago1using System;
2using System.ClientModel;
3using System.ClientModel.Primitives;
a330c2e7Jose Arriaga Maldonado1 years ago4using System.Text.Json;
9f9f2936Jose Arriaga Maldonado2 years ago5using System.Threading.Tasks;
6
7namespace OpenAI.Batch;
8
13a9c686Jose Arriaga Maldonado1 years ago9[CodeGenSuppress("RetrieveBatch", typeof(string), typeof(RequestOptions))]
10[CodeGenSuppress("RetrieveBatchAsync", typeof(string), typeof(RequestOptions))]
e0fee603Jose Arriaga Maldonado1 years ago11[CodeGenSuppress("ListBatches", typeof(string), typeof(int?), typeof(RequestOptions))]
12[CodeGenSuppress("ListBatchesAsync", typeof(string), typeof(int?), typeof(RequestOptions))]
9f9f2936Jose Arriaga Maldonado2 years ago13public 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 ago18/// <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 ago22/// <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 ago26/// <returns> A <see cref="CreateBatchOperation"/> that can be used to wait for
27/// the operation to complete, or cancel the operation. </returns>
28public virtual async Task<CreateBatchOperation> CreateBatchAsync(BinaryContent content, bool waitUntilCompleted, RequestOptions options = null)
9f9f2936Jose Arriaga Maldonado2 years ago29{
30Argument.AssertNotNull(content, nameof(content));
31
32using PipelineMessage message = CreateCreateBatchRequest(content, options);
a330c2e7Jose Arriaga Maldonado1 years ago33
e0fee603Jose Arriaga Maldonado1 years ago34PipelineResponse response = await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false);
a330c2e7Jose Arriaga Maldonado1 years ago35
36using JsonDocument doc = JsonDocument.Parse(response.Content);
37string batchId = doc.RootElement.GetProperty("id"u8).GetString();
38string status = doc.RootElement.GetProperty("status"u8).GetString();
39
40CreateBatchOperation operation = this.CreateCreateBatchOperation(batchId, status, response);
41return await operation.WaitUntilAsync(waitUntilCompleted, options).ConfigureAwait(false);
9f9f2936Jose Arriaga Maldonado2 years ago42}
43
44/// <summary>
45/// [Protocol Method] Creates and executes a batch from an uploaded file of requests
46/// </summary>
a330c2e7Jose Arriaga Maldonado1 years ago47/// <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 ago51/// <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 ago55/// <returns> A <see cref="CreateBatchOperation"/> that can be used to wait for
56/// the operation to complete, or cancel the operation. </returns>
57public virtual CreateBatchOperation CreateBatch(BinaryContent content, bool waitUntilCompleted, RequestOptions options = null)
9f9f2936Jose Arriaga Maldonado2 years ago58{
59Argument.AssertNotNull(content, nameof(content));
60
61using PipelineMessage message = CreateCreateBatchRequest(content, options);
e0fee603Jose Arriaga Maldonado1 years ago62PipelineResponse response = Pipeline.ProcessMessage(message, options);
a330c2e7Jose Arriaga Maldonado1 years ago63
64using JsonDocument doc = JsonDocument.Parse(response.Content);
65string batchId = doc.RootElement.GetProperty("id"u8).GetString();
66string status = doc.RootElement.GetProperty("status"u8).GetString();
67
68CreateBatchOperation operation = this.CreateCreateBatchOperation(batchId, status, response);
69return operation.WaitUntil(waitUntilCompleted, options);
9f9f2936Jose Arriaga Maldonado2 years ago70}
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 ago80public virtual AsyncCollectionResult GetBatchesAsync(string after, int? limit, RequestOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago81{
e0fee603Jose Arriaga Maldonado1 years ago82return new AsyncBatchCollectionResult(this, Pipeline, options, limit, after);
9f9f2936Jose Arriaga Maldonado2 years ago83}
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 ago93public virtual CollectionResult GetBatches(string after, int? limit, RequestOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago94{
e0fee603Jose Arriaga Maldonado1 years ago95return new BatchCollectionResult(this, Pipeline, options, limit, after);
9f9f2936Jose Arriaga Maldonado2 years ago96}
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 ago107internal virtual async Task<ClientResult> GetBatchAsync(string batchId, RequestOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago108{
109Argument.AssertNotNullOrEmpty(batchId, nameof(batchId));
110
111using PipelineMessage message = CreateRetrieveBatchRequest(batchId, options);
e0fee603Jose Arriaga Maldonado1 years ago112return ClientResult.FromResponse(await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
9f9f2936Jose Arriaga Maldonado2 years ago113}
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 ago124internal virtual ClientResult GetBatch(string batchId, RequestOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago125{
126Argument.AssertNotNullOrEmpty(batchId, nameof(batchId));
127
128using PipelineMessage message = CreateRetrieveBatchRequest(batchId, options);
e0fee603Jose Arriaga Maldonado1 years ago129return ClientResult.FromResponse(Pipeline.ProcessMessage(message, options));
9f9f2936Jose Arriaga Maldonado2 years ago130}
131
a330c2e7Jose Arriaga Maldonado1 years ago132internal virtual PipelineMessage CreateCreateBatchRequest(BinaryContent content, RequestOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago133{
e0fee603Jose Arriaga Maldonado1 years ago134var message = Pipeline.CreateMessage();
a330c2e7Jose Arriaga Maldonado1 years ago135message.ResponseClassifier = PipelineMessageClassifier200;
136var request = message.Request;
137request.Method = "POST";
138var uri = new ClientUriBuilder();
139uri.Reset(_endpoint);
140uri.AppendPath("/batches", false);
141request.Uri = uri.ToUri();
142request.Headers.Set("Accept", "application/json");
143request.Headers.Set("Content-Type", "application/json");
144request.Content = content;
145message.Apply(options);
146return message;
147}
9f9f2936Jose Arriaga Maldonado2 years ago148
a330c2e7Jose Arriaga Maldonado1 years ago149internal virtual PipelineMessage CreateGetBatchesRequest(string after, int? limit, RequestOptions options)
150{
e0fee603Jose Arriaga Maldonado1 years ago151var message = Pipeline.CreateMessage();
a330c2e7Jose Arriaga Maldonado1 years ago152message.ResponseClassifier = PipelineMessageClassifier200;
153var request = message.Request;
154request.Method = "GET";
155var uri = new ClientUriBuilder();
156uri.Reset(_endpoint);
157uri.AppendPath("/batches", false);
158if (after != null)
159{
160uri.AppendQuery("after", after, true);
161}
162if (limit != null)
163{
164uri.AppendQuery("limit", limit.Value, true);
165}
166request.Uri = uri.ToUri();
167request.Headers.Set("Accept", "application/json");
168message.Apply(options);
169return message;
9f9f2936Jose Arriaga Maldonado2 years ago170}
171
a330c2e7Jose Arriaga Maldonado1 years ago172internal virtual PipelineMessage CreateRetrieveBatchRequest(string batchId, RequestOptions options)
9f9f2936Jose Arriaga Maldonado2 years ago173{
e0fee603Jose Arriaga Maldonado1 years ago174var message = Pipeline.CreateMessage();
a330c2e7Jose Arriaga Maldonado1 years ago175message.ResponseClassifier = PipelineMessageClassifier200;
176var request = message.Request;
177request.Method = "GET";
178var uri = new ClientUriBuilder();
179uri.Reset(_endpoint);
180uri.AppendPath("/batches/", false);
181uri.AppendPath(batchId, true);
182request.Uri = uri.ToUri();
183request.Headers.Set("Accept", "application/json");
184message.Apply(options);
185return message;
186}
9f9f2936Jose Arriaga Maldonado2 years ago187
a330c2e7Jose Arriaga Maldonado1 years ago188internal virtual PipelineMessage CreateCancelBatchRequest(string batchId, RequestOptions options)
189{
e0fee603Jose Arriaga Maldonado1 years ago190var message = Pipeline.CreateMessage();
a330c2e7Jose Arriaga Maldonado1 years ago191message.ResponseClassifier = PipelineMessageClassifier200;
192var request = message.Request;
193request.Method = "POST";
194var uri = new ClientUriBuilder();
195uri.Reset(_endpoint);
196uri.AppendPath("/batches/", false);
197uri.AppendPath(batchId, true);
198uri.AppendPath("/cancel", false);
199request.Uri = uri.ToUri();
200request.Headers.Set("Accept", "application/json");
201message.Apply(options);
202return message;
9f9f2936Jose Arriaga Maldonado2 years ago203}
204}