From 1b2e2fbd5065284c840ae50e7ae3b81182594e82 Mon Sep 17 00:00:00 2001 From: SandraAhlgrimm Date: Tue, 13 Feb 2024 14:20:17 +0000 Subject: [PATCH] Squashed commit of the following: commit 538774d7c9ce790727d1056b1fbd16d5e8d5c1c2 Merge: 1a5f7447c dcd7e1424 Author: Sandra Ahlgrimm Date: Thu Feb 8 08:50:21 2024 +0100 Merge branch 'main' into update-modelid commit 1a5f7447cafc952e358b5db5515c4157ecb3379b Merge: 699eec828 7e431953e Author: Sandra Ahlgrimm Date: Wed Feb 7 17:35:02 2024 +0100 Merge branch 'main' into update-modelid commit 699eec828de2664f5bbdca60e3d0c54d1ee1505a Merge: 60cf261f8 7e431953e Author: Sandra Ahlgrimm Date: Wed Feb 7 10:30:34 2024 +0100 Merge branch 'main' into update-modelid commit 60cf261f8ad1aa07fef92e973aeee9d6f98560e5 Merge: 2f726c2ab 3a7d3bdc5 Author: Sandra Ahlgrimm Date: Wed Feb 7 10:22:51 2024 +0100 Merge branch 'main' into update-modelid commit 2f726c2ab012e56c7ff0c59b8a8a5033068c31e3 Merge: 5bdbbd6e4 59508cab6 Author: John Oliver <1615532+johnoliver@users.noreply.github.com> Date: Tue Feb 6 13:02:30 2024 +0000 Merge branch 'main' into update-modelid commit 5bdbbd6e494693308c9ec939a9bc926bafce00fe Merge: ffcf68cb6 eed51932f Author: Sandra Ahlgrimm Date: Fri Feb 2 22:44:25 2024 +0100 Merge branch 'main' into update-modelid commit ffcf68cb6de6cbb7d229e18f8b5171a7b451c461 Merge: 3a34f5db1 52e317a79 Author: Sandra Ahlgrimm Date: Fri Feb 2 12:15:47 2024 +0100 Merge branch 'main' into update-modelid commit 3a34f5db192e4f6ace75c0fd48b59b1126835c53 Merge: 7933961f6 e58295123 Author: SandraAhlgrimm Date: Fri Feb 2 12:15:13 2024 +0100 Merge remote-tracking branch 'origin/update-modelid' into update-modelid commit 7933961f6c91ec76e42e14f79a24583f0f67f637 Author: SandraAhlgrimm Date: Fri Feb 2 12:14:50 2024 +0100 make Deployment Name optional and set modelid with default as requested in review commit b74843e02ed1d8a197e6f1bf8680e6dec22bc4cb Author: SandraAhlgrimm Date: Fri Feb 2 12:13:39 2024 +0100 make Deployment Name optional and set modelid with default as requested in review commit e582951234622836c2e5a48455c512ab08647d94 Merge: f7f1664b0 5953beb8b Author: Sandra Ahlgrimm Date: Fri Jan 26 22:56:33 2024 +0100 Merge branch 'main' into update-modelid commit f7f1664b0420fbc5061c8ad902c84e94d697144e Author: SandraAhlgrimm Date: Fri Jan 26 22:51:38 2024 +0100 spring boot starter gets tested with samples commit be6cf95226169c1877f8417df9bea7065461dcc2 Author: SandraAhlgrimm Date: Fri Jan 26 12:46:23 2024 +0100 Need to use JDK 11 to build for JDK 8 commit 1af0f61ddd93f9e977c51ed80416a578eb31d283 Author: SandraAhlgrimm Date: Fri Jan 26 12:43:16 2024 +0100 Need to use JDK 11 to build for JDK 8 commit 5bebe6c43f3040848e63437861f593f7450c9e15 Author: SandraAhlgrimm Date: Fri Jan 26 12:24:46 2024 +0100 move starter to example package commit 2f8a8398f4dbef45d2b70625bcebe65a61d33ebd Author: SandraAhlgrimm Date: Fri Jan 26 10:14:20 2024 +0100 switch back to jdk 8 and 17 as required by PM commit 9fdf375fb3bfae3080b13ecebd4d6882270558b1 Merge: 1e66ccb52 038ca4554 Author: SandraAhlgrimm Date: Fri Jan 26 10:08:32 2024 +0100 Merge branch 'main' of https://github.com/SandraAhlgrimm/semantic-kernel into update-modelid # Conflicts: # .github/workflows/java-build.yml commit 038ca4554aac4e106600098db17d4a44d6366fba Merge: 21d161af6 e0ccee0e1 Author: Sandra Ahlgrimm Date: Fri Jan 26 10:03:52 2024 +0100 Merge branch 'microsoft:main' into main commit 21d161af6c1c7b28c530b12a0783d70694a51b0c Merge: a40202602 44453276a Author: Sandra Ahlgrimm Date: Fri Jan 26 09:31:59 2024 +0100 Merge pull request #1 from bbenz/main bbenz Update java-build.yml commit a40202602de3b183da91824b672d5fe78d4fe529 Author: Sandra Ahlgrimm Date: Fri Jan 26 09:31:19 2024 +0100 Update java-build.yml commit 44453276a9cbcb91d2f29f6916f8d763c465e89a Author: Brian Benz Date: Thu Jan 25 16:07:29 2024 -0800 bbenz Update java-build.yml Set JDK to 11 or 17 commit 1e66ccb5283bd656388c955a2b79e400b80f6e66 Author: Sandra Ahlgrimm Date: Thu Jan 25 21:32:07 2024 +0100 Update java-build.yml update workflow as @bbenz suggested commit b69342a813593c49a7f4caadfd9f9440f335b04c Merge: 79627be66 e0ccee0e1 Author: Sandra Ahlgrimm Date: Thu Jan 25 21:30:42 2024 +0100 Merge branch 'microsoft:main' into update-modelid commit 79627be66aeb9ff6177281dea8bcf0391fafe934 Author: SandraAhlgrimm Date: Thu Jan 25 18:22:56 2024 +0100 jdk 17 for spring boot starter commit 14e17ad953b774297d24d3bb236d0f27590d11dc Merge: 4570ad288 00fbab2d3 Author: SandraAhlgrimm Date: Wed Jan 24 21:41:47 2024 +0100 Merge remote-tracking branch 'origin/update-modelid' into update-modelid commit 4570ad28895fc8d7d756a1a25e65f5d3d257540a Author: SandraAhlgrimm Date: Wed Jan 24 21:41:14 2024 +0100 cleaned up and ready for PR commit 00fbab2d3e01a1efc40abd5cabf9073c317f8c9b Author: Sandra Ahlgrimm Date: Wed Jan 24 21:39:57 2024 +0100 Update java-integration-tests.yml commit f5baf9ee28c9e2c53aef1d67d6da1f82edecdf1b Merge: f76a4b014 f95925622 Author: Sandra Ahlgrimm Date: Wed Jan 24 21:33:28 2024 +0100 Merge branch 'microsoft:main' into update-modelid commit f76a4b014d16370736e6dc1008541a36ac1ad9e0 Merge: a8cf41e5e e4874bb79 Author: Sandra Ahlgrimm Date: Mon Jan 22 16:08:03 2024 +0100 Merge branch 'microsoft:main' into update-modelid commit a8cf41e5ef73d79d0e72020a59b59d3528863930 Author: SandraAhlgrimm Date: Mon Jan 22 16:03:22 2024 +0100 cleaned up and ready for PR commit 8d3d26afc80487d5ed176633291fa3697dd93fa7 Author: SandraAhlgrimm Date: Thu Jan 18 22:43:40 2024 +0100 maven still needs a cleanup commit f811bc3edfb93b10f79f81996b7281d7c182fc64 Merge: 43b0a7f7c c87e45e52 Author: SandraAhlgrimm Date: Wed Jan 17 22:27:39 2024 +0100 Merge remote-tracking branch 'origin/update-modelid' into update-modelid commit c87e45e52ffd4685b1b4d47b5b0a174f9bbdd26d Merge: 355eb6876 fe23d4161 Author: Sandra Ahlgrimm Date: Wed Jan 17 22:20:29 2024 +0100 Merge branch 'microsoft:main' into update-modelid commit 43b0a7f7cfac25a414e41203556828fd3bfcf6ea Author: SandraAhlgrimm Date: Wed Jan 17 22:19:31 2024 +0100 add tests, maven needs a cleanup commit 355eb68765550591afc80f2b77c27f13c2c5cc99 Merge: 4277528ea f9bef6375 Author: Sandra Ahlgrimm Date: Tue Jan 16 15:40:51 2024 +0100 Merge branch 'microsoft:main' into update-modelid commit 4277528ea08460b66a925631a70110a52e3df1a7 Merge: f2cab027e 9db47e561 Author: Sandra Ahlgrimm Date: Mon Jan 8 14:22:56 2024 +0100 Merge branch 'microsoft:main' into update-modelid commit f2cab027ecbd14f45483e144e8324805859d18ef Author: SandraAhlgrimm Date: Fri Jan 5 16:58:21 2024 +0100 update maven dependencies, test needs to be finished commit ec31872597c7d6fdbfdbfde13ff159a27fedf6af Author: SandraAhlgrimm Date: Fri Jan 5 15:52:49 2024 +0100 add spring boot starter commit ff87caa4ed79e8baadc7e6e06603ed8ffe80d932 Merge: c1ccfe087 11369bf28 Author: SandraAhlgrimm Date: Fri Jan 5 13:16:21 2024 +0100 Merge remote-tracking branch 'origin/update-modelid' into update-modelid commit c1ccfe087460bb486d2342f36efe689a8f135778 Author: SandraAhlgrimm Date: Fri Jan 5 13:16:08 2024 +0100 add spring boot starter commit 11369bf2899841eaa382a5dc75c362a1234e731d Merge: ac73cb85e 833a2d7b8 Author: Sandra Ahlgrimm Date: Fri Jan 5 13:13:37 2024 +0100 Merge branch 'microsoft:main' into update-modelid commit ac73cb85ee975b5eb9fcfc16f0b0d93b71f37cf7 Merge: c1f4ef8fb 4b9dbed30 Author: SandraAhlgrimm Date: Fri Jan 5 13:11:50 2024 +0100 Merge branch 'update-modelid' of https://github.com/SandraAhlgrimm/semantic-kernel into update-modelid commit c1f4ef8fb1e55718a1616886516c9a0fcaa52de5 Author: SandraAhlgrimm Date: Fri Jan 5 13:06:20 2024 +0100 add spring boot starter commit 4b9dbed305cff9b5b6fef5526ac08442127f19fe Merge: 9da9b386a aa62b14d7 Author: John Oliver <1615532+johnoliver@users.noreply.github.com> Date: Thu Jan 4 12:46:38 2024 +0000 Merge branch 'main' into update-modelid commit 53d4bf7f5080ce225db914a1d642ba12a4b2e705 Author: SandraAhlgrimm Date: Wed Jan 3 20:14:32 2024 +0100 add spring boot starter commit 9da9b386ad62d2893ae2672ec11f1301bf583126 Merge: a3782cfc6 79908b0cf Author: Sandra Ahlgrimm Date: Wed Jan 3 13:24:58 2024 +0100 Merge branch 'main' into update-modelid commit a3782cfc6fb08d5f847594460bc7a422c17739a5 Merge: a6930990b 240a2cbea Author: Sandra Ahlgrimm Date: Fri Dec 22 11:40:14 2023 +0100 Merge branch 'microsoft:main' into update-modelid commit a6930990bea41e686cffa01174d6bb277ddca993 Author: SandraAhlgrimm Date: Fri Dec 22 11:38:59 2023 +0100 update modelid to the available davinci-002 on Azure Open AI --- .github/workflows/java-build.yml | 14 +- java/samples/pom.xml | 1 + ...mple04_CombineLLMPromptsAndNativeCode.java | 58 +++---- .../Example05_InlineFunctionDefinition.java | 2 +- .../Example06_TemplateLanguage.java | 152 +++++++++--------- .../Example08_RetryHandler.java | 2 +- .../Example09_FunctionTypes.java | 2 +- .../Example13_ConversationSummarySkill.java | 4 +- .../syntaxexamples/Example15_MemorySkill.java | 2 +- .../Example51_StepwisePlanner.java | 2 +- .../SequentialPlanner_AnswerQuestion.java | 2 +- .../semantickernel-spring-starter/pom.xml | 77 +++++++++ .../AzureOpenAIConnectionProperties.java | 51 ++++++ .../SemanticKernelAutoConfiguration.java | 78 +++++++++ .../src/main/resources/application.properties | 3 + .../AzureOpenAIConnectionPropertiesTest.java | 39 +++++ .../SemanticKernelAutoConfigurationTest.java | 37 +++++ .../src/test/resources/application.properties | 3 + .../src/test/resources/log4j2.xml | 13 ++ 19 files changed, 428 insertions(+), 114 deletions(-) create mode 100644 java/samples/semantickernel-spring-starter/pom.xml create mode 100644 java/samples/semantickernel-spring-starter/src/main/java/com/microsoft/semantickernel/starter/AzureOpenAIConnectionProperties.java create mode 100644 java/samples/semantickernel-spring-starter/src/main/java/com/microsoft/semantickernel/starter/SemanticKernelAutoConfiguration.java create mode 100644 java/samples/semantickernel-spring-starter/src/main/resources/application.properties create mode 100644 java/samples/semantickernel-spring-starter/src/test/java/com/microsoft/semantickernel/azureopenai/AzureOpenAIConnectionPropertiesTest.java create mode 100644 java/samples/semantickernel-spring-starter/src/test/java/com/microsoft/semantickernel/azureopenai/SemanticKernelAutoConfigurationTest.java create mode 100644 java/samples/semantickernel-spring-starter/src/test/resources/application.properties create mode 100644 java/samples/semantickernel-spring-starter/src/test/resources/log4j2.xml diff --git a/.github/workflows/java-build.yml b/.github/workflows/java-build.yml index e5ca4090e364..6971515678f4 100644 --- a/.github/workflows/java-build.yml +++ b/.github/workflows/java-build.yml @@ -1,5 +1,7 @@ name: Build Java Semantic Kernel +# Triggers the workflow on manual dispatch, push, and pull request events +# for the specified branches and paths on: workflow_dispatch: push: @@ -15,6 +17,7 @@ permissions: contents: read jobs: + # Determines if Java files have changed paths-filter: runs-on: ubuntu-latest outputs: @@ -28,14 +31,14 @@ jobs: java: - 'java/**' - '**/java/**' - # run only if 'java' files were changed - name: java tests if: steps.filter.outputs.java == 'true' run: echo "Java file" - # run only if not 'java' files were changed - name: not java tests if: steps.filter.outputs.java != 'true' run: echo "NOT java file" + + # Builds and tests the Java project java-build: runs-on: ubuntu-latest needs: paths-filter @@ -43,6 +46,7 @@ jobs: strategy: fail-fast: false matrix: + # Defines a matrix strategy for JDK versions 8 and 17 java-versions: [8, 17] name: Java CI on JDK${{ matrix.java-versions }} @@ -63,20 +67,24 @@ jobs: echo "JDK_VERSION=$version" >> $GITHUB_OUTPUT fi - - uses: actions/setup-java@v3 + # Sets up the specified JDK version from the matrix + uses: actions/setup-java@v3 with: java-version: ${{ steps.set-jdk.outputs.JDK_VERSION }} distribution: microsoft cache: maven + # Builds the project with Maven using the matrix JDK version - name: Build with Maven run: ./mvnw -B -Pbug-check -DskipTests -Pcompile-jdk${{ matrix.java-versions }} clean install --file pom.xml working-directory: java + # Runs tests with Maven using the matrix JDK version - name: Run tests run: ./mvnw -B -Pbug-check -Pcompile-jdk${{ matrix.java-versions }} test --file pom.xml working-directory: java + # Uploads test artifacts for each JDK version - uses: actions/upload-artifact@v2 if: always() with: diff --git a/java/samples/pom.xml b/java/samples/pom.xml index 70134910a9f9..b6fd6270402a 100644 --- a/java/samples/pom.xml +++ b/java/samples/pom.xml @@ -12,6 +12,7 @@ sample-code semantickernel-samples-guice + semantickernel-spring-starter diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example04_CombineLLMPromptsAndNativeCode.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example04_CombineLLMPromptsAndNativeCode.java index 4cd9f86132b0..4ba4e3dd3fb1 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example04_CombineLLMPromptsAndNativeCode.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example04_CombineLLMPromptsAndNativeCode.java @@ -21,38 +21,42 @@ */ public class Example04_CombineLLMPromptsAndNativeCode { - public static class SearchEngineSkill { - - @DefineSKFunction(description = "Search for answer", name = "search") - public Mono search( - @SKFunctionInputAttribute(description = "Text to search") - String input) { - return Mono.just("Gran Torre Santiago is the tallest building in South America"); + public static class SearchEngineSkill { + + @DefineSKFunction(description = "Search for answer", name = "search") + public Mono search( + @SKFunctionInputAttribute(description = "Text to search") + String input) { + return Mono.just("Gran Torre Santiago is the tallest building in South America"); + } } - } - public static void main(String[] args) throws ConfigurationException { - OpenAIAsyncClient client = SamplesConfig.getClient(); + public static void main(String[] args) throws ConfigurationException { + OpenAIAsyncClient client = SamplesConfig.getClient(); + + TextCompletion textCompletion = SKBuilders.textCompletion() + .withModelId("davinci-002") + .withOpenAIClient(client) + .build(); - TextCompletion textCompletion = SKBuilders.chatCompletion() - .withModelId("gpt-35-turbo-2") - .withOpenAIClient(client) - .build(); + Kernel kernel = SKBuilders.kernel().withDefaultAIService(textCompletion).build(); + kernel.importSkill(new SearchEngineSkill(), null); + kernel.importSkillFromDirectory("SummarizeSkill", SampleSkillsUtil.detectSkillDirLocation(), + "SummarizeSkill"); - Kernel kernel = SKBuilders.kernel().withDefaultAIService(textCompletion).build(); - kernel.importSkill(new SearchEngineSkill(), null); - kernel.importSkillFromDirectory("SummarizeSkill", SampleSkillsUtil.detectSkillDirLocation(), - "SummarizeSkill"); - // Run - String ask = "What's the tallest building in South America?"; + // Run + String ask = "What's the tallest building in South America?"; - Mono result = - kernel.runAsync( - ask, - kernel.getSkills().getFunction("Search", null), - kernel.getSkill("SummarizeSkill").getFunction("Summarize", null)); + Mono result = + kernel.runAsync(ask, kernel.getSkills().getFunction("Search", null)); - System.out.println(result.block().getResult()); - } + result = + kernel.runAsync( + ask, + kernel.getSkills().getFunction("Search", null), + kernel.getSkill("SummarizeSkill").getFunction("Summarize", null)); + + System.out.println(result.block().getResult()); + } } diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example05_InlineFunctionDefinition.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example05_InlineFunctionDefinition.java index 5d95d8ab597c..508ad18cd156 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example05_InlineFunctionDefinition.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example05_InlineFunctionDefinition.java @@ -24,7 +24,7 @@ public static void main(String[] args) throws ConfigurationException { OpenAIAsyncClient client = SamplesConfig.getClient(); TextCompletion textCompletion = SKBuilders.textCompletion() - .withModelId("text-davinci-003") + .withModelId("davinci-002") .withOpenAIClient(client) .build(); diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example06_TemplateLanguage.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example06_TemplateLanguage.java index 80fdb4efaec0..a37e900961b1 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example06_TemplateLanguage.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example06_TemplateLanguage.java @@ -11,8 +11,8 @@ import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig; /** - * Show how to invoke a Native Function written in Java from a Semantic Function written in natural - * language + * Show how to invoke a Native Function written in Java + * from a Semantic Function written in natural language *

