33// Code generated by Microsoft (R) AutoRest Code Generator.
44package com .azure .ai .openai ;
55
6+ import static com .azure .ai .openai .implementation .NonAzureOpenAIClientImpl .OPEN_AI_ENDPOINT ;
7+
68import com .azure .ai .openai .implementation .NonAzureOpenAIClientImpl ;
7- import com .azure .ai .openai .implementation .NonAzureOpenAIKeyCredentialPolicy ;
89import com .azure .ai .openai .implementation .OpenAIClientImpl ;
9- import com .azure .ai .openai .models .NonAzureOpenAIKeyCredential ;
1010import com .azure .core .annotation .Generated ;
1111import com .azure .core .annotation .ServiceClientBuilder ;
1212import com .azure .core .client .traits .AzureKeyCredentialTrait ;
1515import com .azure .core .client .traits .HttpTrait ;
1616import com .azure .core .client .traits .TokenCredentialTrait ;
1717import com .azure .core .credential .AzureKeyCredential ;
18+ import com .azure .core .credential .KeyCredential ;
1819import com .azure .core .credential .TokenCredential ;
1920import com .azure .core .http .HttpClient ;
2021import com .azure .core .http .HttpHeaders ;
2425import com .azure .core .http .policy .AddDatePolicy ;
2526import com .azure .core .http .policy .AddHeadersFromContextPolicy ;
2627import com .azure .core .http .policy .AddHeadersPolicy ;
27- import com .azure .core .http .policy .AzureKeyCredentialPolicy ;
2828import com .azure .core .http .policy .BearerTokenAuthenticationPolicy ;
2929import com .azure .core .http .policy .CookiePolicy ;
3030import com .azure .core .http .policy .HttpLogOptions ;
3131import com .azure .core .http .policy .HttpLoggingPolicy ;
3232import com .azure .core .http .policy .HttpPipelinePolicy ;
3333import com .azure .core .http .policy .HttpPolicyProviders ;
34+ import com .azure .core .http .policy .KeyCredentialPolicy ;
3435import com .azure .core .http .policy .RequestIdPolicy ;
3536import com .azure .core .http .policy .RetryOptions ;
3637import com .azure .core .http .policy .RetryPolicy ;
@@ -182,23 +183,22 @@ public OpenAIClientBuilder credential(TokenCredential tokenCredential) {
182183 @ Generated private AzureKeyCredential azureKeyCredential ;
183184
184185 /** {@inheritDoc}. */
185- @ Generated
186186 @ Override
187187 public OpenAIClientBuilder credential (AzureKeyCredential azureKeyCredential ) {
188- this .azureKeyCredential = azureKeyCredential ;
189- return this ;
188+ return this .credential ((KeyCredential ) azureKeyCredential );
190189 }
191190
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 ;
193193
194194 /**
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 .
196196 *
197- * @param nonAzureOpenAIKeyCredential The credential for non-azure public OpenAI authenticaton .
197+ * @param keyCredential The credential for OpenAI authentication .
198198 * @return the object itself.
199199 */
200- public OpenAIClientBuilder credential (NonAzureOpenAIKeyCredential nonAzureOpenAIKeyCredential ) {
201- this .nonAzureOpenAIKeyCredential = nonAzureOpenAIKeyCredential ;
200+ public OpenAIClientBuilder credential (KeyCredential keyCredential ) {
201+ this .keyCredential = keyCredential ;
202202 return this ;
203203 }
204204
@@ -268,7 +268,6 @@ private OpenAIClientImpl buildInnerClient() {
268268 return client ;
269269 }
270270
271- @ Generated
272271 private HttpPipeline createHttpPipeline () {
273272 Configuration buildConfiguration =
274273 (configuration == null ) ? Configuration .getGlobalConfiguration () : configuration ;
@@ -292,8 +291,12 @@ private HttpPipeline createHttpPipeline() {
292291 HttpPolicyProviders .addBeforeRetryPolicies (policies );
293292 policies .add (ClientBuilderUtil .validateAndGetRetryPolicy (retryPolicy , retryOptions , new RetryPolicy ()));
294293 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 ));
297300 }
298301 if (tokenCredential != null ) {
299302 policies .add (new BearerTokenAuthenticationPolicy (tokenCredential , DEFAULT_SCOPES ));
@@ -313,63 +316,21 @@ private HttpPipeline createHttpPipeline() {
313316 }
314317
315318 private NonAzureOpenAIClientImpl buildInnerNonAzureOpenAIClient () {
316- HttpPipeline localPipeline = (pipeline != null ) ? pipeline : createHttpPipelineNonAzureOpenAI ();
319+ HttpPipeline localPipeline = (pipeline != null ) ? pipeline : createHttpPipeline ();
317320 NonAzureOpenAIClientImpl client =
318321 new NonAzureOpenAIClientImpl (localPipeline , JacksonAdapter .createDefaultSerializerAdapter ());
319322 return client ;
320323 }
321324
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-
363325 /**
364326 * Builds an instance of OpenAIAsyncClient class.
365327 *
366328 * @return an instance of OpenAIAsyncClient.
367329 */
368330 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 ());
373334 }
374335
375336 /**
@@ -378,11 +339,18 @@ public OpenAIAsyncClient buildAsyncClient() {
378339 * @return an instance of OpenAIClient.
379340 */
380341 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 ());
385345 }
386346
387347 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+ }
388356}
0 commit comments