-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53d4bf7
commit c1f4ef8
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
...el-spring-boot-autoconfigure/src/main/java/azure/openai/AzureOpenAIAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
...spring-boot-autoconfigure/src/main/java/azure/openai/AzureOpenAiConnectionProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
...tarter/semantickernel-spring-boot-autoconfigure/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
45 changes: 45 additions & 0 deletions
45
java/semantickernel-spring-boot-starter/semantickernel-spring-boot-autoconfigure/src/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |