openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.1.0-beta.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/Custom/Batch/BatchClient.cs

91lines · modecode

1using System;
2using System.ClientModel;
3using System.ClientModel.Primitives;
4using System.Collections.Generic;
5using System.Diagnostics.CodeAnalysis;
6
7namespace OpenAI.Batch;
8
9// CUSTOM:
10// - Renamed.
11// - Suppressed constructor that takes endpoint parameter; endpoint is now a property in the options class.
12// - Suppressed convenience methods for now.
13/// <summary> The service client for OpenAI batch operations. </summary>
14[Experimental("OPENAI001")]
15[CodeGenClient("Batches")]
16[CodeGenSuppress("BatchClient", typeof(ClientPipeline), typeof(ApiKeyCredential), typeof(Uri))]
17[CodeGenSuppress("CreateBatch", typeof(string), typeof(InternalCreateBatchRequestEndpoint), typeof(InternalBatchCompletionTimeframe), typeof(IReadOnlyDictionary<string, string>))]
18[CodeGenSuppress("CreateBatchAsync", typeof(string), typeof(InternalCreateBatchRequestEndpoint), typeof(InternalBatchCompletionTimeframe), typeof(IReadOnlyDictionary<string, string>))]
19[CodeGenSuppress("CreateBatch", typeof(BinaryContent), typeof(RequestOptions))]
20[CodeGenSuppress("CreateBatchAsync", typeof(BinaryContent), typeof(RequestOptions))]
21[CodeGenSuppress("RetrieveBatch", typeof(string))]
22[CodeGenSuppress("RetrieveBatchAsync", typeof(string))]
23[CodeGenSuppress("CancelBatch", typeof(string))]
24[CodeGenSuppress("CancelBatchAsync", typeof(string))]
25[CodeGenSuppress("CancelBatch", typeof(string), typeof(RequestOptions))]
26[CodeGenSuppress("CancelBatchAsync", typeof(string), typeof(RequestOptions))]
27[CodeGenSuppress("GetBatches", typeof(string), typeof(int?))]
28[CodeGenSuppress("GetBatchesAsync", typeof(string), typeof(int?))]
29public partial class BatchClient
30{
31 // CUSTOM: Remove virtual keyword.
32 /// <summary>
33 /// The HTTP pipeline for sending and receiving REST requests and responses.
34 /// </summary>
35 public ClientPipeline Pipeline => _pipeline;
36
37 // CUSTOM: Added as a convenience.
38 /// <summary> Initializes a new instance of <see cref="BatchClient">. </summary>
39 /// <param name="apiKey"> The API key to authenticate with the service. </param>
40 /// <exception cref="ArgumentNullException"> <paramref name="apiKey"/> is null. </exception>
41 public BatchClient(string apiKey) : this(new ApiKeyCredential(apiKey), new OpenAIClientOptions())
42 {
43 }
44
45 // CUSTOM:
46 // - Used a custom pipeline.
47 // - Demoted the endpoint parameter to be a property in the options class.
48 /// <summary> Initializes a new instance of <see cref="BatchClient">. </summary>
49 /// <param name="credential"> The API key to authenticate with the service. </param>
50 /// <exception cref="ArgumentNullException"> <paramref name="credential"/> is null. </exception>
51 public BatchClient(ApiKeyCredential credential) : this(credential, new OpenAIClientOptions())
52 {
53 }
54
55 /// <summary>
56 /// Initializes a new instance of <see cref="BatchClient"/> that will use an API key when authenticating.
57 /// </summary>
58 /// <param name="credential"> The API key used to authenticate with the service endpoint. </param>
59 /// <param name="options"> Additional options to customize the client. </param>
60 /// <exception cref="ArgumentNullException"> The provided <paramref name="credential"/> was null. </exception>
61 public BatchClient(ApiKeyCredential credential, OpenAIClientOptions options)
62 {
63 Argument.AssertNotNull(credential, nameof(credential));
64 options ??= new OpenAIClientOptions();
65
66 _pipeline = OpenAIClient.CreatePipeline(credential, options);
67 _endpoint = OpenAIClient.GetEndpoint(options);
68 }
69
70 // CUSTOM:
71 // - Used a custom pipeline.
72 // - Demoted the endpoint parameter to be a property in the options class.
73 // - Made protected.
74 /// <summary> Initializes a new instance of <see cref="BatchClient">. </summary>
75 /// <param name="pipeline"> The HTTP pipeline to send and receive REST requests and responses. </param>
76 /// <param name="options"> The options to configure the client. </param>
77 /// <exception cref="ArgumentNullException"> <paramref name="pipeline"/> is null. </exception>
78 protected internal BatchClient(ClientPipeline pipeline, OpenAIClientOptions options)
79 {
80 Argument.AssertNotNull(pipeline, nameof(pipeline));
81 options ??= new OpenAIClientOptions();
82
83 _pipeline = pipeline;
84 _endpoint = OpenAIClient.GetEndpoint(options);
85 }
86
87 internal virtual CreateBatchOperation CreateCreateBatchOperation(string batchId, string status, PipelineResponse response)
88 {
89 return new CreateBatchOperation(_pipeline, _endpoint, batchId, status, response);
90 }
91}
92