Skip to content

Commit

Permalink
add spring boot starter
Browse files Browse the repository at this point in the history
  • Loading branch information
SandraAhlgrimm committed Jan 5, 2024
1 parent 53d4bf7 commit c1f4ef8
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package azure.openai;

import com.azure.ai.openai.OpenAIAsyncClient;
import com.azure.ai.openai.OpenAIClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.core.credential.AzureKeyCredential;
import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.SKBuilders;
import com.microsoft.semantickernel.chatcompletion.ChatCompletion;
import com.microsoft.semantickernel.chatcompletion.ChatHistory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
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;

@AutoConfiguration
@EnableConfigurationProperties(AzureOpenAiConnectionProperties.class)
public class AzureOpenAIAutoConfiguration {

@Bean
@ConditionalOnClass(OpenAIClientBuilder.class)
@ConditionalOnMissingBean
public OpenAIClient openAIClient(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()))
.buildClient();
}

@Bean
@ConditionalOnClass(OpenAIAsyncClient.class)
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();
}

@Bean
public Kernel semanticKernel(AzureOpenAiConnectionProperties connectionProperties) {
Assert.hasText(connectionProperties.getDeploymentName(),
"Azure OpenAI deployment name must be set and match the ModelID");
OpenAIAsyncClient openAIAsyncClient = openAIAsyncClient(new AzureOpenAiConnectionProperties());
ChatCompletion<ChatHistory> chatCompletion = SKBuilders.chatCompletion()
.withOpenAIClient(openAIAsyncClient)
.withModelId(connectionProperties.getDeploymentName())
.build();
return SKBuilders.kernel().withDefaultAIService(chatCompletion).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package azure.openai;

import org.springframework.boot.context.properties.ConfigurationProperties;

@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.
*/
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;

public String getEndpoint() {
return endpoint;
}

public String getKey() {
return key;
}

public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}

public void setKey(String key) {
this.key = key;
}

public String getDeploymentName() {
return deploymentName;
}

public void setDeploymentName(String deploymentName) {
this.deploymentName = deploymentName;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
client.azureopenai.key=yourkey
client.azureopenai.endpoint=yourendpoint
client.azureopenai.deploymentname=davinci-002
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<artifactId>semantic-kernel-spring-boot</artifactId>
<groupId>semantickernel-spring-boot-starter</groupId>
<name>Semantic Kernel Spring Boot - Starter</name>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Microsoft Semantic Kernel -->
<dependency>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-core</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-connectors-ai-openai</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.semantic-kernel</groupId>
<artifactId>semantickernel-settings-loader</artifactId>
</dependency>

</dependencies>
</project>

0 comments on commit c1f4ef8

Please sign in to comment.