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.cs

113lines · modecode

1using Microsoft.TypeSpec.Generator.Customizations;
2using System;
3using System.ClientModel;
4using System.ClientModel.Primitives;
5using System.Collections.Generic;
6using System.Diagnostics.CodeAnalysis;
7using System.Threading;
8
9namespace OpenAI.Batch;
10
11// CUSTOM:
12// - Renamed.
13// - Suppressed constructor that takes endpoint parameter; endpoint is now a property in the options class.
14// - Suppressed convenience methods for now.
15/// <summary> The service client for OpenAI batch operations. </summary>
16[CodeGenType("Batches")]
17[CodeGenSuppress("BatchClient", typeof(ClientPipeline), typeof(Uri))]
18[CodeGenSuppress("CreateBatch", typeof(string), typeof(InternalCreateBatchRequestEndpoint), typeof(IDictionary<string, string>), typeof(CancellationToken))]
19[CodeGenSuppress("CreateBatchAsync", typeof(string), typeof(InternalCreateBatchRequestEndpoint), typeof(IDictionary<string, string>), typeof(CancellationToken))]
20[CodeGenSuppress("CreateBatch", typeof(BinaryContent), typeof(RequestOptions))]
21[CodeGenSuppress("CreateBatchAsync", typeof(BinaryContent), typeof(RequestOptions))]
22[CodeGenSuppress("GetBatch", typeof(string), typeof(CancellationToken))]
23[CodeGenSuppress("GetBatchAsync", typeof(string), typeof(CancellationToken))]
24[CodeGenSuppress("CancelBatch", typeof(string), typeof(CancellationToken))]
25[CodeGenSuppress("CancelBatchAsync", typeof(string), typeof(CancellationToken))]
26[CodeGenSuppress("CancelBatch", typeof(string), typeof(RequestOptions))]
27[CodeGenSuppress("CancelBatchAsync", typeof(string), typeof(RequestOptions))]
28public partial class BatchClient
29{
30 // CUSTOM: Added as a convenience.
31 /// <summary> Initializes a new instance of <see cref="BatchClient"/>. </summary>
32 /// <param name="apiKey"> The API key to authenticate with the service. </param>
33 /// <exception cref="ArgumentNullException"> <paramref name="apiKey"/> is null. </exception>
34 public BatchClient(string apiKey) : this(new ApiKeyCredential(apiKey), new OpenAIClientOptions())
35 {
36 }
37
38 // CUSTOM:
39 // - Used a custom pipeline.
40 // - Demoted the endpoint parameter to be a property in the options class.
41 /// <summary> Initializes a new instance of <see cref="BatchClient"/>. </summary>
42 /// <param name="credential"> The <see cref="ApiKeyCredential"/> to authenticate with the service. </param>
43 /// <exception cref="ArgumentNullException"> <paramref name="credential"/> is null. </exception>
44 public BatchClient(ApiKeyCredential credential) : this(credential, new OpenAIClientOptions())
45 {
46 }
47
48 /// <summary>
49 /// Initializes a new instance of <see cref="BatchClient"/> that will use an API key when authenticating.
50 /// </summary>
51 /// <param name="credential"> The <see cref="ApiKeyCredential"/> to authenticate with the service. </param>
52 /// <param name="options"> Additional options to customize the client. </param>
53 /// <exception cref="ArgumentNullException"> The provided <paramref name="credential"/> was null. </exception>
54 public BatchClient(ApiKeyCredential credential, OpenAIClientOptions options) : this(OpenAIClient.CreateApiKeyAuthenticationPolicy(credential), options)
55 {
56 }
57
58 // CUSTOM: Added as a convenience.
59 /// <summary> Initializes a new instance of <see cref="BatchClient"/>. </summary>
60 /// <param name="authenticationPolicy"> The authentication policy used to authenticate with the service. </param>
61 /// <exception cref="ArgumentNullException"> <paramref name="authenticationPolicy"/> is null. </exception>
62 public BatchClient(AuthenticationPolicy authenticationPolicy) : this(authenticationPolicy, new OpenAIClientOptions())
63 {
64 }
65
66 // CUSTOM: Added as a convenience.
67 /// <summary> Initializes a new instance of <see cref="BatchClient"/>. </summary>
68 /// <param name="authenticationPolicy"> The authentication policy used to authenticate with the service. </param>
69 /// <param name="options"> The options to configure the client. </param>
70 /// <exception cref="ArgumentNullException"> <paramref name="authenticationPolicy"/> is null. </exception>
71 public BatchClient(AuthenticationPolicy authenticationPolicy, OpenAIClientOptions options)
72 {
73 Argument.AssertNotNull(authenticationPolicy, nameof(authenticationPolicy));
74 options ??= new OpenAIClientOptions();
75
76 Pipeline = OpenAIClient.CreatePipeline(authenticationPolicy, options);
77 _endpoint = OpenAIClient.GetEndpoint(options);
78 }
79
80 // CUSTOM:
81 // - Used a custom pipeline.
82 // - Demoted the endpoint parameter to be a property in the options class.
83 // - Made protected.
84 /// <summary> Initializes a new instance of <see cref="BatchClient"/>. </summary>
85 /// <param name="pipeline"> The HTTP pipeline to send and receive REST requests and responses. </param>
86 /// <param name="options"> The options to configure the client. </param>
87 /// <exception cref="ArgumentNullException"> <paramref name="pipeline"/> is null. </exception>
88 protected internal BatchClient(ClientPipeline pipeline, OpenAIClientOptions options)
89 {
90 Argument.AssertNotNull(pipeline, nameof(pipeline));
91 options ??= new OpenAIClientOptions();
92
93 Pipeline = pipeline;
94 _endpoint = OpenAIClient.GetEndpoint(options);
95 }
96
97 [Experimental("SCME0002")]
98 public BatchClient(BatchClientSettings settings)
99 : this(AuthenticationPolicy.Create(settings), settings?.Options)
100 {
101 }
102
103 /// <summary>
104 /// Gets the endpoint URI for the service.
105 /// </summary>
106 [Experimental("OPENAI001")]
107 public Uri Endpoint => _endpoint;
108
109 internal virtual CreateBatchOperation CreateCreateBatchOperation(string batchId, string status, PipelineResponse response)
110 {
111 return new CreateBatchOperation(this, _endpoint, batchId, status, response);
112 }
113}
114