cleaned up and ready for PR

This commit is contained in:
SandraAhlgrimm
2024-01-22 16:03:22 +01:00
parent 8d3d26afc8
commit a8cf41e5ef
9 changed files with 119 additions and 94 deletions
+1
View File
@@ -80,6 +80,7 @@
<module>semantickernel-bom</module>
<module>semantickernel-api</module>
<module>semantickernel-core</module>
<module>semantickernel-spring-starter</module>
</modules>
<dependencyManagement>
@@ -1,35 +0,0 @@
package azureopenai;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import com.microsoft.semantickernel.Kernel;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
public class AzureOpenAIAutoConfigurationTest {
@EnableConfigurationProperties(AzureOpenAiConnectionProperties.class)
static class TestConfiguration {
}
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner().withUserConfiguration(
TestConfiguration.class).withPropertyValues(
// @formatter:off
"client.azureopenai.key=TEST_KEY",
"client.azureopenai.endpoint=TEST_ENDPOINT",
"client.azureopenai.deploymentname=TEST_DEPLOYMENT_NAME"
// @formatter:on
).withConfiguration(AutoConfigurations.of(AzureOpenAIAutoConfiguration.class));
@Test
public void testSemanticKernel() {
contextRunner.run(context -> {
Kernel semanticKernel = context.getBean(Kernel.class);
assertNotNull(semanticKernel);
});
}
}
@@ -1,67 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-parent</artifactId>
<version>0.2.10-alpha-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>semantickernel-spring-boot-starter</artifactId>
<name>Semantic Kernel - Spring Boot Starter</name>
<packaging>jar</packaging>
<description>Semantic Kernel Spring Auto Configuration</description>
<artifactId>semantickernel-spring-starter</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- production dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<artifactId>spring-boot-test</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-openai</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-core</artifactId>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.25.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.45.1</version>
<scope>compile</scope>
<artifactId>azure-ai-openai</artifactId>
<version>1.0.0-beta.6</version>
</dependency>
<dependency>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-core</artifactId>
<scope>test</scope>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>3.2.1</version>
<scope>compile</scope>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-connectors-ai-openai</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.9.1</version>
</dependency>
</dependencies>
</project>
@@ -1,11 +1,13 @@
package azureopenai;
package com.microsoft.semantickernel.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(AzureOpenAiConnectionProperties.CONFIG_PREFIX)
public class AzureOpenAiConnectionProperties {
@ConfigurationProperties(AzureOpenAIConnectionProperties.CONFIG_PREFIX)
public class AzureOpenAIConnectionProperties {
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.
@@ -46,4 +48,4 @@ public class AzureOpenAiConnectionProperties {
this.deploymentName = deploymentName;
}
}
}
@@ -1,4 +1,5 @@
package azureopenai;
package com.microsoft.semantickernel.starter;
import com.azure.ai.openai.OpenAIAsyncClient;
import com.azure.ai.openai.OpenAIClientBuilder;
@@ -6,38 +7,41 @@ import com.azure.core.credential.AzureKeyCredential;
import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.SKBuilders;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.util.Assert;
@SpringBootApplication(scanBasePackages = "com.microsoft.semantickernel.starter")
@AutoConfiguration
@EnableConfigurationProperties(AzureOpenAiConnectionProperties.class)
public class AzureOpenAIAutoConfiguration {
String modelId;
@EnableConfigurationProperties(AzureOpenAIConnectionProperties.class)
public class SemanticKernelAutoConfiguration {
/**
* Creates a {@link OpenAIAsyncClient} with the endpoint and key specified in the
* {@link AzureOpenAiConnectionProperties}.
* {@link AzureOpenAIConnectionProperties}.
*
* @param connectionProperties the {@link AzureOpenAiConnectionProperties} to use
* @param connectionProperties the {@link AzureOpenAIConnectionProperties} to use
* @return the {@link OpenAIAsyncClient}
*/
@Bean
@ConditionalOnClass(OpenAIAsyncClient.class)
public OpenAIAsyncClient openAIAsyncClient(AzureOpenAiConnectionProperties connectionProperties) {
modelId = connectionProperties.getDeploymentName();
@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())
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.
* 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}
@@ -46,8 +50,8 @@ public class AzureOpenAIAutoConfiguration {
public Kernel semanticKernel(OpenAIAsyncClient client) {
return SKBuilders.kernel()
.withDefaultAIService(
SKBuilders.chatCompletion()
.withModelId(modelId)
SKBuilders.textCompletion()
.withModelId("modelId")
.withOpenAIClient(client)
.build())
.build();
@@ -1,32 +1,33 @@
package azureopenai;
import azureopenai.AzureOpenAIAutoConfigurationTest.TestConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
package com.microsoft.semantickernel.azureopenai;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import com.microsoft.semantickernel.starter.AzureOpenAIConnectionProperties;
import com.microsoft.semantickernel.starter.SemanticKernelAutoConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.test.context.ActiveProfiles;
@ActiveProfiles("test")
public class AzureOpenAIConnectionPropertiesTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner().withUserConfiguration(
TestConfiguration.class).withPropertyValues(
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(AzureOpenAIAutoConfiguration.class));
).withConfiguration(AutoConfigurations.of(SemanticKernelAutoConfiguration.class));
@Test
public void ConnectionPropertiesTest() {
contextRunner.run(context -> {
AzureOpenAiConnectionProperties props = context.getBean(AzureOpenAiConnectionProperties.class);
AzureOpenAIConnectionProperties props = context.getBean(
AzureOpenAIConnectionProperties.class);
assertNotNull(props.getEndpoint());
assertNotNull(props.getKey());
assertNotNull(props.getDeploymentName());
});
}
}
@@ -0,0 +1,38 @@
package com.microsoft.semantickernel.azureopenai;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.starter.AzureOpenAIConnectionProperties;
import com.microsoft.semantickernel.starter.SemanticKernelAutoConfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
@EnableConfigurationProperties(AzureOpenAIConnectionProperties.class)
@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);
});
}
}
@@ -0,0 +1,3 @@
client.azureopenai.key=yourkey
client.azureopenai.endpoint=yourendpoint
client.azureopenai.deploymentname=davinci-002