3
3
// Code generated by Microsoft (R) AutoRest Code Generator.
4
4
package com .azure .ai .openai ;
5
5
6
+ import static com .azure .ai .openai .implementation .NonAzureOpenAIClientImpl .OPEN_AI_ENDPOINT ;
7
+
6
8
import com .azure .ai .openai .implementation .NonAzureOpenAIClientImpl ;
7
- import com .azure .ai .openai .implementation .NonAzureOpenAIKeyCredentialPolicy ;
8
9
import com .azure .ai .openai .implementation .OpenAIClientImpl ;
9
- import com .azure .ai .openai .models .NonAzureOpenAIKeyCredential ;
10
10
import com .azure .core .annotation .Generated ;
11
11
import com .azure .core .annotation .ServiceClientBuilder ;
12
12
import com .azure .core .client .traits .AzureKeyCredentialTrait ;
15
15
import com .azure .core .client .traits .HttpTrait ;
16
16
import com .azure .core .client .traits .TokenCredentialTrait ;
17
17
import com .azure .core .credential .AzureKeyCredential ;
18
+ import com .azure .core .credential .KeyCredential ;
18
19
import com .azure .core .credential .TokenCredential ;
19
20
import com .azure .core .http .HttpClient ;
20
21
import com .azure .core .http .HttpHeaders ;
24
25
import com .azure .core .http .policy .AddDatePolicy ;
25
26
import com .azure .core .http .policy .AddHeadersFromContextPolicy ;
26
27
import com .azure .core .http .policy .AddHeadersPolicy ;
27
- import com .azure .core .http .policy .AzureKeyCredentialPolicy ;
28
28
import com .azure .core .http .policy .BearerTokenAuthenticationPolicy ;
29
29
import com .azure .core .http .policy .CookiePolicy ;
30
30
import com .azure .core .http .policy .HttpLogOptions ;
31
31
import com .azure .core .http .policy .HttpLoggingPolicy ;
32
32
import com .azure .core .http .policy .HttpPipelinePolicy ;
33
33
import com .azure .core .http .policy .HttpPolicyProviders ;
34
+ import com .azure .core .http .policy .KeyCredentialPolicy ;
34
35
import com .azure .core .http .policy .RequestIdPolicy ;
35
36
import com .azure .core .http .policy .RetryOptions ;
36
37
import com .azure .core .http .policy .RetryPolicy ;
@@ -182,23 +183,22 @@ public OpenAIClientBuilder credential(TokenCredential tokenCredential) {
182
183
@ Generated private AzureKeyCredential azureKeyCredential ;
183
184
184
185
/** {@inheritDoc}. */
185
- @ Generated
186
186
@ Override
187
187
public OpenAIClientBuilder credential (AzureKeyCredential azureKeyCredential ) {
188
- this .azureKeyCredential = azureKeyCredential ;
189
- return this ;
188
+ return this .credential ((KeyCredential ) azureKeyCredential );
190
189
}
191
190
192
- private NonAzureOpenAIKeyCredential nonAzureOpenAIKeyCredential ;
191
+ /** The KeyCredential used for OpenAi authentication. It could be either of Azure or Non-Azure OpenAI API key. */
192
+ private KeyCredential keyCredential ;
193
193
194
194
/**
195
- * The NonAzureOpenAiKeyCredential used for public OpenAi authentication.
195
+ * The KeyCredential used for OpenAi authentication. It could be either of Azure or Non-Azure OpenAI API key .
196
196
*
197
- * @param nonAzureOpenAIKeyCredential The credential for non-azure public OpenAI authenticaton .
197
+ * @param keyCredential The credential for OpenAI authentication .
198
198
* @return the object itself.
199
199
*/
200
- public OpenAIClientBuilder credential (NonAzureOpenAIKeyCredential nonAzureOpenAIKeyCredential ) {
201
- this .nonAzureOpenAIKeyCredential = nonAzureOpenAIKeyCredential ;
200
+ public OpenAIClientBuilder credential (KeyCredential keyCredential ) {
201
+ this .keyCredential = keyCredential ;
202
202
return this ;
203
203
}
204
204
@@ -268,7 +268,6 @@ private OpenAIClientImpl buildInnerClient() {
268
268
return client ;
269
269
}
270
270
271
- @ Generated
272
271
private HttpPipeline createHttpPipeline () {
273
272
Configuration buildConfiguration =
274
273
(configuration == null ) ? Configuration .getGlobalConfiguration () : configuration ;
@@ -292,8 +291,12 @@ private HttpPipeline createHttpPipeline() {
292
291
HttpPolicyProviders .addBeforeRetryPolicies (policies );
293
292
policies .add (ClientBuilderUtil .validateAndGetRetryPolicy (retryPolicy , retryOptions , new RetryPolicy ()));
294
293
policies .add (new AddDatePolicy ());
295
- if (azureKeyCredential != null ) {
296
- policies .add (new AzureKeyCredentialPolicy ("api-key" , azureKeyCredential ));
294
+ policies .add (new CookiePolicy ());
295
+ if (keyCredential != null ) {
296
+ policies .add (
297
+ useNonAzureOpenAIService ()
298
+ ? new KeyCredentialPolicy ("Authorization" , keyCredential , "Bearer" )
299
+ : new KeyCredentialPolicy ("api-key" , keyCredential ));
297
300
}
298
301
if (tokenCredential != null ) {
299
302
policies .add (new BearerTokenAuthenticationPolicy (tokenCredential , DEFAULT_SCOPES ));
@@ -313,63 +316,21 @@ private HttpPipeline createHttpPipeline() {
313
316
}
314
317
315
318
private NonAzureOpenAIClientImpl buildInnerNonAzureOpenAIClient () {
316
- HttpPipeline localPipeline = (pipeline != null ) ? pipeline : createHttpPipelineNonAzureOpenAI ();
319
+ HttpPipeline localPipeline = (pipeline != null ) ? pipeline : createHttpPipeline ();
317
320
NonAzureOpenAIClientImpl client =
318
321
new NonAzureOpenAIClientImpl (localPipeline , JacksonAdapter .createDefaultSerializerAdapter ());
319
322
return client ;
320
323
}
321
324
322
- private HttpPipeline createHttpPipelineNonAzureOpenAI () {
323
- Configuration buildConfiguration =
324
- (configuration == null ) ? Configuration .getGlobalConfiguration () : configuration ;
325
- HttpLogOptions localHttpLogOptions = this .httpLogOptions == null ? new HttpLogOptions () : this .httpLogOptions ;
326
- ClientOptions localClientOptions = this .clientOptions == null ? new ClientOptions () : this .clientOptions ;
327
- List <HttpPipelinePolicy > policies = new ArrayList <>();
328
- String clientName = PROPERTIES .getOrDefault (SDK_NAME , "UnknownName" );
329
- String clientVersion = PROPERTIES .getOrDefault (SDK_VERSION , "UnknownVersion" );
330
- String applicationId = CoreUtils .getApplicationId (localClientOptions , localHttpLogOptions );
331
- policies .add (new UserAgentPolicy (applicationId , clientName , clientVersion , buildConfiguration ));
332
- policies .add (new RequestIdPolicy ());
333
- policies .add (new AddHeadersFromContextPolicy ());
334
- HttpHeaders headers = new HttpHeaders ();
335
- localClientOptions .getHeaders ().forEach (header -> headers .set (header .getName (), header .getValue ()));
336
- if (headers .getSize () > 0 ) {
337
- policies .add (new AddHeadersPolicy (headers ));
338
- }
339
- this .pipelinePolicies .stream ()
340
- .filter (p -> p .getPipelinePosition () == HttpPipelinePosition .PER_CALL )
341
- .forEach (p -> policies .add (p ));
342
- HttpPolicyProviders .addBeforeRetryPolicies (policies );
343
- policies .add (ClientBuilderUtil .validateAndGetRetryPolicy (retryPolicy , retryOptions , new RetryPolicy ()));
344
- policies .add (new AddDatePolicy ());
345
- policies .add (new CookiePolicy ());
346
- if (nonAzureOpenAIKeyCredential != null ) {
347
- policies .add (new NonAzureOpenAIKeyCredentialPolicy ("Authorization" , nonAzureOpenAIKeyCredential , "Bearer" ));
348
- }
349
- this .pipelinePolicies .stream ()
350
- .filter (p -> p .getPipelinePosition () == HttpPipelinePosition .PER_RETRY )
351
- .forEach (p -> policies .add (p ));
352
- HttpPolicyProviders .addAfterRetryPolicies (policies );
353
- policies .add (new HttpLoggingPolicy (httpLogOptions ));
354
- HttpPipeline httpPipeline =
355
- new HttpPipelineBuilder ()
356
- .policies (policies .toArray (new HttpPipelinePolicy [0 ]))
357
- .httpClient (httpClient )
358
- .clientOptions (localClientOptions )
359
- .build ();
360
- return httpPipeline ;
361
- }
362
-
363
325
/**
364
326
* Builds an instance of OpenAIAsyncClient class.
365
327
*
366
328
* @return an instance of OpenAIAsyncClient.
367
329
*/
368
330
public OpenAIAsyncClient buildAsyncClient () {
369
- if (nonAzureOpenAIKeyCredential != null ) {
370
- return new OpenAIAsyncClient (buildInnerNonAzureOpenAIClient ());
371
- }
372
- return new OpenAIAsyncClient (buildInnerClient ());
331
+ return useNonAzureOpenAIService ()
332
+ ? new OpenAIAsyncClient (buildInnerNonAzureOpenAIClient ())
333
+ : new OpenAIAsyncClient (buildInnerClient ());
373
334
}
374
335
375
336
/**
@@ -378,11 +339,18 @@ public OpenAIAsyncClient buildAsyncClient() {
378
339
* @return an instance of OpenAIClient.
379
340
*/
380
341
public OpenAIClient buildClient () {
381
- if (nonAzureOpenAIKeyCredential != null ) {
382
- return new OpenAIClient (buildInnerNonAzureOpenAIClient ());
383
- }
384
- return new OpenAIClient (buildInnerClient ());
342
+ return useNonAzureOpenAIService ()
343
+ ? new OpenAIClient (buildInnerNonAzureOpenAIClient ())
344
+ : new OpenAIClient (buildInnerClient ());
385
345
}
386
346
387
347
private static final ClientLogger LOGGER = new ClientLogger (OpenAIClientBuilder .class );
348
+
349
+ /**
350
+ * OpenAI service can be used by either not setting the endpoint or by setting the endpoint to start with
351
+ * "https://api.openai.com/"
352
+ */
353
+ private boolean useNonAzureOpenAIService () {
354
+ return endpoint == null || endpoint .startsWith (OPEN_AI_ENDPOINT );
355
+ }
388
356
}
0 commit comments