Skip to content

Commit 4570ad2

Browse files
cleaned up and ready for PR
1 parent a8cf41e commit 4570ad2

6 files changed

Lines changed: 152 additions & 117 deletions

File tree

‎java/semantickernel-spring-starter/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010
</parent>
1111

1212
<artifactId>semantickernel-spring-starter</artifactId>
13+
<name>Semantic Kernel Spring Boot Starter</name>
1314

1415
<properties>
1516
<maven.compiler.source>17</maven.compiler.source>
1617
<maven.compiler.target>17</maven.compiler.target>
1718
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1819
</properties>
1920
<dependencies>
21+
<dependency>
22+
<groupId>org.slf4j</groupId>
23+
<artifactId>slf4j-api</artifactId>
24+
</dependency>
2025
<dependency>
2126
<groupId>org.springframework.boot</groupId>
2227
<artifactId>spring-boot-test</artifactId>
Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,52 @@
1+
// Copyright (c) Microsoft. All rights reserved.
12
package com.microsoft.semantickernel.starter;
23

3-
44
import org.springframework.boot.context.properties.ConfigurationProperties;
55

66
@ConfigurationProperties(AzureOpenAIConnectionProperties.CONFIG_PREFIX)
77
public class AzureOpenAIConnectionProperties {
88

9-
public static final String CONFIG_PREFIX = "client.azureopenai";
10-
11-
/**
12-
* Azure OpenAI API endpoint.From the Azure AI OpenAI at 'Resource Management' select `Keys and
13-
* Endpoint` and find it on the right side.
14-
*/
15-
private String endpoint;
16-
/**
17-
* Azure OpenAI API key.From the Azure AI OpenAI at 'Resource Management' select `Keys and
18-
* Endpoint` and find it on the right side.
19-
*/
20-
private String key;
21-
/**
22-
* Azure OpenAI API deployment name specified in the Azure Open AI studio under Management ->
23-
* Deployments.
24-
*/
25-
private String deploymentName;
26-
27-
public String getEndpoint() {
28-
return endpoint;
29-
}
30-
31-
public String getKey() {
32-
return key;
33-
}
34-
35-
public void setEndpoint(String endpoint) {
36-
this.endpoint = endpoint;
37-
}
38-
39-
public void setKey(String key) {
40-
this.key = key;
41-
}
42-
43-
public String getDeploymentName() {
44-
return deploymentName;
45-
}
46-
47-
public void setDeploymentName(String deploymentName) {
48-
this.deploymentName = deploymentName;
49-
}
50-
9+
public static final String CONFIG_PREFIX = "client.azureopenai";
10+
11+
/**
12+
* Azure OpenAI API endpoint.From the Azure AI OpenAI at 'Resource Management' select `Keys and
13+
* Endpoint` and find it on the right side.
14+
*/
15+
private String endpoint;
16+
17+
/**
18+
* Azure OpenAI API key.From the Azure AI OpenAI at 'Resource Management' select `Keys and
19+
* Endpoint` and find it on the right side.
20+
*/
21+
private String key;
22+
23+
/**
24+
* Azure OpenAI API deployment name specified in the Azure Open AI studio under Management ->
25+
* Deployments.
26+
*/
27+
private String deploymentName;
28+
29+
public String getEndpoint() {
30+
return endpoint;
31+
}
32+
33+
public String getKey() {
34+
return key;
35+
}
36+
37+
public void setEndpoint(String endpoint) {
38+
this.endpoint = endpoint;
39+
}
40+
41+
public void setKey(String key) {
42+
this.key = key;
43+
}
44+
45+
public String getDeploymentName() {
46+
return deploymentName;
47+
}
48+
49+
public void setDeploymentName(String deploymentName) {
50+
this.deploymentName = deploymentName;
51+
}
5152
}
Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
// Copyright (c) Microsoft. All rights reserved.
12
package com.microsoft.semantickernel.starter;
23

3-
44
import com.azure.ai.openai.OpenAIAsyncClient;
55
import com.azure.ai.openai.OpenAIClientBuilder;
66
import com.azure.core.credential.AzureKeyCredential;
@@ -19,41 +19,49 @@
1919
@EnableConfigurationProperties(AzureOpenAIConnectionProperties.class)
2020
public 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+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Copyright (c) Microsoft. All rights reserved.
12
package com.microsoft.semantickernel.azureopenai;
23

