Skip to content

Commit c1f4ef8

Browse files
add spring boot starter
1 parent 53d4bf7 commit c1f4ef8

4 files changed

Lines changed: 152 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package azure.openai;
2+
3+
import com.azure.ai.openai.OpenAIAsyncClient;
4+
import com.azure.ai.openai.OpenAIClient;
5+
import com.azure.ai.openai.OpenAIClientBuilder;
6+
import com.azure.core.credential.AzureKeyCredential;
7+
import com.microsoft.semantickernel.Kernel;
8+
import com.microsoft.semantickernel.SKBuilders;
9+
import com.microsoft.semantickernel.chatcompletion.ChatCompletion;
10+
import com.microsoft.semantickernel.chatcompletion.ChatHistory;
11+
import org.springframework.boot.autoconfigure.AutoConfiguration;
12+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
13+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
14+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
15+
import org.springframework.context.annotation.Bean;
16+
import org.springframework.util.Assert;
17+
18+
@AutoConfiguration
19+
@EnableConfigurationProperties(AzureOpenAiConnectionProperties.class)
20+
public class AzureOpenAIAutoConfiguration {
21+
22+
@Bean
23+
@ConditionalOnClass(OpenAIClientBuilder.class)
24+
@ConditionalOnMissingBean
25+
public OpenAIClient openAIClient(AzureOpenAiConnectionProperties connectionProperties) {
26+
Assert.hasText(connectionProperties.getEndpoint(), "Azure OpenAI endpoint must be set");
27+
Assert.hasText(connectionProperties.getKey(), "Azure OpenAI key must be set");
28+
return new OpenAIClientBuilder().endpoint(connectionProperties.getEndpoint())
29+
.credential(new AzureKeyCredential(connectionProperties.getKey()))
30+
.buildClient();
31+
}
32+
33+
@Bean
34+
@ConditionalOnClass(OpenAIAsyncClient.class)
35+
public OpenAIAsyncClient openAIAsyncClient(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+
return new OpenAIClientBuilder().endpoint(connectionProperties.getEndpoint())
39+
.credential(new AzureKeyCredential(connectionProperties.getKey()))
40+
.buildAsyncClient();
41+
}
42+
43+
@Bean
44+
public Kernel semanticKernel(AzureOpenAiConnectionProperties connectionProperties) {
45+
Assert.hasText(connectionProperties.getDeploymentName(),
46+
"Azure OpenAI deployment name must be set and match the ModelID");
47+
OpenAIAsyncClient openAIAsyncClient = openAIAsyncClient(new AzureOpenAiConnectionProperties());
48+
ChatCompletion<ChatHistory> chatCompletion = SKBuilders.chatCompletion()
49+
.withOpenAIClient(openAIAsyncClient)
50+
.withModelId(connectionProperties.getDeploymentName())
51+
.build();
52+
return SKBuilders.kernel().withDefaultAIService(chatCompletion).build();
53+
}
54+
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package azure.openai;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
5+
@ConfigurationProperties(AzureOpenAiConnectionProperties.CONFIG_PREFIX)
6+
public class AzureOpenAiConnectionProperties {
7+
8+
public static final String CONFIG_PREFIX = "client.azureopenai";
9+
/**
10+
* Azure OpenAI API endpoint.From the Azure AI OpenAI at 'Resource Management' select `Keys and
11+
* Endpoint` and find it on the right side.
12+
*/
13+
private String endpoint;
14+
/**
15+
* Azure OpenAI API key.From the Azure AI OpenAI at 'Resource Management' select `Keys and
16+
* Endpoint` and find it on the right side.
17+
*/
18+
private String key;
19+
/**
20+
* Azure OpenAI API deployment name specified in the Azure Open AI studio under Management ->
21+
* Deployments.
22+
*/
23+
private String deploymentName;
24+
25+
public String getEndpoint() {
26+
return endpoint;
27+
}
28+
29+
public String getKey() {
30+
return key;
31+
}
32+
33+
public void setEndpoint(String endpoint) {
34+
this.endpoint = endpoint;
35+
}
36+
37+
public void setKey(String key) {
38+
this.key = key;
39+
}
40+
41+
public String getDeploymentName() {
42+
return deploymentName;
43+
}
44+
45+
public void setDeploymentName(String deploymentName) {
46+
this.deploymentName = deploymentName;
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
client.azureopenai.key=yourkey
2+
client.azureopenai.endpoint=yourendpoint
3+
client.azureopenai.deploymentname=davinci-002
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>semantic-kernel-spring-boot</artifactId>
6+
<groupId>semantickernel-spring-boot-starter</groupId>
7+
<name>Semantic Kernel Spring Boot - Starter</name>
8+
<version>1.0.0-SNAPSHOT</version>
9+
<dependencies>
10+
<dependency>
11+
<groupId>org.projectlombok</groupId>
12+
<artifactId>lombok</artifactId>
13+
<optional>true</optional>
14+
</dependency>
15+
<!-- Spring -->
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-web</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-configuration-processor</artifactId>
23+
<optional>true</optional>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-test</artifactId>
28+
<scope>test</scope>
29+
</dependency>
30+
<!-- Microsoft Semantic Kernel -->
31+
<dependency>
32+
<groupId>com.microsoft.semantic-kernel</groupId>
33+
<artifactId>semantickernel-core</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.microsoft.semantic-kernel</groupId>
37+
<artifactId>semantickernel-connectors-ai-openai</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.microsoft.semantic-kernel</groupId>
41+
<artifactId>semantickernel-settings-loader</artifactId>
42+
</dependency>
43+
44+
</dependencies>
45+
</project>

0 commit comments

Comments
 (0)