* Refer to the @@ -20,89 +20,89 @@ */ public class Example06_TemplateLanguage { - public static void main(String[] args) throws ConfigurationException { - System.out.println("======== TemplateLanguage ========"); + public static void main(String[] args) throws ConfigurationException { + System.out.println("======== TemplateLanguage ========"); - OpenAIAsyncClient client = SamplesConfig.getClient(); + OpenAIAsyncClient client = SamplesConfig.getClient(); - Kernel kernel = SKBuilders.kernel() - .withDefaultAIService(SKBuilders.textCompletion() - .withModelId("text-davinci-003") - .withOpenAIClient(client) - .build()) - .build(); + Kernel kernel = SKBuilders.kernel() + .withDefaultAIService(SKBuilders.textCompletion() + .withModelId("davinci-002") + .withOpenAIClient(client) + .build()) + .build(); - // Load native skill into the kernel skill collection, sharing its functions - // with prompt templates - // Functions loaded here are available as "time.*" - kernel.importSkill(new TimeSkill(), "time"); + // Load native skill into the kernel skill collection, sharing its functions + // with prompt templates + // Functions loaded here are available as "time.*" + kernel.importSkill(new TimeSkill(), "time"); - // Semantic Function invoking time.Date and time.Time native functions - String functionDefinition = """ - Today is: {{time.Date}} - Current time is: {{time.Time}} + // Semantic Function invoking time.Date and time.Time native functions + String functionDefinition = """ + Today is: {{time.Date}} + Current time is: {{time.Time}} - Answer to the following questions using JSON syntax, including the data used. - Is it morning, afternoon, evening, or night (morning/afternoon/evening/night)? - Is it weekend time (weekend/not weekend)? - """; + Answer to the following questions using JSON syntax, including the data used. + Is it morning, afternoon, evening, or night (morning/afternoon/evening/night)? + Is it weekend time (weekend/not weekend)? + """; - // This allows to see the prompt before it's sent to OpenAI - System.out.println("--- Rendered Prompt"); + // This allows to see the prompt before it's sent to OpenAI + System.out.println("--- Rendered Prompt"); - var promptRenderer = SKBuilders.promptTemplate() - .withPromptTemplateConfig(new PromptTemplateConfig()) - .withPromptTemplate(functionDefinition) - .withPromptTemplateEngine(kernel.getPromptTemplateEngine()) - .build(); + var promptRenderer = SKBuilders.promptTemplate() + .withPromptTemplateConfig(new PromptTemplateConfig()) + .withPromptTemplate(functionDefinition) + .withPromptTemplateEngine(kernel.getPromptTemplateEngine()) + .build(); - SKContext skContext = SKBuilders - .context() - .withSkills(kernel.getSkills()) - .build(); + SKContext skContext = SKBuilders + .context() + .withSkills(kernel.getSkills()) + .build(); - var renderedPrompt = promptRenderer.renderAsync(skContext); - System.out.println(renderedPrompt.block()); + var renderedPrompt = promptRenderer.renderAsync(skContext); + System.out.println(renderedPrompt.block()); - // Run the prompt / semantic function - var kindOfDay = kernel - .getSemanticFunctionBuilder() - .withPromptTemplate(functionDefinition) - .withRequestSettings( - SKBuilders.completionRequestSettings() - .temperature(0) - .topP(0) - .maxTokens(256) - .frequencyPenalty(0) - .presencePenalty(0) - .build()) - .build(); + // Run the prompt / semantic function + var kindOfDay = kernel + .getSemanticFunctionBuilder() + .withPromptTemplate(functionDefinition) + .withRequestSettings( + SKBuilders.completionRequestSettings() + .temperature(0) + .topP(0) + .maxTokens(256) + .frequencyPenalty(0) + .presencePenalty(0) + .build()) + .build(); - // Show the result - System.out.println("--- Semantic Function result"); - var result = kindOfDay.invokeAsync("").block().getResult(); - System.out.println(result); - /* - * OUTPUT: - * - * --- Rendered Prompt - * - * Today is: Friday, April 28, 2023 - * Current time is: 11:04:30 PM - * - * Answer to the following questions using JSON syntax, including the data used. - * Is it morning, afternoon, evening, or night - * (morning/afternoon/evening/night)? - * Is it weekend time (weekend/not weekend)? - * - * --- Semantic Function result - * - * { - * "date": "Friday, April 28, 2023", - * "time": "11:04:30 PM", - * "period": "night", - * "weekend": "weekend" - * } - */ - } + // Show the result + System.out.println("--- Semantic Function result"); + var result = kindOfDay.invokeAsync("").block().getResult(); + System.out.println(result); + /* + * OUTPUT: + * + * --- Rendered Prompt + * + * Today is: Friday, April 28, 2023 + * Current time is: 11:04:30 PM + * + * Answer to the following questions using JSON syntax, including the data used. + * Is it morning, afternoon, evening, or night + * (morning/afternoon/evening/night)? + * Is it weekend time (weekend/not weekend)? + * + * --- Semantic Function result + * + * { + * "date": "Friday, April 28, 2023", + * "time": "11:04:30 PM", + * "period": "night", + * "weekend": "weekend" + * } + */ + } } diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example08_RetryHandler.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example08_RetryHandler.java index 0b12de343e0a..bf3c573b67e0 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example08_RetryHandler.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example08_RetryHandler.java @@ -75,7 +75,7 @@ public static void main(String[] args) throws ConfigurationException { String prompt = "{{$input}}\nSummarize the content above."; TextCompletion textCompletion = SKBuilders.textCompletion() - .withModelId("text-davinci-003") + .withModelId("davinci-002") .withOpenAIClient(client) .build(); diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example09_FunctionTypes.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example09_FunctionTypes.java index a7b9be5fbf1e..ed222b68ccdb 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example09_FunctionTypes.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example09_FunctionTypes.java @@ -25,7 +25,7 @@ public static void main(String[] args) throws ConfigurationException { OpenAIAsyncClient client = SamplesConfig.getClient(); TextCompletion textCompletion = SKBuilders.textCompletion() - .withModelId("text-davinci-003") + .withModelId("davinci-002") .withOpenAIClient(client) .build(); diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example13_ConversationSummarySkill.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example13_ConversationSummarySkill.java index f3bccae54256..0e876411b01c 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example13_ConversationSummarySkill.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example13_ConversationSummarySkill.java @@ -186,11 +186,11 @@ private static void getConversationTopicsAsync(OpenAIAsyncClient client) { private static Kernel initializeKernel(OpenAIAsyncClient client) { TextCompletion textCompletion = SKBuilders.textCompletion() - .withModelId("text-davinci-003") + .withModelId("davinci-002") .withOpenAIClient(client) .build(); return SKBuilders.kernel() - .withAIService("text-davinci-003", textCompletion, true, TextCompletion.class).build(); + .withAIService("davinci-002", textCompletion, true, TextCompletion.class).build(); } } diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example15_MemorySkill.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example15_MemorySkill.java index a049e8de78ad..cbddc3f4fa00 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example15_MemorySkill.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example15_MemorySkill.java @@ -43,7 +43,7 @@ public static Mono runAsync() { TextCompletion textCompletionService = SKBuilders.textCompletion() - .withModelId("text-davinci-003") + .withModelId("davinci-002") .withOpenAIClient(client) .build(); diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example51_StepwisePlanner.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example51_StepwisePlanner.java index d717d090dbc1..b869872b3bcb 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example51_StepwisePlanner.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example51_StepwisePlanner.java @@ -121,7 +121,7 @@ private static Kernel getKernel(boolean useChat) throws ConfigurationException { } else { textCompletion = SKBuilders.textCompletion() .withOpenAIClient(client) - .withModelId("text-davinci-003") + .withModelId("davinci-002") .build(); } diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/javaspecific/SequentialPlanner_AnswerQuestion.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/javaspecific/SequentialPlanner_AnswerQuestion.java index 61ecb1f57aa0..b5f95714fef0 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/javaspecific/SequentialPlanner_AnswerQuestion.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/javaspecific/SequentialPlanner_AnswerQuestion.java @@ -45,7 +45,7 @@ public static void main(String[] args) throws IOException, ConfigurationExceptio OpenAIAsyncClient client = SamplesConfig.getClient(); var kernel = SKBuilders.kernel() .withDefaultAIService(SKBuilders.textCompletion() - .withModelId("text-davinci-003") + .withModelId("davinci-002") .withOpenAIClient(client) .build()) .build(); diff --git a/java/samples/semantickernel-spring-starter/pom.xml b/java/samples/semantickernel-spring-starter/pom.xml new file mode 100644 index 000000000000..e071f3837f79 --- /dev/null +++ b/java/samples/semantickernel-spring-starter/pom.xml @@ -0,0 +1,77 @@ + + + 4.0.0 + + com.microsoft.semantic-kernel + samples + 0.2.10-alpha-SNAPSHOT + ../pom.xml + + + semantickernel-spring-starter + Semantic Kernel Spring Boot Starter + + + + org.slf4j + slf4j-api + + + org.springframework.boot + spring-boot-test + 3.2.1 + test + + + org.assertj + assertj-core + 3.25.1 + test + + + org.springframework.boot + spring-boot-autoconfigure + 3.2.1 + + + org.springframework.boot + spring-boot + 3.2.1 + + + com.azure + azure-ai-openai + 1.0.0-beta.6 + + + com.microsoft.semantic-kernel + semantickernel-core + test + + + com.microsoft.semantic-kernel + semantickernel-connectors-ai-openai + test + + + org.springframework + spring-test + 6.1.2 + test + + + com.azure + azure-identity + 1.9.1 + + + org.junit.jupiter + junit-jupiter-api + 5.10.1 + test + + + + \ No newline at end of file diff --git a/java/samples/semantickernel-spring-starter/src/main/java/com/microsoft/semantickernel/starter/AzureOpenAIConnectionProperties.java b/java/samples/semantickernel-spring-starter/src/main/java/com/microsoft/semantickernel/starter/AzureOpenAIConnectionProperties.java new file mode 100644 index 000000000000..cf23fc8b70c4 --- /dev/null +++ b/java/samples/semantickernel-spring-starter/src/main/java/com/microsoft/semantickernel/starter/AzureOpenAIConnectionProperties.java @@ -0,0 +1,51 @@ +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"; + + /** + * 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; + } +} diff --git a/java/samples/semantickernel-spring-starter/src/main/java/com/microsoft/semantickernel/starter/SemanticKernelAutoConfiguration.java b/java/samples/semantickernel-spring-starter/src/main/java/com/microsoft/semantickernel/starter/SemanticKernelAutoConfiguration.java new file mode 100644 index 000000000000..df35c1305f56 --- /dev/null +++ b/java/samples/semantickernel-spring-starter/src/main/java/com/microsoft/semantickernel/starter/SemanticKernelAutoConfiguration.java @@ -0,0 +1,78 @@ +package com.microsoft.semantickernel.starter; + +import com.azure.ai.openai.OpenAIAsyncClient; +import com.azure.ai.openai.OpenAIClientBuilder; +import com.azure.core.credential.AzureKeyCredential; +import com.microsoft.semantickernel.Kernel; +import com.microsoft.semantickernel.SKBuilders; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +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 SemanticKernelAutoConfiguration { + + private static final Logger LOGGER = LoggerFactory.getLogger(SemanticKernelAutoConfiguration.class); + + /** + * 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(); + } + + /** + * 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, + AzureOpenAIConnectionProperties connectionProperties) { + return SKBuilders.kernel() + .withDefaultAIService( + SKBuilders.textCompletion() + .withModelId(setModelID(connectionProperties)) + .withOpenAIClient(client) + .build()) + .build(); + } + + private static String setModelID(AzureOpenAIConnectionProperties connectionProperties) { + String modelId; + if (connectionProperties.getDeploymentName() == null) { + modelId = "text-davinci-003"; + LOGGER.warn( + "No deployment name specified, using default model id: " + modelId); + } else { + modelId = connectionProperties.getDeploymentName(); + LOGGER.info("Using model id: " + modelId); + } + return modelId; + } +} diff --git a/java/samples/semantickernel-spring-starter/src/main/resources/application.properties b/java/samples/semantickernel-spring-starter/src/main/resources/application.properties new file mode 100644 index 000000000000..93406a1e91ac --- /dev/null +++ b/java/samples/semantickernel-spring-starter/src/main/resources/application.properties @@ -0,0 +1,3 @@ +client.azureopenai.key=yourkey +client.azureopenai.endpoint=yourendpoint +client.azureopenai.deploymentname=davinci-002 \ No newline at end of file diff --git a/java/samples/semantickernel-spring-starter/src/test/java/com/microsoft/semantickernel/azureopenai/AzureOpenAIConnectionPropertiesTest.java b/java/samples/semantickernel-spring-starter/src/test/java/com/microsoft/semantickernel/azureopenai/AzureOpenAIConnectionPropertiesTest.java new file mode 100644 index 000000000000..76870b8ff245 --- /dev/null +++ b/java/samples/semantickernel-spring-starter/src/test/java/com/microsoft/semantickernel/azureopenai/AzureOpenAIConnectionPropertiesTest.java @@ -0,0 +1,39 @@ +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 { + + 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)); + + @Test + public void ConnectionPropertiesTest() { + contextRunner.run( + context -> { + AzureOpenAIConnectionProperties props = + context.getBean(AzureOpenAIConnectionProperties.class); + assertNotNull(props.getEndpoint()); + assertNotNull(props.getKey()); + assertNotNull(props.getDeploymentName()); + }); + } +} diff --git a/java/samples/semantickernel-spring-starter/src/test/java/com/microsoft/semantickernel/azureopenai/SemanticKernelAutoConfigurationTest.java b/java/samples/semantickernel-spring-starter/src/test/java/com/microsoft/semantickernel/azureopenai/SemanticKernelAutoConfigurationTest.java new file mode 100644 index 000000000000..c4bec2367b6f --- /dev/null +++ b/java/samples/semantickernel-spring-starter/src/test/java/com/microsoft/semantickernel/azureopenai/SemanticKernelAutoConfigurationTest.java @@ -0,0 +1,37 @@ +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" + // @formatter:on + ); + runner.withConfiguration(AutoConfigurations.of(SemanticKernelAutoConfiguration.class)); + runner.run( + context -> { + assertNotNull(kernel); + }); + } +} diff --git a/java/samples/semantickernel-spring-starter/src/test/resources/application.properties b/java/samples/semantickernel-spring-starter/src/test/resources/application.properties new file mode 100644 index 000000000000..93406a1e91ac --- /dev/null +++ b/java/samples/semantickernel-spring-starter/src/test/resources/application.properties @@ -0,0 +1,3 @@ +client.azureopenai.key=yourkey +client.azureopenai.endpoint=yourendpoint +client.azureopenai.deploymentname=davinci-002 \ No newline at end of file diff --git a/java/samples/semantickernel-spring-starter/src/test/resources/log4j2.xml b/java/samples/semantickernel-spring-starter/src/test/resources/log4j2.xml new file mode 100644 index 000000000000..50a638f0dc9b --- /dev/null +++ b/java/samples/semantickernel-spring-starter/src/test/resources/log4j2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file