cleaned up and ready for PR

This commit is contained in:
SandraAhlgrimm
2024-01-24 21:41:14 +01:00
parent a8cf41e5ef
commit 4570ad2889
6 changed files with 143 additions and 108 deletions
@@ -10,6 +10,7 @@
</parent>
<artifactId>semantickernel-spring-starter</artifactId>
<name>Semantic Kernel Spring Boot Starter</name>
<properties>
<maven.compiler.source>17</maven.compiler.source>
@@ -17,6 +18,10 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
@@ -1,51 +1,52 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(AzureOpenAIConnectionProperties.CONFIG_PREFIX)
public class AzureOpenAIConnectionProperties {
public static final String CONFIG_PREFIX = "client.azureopenai";
public static final String CONFIG_PREFIX = "client.azureopenai";
/**
* Azure OpenAI API endpoint.From the Azure AI OpenAI at 'Resource Management' select `Keys and
* Endpoint` and find it on the right side.
*/
private String endpoint;
/**
* Azure OpenAI API key.From the Azure AI OpenAI at 'Resource Management' select `Keys and
* Endpoint` and find it on the right side.
*/
private String key;
/**
* Azure OpenAI API deployment name specified in the Azure Open AI studio under Management ->
* Deployments.
*/
private String deploymentName;
/**
* Azure OpenAI API endpoint.From the Azure AI OpenAI at 'Resource Management' select `Keys and
* Endpoint` and find it on the right side.
*/
private String endpoint;
public String getEndpoint() {
return endpoint;
}
/**
* Azure OpenAI API key.From the Azure AI OpenAI at 'Resource Management' select `Keys and
* Endpoint` and find it on the right side.
*/
private String key;
public String getKey() {
return key;
}
/**
* Azure OpenAI API deployment name specified in the Azure Open AI studio under Management ->
* Deployments.
*/
private String deploymentName;
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public String getEndpoint() {
return endpoint;
}
public void setKey(String key) {
this.key = key;
}
public String getKey() {
return key;
}
public String getDeploymentName() {
return deploymentName;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
public void setDeploymentName(String deploymentName) {
this.deploymentName = deploymentName;
}
public void setKey(String key) {
this.key = key;
}
public String getDeploymentName() {
return deploymentName;
}
public void setDeploymentName(String deploymentName) {
this.deploymentName = deploymentName;
}
}
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.starter;
import com.azure.ai.openai.OpenAIAsyncClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.core.credential.AzureKeyCredential;
@@ -19,41 +19,49 @@ import org.springframework.util.Assert;
@EnableConfigurationProperties(AzureOpenAIConnectionProperties.class)
public class SemanticKernelAutoConfiguration {
/**
* Creates a {@link OpenAIAsyncClient} with the endpoint and key specified in the
* {@link AzureOpenAIConnectionProperties}.
*
* @param connectionProperties the {@link AzureOpenAIConnectionProperties} to use
* @return the {@link OpenAIAsyncClient}
*/
@Bean
@ConditionalOnClass(OpenAIAsyncClient.class)
@ConditionalOnMissingBean
public OpenAIAsyncClient openAIAsyncClient(AzureOpenAIConnectionProperties connectionProperties) {
Assert.hasText(connectionProperties.getEndpoint(), "Azure OpenAI endpoint must be set");
Assert.hasText(connectionProperties.getKey(), "Azure OpenAI key must be set");
return new OpenAIClientBuilder()
.endpoint(connectionProperties.getEndpoint())
.credential(new AzureKeyCredential(connectionProperties.getKey()))
.buildAsyncClient();
}
String modelId;
/**
* Creates a {@link Kernel} with a default {@link com.microsoft.semantickernel.services.AIService}
* that uses the {@link com.microsoft.semantickernel.chatcompletion;} with the model id specified
* in the {@link AzureOpenAIConnectionProperties} as DeploymentName.
*
* @param client the {@link OpenAIAsyncClient} to use
* @return the {@link Kernel}
*/
@Bean
public Kernel semanticKernel(OpenAIAsyncClient client) {
return SKBuilders.kernel()
.withDefaultAIService(
SKBuilders.textCompletion()
.withModelId("modelId")
.withOpenAIClient(client)
.build())
.build();
}
}
/**
* Creates a {@link OpenAIAsyncClient} with the endpoint and key specified in the {@link
* AzureOpenAIConnectionProperties}.
*
* @param connectionProperties the {@link AzureOpenAIConnectionProperties} to use
* @return the {@link OpenAIAsyncClient}
*/
@Bean
@ConditionalOnClass(OpenAIAsyncClient.class)
@ConditionalOnMissingBean
public OpenAIAsyncClient openAIAsyncClient(
AzureOpenAIConnectionProperties connectionProperties) {
Assert.hasText(connectionProperties.getEndpoint(), "Azure OpenAI endpoint must be set");
Assert.hasText(connectionProperties.getKey(), "Azure OpenAI key must be set");
modelId = connectionProperties.getDeploymentName();
return new OpenAIClientBuilder()
.endpoint(connectionProperties.getEndpoint())
.credential(new AzureKeyCredential(connectionProperties.getKey()))
.buildAsyncClient();
}
/**
* Creates a {@link Kernel} with a default {@link
* com.microsoft.semantickernel.services.AIService} that uses the {@link
* com.microsoft.semantickernel.chatcompletion;} with the model id specified in the {@link
* AzureOpenAIConnectionProperties} as DeploymentName.
*
* @param client the {@link OpenAIAsyncClient} to use
* @return the {@link Kernel}
*/
@Bean
public Kernel semanticKernel(OpenAIAsyncClient client) {
return SKBuilders.kernel()
.withDefaultAIService(
SKBuilders.textCompletion()
.withModelId(modelId)
.withOpenAIClient(client)
.build())
.build();
}
}
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.azureopenai;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -12,22 +13,28 @@ import org.springframework.test.context.ActiveProfiles;
@ActiveProfiles("test")
public class AzureOpenAIConnectionPropertiesTest {
ApplicationContextRunner contextRunner = new ApplicationContextRunner().withPropertyValues(
// @formatter:off
"client.azureopenai.key=TEST_KEY",
"client.azureopenai.endpoint=TEST_ENDPOINT",
"client.azureopenai.deploymentname=TEST_DEPLOYMENT_NAME"
// @formatter:on
).withConfiguration(AutoConfigurations.of(SemanticKernelAutoConfiguration.class));
ApplicationContextRunner contextRunner =
new ApplicationContextRunner()
.withPropertyValues(
// @formatter:off
"client.azureopenai.key=TEST_KEY",
"client.azureopenai.endpoint=TEST_ENDPOINT",
"client.azureopenai.deploymentname=TEST_DEPLOYMENT_NAME"
@Test
public void ConnectionPropertiesTest() {
contextRunner.run(context -> {
AzureOpenAIConnectionProperties props = context.getBean(
AzureOpenAIConnectionProperties.class);
assertNotNull(props.getEndpoint());
assertNotNull(props.getKey());
assertNotNull(props.getDeploymentName());
});
}
// @formatter:on
)
.withConfiguration(
AutoConfigurations.of(SemanticKernelAutoConfiguration.class));
@Test
public void ConnectionPropertiesTest() {
contextRunner.run(
context -> {
AzureOpenAIConnectionProperties props =
context.getBean(AzureOpenAIConnectionProperties.class);
assertNotNull(props.getEndpoint());
assertNotNull(props.getKey());
assertNotNull(props.getDeploymentName());
});
}
}
@@ -1,3 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
package com.microsoft.semantickernel.azureopenai;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -16,23 +17,23 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@SpringBootTest(classes = SemanticKernelAutoConfiguration.class)
public class SemanticKernelAutoConfigurationTest {
@Autowired
Kernel kernel;
@Test
public void testSemanticKernelAutoConfig() {
ApplicationContextRunner runner = new ApplicationContextRunner();
runner.withPropertyValues(
// @formatter:off
"client.azureopenai.key=TEST_KEY",
"client.azureopenai.endpoint=TEST_ENDPOINT",
"client.azureopenai.deploymentname=TEST_DEPLOYMENT_NAME"
// @formatter:on
);
runner.withConfiguration(AutoConfigurations.of(SemanticKernelAutoConfiguration.class));
runner.run(context -> {
assertNotNull(kernel);
});
}
@Autowired
Kernel kernel;
@Test
public void testSemanticKernelAutoConfig() {
ApplicationContextRunner runner = new ApplicationContextRunner();
runner.withPropertyValues(
// @formatter:off
"client.azureopenai.key=TEST_KEY",
"client.azureopenai.endpoint=TEST_ENDPOINT",
"client.azureopenai.deploymentname=TEST_DEPLOYMENT_NAME"
// @formatter:on
);
runner.withConfiguration(AutoConfigurations.of(SemanticKernelAutoConfiguration.class));
runner.run(
context -> {
assertNotNull(kernel);
});
}
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss} %-5level %c{1.} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>