1+ // Copyright (c) Microsoft. All rights reserved.
12package com .microsoft .semantickernel .starter ;
23
3-
44import com .azure .ai .openai .OpenAIAsyncClient ;
55import com .azure .ai .openai .OpenAIClientBuilder ;
66import com .azure .core .credential .AzureKeyCredential ;
1919@ EnableConfigurationProperties (AzureOpenAIConnectionProperties .class )
2020public class SemanticKernelAutoConfiguration {
2121
22- /**
23- * Creates a {@link OpenAIAsyncClient} with the endpoint and key specified in the
24- * {@link AzureOpenAIConnectionProperties}.
25- *
26- * @param connectionProperties the {@link AzureOpenAIConnectionProperties} to use
27- * @return the {@link OpenAIAsyncClient}
28- */
29- @ Bean
30- @ ConditionalOnClass (OpenAIAsyncClient .class )
31- @ ConditionalOnMissingBean
32- public OpenAIAsyncClient openAIAsyncClient (AzureOpenAIConnectionProperties connectionProperties ) {
33- Assert .hasText (connectionProperties .getEndpoint (), "Azure OpenAI endpoint must be set" );
34- Assert .hasText (connectionProperties .getKey (), "Azure OpenAI key must be set" );
35- return new OpenAIClientBuilder ()
36- .endpoint (connectionProperties .getEndpoint ())
37- .credential (new AzureKeyCredential (connectionProperties .getKey ()))
38- .buildAsyncClient ();
39- }
40-
41- /**
42- * Creates a {@link Kernel} with a default {@link com.microsoft.semantickernel.services.AIService}
43- * that uses the {@link com.microsoft.semantickernel.chatcompletion;} with the model id specified
44- * in the {@link AzureOpenAIConnectionProperties} as DeploymentName.
45- *
46- * @param client the {@link OpenAIAsyncClient} to use
47- * @return the {@link Kernel}
48- */
49- @ Bean
50- public Kernel semanticKernel (OpenAIAsyncClient client ) {
51- return SKBuilders .kernel ()
52- .withDefaultAIService (
53- SKBuilders .textCompletion ()
54- .withModelId ("modelId" )
55- .withOpenAIClient (client )
56- .build ())
57- .build ();
58- }
59- }
22+ String modelId ;
23+
24+ /**
25+ * Creates a {@link OpenAIAsyncClient} with the endpoint and key specified in the {@link
26+ * AzureOpenAIConnectionProperties}.
27+ *
28+ * @param connectionProperties the {@link AzureOpenAIConnectionProperties} to use
29+ * @return the {@link OpenAIAsyncClient}
30+ */
31+ @ Bean
32+ @ ConditionalOnClass (OpenAIAsyncClient .class )
33+ @ ConditionalOnMissingBean
34+ public OpenAIAsyncClient openAIAsyncClient (
35+ AzureOpenAIConnectionProperties connectionProperties ) {
36+ Assert .hasText (connectionProperties .getEndpoint (), "Azure OpenAI endpoint must be set" );
37+ Assert .hasText (connectionProperties .getKey (), "Azure OpenAI key must be set" );
38+
39+ modelId = connectionProperties .getDeploymentName ();
40+
41+ return new OpenAIClientBuilder ()
42+ .endpoint (connectionProperties .getEndpoint ())
43+ .credential (new AzureKeyCredential (connectionProperties .getKey ()))
44+ .buildAsyncClient ();
45+ }
46+
47+ /**
48+ * Creates a {@link Kernel} with a default {@link
49+ * com.microsoft.semantickernel.services.AIService} that uses the {@link
50+ * com.microsoft.semantickernel.chatcompletion;} with the model id specified in the {@link
51+ * AzureOpenAIConnectionProperties} as DeploymentName.
52+ *
53+ * @param client the {@link OpenAIAsyncClient} to use
54+ * @return the {@link Kernel}
55+ */
56+ @ Bean
57+ public Kernel semanticKernel (OpenAIAsyncClient client ) {
58+ return SKBuilders .kernel ()
59+ .withDefaultAIService (
60+ SKBuilders .textCompletion ()
61+ .withModelId (modelId )
62+ .withOpenAIClient (client )
63+ .build ())
64+ .build ();
65+ }
66+
67+ }
0 commit comments