openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fe4d6cf1ccfea48fa5c2baf67103495ac9b459dd

Branches

Tags

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

Clone

HTTPS

Download ZIP

OpenAI/src/Custom/Batch/BatchClient.Protocol.cs

67lines · modecode

1using System;
2using System.ClientModel;
3using System.ClientModel.Primitives;
4using System.Text.Json;
5using System.Threading.Tasks;
6
7namespace OpenAI.Batch;
8
9public partial class BatchClient
10{
11 /// <summary>
12 /// [Protocol Method] Creates and executes a batch from an uploaded file of requests
13 /// </summary>
14 /// <param name="waitUntilCompleted"> Value indicating whether the method
15 /// should return after the operation has been started and is still running
16 /// on the service, or wait until the operation has completed to return.
17 /// </param>
18 /// <param name="content"> The content to send as the body of the request. </param>
19 /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
20 /// <exception cref="ArgumentNullException"> <paramref name="content"/> is null. </exception>
21 /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
22 /// <returns> A <see cref="CreateBatchOperation"/> that can be used to wait for
23 /// the operation to complete, or cancel the operation. </returns>
24 public virtual async Task<CreateBatchOperation> CreateBatchAsync(BinaryContent content, bool waitUntilCompleted, RequestOptions options = null)
25 {
26 Argument.AssertNotNull(content, nameof(content));
27
28 using PipelineMessage message = CreateCreateBatchRequest(content, options);
29
30 PipelineResponse response = await Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false);
31
32 using JsonDocument doc = JsonDocument.Parse(response.Content);
33 string batchId = doc.RootElement.GetProperty("id"u8).GetString();
34 string status = doc.RootElement.GetProperty("status"u8).GetString();
35
36 CreateBatchOperation operation = this.CreateCreateBatchOperation(batchId, status, response);
37 return await operation.WaitUntilAsync(waitUntilCompleted, options).ConfigureAwait(false);
38 }
39
40 /// <summary>
41 /// [Protocol Method] Creates and executes a batch from an uploaded file of requests
42 /// </summary>
43 /// <param name="waitUntilCompleted"> Value indicating whether the method
44 /// should return after the operation has been started and is still running
45 /// on the service, or wait until the operation has completed to return.
46 /// </param>
47 /// <param name="content"> The content to send as the body of the request. </param>
48 /// <param name="options"> The request options, which can override default behaviors of the client pipeline on a per-call basis. </param>
49 /// <exception cref="ArgumentNullException"> <paramref name="content"/> is null. </exception>
50 /// <exception cref="ClientResultException"> Service returned a non-success status code. </exception>
51 /// <returns> A <see cref="CreateBatchOperation"/> that can be used to wait for
52 /// the operation to complete, or cancel the operation. </returns>
53 public virtual CreateBatchOperation CreateBatch(BinaryContent content, bool waitUntilCompleted, RequestOptions options = null)
54 {
55 Argument.AssertNotNull(content, nameof(content));
56
57 using PipelineMessage message = CreateCreateBatchRequest(content, options);
58 PipelineResponse response = Pipeline.ProcessMessage(message, options);
59
60 using JsonDocument doc = JsonDocument.Parse(response.Content);
61 string batchId = doc.RootElement.GetProperty("id"u8).GetString();
62 string status = doc.RootElement.GetProperty("status"u8).GetString();
63
64 CreateBatchOperation operation = this.CreateCreateBatchOperation(batchId, status, response);
65 return operation.WaitUntil(waitUntilCompleted, options);
66 }
67}
68