34
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -12,22 +13,28 @@
1213
@ActiveProfiles("test")
1314
public class AzureOpenAIConnectionPropertiesTest {
1415

15-
ApplicationContextRunner contextRunner = new ApplicationContextRunner().withPropertyValues(
16-
// @formatter:off
17-
"client.azureopenai.key=TEST_KEY",
18-
"client.azureopenai.endpoint=TEST_ENDPOINT",
19-
"client.azureopenai.deploymentname=TEST_DEPLOYMENT_NAME"
20-
// @formatter:on
21-
).withConfiguration(AutoConfigurations.of(SemanticKernelAutoConfiguration.class));
16+
ApplicationContextRunner contextRunner =
17+
new ApplicationContextRunner()
18+
.withPropertyValues(
19+
// @formatter:off
20+
"client.azureopenai.key=TEST_KEY",
21+
"client.azureopenai.endpoint=TEST_ENDPOINT",
22+
"client.azureopenai.deploymentname=TEST_DEPLOYMENT_NAME"
2223

23-
@Test
24-
public void ConnectionPropertiesTest() {
25-
contextRunner.run(context -> {
26-
AzureOpenAIConnectionProperties props = context.getBean(
27-
AzureOpenAIConnectionProperties.class);
28-
assertNotNull(props.getEndpoint());
29-
assertNotNull(props.getKey());
30-
assertNotNull(props.getDeploymentName());
31-
});
32-
}
24+
// @formatter:on
25+
)
26+
.withConfiguration(
27+
AutoConfigurations.of(SemanticKernelAutoConfiguration.class));
28+
29+
@Test
30+
public void ConnectionPropertiesTest() {
31+
contextRunner.run(
32+
context -> {
33+
AzureOpenAIConnectionProperties props =
34+
context.getBean(AzureOpenAIConnectionProperties.class);
35+
assertNotNull(props.getEndpoint());
36+
assertNotNull(props.getKey());
37+
assertNotNull(props.getDeploymentName());
38+
});
39+
}
3340
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Copyright (c) Microsoft. All rights reserved.
12
package com.microsoft.semantickernel.azureopenai;
23

34
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -16,23 +17,23 @@
1617
@SpringBootTest(classes = SemanticKernelAutoConfiguration.class)
1718
public class SemanticKernelAutoConfigurationTest {
1819

19-
@Autowired
20-
Kernel kernel;
21-
22-
@Test
23-
public void testSemanticKernelAutoConfig() {
24-
ApplicationContextRunner runner = new ApplicationContextRunner();
25-
runner.withPropertyValues(
26-
// @formatter:off
27-
"client.azureopenai.key=TEST_KEY",
28-
"client.azureopenai.endpoint=TEST_ENDPOINT",
29-
"client.azureopenai.deploymentname=TEST_DEPLOYMENT_NAME"
30-
// @formatter:on
31-
);
32-
runner.withConfiguration(AutoConfigurations.of(SemanticKernelAutoConfiguration.class));
33-
runner.run(context -> {
34-
assertNotNull(kernel);
35-
});
36-
}
20+
@Autowired
21+
Kernel kernel;
3722

23+
@Test
24+
public void testSemanticKernelAutoConfig() {
25+
ApplicationContextRunner runner = new ApplicationContextRunner();
26+
runner.withPropertyValues(
27+
// @formatter:off
28+
"client.azureopenai.key=TEST_KEY",
29+
"client.azureopenai.endpoint=TEST_ENDPOINT",
30+
"client.azureopenai.deploymentname=TEST_DEPLOYMENT_NAME"
31+
// @formatter:on
32+
);
33+
runner.withConfiguration(AutoConfigurations.of(SemanticKernelAutoConfiguration.class));
34+
runner.run(
35+
context -> {
36+
assertNotNull(kernel);
37+
});
38+
}
3839
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="info">
3+
<Appenders>
4+
<Console name="Console" target="SYSTEM_OUT">
5+
<PatternLayout pattern="%d{HH:mm:ss} %-5level %c{1.} - %msg%n"/>
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Root level="info">
10+
<AppenderRef ref="Console"/>
11+
</Root>
12+
</Loggers>
13+
</Configuration>

0 commit comments

Comments
 (0)