From 8497b4d0b6b28bccbd9b9f252c4564250fb72b62 Mon Sep 17 00:00:00 2001 From: John Oliver <1615532+johnoliver@users.noreply.github.com> Date: Thu, 10 Aug 2023 18:11:53 +0100 Subject: [PATCH] Java: Apply proposed builder refactor to make them more consistent (#2270) Apply proposed Builder updates to make them more consistent --- .../src/main/java/InlineFunctionExample.java | 77 +++--- ...nlineFunctionWithPreBuiltSkillExample.java | 22 +- .../e2e/AbstractKernelTest.java | 7 +- .../e2e/ContextVariableFunctionTest.java | 15 +- .../e2e/InlineFunctionTest.java | 11 +- .../semantickernel/e2e/KernelTest.java | 13 +- .../e2e/TextEmbeddingsTest.java | 14 +- .../sequentialplanner/SequentialPlanTest.java | 2 +- .../chatcompletion/OpenAIChatCompletion.java | 26 ++- .../textcompletion/OpenAITextCompletion.java | 24 +- .../OpenAITextEmbeddingGeneration.java | 22 +- .../connectors/memory/sqlite/Database.java | 6 +- .../memory/sqlite/SQLiteMemoryStore.java | 4 +- .../planner/actionplanner/ActionPlanner.java | 19 +- .../planner/actionplanner/Plan.java | 29 +-- .../SequentialPlanParser.java | 2 +- .../sequentialplanner/SequentialPlanner.java | 21 +- .../coreskills/ConversationSummarySkill.java | 58 +++-- .../coreskills/TextMemorySkill.java | 8 +- java/samples/pom.xml | 2 +- .../Example99_BlogAnnouncement.java | 6 +- .../Example_PlanWithNativeFunctions.java | 6 +- .../samples/Example00_GettingStarted.java | 7 +- .../Example03_SemanticFunctionInline.java | 11 +- .../Example04_ContextVariablesChat.java | 15 +- .../syntaxexamples/Example02_Pipeline.java | 3 +- .../syntaxexamples/Example03_Variables.java | 2 +- ...mple04_CombineLLMPromptsAndNativeCode.java | 9 +- .../Example05_InlineFunctionDefinition.java | 26 ++- .../Example06_TemplateLanguage.java | 27 ++- .../Example08_RetryHandler.java | 23 +- .../Example09_FunctionTypes.java | 10 +- .../Example12_SequentialPlanner.java | 11 +- .../Example13_ConversationSummarySkill.java | 7 +- .../Example14_SemanticMemory.java | 67 +++--- .../syntaxexamples/Example15_MemorySkill.java | 52 +++-- .../syntaxexamples/Example17_ChatGPT.java | 7 +- .../Example28_ActionPlanner.java | 7 +- .../Example33_StreamingChat.java | 7 +- .../samples/syntaxexamples/RunAll.java | 10 +- .../Java_Example01_ResourceLoading.java | 2 +- .../SequentialPlanner_AnswerQuestion.java | 6 +- .../src/test/java/Foo.java | 2 - .../pom.xml | 2 +- .../guice/CompletionFunctionFactory.java | 9 +- .../semantickernel/guice/Config.java | 0 .../guice/SemanticKernelModule.java | 2 +- .../guice/CompletionFunctionExample.java | 0 .../guice/GuiceEmbeddingExample.java | 0 .../com/microsoft/semantickernel/Kernel.java | 124 ++-------- .../{builders => }/SKBuilders.java | 24 +- .../ai/embeddings/EmbeddingGeneration.java | 16 +- .../semantickernel/builders/Buildable.java | 8 + .../builders/BuildersSingleton.java | 47 ++-- .../builders/FunctionBuilders.java | 15 -- .../builders/SemanticKernelBuilder.java | 18 ++ .../builders/ServiceLoadUtil.java | 37 +-- .../chatcompletion/ChatCompletion.java | 20 +- .../extensions/KernelExtensions.java | 16 +- .../semantickernel/memory/MemoryStore.java | 11 +- .../memory/SemanticTextMemory.java | 13 +- .../orchestration/AbstractSKContext.java | 5 +- .../orchestration/AbstractSkFunction.java | 16 +- .../orchestration/ContextVariables.java | 12 +- .../InternalBuildersSingleton.java | 55 ----- .../orchestration/SKContext.java | 25 +- .../semanticfunctions/PromptTemplate.java | 18 +- .../ReadOnlySkillCollection.java | 13 +- .../templateengine/PromptTemplateEngine.java | 13 +- .../textcompletion/CompletionSKFunction.java | 68 ++---- .../textcompletion/TextCompletion.java | 17 +- .../semantickernel/DefaultKernel.java | 213 +++++++++++++++-- .../semantickernel/SkFunctionBuilders.java | 101 +------- .../DefaultCompletionSKFunction.java | 221 ++++++++++-------- .../orchestration/DefaultSKContext.java | 29 +-- .../DefaultSemanticSKFunction.java | 8 +- .../DefaultPromptTemplate.java | 30 ++- .../DefaultPromptTemplateEngine.java | 2 +- ...m.microsoft.semantickernel.Kernel$Builder} | 0 ...extcompletion.CompletionSKFunction$Builder | 1 + ...rnel.textcompletion.TextCompletion$Builder | 1 - .../semantickernel/AiServiceRegistryTest.java | 1 - .../semantickernel/DefaultKernelTest.java | 30 +-- .../OpenAIChatCompletionTest.java | 12 +- .../AzureCognitiveSearchMemoryTests.java | 2 +- .../orchestration/NativeSKFunctionTest.java | 2 +- .../actionplanner/ActionPlannerTest.java | 6 +- .../Example03VariablesTest.java | 2 +- ...e04CombineLLMPromptsAndNativeCodeTest.java | 2 +- .../Example05UsingThePlannerTest.java | 6 +- .../Example06TemplateLanguageTest.java | 26 ++- .../Example12SequentialPlannerTest.java | 6 +- .../syntaxexamples/Example17ChatGPTTest.java | 7 +- .../Example28ActionPlannerTest.java | 6 +- .../Example33StreamingChatTest.java | 7 +- 95 files changed, 1098 insertions(+), 932 deletions(-) delete mode 100644 java/samples/semantickernel-guice/src/test/java/Foo.java rename java/samples/{semantickernel-guice => semantickernel-samples-guice}/pom.xml (96%) rename java/samples/{semantickernel-guice => semantickernel-samples-guice}/src/main/java/com/microsoft/semantickernel/guice/CompletionFunctionFactory.java (79%) rename java/samples/{semantickernel-guice => semantickernel-samples-guice}/src/main/java/com/microsoft/semantickernel/guice/Config.java (100%) rename java/samples/{semantickernel-guice => semantickernel-samples-guice}/src/main/java/com/microsoft/semantickernel/guice/SemanticKernelModule.java (98%) rename java/samples/{semantickernel-guice => semantickernel-samples-guice}/src/test/java/com/microsoft/semantickernel/guice/CompletionFunctionExample.java (100%) rename java/samples/{semantickernel-guice => semantickernel-samples-guice}/src/test/java/com/microsoft/semantickernel/guice/GuiceEmbeddingExample.java (100%) rename java/semantickernel-api/src/main/java/com/microsoft/semantickernel/{builders => }/SKBuilders.java (82%) create mode 100644 java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/Buildable.java delete mode 100644 java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/FunctionBuilders.java create mode 100644 java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/SemanticKernelBuilder.java delete mode 100644 java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/InternalBuildersSingleton.java rename java/semantickernel-core/src/main/resources/META-INF/services/{com.microsoft.semantickernel.Kernel$InternalBuilder => com.microsoft.semantickernel.Kernel$Builder} (100%) create mode 100644 java/semantickernel-core/src/main/resources/META-INF/services/com.microsoft.semantickernel.textcompletion.CompletionSKFunction$Builder delete mode 100644 java/semantickernel-core/src/main/resources/META-INF/services/com.microsoft.semantickernel.textcompletion.TextCompletion$Builder diff --git a/java/api-test/api-usage-example/src/main/java/InlineFunctionExample.java b/java/api-test/api-usage-example/src/main/java/InlineFunctionExample.java index b9868100e832..71787f3b9eed 100644 --- a/java/api-test/api-usage-example/src/main/java/InlineFunctionExample.java +++ b/java/api-test/api-usage-example/src/main/java/InlineFunctionExample.java @@ -3,7 +3,7 @@ import com.azure.ai.openai.OpenAIClientBuilder; import com.azure.core.credential.AzureKeyCredential; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig; import com.microsoft.semantickernel.textcompletion.CompletionSKFunction; import com.microsoft.semantickernel.textcompletion.TextCompletion; @@ -25,35 +25,35 @@ public class InlineFunctionExample { private static final String ENDPOINT; private static final String TEXT_TO_SUMMARIZE = """ - Demo (ancient Greek poet) - From Wikipedia, the free encyclopedia - Demo or Damo (Greek: Δεμώ, Δαμώ; fl. c. AD 200) was a Greek woman of the - Roman period, known for a single epigram, engraved upon the Colossus of - Memnon, which bears her name. She speaks of herself therein as a lyric - poetess dedicated to the Muses, but nothing is known of her life.[1] - Identity - Demo was evidently Greek, as her name, a traditional epithet of Demeter, - signifies. The name was relatively common in the Hellenistic world, in - Egypt and elsewhere, and she cannot be further identified. The date of her - visit to the Colossus of Memnon cannot be established with certainty, but - internal evidence on the left leg suggests her poem was inscribed there at - some point in or after AD 196.[2] - Epigram - There are a number of graffiti inscriptions on the Colossus of Memnon. - Following three epigrams by Julia Balbilla, a fourth epigram, in elegiac - couplets, entitled and presumably authored by "Demo" or "Damo" (the - Greek inscription is difficult to read), is a dedication to the Muses.[2] - The poem is traditionally published with the works of Balbilla, though the - internal evidence suggests a different author.[1] - In the poem, Demo explains that Memnon has shown her special respect. In - return, Demo offers the gift for poetry, as a gift to the hero. At the end - of this epigram, she addresses Memnon, highlighting his divine status by - recalling his strength and holiness.[2] - Demo, like Julia Balbilla, writes in the artificial and poetic Aeolic - dialect. The language indicates she was knowledgeable in Homeric - poetry—'bearing a pleasant gift', for example, alludes to the use of that - phrase throughout the Iliad and Odyssey.[a][2]; - """; + Demo (ancient Greek poet) + From Wikipedia, the free encyclopedia + Demo or Damo (Greek: Δεμώ, Δαμώ; fl. c. AD 200) was a Greek woman of the + Roman period, known for a single epigram, engraved upon the Colossus of + Memnon, which bears her name. She speaks of herself therein as a lyric + poetess dedicated to the Muses, but nothing is known of her life.[1] + Identity + Demo was evidently Greek, as her name, a traditional epithet of Demeter, + signifies. The name was relatively common in the Hellenistic world, in + Egypt and elsewhere, and she cannot be further identified. The date of her + visit to the Colossus of Memnon cannot be established with certainty, but + internal evidence on the left leg suggests her poem was inscribed there at + some point in or after AD 196.[2] + Epigram + There are a number of graffiti inscriptions on the Colossus of Memnon. + Following three epigrams by Julia Balbilla, a fourth epigram, in elegiac + couplets, entitled and presumably authored by "Demo" or "Damo" (the + Greek inscription is difficult to read), is a dedication to the Muses.[2] + The poem is traditionally published with the works of Balbilla, though the + internal evidence suggests a different author.[1] + In the poem, Demo explains that Memnon has shown her special respect. In + return, Demo offers the gift for poetry, as a gift to the hero. At the end + of this epigram, she addresses Memnon, highlighting his divine status by + recalling his strength and holiness.[2] + Demo, like Julia Balbilla, writes in the artificial and poetic Aeolic + dialect. The language indicates she was knowledgeable in Homeric + poetry—'bearing a pleasant gift', for example, alludes to the use of that + phrase throughout the Iliad and Odyssey.[a][2]; + """; static { try { @@ -96,19 +96,22 @@ public static void main(String[] args) { .credential(new AzureKeyCredential(API_KEY)) .buildAsyncClient(); - TextCompletion textCompletion = SKBuilders.textCompletionService().build(client, MODEL); + TextCompletion textCompletion = + SKBuilders.textCompletionService() + .withOpenAIClient(client) + .setModelId(MODEL) + .build(); String prompt = "{{$input}}\n" + "Summarize the content above."; Kernel kernel = SKBuilders.kernel().withDefaultAIService(textCompletion).build(); CompletionSKFunction summarize = kernel.getSemanticFunctionBuilder() - .createFunction( - prompt, - "summarize", - null, - null, - new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000)); + .setPromptTemplate(prompt) + .setFunctionName("summarize") + .setCompletionConfig( + new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000)) + .build(); if (summarize == null) { LOGGER.error("Null function"); diff --git a/java/api-test/api-usage-example/src/main/java/InlineFunctionWithPreBuiltSkillExample.java b/java/api-test/api-usage-example/src/main/java/InlineFunctionWithPreBuiltSkillExample.java index 7913ba6f987e..261e35d8491b 100644 --- a/java/api-test/api-usage-example/src/main/java/InlineFunctionWithPreBuiltSkillExample.java +++ b/java/api-test/api-usage-example/src/main/java/InlineFunctionWithPreBuiltSkillExample.java @@ -3,7 +3,7 @@ import com.azure.ai.openai.OpenAIClientBuilder; import com.azure.core.credential.AzureKeyCredential; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig; import com.microsoft.semantickernel.textcompletion.CompletionSKFunction; import com.microsoft.semantickernel.textcompletion.TextCompletion; @@ -97,19 +97,23 @@ public static void main(String[] args) { .credential(new AzureKeyCredential(API_KEY)) .buildAsyncClient(); - TextCompletion textCompletion = SKBuilders.textCompletionService().build(client, MODEL); + TextCompletion textCompletion = + SKBuilders.textCompletionService() + .withOpenAIClient(client) + .setModelId(MODEL) + .build(); String prompt = "{{$input}}\nSummarize the content above."; Kernel kernel = SKBuilders.kernel().withDefaultAIService(textCompletion).build(); CompletionSKFunction summarize = - SKBuilders.completionFunctions(kernel) - .createFunction( - prompt, - "summarize", - null, - null, - new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000)); + SKBuilders.completionFunctions() + .withKernel(kernel) + .setPromptTemplate(prompt) + .setFunctionName("summarize") + .setCompletionConfig( + new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000)) + .build(); if (summarize == null) { LOGGER.error("Null function"); diff --git a/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/AbstractKernelTest.java b/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/AbstractKernelTest.java index c1725ee347f6..486eccec1167 100644 --- a/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/AbstractKernelTest.java +++ b/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/AbstractKernelTest.java @@ -3,8 +3,8 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.SamplesConfig; -import com.microsoft.semantickernel.builders.SKBuilders; import com.microsoft.semantickernel.connectors.ai.openai.textcompletion.OpenAITextCompletion; import com.microsoft.semantickernel.exceptions.ConfigurationException; import com.microsoft.semantickernel.memory.VolatileMemoryStore; @@ -29,7 +29,10 @@ public static Kernel buildTextCompletionKernel() throws ConfigurationException { return SKBuilders.kernel() .withDefaultAIService(textCompletion) .withDefaultAIService( - SKBuilders.textEmbeddingGenerationService().build(openAIClient, model)) + SKBuilders.textEmbeddingGenerationService() + .withOpenAIClient(openAIClient) + .setModelId(model) + .build()) .withMemoryStorage(new VolatileMemoryStore()) .build(); } diff --git a/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/ContextVariableFunctionTest.java b/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/ContextVariableFunctionTest.java index e1e2de3591f0..110e8356ba4d 100644 --- a/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/ContextVariableFunctionTest.java +++ b/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/ContextVariableFunctionTest.java @@ -2,7 +2,7 @@ package com.microsoft.semantickernel.e2e; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.exceptions.ConfigurationException; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig; @@ -48,14 +48,13 @@ public void runContextVariableTest() CompletionSKFunction chat = kernel.getSemanticFunctionBuilder() - .createFunction( - prompt, - "ChatBot", - null, - null, - new PromptTemplateConfig.CompletionConfig(0.7, 0.5, 0, 0, 2000)); + .setPromptTemplate(prompt) + .setFunctionName("ChatBot") + .setCompletionConfig( + new PromptTemplateConfig.CompletionConfig(0.7, 0.5, 0, 0, 2000)) + .build(); - SKContext readOnlySkContext = SKBuilders.context().build(kernel); + SKContext readOnlySkContext = SKBuilders.context().withKernel(kernel).build(); chat("Hi, I'm looking for book suggestions?", chat, readOnlySkContext) .flatMap( diff --git a/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/InlineFunctionTest.java b/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/InlineFunctionTest.java index a0febc951c39..4e16510ed408 100644 --- a/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/InlineFunctionTest.java +++ b/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/InlineFunctionTest.java @@ -24,12 +24,11 @@ public void executeInlineFunction() throws ConfigurationException { CompletionSKFunction summarize = kernel.getSemanticFunctionBuilder() - .createFunction( - prompt, - "summarize", - null, - null, - new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000)); + .setPromptTemplate(prompt) + .setFunctionName("summarize") + .setCompletionConfig( + new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000)) + .build(); String text = """ diff --git a/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/KernelTest.java b/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/KernelTest.java index a2bf6a991280..2ec406cb888e 100644 --- a/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/KernelTest.java +++ b/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/KernelTest.java @@ -3,7 +3,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.connectors.ai.openai.textcompletion.OpenAITextCompletion; import com.microsoft.semantickernel.exceptions.ConfigurationException; import com.microsoft.semantickernel.orchestration.SKContext; @@ -23,15 +23,14 @@ public class KernelTest extends AbstractKernelTest { private static void executeCompletion(Kernel kernel) { CompletionSKFunction summarize = kernel.getSemanticFunctionBuilder() - .createFunction( + .setPromptTemplate( """ {{$input}} - One line TLDR with the fewest words.""", - null, - "", - null, - new PromptTemplateConfig.CompletionConfig(0, 0, 0, 0, 256)); + One line TLDR with the fewest words.""") + .setCompletionConfig( + new PromptTemplateConfig.CompletionConfig(0, 0, 0, 0, 256)) + .build(); String text1 = """ diff --git a/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/TextEmbeddingsTest.java b/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/TextEmbeddingsTest.java index 714d27826239..2a5ca231b3e8 100644 --- a/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/TextEmbeddingsTest.java +++ b/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/e2e/TextEmbeddingsTest.java @@ -3,7 +3,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.connectors.ai.openai.textembeddings.OpenAITextEmbeddingGeneration; import com.microsoft.semantickernel.coreskills.TextMemorySkill; import com.microsoft.semantickernel.exceptions.ConfigurationException; @@ -86,8 +86,10 @@ public void testMemory() throws ConfigurationException { CompletionSKFunction chat = kernel.getSemanticFunctionBuilder() - .createFunction( - skPrompt, "recall", "aboutMe", "TextEmbeddingTest#testMemory"); + .setPromptTemplate(skPrompt) + .setFunctionName("recall") + .setSkillName("aboutMe") + .build(); VolatileMemoryStore volatileMemoryStore = new VolatileMemoryStore(); volatileMemoryStore.createCollectionAsync("aboutMe").block(); @@ -103,9 +105,9 @@ public void testMemory() throws ConfigurationException { SKContext context = SKBuilders.context() - .with(SKBuilders.variables().build()) - .with(memory) - .with(kernel.getSkills()) + .setVariables(SKBuilders.variables().build()) + .setMemory(memory) + .setSkills(kernel.getSkills()) .build(); context.getSemanticMemory() diff --git a/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/planner/sequentialplanner/SequentialPlanTest.java b/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/planner/sequentialplanner/SequentialPlanTest.java index 919e1f1ef977..6191391f05c0 100644 --- a/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/planner/sequentialplanner/SequentialPlanTest.java +++ b/java/api-test/integration-tests/src/test/java/com/microsoft/semantickernel/planner/sequentialplanner/SequentialPlanTest.java @@ -2,7 +2,7 @@ package com.microsoft.semantickernel.planner.sequentialplanner; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.planner.actionplanner.Plan; import com.microsoft.semantickernel.skilldefinition.annotations.DefineSKFunction; diff --git a/java/connectors/semantickernel-connectors-ai-openai/src/main/java/com/microsoft/semantickernel/connectors/ai/openai/chatcompletion/OpenAIChatCompletion.java b/java/connectors/semantickernel-connectors-ai-openai/src/main/java/com/microsoft/semantickernel/connectors/ai/openai/chatcompletion/OpenAIChatCompletion.java index 5e923871838b..e9a75c1fc07d 100644 --- a/java/connectors/semantickernel-connectors-ai-openai/src/main/java/com/microsoft/semantickernel/connectors/ai/openai/chatcompletion/OpenAIChatCompletion.java +++ b/java/connectors/semantickernel-connectors-ai-openai/src/main/java/com/microsoft/semantickernel/connectors/ai/openai/chatcompletion/OpenAIChatCompletion.java @@ -12,6 +12,7 @@ import com.microsoft.semantickernel.chatcompletion.ChatHistory; import com.microsoft.semantickernel.chatcompletion.ChatRequestSettings; import com.microsoft.semantickernel.connectors.ai.openai.azuresdk.ClientBase; +import com.microsoft.semantickernel.exceptions.NotSupportedException; import com.microsoft.semantickernel.textcompletion.CompletionRequestSettings; import java.util.Arrays; import java.util.HashMap; @@ -38,8 +39,27 @@ public Mono> completeAsync( public static class Builder implements ChatCompletion.Builder { public Builder() {} + @Nullable private OpenAIAsyncClient client; + @Nullable private String modelId; + + public Builder withOpenAIClient(OpenAIAsyncClient client) { + this.client = client; + return this; + } + + public Builder setModelId(String modelId) { + this.modelId = modelId; + return this; + } + @Override - public ChatCompletion build(OpenAIAsyncClient client, String modelId) { + public ChatCompletion build() { + if (client == null) { + throw new NotSupportedException("OpenAI client not set"); + } + if (modelId == null) { + throw new NotSupportedException("Model ID not set"); + } return new OpenAIChatCompletion(client, modelId); } } @@ -137,7 +157,7 @@ public OpenAIChatHistory createNewChat(@Nullable String instructions) { @Override public Flux generateMessageStream( - ChatHistory chat, @Nullable ChatRequestSettings requestSettings) { + OpenAIChatHistory chat, @Nullable ChatRequestSettings requestSettings) { return this.getStreamingChatCompletionsAsync(chat, requestSettings) .concatMap( chatCompletionResult -> { @@ -155,7 +175,7 @@ public Flux generateMessageStream( @Override public Flux getStreamingChatCompletionsAsync( - ChatHistory chat, ChatRequestSettings requestSettings) { + OpenAIChatHistory chat, ChatRequestSettings requestSettings) { return internalGetChatStreamingResultsAsync(chat, requestSettings); } diff --git a/java/connectors/semantickernel-connectors-ai-openai/src/main/java/com/microsoft/semantickernel/connectors/ai/openai/textcompletion/OpenAITextCompletion.java b/java/connectors/semantickernel-connectors-ai-openai/src/main/java/com/microsoft/semantickernel/connectors/ai/openai/textcompletion/OpenAITextCompletion.java index aac70ea18762..09a12dbb3cee 100644 --- a/java/connectors/semantickernel-connectors-ai-openai/src/main/java/com/microsoft/semantickernel/connectors/ai/openai/textcompletion/OpenAITextCompletion.java +++ b/java/connectors/semantickernel-connectors-ai-openai/src/main/java/com/microsoft/semantickernel/connectors/ai/openai/textcompletion/OpenAITextCompletion.java @@ -7,6 +7,7 @@ import com.azure.ai.openai.models.CompletionsOptions; import com.microsoft.semantickernel.ai.AIException; import com.microsoft.semantickernel.connectors.ai.openai.azuresdk.ClientBase; +import com.microsoft.semantickernel.exceptions.NotSupportedException; import com.microsoft.semantickernel.textcompletion.CompletionRequestSettings; import com.microsoft.semantickernel.textcompletion.TextCompletion; import jakarta.inject.Inject; @@ -14,6 +15,7 @@ import java.util.HashMap; import java.util.List; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import reactor.core.publisher.Mono; /// @@ -69,10 +71,28 @@ protected Mono> internalCompleteTextAsync( .collectList(); } - public static final class Builder extends TextCompletion.Builder { + public static final class Builder implements TextCompletion.Builder { + @Nullable private OpenAIAsyncClient client; + @Nullable private String modelId; + + public Builder withOpenAIClient(OpenAIAsyncClient client) { + this.client = client; + return this; + } + + public Builder setModelId(String modelId) { + this.modelId = modelId; + return this; + } @Override - public TextCompletion build(OpenAIAsyncClient client, String modelId) { + public TextCompletion build() { + if (client == null) { + throw new NotSupportedException("OpenAI client not set"); + } + if (modelId == null) { + throw new NotSupportedException("Model ID not set"); + } return new OpenAITextCompletion(client, modelId); } } diff --git a/java/connectors/semantickernel-connectors-ai-openai/src/main/java/com/microsoft/semantickernel/connectors/ai/openai/textembeddings/OpenAITextEmbeddingGeneration.java b/java/connectors/semantickernel-connectors-ai-openai/src/main/java/com/microsoft/semantickernel/connectors/ai/openai/textembeddings/OpenAITextEmbeddingGeneration.java index d6d229d3cf5d..8028ccd1c90b 100644 --- a/java/connectors/semantickernel-connectors-ai-openai/src/main/java/com/microsoft/semantickernel/connectors/ai/openai/textembeddings/OpenAITextEmbeddingGeneration.java +++ b/java/connectors/semantickernel-connectors-ai-openai/src/main/java/com/microsoft/semantickernel/connectors/ai/openai/textembeddings/OpenAITextEmbeddingGeneration.java @@ -8,6 +8,7 @@ import com.microsoft.semantickernel.ai.embeddings.Embedding; import com.microsoft.semantickernel.ai.embeddings.EmbeddingGeneration; import com.microsoft.semantickernel.connectors.ai.openai.azuresdk.ClientBase; +import com.microsoft.semantickernel.exceptions.NotSupportedException; import java.util.List; import java.util.stream.Collectors; import reactor.core.publisher.Mono; @@ -42,8 +43,27 @@ protected Mono> internalGenerateTextEmbeddingsAsync(List } public static class Builder implements EmbeddingGeneration.Builder { + private OpenAIAsyncClient client; + private String modelId; + + public Builder withOpenAIClient(OpenAIAsyncClient client) { + this.client = client; + return this; + } + + public Builder setModelId(String modelId) { + this.modelId = modelId; + return this; + } + @Override - public EmbeddingGeneration build(OpenAIAsyncClient client, String modelId) { + public EmbeddingGeneration build() { + if (client == null) { + throw new NotSupportedException("OpenAI client not set"); + } + if (modelId == null) { + throw new NotSupportedException("Model ID not set"); + } return new OpenAITextEmbeddingGeneration(client, modelId); } } diff --git a/java/connectors/semantickernel-connectors-memory-sqlite/src/main/java/com/microsoft/semantickernel/connectors/memory/sqlite/Database.java b/java/connectors/semantickernel-connectors-memory-sqlite/src/main/java/com/microsoft/semantickernel/connectors/memory/sqlite/Database.java index 1d9ea423e551..c02a772e8df6 100644 --- a/java/connectors/semantickernel-connectors-memory-sqlite/src/main/java/com/microsoft/semantickernel/connectors/memory/sqlite/Database.java +++ b/java/connectors/semantickernel-connectors-memory-sqlite/src/main/java/com/microsoft/semantickernel/connectors/memory/sqlite/Database.java @@ -4,7 +4,11 @@ import com.microsoft.semantickernel.memory.DataEntryBase; import com.microsoft.semantickernel.memory.MemoryException; import com.microsoft.semantickernel.memory.MemoryException.ErrorCodes; -import java.sql.*; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; diff --git a/java/connectors/semantickernel-connectors-memory-sqlite/src/main/java/com/microsoft/semantickernel/connectors/memory/sqlite/SQLiteMemoryStore.java b/java/connectors/semantickernel-connectors-memory-sqlite/src/main/java/com/microsoft/semantickernel/connectors/memory/sqlite/SQLiteMemoryStore.java index 3f7d8999c929..19c46ce90d2d 100644 --- a/java/connectors/semantickernel-connectors-memory-sqlite/src/main/java/com/microsoft/semantickernel/connectors/memory/sqlite/SQLiteMemoryStore.java +++ b/java/connectors/semantickernel-connectors-memory-sqlite/src/main/java/com/microsoft/semantickernel/connectors/memory/sqlite/SQLiteMemoryStore.java @@ -8,7 +8,9 @@ import com.microsoft.semantickernel.memory.MemoryException.ErrorCodes; import com.microsoft.semantickernel.memory.MemoryRecord; import com.microsoft.semantickernel.memory.MemoryStore; -import java.sql.*; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; diff --git a/java/extensions/semantickernel-planners/src/main/java/com/microsoft/semantickernel/planner/actionplanner/ActionPlanner.java b/java/extensions/semantickernel-planners/src/main/java/com/microsoft/semantickernel/planner/actionplanner/ActionPlanner.java index 255d775d6149..018b5094cd53 100644 --- a/java/extensions/semantickernel-planners/src/main/java/com/microsoft/semantickernel/planner/actionplanner/ActionPlanner.java +++ b/java/extensions/semantickernel-planners/src/main/java/com/microsoft/semantickernel/planner/actionplanner/ActionPlanner.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.orchestration.SKFunction; import com.microsoft.semantickernel.orchestration.WritableContextVariables; @@ -67,19 +67,18 @@ public ActionPlanner(Kernel kernel, @Nullable String prompt) { } this.plannerFunction = - SKBuilders.completionFunctions(kernel) - .createFunction( - promptTemplate, - null, - SkillName, - null, - new PromptTemplateConfig.CompletionConfig( - 0.0, 0.0, 0.0, 0.0, 1024)); + SKBuilders.completionFunctions() + .withKernel(kernel) + .setPromptTemplate(promptTemplate) + .setSkillName(SkillName) + .setCompletionConfig( + new PromptTemplateConfig.CompletionConfig(0.0, 0.0, 0.0, 0.0, 1024)) + .build(); kernel.importSkill(this, SkillName); this.kernel = kernel; - this.context = SKBuilders.context().with(kernel.getSkills()).build(); + this.context = SKBuilders.context().setSkills(kernel.getSkills()).build(); } public static String read(String file) { diff --git a/java/extensions/semantickernel-planners/src/main/java/com/microsoft/semantickernel/planner/actionplanner/Plan.java b/java/extensions/semantickernel-planners/src/main/java/com/microsoft/semantickernel/planner/actionplanner/Plan.java index 39d62247c292..c37f6f55b4c5 100644 --- a/java/extensions/semantickernel-planners/src/main/java/com/microsoft/semantickernel/planner/actionplanner/Plan.java +++ b/java/extensions/semantickernel-planners/src/main/java/com/microsoft/semantickernel/planner/actionplanner/Plan.java @@ -2,8 +2,8 @@ package com.microsoft.semantickernel.planner.actionplanner; import com.microsoft.semantickernel.Kernel; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.Verify; -import com.microsoft.semantickernel.builders.SKBuilders; import com.microsoft.semantickernel.memory.SemanticTextMemory; import com.microsoft.semantickernel.orchestration.AbstractSkFunction; import com.microsoft.semantickernel.orchestration.ContextVariables; @@ -190,9 +190,9 @@ public Mono invokeNextStepAsync( // Execute the step SKContext initialContext = SKBuilders.context() - .with(context.getVariables().writableClone()) - .with(context.getSemanticMemory()) - .with(context.getSkills()) + .setVariables(context.getVariables().writableClone()) + .setMemory(context.getSemanticMemory()) + .setSkills(context.getSkills()) .build(); return Flux.fromIterable(this.steps) @@ -226,9 +226,9 @@ private Mono executeStep( // Execute the step SKContext functionContext = SKBuilders.context() - .with(functionVariables) - .with(context.getSemanticMemory()) - .with(context.getSkills()) + .setVariables(functionVariables) + .setMemory(context.getSemanticMemory()) + .setSkills(context.getSkills()) .build(); return step.invokeAsync(functionContext, settings) @@ -281,7 +281,7 @@ private Mono executeStep( return Mono.just( SKBuilders.context() .clone(context) - .with(updatedContext) + .setVariables(updatedContext) .build()); }); }); @@ -302,9 +302,9 @@ public Mono invokeAsync( SKContext context = SKBuilders.context() - .with(variables) - .with(memory) - .with(skillsSupplier == null ? null : skillsSupplier.get()) + .setVariables(variables) + .setMemory(memory) + .setSkills(skillsSupplier == null ? null : skillsSupplier.get()) .build(); return this.invokeAsync(context, settings); @@ -322,8 +322,9 @@ public Mono invokeAsync( if (context == null) { context = SKBuilders.context() - .with(state) - .with(getSkillsSupplier() == null ? null : getSkillsSupplier().get()) + .setVariables(state) + .setSkills( + getSkillsSupplier() == null ? null : getSkillsSupplier().get()) .build(); } else { ContextVariables variables = context.getVariables().writableClone().update(state, true); @@ -386,7 +387,7 @@ private static SKContext addVariablesToContext(ContextVariables vars, SKContext } }); - return SKBuilders.context().clone(context).with(clone).build(); + return SKBuilders.context().clone(context).setVariables(clone).build(); } /** diff --git a/java/extensions/semantickernel-planners/src/main/java/com/microsoft/semantickernel/planner/sequentialplanner/SequentialPlanParser.java b/java/extensions/semantickernel-planners/src/main/java/com/microsoft/semantickernel/planner/sequentialplanner/SequentialPlanParser.java index 05f3ba4d4275..7164ec6fb1f3 100644 --- a/java/extensions/semantickernel-planners/src/main/java/com/microsoft/semantickernel/planner/sequentialplanner/SequentialPlanParser.java +++ b/java/extensions/semantickernel-planners/src/main/java/com/microsoft/semantickernel/planner/sequentialplanner/SequentialPlanParser.java @@ -1,7 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. package com.microsoft.semantickernel.planner.sequentialplanner; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.orchestration.SKFunction; import com.microsoft.semantickernel.orchestration.WritableContextVariables; import com.microsoft.semantickernel.planner.PlanningException; diff --git a/java/extensions/semantickernel-planners/src/main/java/com/microsoft/semantickernel/planner/sequentialplanner/SequentialPlanner.java b/java/extensions/semantickernel-planners/src/main/java/com/microsoft/semantickernel/planner/sequentialplanner/SequentialPlanner.java index 3eb2869bfb17..d36ffe63d35b 100644 --- a/java/extensions/semantickernel-planners/src/main/java/com/microsoft/semantickernel/planner/sequentialplanner/SequentialPlanner.java +++ b/java/extensions/semantickernel-planners/src/main/java/com/microsoft/semantickernel/planner/sequentialplanner/SequentialPlanner.java @@ -2,8 +2,7 @@ package com.microsoft.semantickernel.planner.sequentialplanner; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.FunctionBuilders; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.planner.PlanningException; import com.microsoft.semantickernel.planner.actionplanner.Plan; @@ -50,16 +49,18 @@ public SequentialPlanner( } this.functionFlowFunction = - FunctionBuilders.getCompletionBuilder(kernel) - .createFunction( - promptTemplate, - null, - RestrictedSkillName, + SKBuilders.completionFunctions() + .withKernel(kernel) + .setPromptTemplate(promptTemplate) + .setSkillName(RestrictedSkillName) + .setDescription( "Given a request or command or goal generate a step by step plan to" + " fulfill the request using functions. This ability is also" - + " known as decision making and function flow", + + " known as decision making and function flow") + .setCompletionConfig( new PromptTemplateConfig.CompletionConfig( - 0.0, 0.0, 0.0, 0.0, this.config.getMaxTokens())); + 0.0, 0.0, 0.0, 0.0, this.config.getMaxTokens())) + .build(); this.kernel = kernel; } @@ -71,7 +72,7 @@ public SequentialPlanner( * @return The plan */ public Mono createPlanAsync(String goal) { - return createPlanAsync(goal, SKBuilders.context().build(kernel)); + return createPlanAsync(goal, SKBuilders.context().withKernel(kernel).build()); } /** diff --git a/java/plugins/semantickernel-plugin-core/src/main/java/com/microsoft/semantickernel/coreskills/ConversationSummarySkill.java b/java/plugins/semantickernel-plugin-core/src/main/java/com/microsoft/semantickernel/coreskills/ConversationSummarySkill.java index 4ea7a9dedb5e..1e1172f3f5ad 100644 --- a/java/plugins/semantickernel-plugin-core/src/main/java/com/microsoft/semantickernel/coreskills/ConversationSummarySkill.java +++ b/java/plugins/semantickernel-plugin-core/src/main/java/com/microsoft/semantickernel/coreskills/ConversationSummarySkill.java @@ -2,7 +2,7 @@ package com.microsoft.semantickernel.coreskills; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.skilldefinition.annotations.DefineSKFunction; import com.microsoft.semantickernel.skilldefinition.annotations.SKFunctionInputAttribute; @@ -30,46 +30,58 @@ public class ConversationSummarySkill { public ConversationSummarySkill(Kernel kernel) { this.kernel = kernel; this.summarizeConversationFunction = - SKBuilders.completionFunctions(kernel) - .createFunction( - SemanticFunctionConstants.SummarizeConversationDefinition, - "summarize", - "ConversationSummarySkill", + SKBuilders.completionFunctions() + .withKernel(kernel) + .setPromptTemplate( + SemanticFunctionConstants.SummarizeConversationDefinition) + .setFunctionName("summarize") + .setSkillName("ConversationSummarySkill") + .setDescription( "Given a section of a conversation transcript, summarize the part" - + " of the conversation.", + + " of the conversation.") + .setCompletionConfig( SKBuilders.completionConfig() .maxTokens(MaxTokens) .temperature(0.1) .topP(0.5) - .build()); + .build()) + .build(); this.conversationActionItemsFunction = - SKBuilders.completionFunctions(kernel) - .createFunction( - SemanticFunctionConstants.GetConversationActionItemsDefinition, - "ActionItems", - "ConversationSummarySkill", + SKBuilders.completionFunctions() + .withKernel(kernel) + .setPromptTemplate( + SemanticFunctionConstants.GetConversationActionItemsDefinition) + .setFunctionName("ActionItems") + .setSkillName("ConversationSummarySkill") + .setDescription( "Given a section of a conversation transcript, identify action" - + " items.", + + " items.") + .setCompletionConfig( SKBuilders.completionConfig() .maxTokens(MaxTokens) .temperature(0.1) .topP(0.5) - .build()); + .build()) + .build(); this.conversationTopicsFunction = - SKBuilders.completionFunctions(kernel) - .createFunction( - SemanticFunctionConstants.GetConversationTopicsDefinition, - "Topics", - "ConversationSummarySkill", + SKBuilders.completionFunctions() + .withKernel(kernel) + .setPromptTemplate( + SemanticFunctionConstants.GetConversationTopicsDefinition) + .setFunctionName("Topics") + .setSkillName("ConversationSummarySkill") + .setDescription( "Analyze a conversation transcript and extract key topics worth" - + " remembering.", + + " remembering.") + .setCompletionConfig( SKBuilders.completionConfig() .maxTokens(MaxTokens) .temperature(0.1) .topP(0.5) - .build()); + .build()) + .build(); } /** @@ -89,7 +101,7 @@ public Mono summarizeConversationAsync( List paragraphs = TextChunker.splitPlainTextParagraphs(lines, MaxTokens); if (context == null) { - context = SKBuilders.context().with(kernel.getSkills()).build(); + context = SKBuilders.context().setSkills(kernel.getSkills()).build(); } SKContext completionContext = context.copy(); diff --git a/java/plugins/semantickernel-plugin-core/src/main/java/com/microsoft/semantickernel/coreskills/TextMemorySkill.java b/java/plugins/semantickernel-plugin-core/src/main/java/com/microsoft/semantickernel/coreskills/TextMemorySkill.java index 059b2d2a1a9c..e3313335d94f 100644 --- a/java/plugins/semantickernel-plugin-core/src/main/java/com/microsoft/semantickernel/coreskills/TextMemorySkill.java +++ b/java/plugins/semantickernel-plugin-core/src/main/java/com/microsoft/semantickernel/coreskills/TextMemorySkill.java @@ -1,7 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. package com.microsoft.semantickernel.coreskills; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.memory.MemoryException; import com.microsoft.semantickernel.memory.MemoryQueryResult; import com.microsoft.semantickernel.memory.MemoryRecordMetadata; @@ -126,9 +126,9 @@ public Mono saveAsync( it -> { context.setVariable(TextMemorySkill.KEY_PARAM, it); return SKBuilders.context() - .with(context.getVariables()) - .with(context.getSkills()) - .with(context.getSemanticMemory()) + .setVariables(context.getVariables()) + .setSkills(context.getSkills()) + .setMemory(context.getSemanticMemory()) .build(); }); } diff --git a/java/samples/pom.xml b/java/samples/pom.xml index 5950fda75256..62762ee92348 100644 --- a/java/samples/pom.xml +++ b/java/samples/pom.xml @@ -11,7 +11,7 @@ sample-code - semantickernel-guice + semantickernel-samples-guice diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/Example99_BlogAnnouncement.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/Example99_BlogAnnouncement.java index 027918f807e4..0afa2be747b7 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/Example99_BlogAnnouncement.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/Example99_BlogAnnouncement.java @@ -1,7 +1,6 @@ package com.microsoft.semantickernel; import com.azure.ai.openai.OpenAIAsyncClient; -import com.microsoft.semantickernel.builders.SKBuilders; import com.microsoft.semantickernel.connectors.ai.openai.util.OpenAIClientProvider; import com.microsoft.semantickernel.exceptions.ConfigurationException; import com.microsoft.semantickernel.planner.actionplanner.Plan; @@ -22,7 +21,10 @@ public class Example99_BlogAnnouncement { public static void main(String[] args) throws IOException, ConfigurationException { OpenAIAsyncClient client = OpenAIClientProvider.getClient(); - TextCompletion textCompletionService = SKBuilders.textCompletionService().build(client, "text-davinci-003"); + TextCompletion textCompletionService = SKBuilders.textCompletionService() + .setModelId("text-davinci-003") + .withOpenAIClient(client) + .build(); Kernel kernel = SKBuilders.kernel().withDefaultAIService(textCompletionService).build(); kernel.importSkill(new MyAppSkills(), "MyAppSkills"); diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/Example_PlanWithNativeFunctions.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/Example_PlanWithNativeFunctions.java index 42a0ae82457a..7a3b0fc9d42f 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/Example_PlanWithNativeFunctions.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/Example_PlanWithNativeFunctions.java @@ -1,7 +1,6 @@ package com.microsoft.semantickernel; import com.azure.ai.openai.OpenAIAsyncClient; -import com.microsoft.semantickernel.builders.SKBuilders; import com.microsoft.semantickernel.connectors.ai.openai.util.OpenAIClientProvider; import com.microsoft.semantickernel.exceptions.ConfigurationException; import com.microsoft.semantickernel.orchestration.SKContext; @@ -17,7 +16,10 @@ public class Example_PlanWithNativeFunctions { public static void main(String[] args) throws ConfigurationException { OpenAIAsyncClient client = OpenAIClientProvider.getClient(); - TextCompletion textCompletionService = SKBuilders.textCompletionService().build(client, "text-davinci-003"); + TextCompletion textCompletionService = SKBuilders.textCompletionService() + .setModelId("text-davinci-003") + .withOpenAIClient(client) + .build(); Kernel kernel = SKBuilders.kernel() .withDefaultAIService(textCompletionService) diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/Example00_GettingStarted.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/Example00_GettingStarted.java index e8e7604ef25d..b4393fd79bcc 100755 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/Example00_GettingStarted.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/Example00_GettingStarted.java @@ -8,7 +8,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.connectors.ai.openai.util.OpenAIClientProvider; import com.microsoft.semantickernel.exceptions.ConfigurationException; import com.microsoft.semantickernel.orchestration.SKContext; @@ -40,7 +40,10 @@ public class Example00_GettingStarted { */ public static Kernel getKernel(OpenAIAsyncClient client) { Kernel kernel = SKBuilders.kernel() - .withDefaultAIService(SKBuilders.textCompletionService().build(client, "text-davinci-003")) + .withDefaultAIService(SKBuilders.textCompletionService() + .setModelId("text-davinci-003") + .withOpenAIClient(client) + .build()) .build(); return kernel; diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/Example03_SemanticFunctionInline.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/Example03_SemanticFunctionInline.java index e75a7ad6ecaf..39b71537768f 100755 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/Example03_SemanticFunctionInline.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/Example03_SemanticFunctionInline.java @@ -33,17 +33,16 @@ public class Example03_SemanticFunctionInline { public static void inlineFunction(Kernel kernel, String prompt, String functionName, String text) { SKFunction summarize = kernel .getSemanticFunctionBuilder() - .createFunction( - prompt, - functionName, - null, - null, + .setPromptTemplate(prompt) + .setFunctionName(functionName) + .setCompletionConfig( new PromptTemplateConfig.CompletionConfig( 0.2, 0.5, 0, 0, - 2000)); + 2000)) + .build(); Mono result = summarize.invokeAsync(text); diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/Example04_ContextVariablesChat.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/Example04_ContextVariablesChat.java index 2f2bdbfa2215..1bdb5abc5251 100755 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/Example04_ContextVariablesChat.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/Example04_ContextVariablesChat.java @@ -8,7 +8,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.connectors.ai.openai.util.OpenAIClientProvider; import com.microsoft.semantickernel.exceptions.ConfigurationException; import com.microsoft.semantickernel.orchestration.SKContext; @@ -41,19 +41,18 @@ public static void startChat(Kernel kernel) CompletionSKFunction chat = kernel .getSemanticFunctionBuilder() - .createFunction( - prompt, - "ChatBot", - null, - null, + .setPromptTemplate(prompt) + .setFunctionName("ChatBot") + .setCompletionConfig( new PromptTemplateConfig.CompletionConfig( 0.7, 0.5, 0, 0, - 2000)); + 2000)) + .build(); - SKContext readOnlySkContext = SKBuilders.context().build(kernel); + SKContext readOnlySkContext = SKBuilders.context().withKernel(kernel).build(); chat("Hi, I'm looking for book suggestions?", chat, readOnlySkContext) .flatMap( diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example02_Pipeline.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example02_Pipeline.java index a0034323777a..762de8758500 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example02_Pipeline.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example02_Pipeline.java @@ -1,8 +1,7 @@ package com.microsoft.semantickernel.samples.syntaxexamples; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.KernelConfig; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.coreskills.TextSkill; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.skilldefinition.ReadOnlyFunctionCollection; diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example03_Variables.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example03_Variables.java index dd5a5f306847..440ad493f1ee 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example03_Variables.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example03_Variables.java @@ -2,7 +2,7 @@ package com.microsoft.semantickernel.samples.syntaxexamples; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.orchestration.ContextVariables; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.skilldefinition.ReadOnlyFunctionCollection; 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 fe41c65a3d98..af06b27738ab 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 @@ -3,14 +3,12 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.KernelConfig; import com.microsoft.semantickernel.SamplesConfig; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.exceptions.ConfigurationException; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.skilldefinition.annotations.DefineSKFunction; import com.microsoft.semantickernel.skilldefinition.annotations.SKFunctionInputAttribute; -import com.microsoft.semantickernel.skilldefinition.annotations.SKFunctionParameters; import com.microsoft.semantickernel.textcompletion.TextCompletion; import reactor.core.publisher.Mono; @@ -28,7 +26,10 @@ public Mono search( public static void main(String[] args) throws ConfigurationException { OpenAIAsyncClient client = SamplesConfig.getClient(); - TextCompletion textCompletion = SKBuilders.textCompletionService().build(client, "text-davinci-003"); + TextCompletion textCompletion = SKBuilders.textCompletionService() + .setModelId("text-davinci-003") + .withOpenAIClient(client) + .build(); Kernel kernel = SKBuilders.kernel().withDefaultAIService(textCompletion).build(); kernel.importSkill(new SearchEngineSkill(), null); 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 abf2683642de..49d6eae2be4a 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 @@ -4,9 +4,8 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.KernelConfig; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.SamplesConfig; -import com.microsoft.semantickernel.builders.SKBuilders; import com.microsoft.semantickernel.exceptions.ConfigurationException; import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig; import com.microsoft.semantickernel.textcompletion.TextCompletion; @@ -19,7 +18,10 @@ public class Example05_InlineFunctionDefinition { public static void main(String[] args) throws ConfigurationException { OpenAIAsyncClient client = SamplesConfig.getClient(); - TextCompletion textCompletion = SKBuilders.textCompletionService().build(client, "text-davinci-003"); + TextCompletion textCompletion = SKBuilders.textCompletionService() + .setModelId("text-davinci-003") + .withOpenAIClient(client) + .build(); Kernel kernel = SKBuilders.kernel().withDefaultAIService(textCompletion).build(); @@ -41,14 +43,16 @@ public static void main(String[] args) throws ConfigurationException { // Create function via builder var excuseFunction = SKBuilders - .completionFunctions(kernel) - .createFunction( - functionDefinition, + .completionFunctions() + .withKernel(kernel) + .setPromptTemplate(functionDefinition) + .setCompletionConfig( new PromptTemplateConfig.CompletionConfigBuilder() .maxTokens(100) .temperature(0.4) .topP(1) - .build()); + .build()) + .build(); var result = excuseFunction.invokeAsync("I missed the F1 final race").block(); @@ -60,13 +64,15 @@ public static void main(String[] args) throws ConfigurationException { // Create function via kernel var fixedFunction = kernel. getSemanticFunctionBuilder() - .createFunction("Translate this date " + - DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(LocalDateTime.now()) + " to French format", + .setPromptTemplate("Translate this date " + + DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).format(LocalDateTime.now()) + " to French format") + .setCompletionConfig( new PromptTemplateConfig.CompletionConfigBuilder() .maxTokens(100) .temperature(0.4) .topP(1) - .build()); + .build()) + .build(); System.out.println(fixedFunction.invokeAsync().block().getResult()); } 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 6aa190d55e6a..205166f1972e 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 @@ -3,8 +3,8 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.SamplesConfig; -import com.microsoft.semantickernel.builders.SKBuilders; import com.microsoft.semantickernel.coreskills.TimeSkill; import com.microsoft.semantickernel.exceptions.ConfigurationException; import com.microsoft.semantickernel.orchestration.SKContext; @@ -22,7 +22,10 @@ public static void main(String[] args) throws ConfigurationException { OpenAIAsyncClient client = SamplesConfig.getClient(); Kernel kernel = SKBuilders.kernel() - .withDefaultAIService(SKBuilders.textCompletionService().build(client, "text-davinci-003")) + .withDefaultAIService(SKBuilders.textCompletionService() + .setModelId("text-davinci-003") + .withOpenAIClient(client) + .build()) .build(); // Load native skill into the kernel skill collection, sharing its functions @@ -44,13 +47,15 @@ Is it weekend time (weekend/not weekend)? System.out.println("--- Rendered Prompt"); var promptRenderer = SKBuilders.promptTemplate() - .withPromptTemplateConfig(new PromptTemplateConfig()) - .withPromptTemplate(functionDefinition) - .build(kernel.getPromptTemplateEngine()); + .setPromptTemplateConfig(new PromptTemplateConfig()) + .setPromptTemplate(functionDefinition) + .setPromptTemplateEngine(kernel.getPromptTemplateEngine()) + .build(); SKContext skContext = SKBuilders .context() - .build(kernel.getSkills()); + .setSkills(kernel.getSkills()) + .build(); var renderedPrompt = promptRenderer.renderAsync(skContext); System.out.println(renderedPrompt.block()); @@ -58,13 +63,11 @@ Is it weekend time (weekend/not weekend)? // Run the prompt / semantic function var kindOfDay = kernel .getSemanticFunctionBuilder() - .createFunction( - functionDefinition, - null, - null, - null, + .setPromptTemplate(functionDefinition) + .setCompletionConfig( new PromptTemplateConfig.CompletionConfig( - 0, 0, 0, 0, 256)); + 0, 0, 0, 0, 256)) + .build(); // Show the result System.out.println("--- Semantic Function result"); 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 6a6068df81ef..e63e98042d9a 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 @@ -7,8 +7,7 @@ import com.azure.core.http.policy.ExponentialBackoffOptions; import com.azure.core.http.policy.RetryOptions; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.KernelConfig; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.connectors.ai.openai.util.AzureOpenAISettings; import com.microsoft.semantickernel.connectors.ai.openai.util.SettingsMap; import com.microsoft.semantickernel.exceptions.ConfigurationException; @@ -70,21 +69,25 @@ public static void main(String[] args) throws ConfigurationException { String prompt = "{{$input}}\nSummarize the content above."; - TextCompletion textCompletion = SKBuilders.textCompletionService().build(client, "text-davinci-003"); + TextCompletion textCompletion = SKBuilders.textCompletionService() + .setModelId("text-davinci-003") + .withOpenAIClient(client) + .build(); Kernel kernel = SKBuilders .kernel() .withDefaultAIService(textCompletion) .build(); - CompletionSKFunction summarizeFunc = SKBuilders.completionFunctions(kernel) - .createFunction( - prompt, - "summarize", - null, - null, + CompletionSKFunction summarizeFunc = SKBuilders + .completionFunctions() + .withKernel(kernel) + .setPromptTemplate(prompt) + .setFunctionName("summarize") + .setCompletionConfig( new PromptTemplateConfig.CompletionConfig( - 0.2, 0.5, 0, 0, 2000)); + 0.2, 0.5, 0, 0, 2000)) + .build(); kernel.registerSemanticFunction(summarizeFunc); 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 d1a9b3c7205c..30614baf22c6 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 @@ -3,9 +3,8 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.KernelConfig; import com.microsoft.semantickernel.SamplesConfig; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.exceptions.ConfigurationException; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.skilldefinition.annotations.DefineSKFunction; @@ -19,7 +18,10 @@ public class Example09_FunctionTypes { public static void main(String[] args) throws ConfigurationException { OpenAIAsyncClient client = SamplesConfig.getClient(); - TextCompletion textCompletion = SKBuilders.textCompletionService().build(client, "text-davinci-003"); + TextCompletion textCompletion = SKBuilders.textCompletionService() + .setModelId("text-davinci-003") + .withOpenAIClient(client) + .build(); Kernel kernel = SKBuilders.kernel().withDefaultAIService(textCompletion).build(); @@ -33,7 +35,7 @@ public static void main(String[] args) throws ConfigurationException { var fakeContext = SKBuilders.context() - .with(kernel.getSkills()) + .setSkills(kernel.getSkills()) .build(); // The kernel takes care of wiring the input appropriately diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example12_SequentialPlanner.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example12_SequentialPlanner.java index 7956018ad32b..023c505e6fde 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example12_SequentialPlanner.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example12_SequentialPlanner.java @@ -4,7 +4,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; import com.microsoft.semantickernel.SamplesConfig; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.coreskills.TextSkill; import com.microsoft.semantickernel.exceptions.ConfigurationException; import com.microsoft.semantickernel.memory.VolatileMemoryStore; @@ -175,11 +175,16 @@ private static Kernel initializeKernel() throws ConfigurationException { OpenAIAsyncClient client = SamplesConfig.getClient(); var kernel = SKBuilders.kernel() .withDefaultAIService(SKBuilders.chatCompletion() - .build(client, "gpt-35-turbo")) + .withOpenAIClient(client) + .setModelId("gpt-35-turbo") + .build()) .withMemory(SKBuilders .semanticTextMemory() .setEmbeddingGenerator( - SKBuilders.textEmbeddingGenerationService().build(client, "text-davinci-003") + SKBuilders.textEmbeddingGenerationService() + .withOpenAIClient(client) + .setModelId("gpt-35-turbo") + .build() ) .setStorage(new VolatileMemoryStore()) .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 dc44206adb6b..441be9e97154 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 @@ -4,7 +4,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; import com.microsoft.semantickernel.SamplesConfig; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.coreskills.ConversationSummarySkill; import com.microsoft.semantickernel.exceptions.ConfigurationException; import com.microsoft.semantickernel.orchestration.SKContext; @@ -178,7 +178,10 @@ private static void getConversationTopicsAsync(OpenAIAsyncClient client) { } private static Kernel initializeKernel(OpenAIAsyncClient client) { - TextCompletion textCompletion = SKBuilders.textCompletionService().build(client, "text-davinci-003"); + TextCompletion textCompletion = SKBuilders.textCompletionService() + .setModelId("text-davinci-003") + .withOpenAIClient(client) + .build(); return SKBuilders.kernel() .withAIService("text-davinci-003", textCompletion, true, TextCompletion.class).build(); diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example14_SemanticMemory.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example14_SemanticMemory.java index 2e887a7fde36..a535ec171d52 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example14_SemanticMemory.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example14_SemanticMemory.java @@ -2,8 +2,8 @@ package com.microsoft.semantickernel.samples.syntaxexamples; import com.microsoft.semantickernel.Kernel; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.SamplesConfig; -import com.microsoft.semantickernel.builders.SKBuilders; import com.microsoft.semantickernel.connectors.memory.azurecognitivesearch.AzureCognitiveSearchMemory; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -23,12 +23,10 @@ */ // ReSharper disable once InconsistentNaming -public class Example14_SemanticMemory -{ +public class Example14_SemanticMemory { private static final String MEMORY_COLLECTION_NAME = "SKGitHub"; - public static void main(String[] args) throws Exception - { + public static void main(String[] args) throws Exception { System.out.println("=============================================================="); System.out.println("======== Semantic Memory using Azure Cognitive Search ========"); System.out.println("=============================================================="); @@ -59,32 +57,34 @@ public static void main(String[] args) throws Exception var openAIAsyncClient = SamplesConfig.getClient(); var kernelWithCustomDb = SKBuilders.kernel() - .withDefaultAIService(SKBuilders.textEmbeddingGenerationService().build(openAIAsyncClient, "text-embedding-ada-002")) + .withDefaultAIService(SKBuilders.textEmbeddingGenerationService() + .withOpenAIClient(openAIAsyncClient) + .setModelId("text-embedding-ada-002") + .build()) .withMemoryStorage(SKBuilders.memoryStore().build()) .build(); runExampleAsync(kernelWithCustomDb).block(); } - public static Mono runExampleAsync(Kernel kernel) - { + public static Mono runExampleAsync(Kernel kernel) { return storeMemoryAsync(kernel) .then(searchMemoryAsync(kernel, "How do I get started?")) - /* - Output: + /* + Output: - Query: How do I get started? + Query: How do I get started? - Result 1: - URL: : https://github.com/microsoft/semantic-kernel/blob/main/README.md - Title : README: Installation, getting started, and how to contribute + Result 1: + URL: : https://github.com/microsoft/semantic-kernel/blob/main/README.md + Title : README: Installation, getting started, and how to contribute - Result 2: - URL: : https://github.com/microsoft/semantic-kernel/blob/main/samples/dotnet-jupyter-notebooks/00-getting-started.ipynb - Title : Jupyter notebook describing how to get started with the Semantic Kernel + Result 2: + URL: : https://github.com/microsoft/semantic-kernel/blob/main/samples/dotnet-jupyter-notebooks/00-getting-started.ipynb + Title : Jupyter notebook describing how to get started with the Semantic Kernel - */ + */ .then(searchMemoryAsync(kernel, "Can I build a chat with SK?")); @@ -104,8 +104,7 @@ public static Mono runExampleAsync(Kernel kernel) */ } - private static Mono searchMemoryAsync(Kernel kernel, String query) - { + private static Mono searchMemoryAsync(Kernel kernel, String query) { return kernel.getMemory().searchAsync(MEMORY_COLLECTION_NAME, query, 2, 0.5, false) .mapNotNull(memories -> { System.out.println("\nQuery: " + query + "\n"); @@ -121,8 +120,7 @@ private static Mono searchMemoryAsync(Kernel kernel, String query) }); } - private static Mono storeMemoryAsync(Kernel kernel) - { + private static Mono storeMemoryAsync(Kernel kernel) { /* Store some data in the semantic memory. * * When using Azure Cognitive Search the data is automatically indexed on write. @@ -134,31 +132,30 @@ private static Mono storeMemoryAsync(Kernel kernel) return Flux.fromIterable(sampleData().entrySet()) .doFirst(() -> System.out.println("\nAdding some GitHub file URLs and their descriptions to the semantic memory.")) .map(entry -> { - System.out.println("Save '" + entry.getKey() + "' to memory."); - return kernel.getMemory().saveReferenceAsync( + System.out.println("Save '" + entry.getKey() + "' to memory."); + return kernel.getMemory().saveReferenceAsync( MEMORY_COLLECTION_NAME, entry.getValue(), entry.getKey(), "GitHub", entry.getValue(), null); - } + } ) .mapNotNull(Mono::block) .doFinally(signalType -> System.out.println("\n----------------------")) .then(); } - private static Map sampleData() - { - return Arrays.stream(new String[][] { - {"https://github.com/microsoft/semantic-kernel/blob/main/README.md", "README: Installation, getting started, and how to contribute"}, - {"https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/02-running-prompts-from-file.ipynb", "Jupyter notebook describing how to pass prompts from a file to a semantic skill or function"}, - {"https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/00-getting-started.ipynb", "Jupyter notebook describing how to get started with the Semantic Kernel"}, - {"https://github.com/microsoft/semantic-kernel/tree/main/samples/skills/ChatSkill/ChatGPT", "Sample demonstrating how to create a chat skill interfacing with ChatGPT"}, - {"https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel/Memory/VolatileMemoryStore.cs", "C# class that defines a volatile embedding store"}, - {"https://github.com/microsoft/semantic-kernel/blob/main/samples/dotnet/KernelHttpServer/README.md", "README: How to set up a Semantic Kernel Service API using Azure Function Runtime v4"}, - {"https://github.com/microsoft/semantic-kernel/blob/main/samples/apps/chat-summary-webapp-react/README.md", "README: README associated with a sample chat summary react-based webapp"}, + private static Map sampleData() { + return Arrays.stream(new String[][]{ + {"https://github.com/microsoft/semantic-kernel/blob/main/README.md", "README: Installation, getting started, and how to contribute"}, + {"https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/02-running-prompts-from-file.ipynb", "Jupyter notebook describing how to pass prompts from a file to a semantic skill or function"}, + {"https://github.com/microsoft/semantic-kernel/blob/main/samples/notebooks/dotnet/00-getting-started.ipynb", "Jupyter notebook describing how to get started with the Semantic Kernel"}, + {"https://github.com/microsoft/semantic-kernel/tree/main/samples/skills/ChatSkill/ChatGPT", "Sample demonstrating how to create a chat skill interfacing with ChatGPT"}, + {"https://github.com/microsoft/semantic-kernel/blob/main/dotnet/src/SemanticKernel/Memory/VolatileMemoryStore.cs", "C# class that defines a volatile embedding store"}, + {"https://github.com/microsoft/semantic-kernel/blob/main/samples/dotnet/KernelHttpServer/README.md", "README: How to set up a Semantic Kernel Service API using Azure Function Runtime v4"}, + {"https://github.com/microsoft/semantic-kernel/blob/main/samples/apps/chat-summary-webapp-react/README.md", "README: README associated with a sample chat summary react-based webapp"}, }).collect(Collectors.toMap(element -> element[0], element -> element[1])); } } 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 25fb7c6c00c4..13b7288f553f 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 @@ -3,9 +3,9 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.SamplesConfig; import com.microsoft.semantickernel.ai.embeddings.EmbeddingGeneration; -import com.microsoft.semantickernel.builders.SKBuilders; import com.microsoft.semantickernel.coreskills.TextMemorySkill; import com.microsoft.semantickernel.memory.MemoryStore; import com.microsoft.semantickernel.orchestration.SKContext; @@ -18,11 +18,13 @@ import java.util.List; import java.util.stream.Collectors; -public class Example15_MemorySkill -{ +public class Example15_MemorySkill { private static final String MEMORY_COLLECTION_NAME = "aboutMe"; - public static void main(String[] args) { runAsync().block(); } + public static void main(String[] args) { + runAsync().block(); + } + public static Mono runAsync() { // ========= Create a kernel ========= OpenAIAsyncClient client = null; @@ -34,11 +36,15 @@ public static Mono runAsync() { TextCompletion textCompletionService = SKBuilders.textCompletionService() - .build(client, "text-davinci-003"); + .setModelId("text-davinci-003") + .withOpenAIClient(client) + .build(); EmbeddingGeneration textEmbeddingGenerationService = SKBuilders.textEmbeddingGenerationService() - .build(client, "text-embedding-ada-002"); + .withOpenAIClient(client) + .setModelId("text-embedding-ada-002") + .build(); MemoryStore memoryStore = SKBuilders.memoryStore().build(); @@ -69,21 +75,20 @@ public static Mono runAsync() { .maxTokens(2000) .build(); - CompletionSKFunction saveFunctionDefinition = SKBuilders.completionFunctions(kernel) - .createFunction( - "{{memory.save $info}}", - "save", - "", - "save information to memory", - completionConfig - ); + CompletionSKFunction saveFunctionDefinition = SKBuilders.completionFunctions() + .withKernel(kernel) + .setPromptTemplate("{{memory.save $info}}") + .setFunctionName("save") + .setDescription("save information to memory") + .setCompletionConfig(completionConfig) + .build(); CompletionSKFunction memorySaver = kernel.registerSemanticFunction(saveFunctionDefinition); SKContext context = SKBuilders.context() - .with(kernel.getMemory()) - .with(kernel.getSkills()) + .setMemory(kernel.getMemory()) + .setSkills(kernel.getSkills()) .build(); context.setVariable(TextMemorySkill.COLLECTION_PARAM, MEMORY_COLLECTION_NAME) @@ -97,8 +102,8 @@ public static Mono runAsync() { // create a new context to avoid using the variables from the previous example context = SKBuilders.context() - .with(kernel.getMemory()) - .with(kernel.getSkills()) + .setMemory(kernel.getMemory()) + .setSkills(kernel.getSkills()) .build(); String answer = memorySkill.retrieveAsync(MEMORY_COLLECTION_NAME, "info1", context).block(); @@ -154,12 +159,11 @@ public static Mono runAsync() { "Question: {{$input}}\n" + "Answer: "; - CompletionSKFunction recallFunctionDefinition = - kernel.getSemanticFunctionBuilder() - .createFunction( - prompt, - completionConfig - ); + CompletionSKFunction recallFunctionDefinition = SKBuilders.completionFunctions() + .withKernel(kernel) + .setPromptTemplate(prompt) + .setCompletionConfig(completionConfig) + .build(); SKFunction aboutMeOracle = kernel.registerSemanticFunction(recallFunctionDefinition); diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example17_ChatGPT.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example17_ChatGPT.java index d41313b2c894..52658ccf8603 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example17_ChatGPT.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example17_ChatGPT.java @@ -3,8 +3,8 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.SamplesConfig; -import com.microsoft.semantickernel.builders.SKBuilders; import com.microsoft.semantickernel.chatcompletion.ChatCompletion; import com.microsoft.semantickernel.chatcompletion.ChatHistory; import com.microsoft.semantickernel.connectors.ai.openai.chatcompletion.OpenAIChatHistory; @@ -22,7 +22,10 @@ public static void main(String[] args) throws ConfigurationException { Kernel kernel = SKBuilders.kernel() .withAIService( "chat-test", - SKBuilders.chatCompletion().build(client, "chat-test"), + SKBuilders.chatCompletion() + .withOpenAIClient(client) + .setModelId("chat-test") + .build(), true, ChatCompletion.class ) diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example28_ActionPlanner.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example28_ActionPlanner.java index f68a82435447..178a38fcba22 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example28_ActionPlanner.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example28_ActionPlanner.java @@ -3,7 +3,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.SamplesConfig; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.coreskills.TextSkill; import com.microsoft.semantickernel.exceptions.ConfigurationException; import com.microsoft.semantickernel.planner.actionplanner.ActionPlanner; @@ -15,7 +15,10 @@ public static void main(String[] args) throws ConfigurationException { System.out.println("======== Action Planner ========"); var kernel = SKBuilders.kernel() - .withDefaultAIService(SKBuilders.textCompletionService().build(client, "text-davinci-002")) + .withDefaultAIService(SKBuilders.textCompletionService() + .setModelId("text-davinci-002") + .withOpenAIClient(client) + .build()) .build(); kernel.importSkillFromDirectory("SummarizeSkill", SampleSkillsUtil.detectSkillDirLocation(), "SummarizeSkill"); diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example33_StreamingChat.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example33_StreamingChat.java index 400bd2169663..8b002a737ae6 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example33_StreamingChat.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/Example33_StreamingChat.java @@ -4,7 +4,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.SamplesConfig; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.chatcompletion.ChatCompletion; import com.microsoft.semantickernel.chatcompletion.ChatHistory; import com.microsoft.semantickernel.exceptions.ConfigurationException; @@ -25,7 +25,10 @@ public static void main(String[] args) throws ConfigurationException { private static void StartStreamingChatAsync() throws ConfigurationException { OpenAIAsyncClient client = SamplesConfig.getClient(); - ChatCompletion chatCompletion = SKBuilders.chatCompletion().build(client, "gpt-35-turbo"); + ChatCompletion chatCompletion = SKBuilders.chatCompletion() + .withOpenAIClient(client) + .setModelId("gpt-35-turbo") + .build(); System.out.println("Chat content:"); System.out.println("------------------------"); diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/RunAll.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/RunAll.java index 532f5f6efe9c..ca1841d3b83b 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/RunAll.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/RunAll.java @@ -12,13 +12,21 @@ public static void main(String[] args) { List mains = Arrays.asList( Example01_NativeFunctions::main, Example02_Pipeline::main, + Example03_Variables::main, + Example04_CombineLLMPromptsAndNativeCode::main, + Example05_InlineFunctionDefinition::main, Example06_TemplateLanguage::main, Example08_RetryHandler::main, + Example09_FunctionTypes::main, Example12_SequentialPlanner::main, Example13_ConversationSummarySkill::main, Example14_SemanticMemory::main, + Example15_MemorySkill::main, Example17_ChatGPT::main, - Example28_ActionPlanner::main + Example25_ReadOnlyMemoryStore::main, + Example28_ActionPlanner::main, + Example29_Tokenizer::main, + Example33_StreamingChat::main ); mains.forEach(mainMethod -> { diff --git a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/javaspecific/Java_Example01_ResourceLoading.java b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/javaspecific/Java_Example01_ResourceLoading.java index a164f0dbf0de..fcb1a1a1f550 100644 --- a/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/javaspecific/Java_Example01_ResourceLoading.java +++ b/java/samples/sample-code/src/main/java/com/microsoft/semantickernel/samples/syntaxexamples/javaspecific/Java_Example01_ResourceLoading.java @@ -1,7 +1,7 @@ package com.microsoft.semantickernel.samples.syntaxexamples.javaspecific; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.orchestration.SKFunction; import java.io.IOException; 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 6d4c43c9c8d5..db0ea1ab43db 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 @@ -2,7 +2,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.SamplesConfig; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.exceptions.ConfigurationException; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.planner.sequentialplanner.SequentialPlanner; @@ -35,7 +35,9 @@ public static void main(String[] args) throws IOException, ConfigurationExceptio OpenAIAsyncClient client = SamplesConfig.getClient(); var kernel = SKBuilders.kernel() .withDefaultAIService(SKBuilders.textCompletionService() - .build(client, "text-davinci-003")) + .setModelId("text-davinci-003") + .withOpenAIClient(client) + .build()) .build(); diff --git a/java/samples/semantickernel-guice/src/test/java/Foo.java b/java/samples/semantickernel-guice/src/test/java/Foo.java deleted file mode 100644 index b8fde7bc4a48..000000000000 --- a/java/samples/semantickernel-guice/src/test/java/Foo.java +++ /dev/null @@ -1,2 +0,0 @@ -public interface Foo { -} diff --git a/java/samples/semantickernel-guice/pom.xml b/java/samples/semantickernel-samples-guice/pom.xml similarity index 96% rename from java/samples/semantickernel-guice/pom.xml rename to java/samples/semantickernel-samples-guice/pom.xml index 3173223df31d..fe052b6dde35 100644 --- a/java/samples/semantickernel-guice/pom.xml +++ b/java/samples/semantickernel-samples-guice/pom.xml @@ -10,7 +10,7 @@ ../pom.xml - semantickernel-guice + semantickernel-samples-guice jar diff --git a/java/samples/semantickernel-guice/src/main/java/com/microsoft/semantickernel/guice/CompletionFunctionFactory.java b/java/samples/semantickernel-samples-guice/src/main/java/com/microsoft/semantickernel/guice/CompletionFunctionFactory.java similarity index 79% rename from java/samples/semantickernel-guice/src/main/java/com/microsoft/semantickernel/guice/CompletionFunctionFactory.java rename to java/samples/semantickernel-samples-guice/src/main/java/com/microsoft/semantickernel/guice/CompletionFunctionFactory.java index 7404e33979b2..7dcbf4b1848b 100644 --- a/java/samples/semantickernel-guice/src/main/java/com/microsoft/semantickernel/guice/CompletionFunctionFactory.java +++ b/java/samples/semantickernel-samples-guice/src/main/java/com/microsoft/semantickernel/guice/CompletionFunctionFactory.java @@ -4,7 +4,6 @@ import com.microsoft.semantickernel.Kernel; import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig; import com.microsoft.semantickernel.textcompletion.CompletionSKFunction; - import jakarta.inject.Inject; public interface CompletionFunctionFactory { @@ -13,7 +12,8 @@ CompletionSKFunction createFunction( String prompt, String name, PromptTemplateConfig.CompletionConfig completionConfig); class CompletionFunctionFactoryImpl implements CompletionFunctionFactory { - @Inject private Kernel kernel; + @Inject + private Kernel kernel; @Override public CompletionSKFunction createFunction( @@ -21,7 +21,10 @@ public CompletionSKFunction createFunction( String name, PromptTemplateConfig.CompletionConfig completionConfig) { return kernel.getSemanticFunctionBuilder() - .createFunction(prompt, name, null, null, completionConfig); + .setPromptTemplate(prompt) + .setFunctionName(name) + .setCompletionConfig(completionConfig) + .build(); } } } diff --git a/java/samples/semantickernel-guice/src/main/java/com/microsoft/semantickernel/guice/Config.java b/java/samples/semantickernel-samples-guice/src/main/java/com/microsoft/semantickernel/guice/Config.java similarity index 100% rename from java/samples/semantickernel-guice/src/main/java/com/microsoft/semantickernel/guice/Config.java rename to java/samples/semantickernel-samples-guice/src/main/java/com/microsoft/semantickernel/guice/Config.java diff --git a/java/samples/semantickernel-guice/src/main/java/com/microsoft/semantickernel/guice/SemanticKernelModule.java b/java/samples/semantickernel-samples-guice/src/main/java/com/microsoft/semantickernel/guice/SemanticKernelModule.java similarity index 98% rename from java/samples/semantickernel-guice/src/main/java/com/microsoft/semantickernel/guice/SemanticKernelModule.java rename to java/samples/semantickernel-samples-guice/src/main/java/com/microsoft/semantickernel/guice/SemanticKernelModule.java index 835e571e10ee..bb2e7222741b 100644 --- a/java/samples/semantickernel-guice/src/main/java/com/microsoft/semantickernel/guice/SemanticKernelModule.java +++ b/java/samples/semantickernel-samples-guice/src/main/java/com/microsoft/semantickernel/guice/SemanticKernelModule.java @@ -8,7 +8,7 @@ import com.microsoft.semantickernel.Kernel; import com.microsoft.semantickernel.KernelConfig; import com.microsoft.semantickernel.ai.embeddings.EmbeddingGeneration; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.connectors.ai.openai.textcompletion.OpenAITextCompletion; import com.microsoft.semantickernel.connectors.ai.openai.textembeddings.OpenAITextEmbeddingGeneration; import com.microsoft.semantickernel.memory.NullMemory; diff --git a/java/samples/semantickernel-guice/src/test/java/com/microsoft/semantickernel/guice/CompletionFunctionExample.java b/java/samples/semantickernel-samples-guice/src/test/java/com/microsoft/semantickernel/guice/CompletionFunctionExample.java similarity index 100% rename from java/samples/semantickernel-guice/src/test/java/com/microsoft/semantickernel/guice/CompletionFunctionExample.java rename to java/samples/semantickernel-samples-guice/src/test/java/com/microsoft/semantickernel/guice/CompletionFunctionExample.java diff --git a/java/samples/semantickernel-guice/src/test/java/com/microsoft/semantickernel/guice/GuiceEmbeddingExample.java b/java/samples/semantickernel-samples-guice/src/test/java/com/microsoft/semantickernel/guice/GuiceEmbeddingExample.java similarity index 100% rename from java/samples/semantickernel-guice/src/test/java/com/microsoft/semantickernel/guice/GuiceEmbeddingExample.java rename to java/samples/semantickernel-samples-guice/src/test/java/com/microsoft/semantickernel/guice/GuiceEmbeddingExample.java diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/Kernel.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/Kernel.java index c5e621769a9b..66419818d730 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/Kernel.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/Kernel.java @@ -2,17 +2,15 @@ package com.microsoft.semantickernel; import com.microsoft.semantickernel.ai.embeddings.TextEmbeddingGeneration; +import com.microsoft.semantickernel.builders.Buildable; import com.microsoft.semantickernel.builders.BuildersSingleton; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.builders.SemanticKernelBuilder; import com.microsoft.semantickernel.memory.MemoryStore; -import com.microsoft.semantickernel.memory.NullMemory; import com.microsoft.semantickernel.memory.SemanticTextMemory; import com.microsoft.semantickernel.orchestration.ContextVariables; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.orchestration.SKFunction; import com.microsoft.semantickernel.services.AIService; -import com.microsoft.semantickernel.services.AIServiceCollection; -import com.microsoft.semantickernel.services.AIServiceProvider; import com.microsoft.semantickernel.templateengine.PromptTemplateEngine; import com.microsoft.semantickernel.textcompletion.CompletionSKFunction; import java.util.function.Function; @@ -21,7 +19,7 @@ import reactor.core.publisher.Mono; /** Interface for the semantic kernel. */ -public interface Kernel extends SkillExecutor { +public interface Kernel extends SkillExecutor, Buildable { /** * Settings required to execute functions, including details about AI dependencies, e.g. @@ -86,24 +84,18 @@ T getService(@Nullable String name, Class clazz) /** Obtains a semantic function with the given name */ SKFunction getFunction(String skill, String function); - class Builder { - @Nullable private KernelConfig config = null; - @Nullable private PromptTemplateEngine promptTemplateEngine = null; - @Nullable private AIServiceCollection aiServices = new AIServiceCollection(); - private Supplier memoryFactory = () -> new NullMemory(); - private Supplier memoryStorageFactory = null; - ; + static Builder builder() { + return BuildersSingleton.INST.getInstance(Kernel.Builder.class); + } + interface Builder extends SemanticKernelBuilder { /** * Set the kernel configuration * * @param kernelConfig Kernel configuration * @return Builder */ - public Builder withConfiguration(KernelConfig kernelConfig) { - this.config = kernelConfig; - return this; - } + Builder withConfiguration(KernelConfig kernelConfig); /** * Add prompt template engine to the kernel to be built. @@ -111,11 +103,7 @@ public Builder withConfiguration(KernelConfig kernelConfig) { * @param promptTemplateEngine Prompt template engine to add. * @return Updated kernel builder including the prompt template engine. */ - public Builder withPromptTemplateEngine(PromptTemplateEngine promptTemplateEngine) { - Verify.notNull(promptTemplateEngine); - this.promptTemplateEngine = promptTemplateEngine; - return this; - } + Builder withPromptTemplateEngine(PromptTemplateEngine promptTemplateEngine); /** * Add memory storage to the kernel to be built. @@ -123,11 +111,7 @@ public Builder withPromptTemplateEngine(PromptTemplateEngine promptTemplateEngin * @param storage Storage to add. * @return Updated kernel builder including the memory storage. */ - public Builder withMemoryStorage(MemoryStore storage) { - Verify.notNull(storage); - this.memoryStorageFactory = () -> storage; - return this; - } + Builder withMemoryStorage(MemoryStore storage); /** * Add memory storage factory to the kernel. @@ -135,11 +119,7 @@ public Builder withMemoryStorage(MemoryStore storage) { * @param factory The storage factory. * @return Updated kernel builder including the memory storage. */ - public Builder withMemoryStorage(Supplier factory) { - Verify.notNull(factory); - this.memoryStorageFactory = factory::get; - return this; - } + Builder withMemoryStorage(Supplier factory); /** * Adds an instance to the services collection @@ -147,11 +127,7 @@ public Builder withMemoryStorage(Supplier factory) { * @param instance The instance. * @return The builder. */ - public Builder withDefaultAIService(T instance) { - Class clazz = (Class) instance.getClass(); - this.aiServices.setService(instance, clazz); - return this; - } + Builder withDefaultAIService(T instance); /** * Adds an instance to the services collection @@ -160,10 +136,7 @@ public Builder withDefaultAIService(T instance) { * @param clazz The class of the instance. * @return The builder. */ - public Builder withDefaultAIService(T instance, Class clazz) { - this.aiServices.setService(instance, clazz); - return this; - } + Builder withDefaultAIService(T instance, Class clazz); /** * Adds a factory method to the services collection @@ -171,11 +144,7 @@ public Builder withDefaultAIService(T instance, Class c * @param factory The factory method that creates the AI service instances of type T. * @param clazz The class of the instance. */ - public Builder withDefaultAIService( - Supplier factory, Class clazz) { - this.aiServices.setService(factory, clazz); - return this; - } + Builder withDefaultAIService(Supplier factory, Class clazz); /** * Adds an instance to the services collection @@ -185,12 +154,8 @@ public Builder withDefaultAIService( * @param setAsDefault Optional: set as the default AI service for type T * @param clazz The class of the instance. */ - public Builder withAIService( - @Nullable String serviceId, T instance, boolean setAsDefault, Class clazz) { - this.aiServices.setService(serviceId, instance, setAsDefault, clazz); - - return this; - } + Builder withAIService( + @Nullable String serviceId, T instance, boolean setAsDefault, Class clazz); /** * Adds a factory method to the services collection @@ -200,15 +165,11 @@ public Builder withAIService( * @param setAsDefault Optional: set as the default AI service for type T * @param clazz The class of the instance. */ - public Builder withAIServiceFactory( + Builder withAIServiceFactory( @Nullable String serviceId, Function factory, boolean setAsDefault, - Class clazz) { - this.aiServices.setService( - serviceId, (Supplier) () -> factory.apply(this.config), setAsDefault, clazz); - return this; - } + Class clazz); /** * Add a semantic text memory entity to the kernel to be built. @@ -216,11 +177,7 @@ public Builder withAIServiceFactory( * @param memory Semantic text memory entity to add. * @return Updated kernel builder including the semantic text memory entity. */ - public Builder withMemory(SemanticTextMemory memory) { - Verify.notNull(memory); - this.memoryFactory = () -> memory; - return this; - } + Builder withMemory(SemanticTextMemory memory); /** * Add memory storage and an embedding generator to the kernel to be built. @@ -229,46 +186,7 @@ public Builder withMemory(SemanticTextMemory memory) { * @param embeddingGenerator Embedding generator to add. * @return Updated kernel builder including the memory storage and embedding generator. */ - public Builder withMemoryStorageAndTextEmbeddingGeneration( - MemoryStore storage, TextEmbeddingGeneration embeddingGenerator) { - Verify.notNull(storage); - Verify.notNull(embeddingGenerator); - this.memoryFactory = - () -> - SKBuilders.semanticTextMemory() - .setEmbeddingGenerator(embeddingGenerator) - .setStorage(storage) - .build(); - return this; - } - - /** - * Build the kernel - * - * @return Kernel - */ - public Kernel build() { - if (config == null) { - config = SKBuilders.kernelConfig().build(); - } - - return BuildersSingleton.INST - .getKernelBuilder() - .build( - config, - promptTemplateEngine, - memoryFactory.get(), - memoryStorageFactory == null ? null : memoryStorageFactory.get(), - aiServices.build()); - } - } - - interface InternalBuilder { - Kernel build( - KernelConfig kernelConfig, - @Nullable PromptTemplateEngine promptTemplateEngine, - @Nullable SemanticTextMemory memory, - @Nullable MemoryStore memoryStore, - @Nullable AIServiceProvider aiServiceProvider); + Builder withMemoryStorageAndTextEmbeddingGeneration( + MemoryStore storage, TextEmbeddingGeneration embeddingGenerator); } } diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/SKBuilders.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/SKBuilders.java similarity index 82% rename from java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/SKBuilders.java rename to java/semantickernel-api/src/main/java/com/microsoft/semantickernel/SKBuilders.java index 63657a72bd56..685c12288f77 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/SKBuilders.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/SKBuilders.java @@ -1,9 +1,8 @@ // Copyright (c) Microsoft. All rights reserved. -package com.microsoft.semantickernel.builders; +package com.microsoft.semantickernel; -import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.KernelConfig; import com.microsoft.semantickernel.ai.embeddings.EmbeddingGeneration; +import com.microsoft.semantickernel.builders.BuildersSingleton; import com.microsoft.semantickernel.chatcompletion.ChatCompletion; import com.microsoft.semantickernel.memory.MemoryStore; import com.microsoft.semantickernel.memory.SemanticTextMemory; @@ -20,19 +19,8 @@ public class SKBuilders { // Prevent creating object private SKBuilders() {} - /** - * A CompletionSKFunction builder, the created function will be registered on the kernel - * provided - * - * @param kernel The kernel to register the function on - * @return a function builder - */ - public static CompletionSKFunction.Builder completionFunctions(Kernel kernel) { - return FunctionBuilders.getCompletionBuilder(kernel); - } - public static TextCompletion.Builder textCompletionService() { - return BuildersSingleton.INST.getTextCompletionBuilder(); + return BuildersSingleton.INST.getInstance(TextCompletion.Builder.class); } public static EmbeddingGeneration.Builder textEmbeddingGenerationService() { @@ -40,7 +28,7 @@ public static EmbeddingGeneration.Builder textEmbeddingGenerationService } public static Kernel.Builder kernel() { - return new Kernel.Builder(); + return BuildersSingleton.INST.getInstance(Kernel.Builder.class); } public static KernelConfig.Builder kernelConfig() { @@ -82,4 +70,8 @@ public static ChatCompletion.Builder chatCompletion() { public static MemoryStore.Builder memoryStore() { return BuildersSingleton.INST.getMemoryStoreBuilder(); } + + public static CompletionSKFunction.Builder completionFunctions() { + return BuildersSingleton.INST.getInstance(CompletionSKFunction.Builder.class); + } } diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/ai/embeddings/EmbeddingGeneration.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/ai/embeddings/EmbeddingGeneration.java index e3d583e486d0..8af083f0a3f2 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/ai/embeddings/EmbeddingGeneration.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/ai/embeddings/EmbeddingGeneration.java @@ -2,12 +2,15 @@ package com.microsoft.semantickernel.ai.embeddings; import com.azure.ai.openai.OpenAIAsyncClient; +import com.microsoft.semantickernel.builders.Buildable; +import com.microsoft.semantickernel.builders.BuildersSingleton; +import com.microsoft.semantickernel.builders.SemanticKernelBuilder; import com.microsoft.semantickernel.services.AIService; import java.util.List; import reactor.core.publisher.Mono; /** Interface for text embedding generation services */ -public interface EmbeddingGeneration extends AIService { +public interface EmbeddingGeneration extends AIService, Buildable { /** * Generates a list of embeddings associated to the data * @@ -16,7 +19,14 @@ public interface EmbeddingGeneration extends AIService { */ Mono> generateEmbeddingsAsync(List data); - interface Builder { - EmbeddingGeneration build(OpenAIAsyncClient client, String modelId); + static Builder builder() { + return BuildersSingleton.INST.getInstance(Builder.class); + } + + interface Builder extends SemanticKernelBuilder> { + + Builder withOpenAIClient(OpenAIAsyncClient client); + + Builder setModelId(String modelId); } } diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/Buildable.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/Buildable.java new file mode 100644 index 000000000000..b04296b6c2fc --- /dev/null +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/Buildable.java @@ -0,0 +1,8 @@ +// Copyright (c) Microsoft. All rights reserved. +package com.microsoft.semantickernel.builders; + +/** + * A marker interface for all classes that are buildable, typically via obtaining a builder from + * {@link com.microsoft.semantickernel.SKBuilders}. + */ +public interface Buildable {} diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/BuildersSingleton.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/BuildersSingleton.java index 82fa02fdd8d3..3702a5931d0f 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/BuildersSingleton.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/BuildersSingleton.java @@ -11,6 +11,7 @@ import com.microsoft.semantickernel.semanticfunctions.PromptTemplate; import com.microsoft.semantickernel.skilldefinition.ReadOnlySkillCollection; import com.microsoft.semantickernel.templateengine.PromptTemplateEngine; +import com.microsoft.semantickernel.textcompletion.CompletionSKFunction; import com.microsoft.semantickernel.textcompletion.TextCompletion; import java.util.HashMap; import java.util.Map; @@ -24,8 +25,8 @@ public enum BuildersSingleton { INST; // Fallback classes in case the META-INF/services directory is missing - private static final String FALLBACK_FUNCTION_BUILDER_CLASS = - "com.microsoft.semantickernel.SkFunctionBuilders"; + private static final String FALLBACK_COMPLETION_FUNCTION_BUILDER_CLASS = + "com.microsoft.semantickernel.orchestration.DefaultCompletionSKFunction$Builder"; private static final String FALLBACK_KERNEL_BUILDER_CLASS = "com.microsoft.semantickernel.DefaultKernel$Builder"; private static final String FALLBACK_TEXT_COMPLETION_BUILDER_CLASS = @@ -49,28 +50,33 @@ public enum BuildersSingleton { private static final String FALLBACK_MEMORY_STORE_BUILDER_CLASS = "com.microsoft.semantickernel.memory.VolatileMemoryStore$Builder"; - private final Map builders = new HashMap<>(); + private final Map, Supplier> + builders = new HashMap<>(); BuildersSingleton() { try { - registerBuilder(FunctionBuilders.class, FALLBACK_FUNCTION_BUILDER_CLASS); - registerBuilder(Kernel.InternalBuilder.class, FALLBACK_KERNEL_BUILDER_CLASS); + registerBuilder( + CompletionSKFunction.Builder.class, FALLBACK_COMPLETION_FUNCTION_BUILDER_CLASS); + registerBuilder(Kernel.Builder.class, FALLBACK_KERNEL_BUILDER_CLASS); registerBuilder(TextCompletion.Builder.class, FALLBACK_TEXT_COMPLETION_BUILDER_CLASS); registerBuilder( EmbeddingGeneration.Builder.class, FALLBACK_TEXT_EMBEDDING_GENERATION_BUILDER_CLASS); registerBuilder( ReadOnlySkillCollection.Builder.class, FALLBACK_SKILL_COLLECTION_BUILDER_CLASS); + registerBuilder(PromptTemplate.Builder.class, FALLBACK_PROMPT_TEMPLATE_BUILDER_CLASS); registerBuilder(ContextVariables.Builder.class, FALLBACK_VARIABLE_BUILDER_CLASS); + registerBuilder(SemanticTextMemory.Builder.class, FALLBACK_SEMANTIC_TEXT_MEMORY_CLASS); + registerBuilder(SKContext.Builder.class, FALLBACK_CONTEXT_BUILDER_CLASS); registerBuilder( PromptTemplateEngine.Builder.class, FALLBACK_PROMPT_TEMPLATE_ENGINE_BUILDER_CLASS); + registerBuilder(ChatCompletion.Builder.class, FALLBACK_CHAT_COMPLETION_BUILDER_CLASS); registerBuilder(MemoryStore.Builder.class, FALLBACK_MEMORY_STORE_BUILDER_CLASS); - } catch (Throwable e) { Logger LOGGER = LoggerFactory.getLogger(BuildersSingleton.class); LOGGER.error("Failed to discover Semantic Kernel Builders", e); @@ -96,33 +102,18 @@ public enum BuildersSingleton { } } - private void registerBuilder(Class clazz, String fallbackClassName) { - builders.put(clazz, ServiceLoadUtil.findServiceLoader(clazz, fallbackClassName)); + private > void registerBuilder( + Class clazz, String fallbackClassName) { + builders.put( + clazz, + (Supplier) + ServiceLoadUtil.findServiceLoader(clazz, fallbackClassName)); } - private T getInstance(Class clazz) { + public > T getInstance(Class clazz) { return (T) builders.get(clazz).get(); } - /** - * Builder for creating a {@link - * com.microsoft.semantickernel.textcompletion.CompletionSKFunction} - * - * @return a {@link FunctionBuilders} - */ - public FunctionBuilders getFunctionBuilders() { - return getInstance(FunctionBuilders.class); - } - - /** - * Builder for creating a {@link Kernel} - * - * @return a {@link Kernel.InternalBuilder} - */ - public Kernel.InternalBuilder getKernelBuilder() { - return getInstance(Kernel.InternalBuilder.class); - } - /** * Builder for creating a {@link TextCompletion} * diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/FunctionBuilders.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/FunctionBuilders.java deleted file mode 100644 index a77bcf1ee68c..000000000000 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/FunctionBuilders.java +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -package com.microsoft.semantickernel.builders; - -import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.textcompletion.CompletionSKFunction; - -/** Provides various builders for completion functions */ -public interface FunctionBuilders { - - static CompletionSKFunction.Builder getCompletionBuilder(Kernel kernel) { - return BuildersSingleton.INST.getFunctionBuilders().completionBuilders(kernel); - } - - CompletionSKFunction.Builder completionBuilders(Kernel kernel); -} diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/SemanticKernelBuilder.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/SemanticKernelBuilder.java new file mode 100644 index 000000000000..784e460b5a4b --- /dev/null +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/SemanticKernelBuilder.java @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft. All rights reserved. +package com.microsoft.semantickernel.builders; + +/** + * Interface for all builders that create Buildable objects. Typically, these builders can be + * obtained via {@link com.microsoft.semantickernel.SKBuilders}. + * + * @param + */ +public interface SemanticKernelBuilder { + + /** + * Build the object. + * + * @return a constructed object. + */ + T build(); +} diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/ServiceLoadUtil.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/ServiceLoadUtil.java index f2f17af31a43..fd1b2a1b1ed7 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/ServiceLoadUtil.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/builders/ServiceLoadUtil.java @@ -16,7 +16,8 @@ public class ServiceLoadUtil { private ServiceLoadUtil() {} - public static Supplier findServiceLoader(Class clazz, String alternativeClassName) { + public static > + Supplier findServiceLoader(Class clazz, String alternativeClassName) { List services = findAllServiceLoaders(clazz); T impl = null; @@ -25,24 +26,26 @@ public static Supplier findServiceLoader(Class clazz, String alternati impl = services.get(0); } - try { - // Service loader not found, attempt to load the alternative class - Object instance = - Class.forName(alternativeClassName).getDeclaredConstructor().newInstance(); - if (clazz.isInstance(instance)) { - impl = (T) instance; + if (impl == null) { + try { + // Service loader not found, attempt to load the alternative class + Object instance = + Class.forName(alternativeClassName).getDeclaredConstructor().newInstance(); + if (clazz.isInstance(instance)) { + impl = (T) instance; + } + } catch (ClassNotFoundException + | InvocationTargetException + | InstantiationException + | IllegalAccessException + | NoSuchMethodException + | RuntimeException e) { + LOGGER.error("Unable to load service " + clazz.getName() + " ", e); } - } catch (ClassNotFoundException - | InvocationTargetException - | InstantiationException - | IllegalAccessException - | NoSuchMethodException - | RuntimeException e) { - LOGGER.error("Unable to load service " + clazz.getName() + " ", e); - } - if (impl == null) { - throw new RuntimeException("Service not found: " + clazz.getName()); + if (impl == null) { + throw new RuntimeException("Service not found: " + clazz.getName()); + } } try { diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/chatcompletion/ChatCompletion.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/chatcompletion/ChatCompletion.java index 184bf78a1162..29004e741329 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/chatcompletion/ChatCompletion.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/chatcompletion/ChatCompletion.java @@ -3,6 +3,9 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.azure.ai.openai.models.ChatCompletions; +import com.microsoft.semantickernel.builders.Buildable; +import com.microsoft.semantickernel.builders.BuildersSingleton; +import com.microsoft.semantickernel.builders.SemanticKernelBuilder; import com.microsoft.semantickernel.services.AIService; import com.microsoft.semantickernel.textcompletion.TextCompletion; import javax.annotation.Nullable; @@ -10,7 +13,7 @@ import reactor.core.publisher.Mono; public interface ChatCompletion - extends AIService, TextCompletion { + extends AIService, TextCompletion, Buildable { /** * Generate a new chat message * @@ -30,12 +33,19 @@ Mono generateMessageAsync( ChatHistoryType createNewChat(@Nullable String instructions); Flux generateMessageStream( - ChatHistory chatHistory, @Nullable ChatRequestSettings requestSettings); + ChatHistoryType chatHistory, @Nullable ChatRequestSettings requestSettings); Flux getStreamingChatCompletionsAsync( - ChatHistory chat, ChatRequestSettings requestSettings); + ChatHistoryType chat, ChatRequestSettings requestSettings); - interface Builder { - ChatCompletion build(OpenAIAsyncClient client, String modelId); + static Builder builder() { + return BuildersSingleton.INST.getInstance(Builder.class); + } + + interface Builder extends SemanticKernelBuilder { + + Builder withOpenAIClient(OpenAIAsyncClient client); + + Builder setModelId(String modelId); } } diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/extensions/KernelExtensions.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/extensions/KernelExtensions.java index f8dee533d4f1..f4c3c2026ee5 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/extensions/KernelExtensions.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/extensions/KernelExtensions.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.microsoft.semantickernel.KernelException; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.semanticfunctions.PromptTemplate; import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig; import com.microsoft.semantickernel.semanticfunctions.SemanticFunctionConfig; @@ -77,9 +77,10 @@ public static Map importSemanticSkillFromDirecto PromptTemplate promptTemplate = SKBuilders.promptTemplate() - .withPromptTemplate(template) - .withPromptTemplateConfig(config) - .build(promptTemplateEngine); + .setPromptTemplate(template) + .setPromptTemplateConfig(config) + .setPromptTemplateEngine(promptTemplateEngine) + .build(); skills.put(dir.getName(), new SemanticFunctionConfig(config, promptTemplate)); } catch (IOException e) { @@ -110,9 +111,10 @@ public static Map importSemanticSkillFromResourc PromptTemplate promptTemplate = SKBuilders.promptTemplate() - .withPromptTemplate(template) - .withPromptTemplateConfig(config) - .build(promptTemplateEngine); + .setPromptTemplate(template) + .setPromptTemplateConfig(config) + .setPromptTemplateEngine(promptTemplateEngine) + .build(); skills.put(functionName, new SemanticFunctionConfig(config, promptTemplate)); diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/memory/MemoryStore.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/memory/MemoryStore.java index 0846cf16f2c4..0be920930c44 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/memory/MemoryStore.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/memory/MemoryStore.java @@ -2,6 +2,9 @@ package com.microsoft.semantickernel.memory; import com.microsoft.semantickernel.ai.embeddings.Embedding; +import com.microsoft.semantickernel.builders.Buildable; +import com.microsoft.semantickernel.builders.BuildersSingleton; +import com.microsoft.semantickernel.builders.SemanticKernelBuilder; import java.util.Collection; import java.util.List; import javax.annotation.Nonnull; @@ -9,7 +12,7 @@ import reactor.util.function.Tuple2; /** An interface for storing and retrieving indexed {@link MemoryRecord} objects in a data store. */ -public interface MemoryStore { +public interface MemoryStore extends Buildable { /** * Creates a new collection in the data store. @@ -145,7 +148,9 @@ Mono> getNearestMatchAsync( double minRelevanceScore, boolean withEmbedding); - interface Builder { - MemoryStore build(); + static Builder builder() { + return BuildersSingleton.INST.getInstance(Builder.class); } + + interface Builder extends SemanticKernelBuilder {} } diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/memory/SemanticTextMemory.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/memory/SemanticTextMemory.java index fa42c0584b59..556b35dc0d84 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/memory/SemanticTextMemory.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/memory/SemanticTextMemory.java @@ -2,13 +2,16 @@ package com.microsoft.semantickernel.memory; import com.microsoft.semantickernel.ai.embeddings.EmbeddingGeneration; +import com.microsoft.semantickernel.builders.Buildable; +import com.microsoft.semantickernel.builders.BuildersSingleton; +import com.microsoft.semantickernel.builders.SemanticKernelBuilder; import java.util.List; import javax.annotation.CheckReturnValue; import javax.annotation.Nullable; import reactor.core.publisher.Mono; /** An interface for semantic memory that creates and recalls memories associated with text. */ -public interface SemanticTextMemory { +public interface SemanticTextMemory extends Buildable { @CheckReturnValue SemanticTextMemory copy(); @@ -96,11 +99,13 @@ public Mono> searchAsync( */ public Mono> getCollectionsAsync(); - interface Builder { + static Builder builder() { + return BuildersSingleton.INST.getInstance(Builder.class); + } + + interface Builder extends SemanticKernelBuilder { Builder setStorage(MemoryStore storage); Builder setEmbeddingGenerator(EmbeddingGeneration embeddingGenerator); - - SemanticTextMemory build(); } } diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/AbstractSKContext.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/AbstractSKContext.java index 59614e6c57cb..60e70d29bcb0 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/AbstractSKContext.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/AbstractSKContext.java @@ -1,7 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. package com.microsoft.semantickernel.orchestration; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.memory.SemanticTextMemory; import com.microsoft.semantickernel.skilldefinition.ReadOnlySkillCollection; import javax.annotation.CheckReturnValue; @@ -43,7 +43,8 @@ protected AbstractSKContext( ContextVariables variables, @Nullable SemanticTextMemory memory, @Nullable ReadOnlySkillCollection skills) { - this.variables = InternalBuildersSingleton.variables().build(variables.asMap()); + this.variables = + SKBuilders.variables().withVariables(variables.asMap()).build().writableClone(); if (memory != null) { this.memory = memory.copy(); diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/AbstractSkFunction.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/AbstractSkFunction.java index 771a8b34ed44..e7bb28a0ce33 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/AbstractSkFunction.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/AbstractSkFunction.java @@ -1,7 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. package com.microsoft.semantickernel.orchestration; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.memory.NullMemory; import com.microsoft.semantickernel.memory.SemanticTextMemory; import com.microsoft.semantickernel.skilldefinition.KernelSkillsSupplier; @@ -88,8 +88,8 @@ public Mono invokeAsync( context = SKBuilders.context() - .with(NullMemory.getInstance()) - .with(skillsSupplier == null ? null : skillsSupplier.get()) + .setMemory(NullMemory.getInstance()) + .setSkills(skillsSupplier == null ? null : skillsSupplier.get()) .build(); } else { context = context.copy(); @@ -113,8 +113,8 @@ public Mono invokeAsync( if (context == null) { context = SKBuilders.context() - .with(SKBuilders.variables().build()) - .with(NullMemory.getInstance()) + .setVariables(SKBuilders.variables().build()) + .setMemory(NullMemory.getInstance()) .build(); } else { context = context.copy(); @@ -223,7 +223,11 @@ public Mono invokeWithCustomInputAsync( @Nullable SemanticTextMemory semanticMemory, @Nullable ReadOnlySkillCollection skills) { SKContext tmpContext = - SKBuilders.context().with(variables).with(semanticMemory).with(skills).build(); + SKBuilders.context() + .setVariables(variables) + .setMemory(semanticMemory) + .setSkills(skills) + .build(); return invokeAsync(tmpContext, null); } diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/ContextVariables.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/ContextVariables.java index 7f4d1586836b..5b8d18a2f162 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/ContextVariables.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/ContextVariables.java @@ -3,6 +3,9 @@ // Copyright (c) Microsoft. All rights reserved. +import com.microsoft.semantickernel.builders.Buildable; +import com.microsoft.semantickernel.builders.BuildersSingleton; +import com.microsoft.semantickernel.builders.SemanticKernelBuilder; import java.util.Map; import javax.annotation.Nullable; @@ -10,7 +13,7 @@ * Context Variables is a data structure that holds temporary data while a task is being performed. * It is accessed by functions in the pipeline. */ -public interface ContextVariables { +public interface ContextVariables extends Buildable { /** Default key for the main input */ String MAIN_KEY = "input"; @@ -44,9 +47,12 @@ public interface ContextVariables { */ String prettyPrint(); + static Builder builder() { + return BuildersSingleton.INST.getInstance(ContextVariables.Builder.class); + } + /** Builder for ContextVariables */ - interface Builder { - ContextVariables build(); + interface Builder extends SemanticKernelBuilder { /** * Builds an instance with the given content in the default main key diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/InternalBuildersSingleton.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/InternalBuildersSingleton.java deleted file mode 100644 index 5d6fca862f9d..000000000000 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/InternalBuildersSingleton.java +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -package com.microsoft.semantickernel.orchestration; - -import com.microsoft.semantickernel.builders.ServiceLoadUtil; -import java.util.function.Supplier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@SuppressWarnings("ImmutableEnumChecker") -enum InternalBuildersSingleton { - INST; - - // Fallback classes in case the META-INF/services directory is missing - - private static final String FALLBACK_VARIABLE_BUILDER_CLASS = - "com.microsoft.semantickernel.orchestration.DefaultContextVariables$WritableBuilder"; - - private final Supplier variables; - - InternalBuildersSingleton() { - try { - variables = - ServiceLoadUtil.findServiceLoader( - WritableContextVariables.Builder.class, - FALLBACK_VARIABLE_BUILDER_CLASS); - - } catch (Throwable e) { - Logger LOGGER = LoggerFactory.getLogger(InternalBuildersSingleton.class); - LOGGER.error("Failed to discover Semantic Kernel Builders", e); - LOGGER.error( - "This is likely due to:\n\n" - + "- The Semantic Kernel implementation (typically provided by" - + " semantickernel-core) is not present on the classpath at runtime, ensure" - + " that this dependency is available at runtime. In maven this would be" - + " achieved by adding:\n" - + "\n" - + " \n" - + " com.microsoft.semantickernel\n" - + " semantickernel-core\n" - + " ${skversion}\n" - + " runtime\n" - + " \n\n" - + "- The META-INF/services files that define the service loading have been" - + " filtered out and are not present within the running application\n\n" - + "- The class names have been changed (for instance shaded) preventing" - + " discovering the classes"); - - throw e; - } - } - - public static WritableContextVariables.Builder variables() { - return INST.variables.get(); - } -} diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/SKContext.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/SKContext.java index 096070d665d1..1e64ddeddee2 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/SKContext.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/orchestration/SKContext.java @@ -2,14 +2,16 @@ package com.microsoft.semantickernel.orchestration; import com.microsoft.semantickernel.Kernel; +import com.microsoft.semantickernel.builders.Buildable; +import com.microsoft.semantickernel.builders.BuildersSingleton; +import com.microsoft.semantickernel.builders.SemanticKernelBuilder; import com.microsoft.semantickernel.memory.SemanticTextMemory; import com.microsoft.semantickernel.skilldefinition.ReadOnlySkillCollection; -import java.util.function.Supplier; import javax.annotation.CheckReturnValue; import reactor.util.annotation.Nullable; /** Semantic Kernel context. */ -public interface SKContext { +public interface SKContext extends Buildable { SKContext build( ContextVariables variables, @@ -91,16 +93,13 @@ SKContext build( */ SKContext copy(); - interface BuilderSupplier extends Supplier {} - - interface Builder { - SKContext build(ReadOnlySkillCollection skills); - - SKContext build(); + static Builder builder() { + return BuildersSingleton.INST.getInstance(SKContext.Builder.class); + } - SKContext build(Class clazz); + interface Builder extends SemanticKernelBuilder { - Builder with(ContextVariables variables); + Builder setVariables(ContextVariables variables); /** * Sets the skills @@ -108,7 +107,7 @@ interface Builder { * @param skills null argument will be ignored * @return Context for fluent calls */ - Builder with(@Nullable ReadOnlySkillCollection skills); + Builder setSkills(@Nullable ReadOnlySkillCollection skills); /** * Sets the memory @@ -116,7 +115,7 @@ interface Builder { * @param memory null argument will be ignored * @return Context for fluent calls */ - Builder with(@Nullable SemanticTextMemory memory); + Builder setMemory(@Nullable SemanticTextMemory memory); Builder clone(SKContext context); @@ -127,6 +126,6 @@ interface Builder { * @param kernel Kernel to use * @return Context */ - SKContext build(Kernel kernel); + Builder withKernel(Kernel kernel); } } diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/PromptTemplate.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/PromptTemplate.java index bfae81ea8cee..21c7ca4b0fdc 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/PromptTemplate.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/semanticfunctions/PromptTemplate.java @@ -1,6 +1,9 @@ // Copyright (c) Microsoft. All rights reserved. package com.microsoft.semantickernel.semanticfunctions; +import com.microsoft.semantickernel.builders.Buildable; +import com.microsoft.semantickernel.builders.BuildersSingleton; +import com.microsoft.semantickernel.builders.SemanticKernelBuilder; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.skilldefinition.ParameterView; import com.microsoft.semantickernel.templateengine.PromptTemplateEngine; @@ -8,7 +11,7 @@ import reactor.core.publisher.Mono; /** Interface for prompt template */ -public interface PromptTemplate { +public interface PromptTemplate extends Buildable { /** * Get the list of parameters required by the template, using configuration and template info * @@ -24,13 +27,16 @@ public interface PromptTemplate { */ Mono renderAsync(SKContext executionContext); - abstract class Builder { - protected Builder() {} + static Builder builder() { + return BuildersSingleton.INST.getInstance(Builder.class); + } + + interface Builder extends SemanticKernelBuilder { - public abstract PromptTemplate build(PromptTemplateEngine promptTemplateEngine); + Builder setPromptTemplate(String promptTemplate); - public abstract Builder withPromptTemplate(String promptTemplate); + Builder setPromptTemplateConfig(PromptTemplateConfig config); - public abstract Builder withPromptTemplateConfig(PromptTemplateConfig config); + Builder setPromptTemplateEngine(PromptTemplateEngine promptTemplateEngine); } } diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/skilldefinition/ReadOnlySkillCollection.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/skilldefinition/ReadOnlySkillCollection.java index 19e94be0dfa4..27dee0903247 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/skilldefinition/ReadOnlySkillCollection.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/skilldefinition/ReadOnlySkillCollection.java @@ -1,6 +1,9 @@ // Copyright (c) Microsoft. All rights reserved. package com.microsoft.semantickernel.skilldefinition; +import com.microsoft.semantickernel.builders.Buildable; +import com.microsoft.semantickernel.builders.BuildersSingleton; +import com.microsoft.semantickernel.builders.SemanticKernelBuilder; import com.microsoft.semantickernel.orchestration.SKFunction; import java.util.Map; import javax.annotation.CheckReturnValue; @@ -11,7 +14,7 @@ * *

This is read only */ -public interface ReadOnlySkillCollection { +public interface ReadOnlySkillCollection extends Buildable { String GlobalSkill = "_GLOBAL_FUNCTIONS_"; @@ -46,7 +49,11 @@ > T getFunction( boolean hasFunction(String skillName, String functionName); - public interface Builder { - public ReadOnlySkillCollection build(); + static Builder builder() { + return BuildersSingleton.INST.getInstance(Builder.class); + } + + interface Builder extends SemanticKernelBuilder { + ReadOnlySkillCollection build(); } } diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/templateengine/PromptTemplateEngine.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/templateengine/PromptTemplateEngine.java index ec901c91fef9..77c8054ddd12 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/templateengine/PromptTemplateEngine.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/templateengine/PromptTemplateEngine.java @@ -1,13 +1,16 @@ // Copyright (c) Microsoft. All rights reserved. package com.microsoft.semantickernel.templateengine; +import com.microsoft.semantickernel.builders.Buildable; +import com.microsoft.semantickernel.builders.BuildersSingleton; +import com.microsoft.semantickernel.builders.SemanticKernelBuilder; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.templateengine.blocks.Block; import java.util.List; import reactor.core.publisher.Mono; /** Prompt template engine interface */ -public interface PromptTemplateEngine { +public interface PromptTemplateEngine extends Buildable { /* ///

@@ -40,9 +43,9 @@ IList ExtractBlocks( */ List extractBlocks(String promptTemplate); - abstract class Builder { - protected Builder() {} - - public abstract PromptTemplateEngine build(); + static Builder builder() { + return BuildersSingleton.INST.getInstance(Builder.class); } + + interface Builder extends SemanticKernelBuilder {} } diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/textcompletion/CompletionSKFunction.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/textcompletion/CompletionSKFunction.java index 64e4f2b0023b..c94d85d4e2ae 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/textcompletion/CompletionSKFunction.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/textcompletion/CompletionSKFunction.java @@ -1,6 +1,10 @@ // Copyright (c) Microsoft. All rights reserved. package com.microsoft.semantickernel.textcompletion; +import com.microsoft.semantickernel.Kernel; +import com.microsoft.semantickernel.builders.Buildable; +import com.microsoft.semantickernel.builders.BuildersSingleton; +import com.microsoft.semantickernel.builders.SemanticKernelBuilder; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.orchestration.SKFunction; import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig; @@ -10,7 +14,7 @@ import reactor.core.publisher.Mono; /** Semantic function for text completion */ -public interface CompletionSKFunction extends SKFunction { +public interface CompletionSKFunction extends SKFunction, Buildable { /** * Method to aggregate partitioned results of a semantic function. @@ -22,59 +26,27 @@ public interface CompletionSKFunction extends SKFunction aggregatePartitionedResultsAsync( List partitionedInput, @Nullable SKContext context); + static Builder builder() { + return BuildersSingleton.INST.getInstance(CompletionSKFunction.Builder.class); + } + /** Builder for completion functions */ - abstract class Builder { + interface Builder extends SemanticKernelBuilder { + + Builder withKernel(Kernel kernel); + + Builder setPromptTemplate(String promptTemplate); - protected Builder() {} + Builder setPromptTemplateConfig(PromptTemplateConfig config); - /** - * Create a new completion function - * - * @param promptTemplate Prompt template - * @param config Prompt template config - * @param functionName Function name - * @param skillName Skill name - * @return Completion function - */ - public abstract CompletionSKFunction createFunction( - String promptTemplate, - PromptTemplateConfig config, - String functionName, - @Nullable String skillName); + Builder setCompletionConfig(PromptTemplateConfig.CompletionConfig completionConfig); - /** - * Create a new completion function - * - * @param prompt Prompt - * @param functionConfig Function config - * @return Completion function - */ - public abstract CompletionSKFunction createFunction( - String prompt, PromptTemplateConfig.CompletionConfig functionConfig); + Builder setSemanticFunctionConfig(SemanticFunctionConfig functionConfig); - /** - * Create a new completion function - * - * @param functionName Function name - * @param skillName Skill name - * @return Completion function - */ - public abstract CompletionSKFunction createFunction( - @Nullable String skillName, - String functionName, - SemanticFunctionConfig functionConfig); + Builder setSkillName(@Nullable String skillName); - public abstract CompletionSKFunction createFunction( - String promptTemplate, - @Nullable String functionName, - @Nullable String skillName, - @Nullable String description); + Builder setFunctionName(@Nullable String functionName); - public abstract CompletionSKFunction createFunction( - String prompt, - @Nullable String functionName, - @Nullable String skillName, - @Nullable String description, - PromptTemplateConfig.CompletionConfig completionConfig); + Builder setDescription(String description); } } diff --git a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/textcompletion/TextCompletion.java b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/textcompletion/TextCompletion.java index 49fb43b1c105..99ab029f8c30 100644 --- a/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/textcompletion/TextCompletion.java +++ b/java/semantickernel-api/src/main/java/com/microsoft/semantickernel/textcompletion/TextCompletion.java @@ -2,14 +2,16 @@ package com.microsoft.semantickernel.textcompletion; import com.azure.ai.openai.OpenAIAsyncClient; +import com.microsoft.semantickernel.builders.Buildable; +import com.microsoft.semantickernel.builders.BuildersSingleton; +import com.microsoft.semantickernel.builders.SemanticKernelBuilder; import com.microsoft.semantickernel.services.AIService; import java.util.List; import javax.annotation.Nonnull; import reactor.core.publisher.Mono; /** Interface for text completion services */ -public interface TextCompletion extends AIService { - // TODO: Support Cancellation Token +public interface TextCompletion extends AIService, Buildable { /** * Creates a completion for the prompt and settings. @@ -21,9 +23,14 @@ public interface TextCompletion extends AIService { Mono> completeAsync( @Nonnull String text, @Nonnull CompletionRequestSettings requestSettings); - abstract class Builder { - protected Builder() {} + static Builder builder() { + return BuildersSingleton.INST.getInstance(Builder.class); + } + + interface Builder extends SemanticKernelBuilder { + + Builder withOpenAIClient(OpenAIAsyncClient client); - public abstract TextCompletion build(OpenAIAsyncClient client, String modelId); + Builder setModelId(String modelId); } } diff --git a/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/DefaultKernel.java b/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/DefaultKernel.java index a1e2cd01c1ca..d8a5436d3bff 100644 --- a/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/DefaultKernel.java +++ b/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/DefaultKernel.java @@ -2,8 +2,7 @@ package com.microsoft.semantickernel; import com.microsoft.semantickernel.ai.AIException; -import com.microsoft.semantickernel.builders.FunctionBuilders; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.ai.embeddings.TextEmbeddingGeneration; import com.microsoft.semantickernel.coreskills.SkillImporter; import com.microsoft.semantickernel.exceptions.SkillsNotFoundException; import com.microsoft.semantickernel.extensions.KernelExtensions; @@ -12,12 +11,12 @@ import com.microsoft.semantickernel.memory.NullMemory; import com.microsoft.semantickernel.memory.SemanticTextMemory; import com.microsoft.semantickernel.orchestration.ContextVariables; -import com.microsoft.semantickernel.orchestration.DefaultCompletionSKFunction; import com.microsoft.semantickernel.orchestration.RegistrableSkFunction; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.orchestration.SKFunction; import com.microsoft.semantickernel.semanticfunctions.SemanticFunctionConfig; import com.microsoft.semantickernel.services.AIService; +import com.microsoft.semantickernel.services.AIServiceCollection; import com.microsoft.semantickernel.services.AIServiceProvider; import com.microsoft.semantickernel.skilldefinition.DefaultSkillCollection; import com.microsoft.semantickernel.skilldefinition.FunctionNotFound; @@ -29,6 +28,8 @@ import jakarta.inject.Inject; import java.util.Arrays; import java.util.Map; +import java.util.function.Function; +import java.util.function.Supplier; import javax.annotation.Nonnull; import javax.annotation.Nullable; import reactor.core.publisher.Mono; @@ -133,11 +134,12 @@ public ReadOnlyFunctionCollection importSkill( skills.entrySet().stream() .map( (entry) -> { - return DefaultCompletionSKFunction.createFunction( - skillName, - entry.getKey(), - entry.getValue(), - promptTemplateEngine); + return SKBuilders.completionFunctions() + .withKernel(this) + .setSkillName(skillName) + .setFunctionName(entry.getKey()) + .setSemanticFunctionConfig(entry.getValue()) + .build(); }) .forEach(this::registerSemanticFunction); @@ -185,7 +187,7 @@ public ReadOnlySkillCollection getSkills() { @Override public CompletionSKFunction.Builder getSemanticFunctionBuilder() { - return FunctionBuilders.getCompletionBuilder(this); + return SKBuilders.completionFunctions().withKernel(this); } @Override @@ -269,7 +271,11 @@ public Mono runAsync(ContextVariables variables, SKFunction... pip // TODO: The SemanticTextMemory can be null, but there should be a way to provide it. // Not sure registerMemory is the right way. Mono pipelineBuilder = - Mono.just(SKBuilders.context().with(variables).with(getSkills()).build()); + Mono.just( + SKBuilders.context() + .setVariables(variables) + .setSkills(getSkills()) + .build()); for (SKFunction f : Arrays.asList(pipeline)) { pipelineBuilder = @@ -279,17 +285,17 @@ public Mono runAsync(ContextVariables variables, SKFunction... pip () -> { // Previous pipeline did not produce a result return SKBuilders.context() - .with(variables) - .with(getSkills()) + .setVariables(variables) + .setSkills(getSkills()) .build(); })) .flatMap( newContext -> { SKContext context = SKBuilders.context() - .with(newContext.getVariables()) - .with(newContext.getSemanticMemory()) - .with(newContext.getSkills()) + .setVariables(newContext.getVariables()) + .setMemory(newContext.getSemanticMemory()) + .setSkills(newContext.getSkills()) .build(); return f.invokeAsync(context, null); }); @@ -298,10 +304,181 @@ public Mono runAsync(ContextVariables variables, SKFunction... pip return pipelineBuilder; } - public static class Builder implements Kernel.InternalBuilder { + public static class Builder implements Kernel.Builder { + @Nullable private KernelConfig config = null; + @Nullable private PromptTemplateEngine promptTemplateEngine = null; + @Nullable private final AIServiceCollection aiServices = new AIServiceCollection(); + private Supplier memoryFactory = NullMemory::new; + private Supplier memoryStorageFactory = null; + + /** + * Set the kernel configuration + * + * @param kernelConfig Kernel configuration + * @return Builder + */ + public Kernel.Builder withConfiguration(KernelConfig kernelConfig) { + this.config = kernelConfig; + return this; + } + + /** + * Add prompt template engine to the kernel to be built. + * + * @param promptTemplateEngine Prompt template engine to add. + * @return Updated kernel builder including the prompt template engine. + */ + public Kernel.Builder withPromptTemplateEngine(PromptTemplateEngine promptTemplateEngine) { + Verify.notNull(promptTemplateEngine); + this.promptTemplateEngine = promptTemplateEngine; + return this; + } + + /** + * Add memory storage to the kernel to be built. + * + * @param storage Storage to add. + * @return Updated kernel builder including the memory storage. + */ + public Kernel.Builder withMemoryStorage(MemoryStore storage) { + Verify.notNull(storage); + this.memoryStorageFactory = () -> storage; + return this; + } + + /** + * Add memory storage factory to the kernel. + * + * @param factory The storage factory. + * @return Updated kernel builder including the memory storage. + */ + public Kernel.Builder withMemoryStorage(Supplier factory) { + Verify.notNull(factory); + this.memoryStorageFactory = factory::get; + return this; + } + + /** + * Adds an instance to the services collection + * + * @param instance The instance. + * @return The builder. + */ + public Kernel.Builder withDefaultAIService(T instance) { + Class clazz = (Class) instance.getClass(); + this.aiServices.setService(instance, clazz); + return this; + } + + /** + * Adds an instance to the services collection + * + * @param instance The instance. + * @param clazz The class of the instance. + * @return The builder. + */ + public Kernel.Builder withDefaultAIService( + T instance, Class clazz) { + this.aiServices.setService(instance, clazz); + return this; + } + + /** + * Adds a factory method to the services collection + * + * @param factory The factory method that creates the AI service instances of type T. + * @param clazz The class of the instance. + */ + public Kernel.Builder withDefaultAIService( + Supplier factory, Class clazz) { + this.aiServices.setService(factory, clazz); + return this; + } + + /** + * Adds an instance to the services collection + * + * @param serviceId The service ID + * @param instance The instance. + * @param setAsDefault Optional: set as the default AI service for type T + * @param clazz The class of the instance. + */ + public Kernel.Builder withAIService( + @Nullable String serviceId, T instance, boolean setAsDefault, Class clazz) { + this.aiServices.setService(serviceId, instance, setAsDefault, clazz); + + return this; + } + + /** + * Adds a factory method to the services collection + * + * @param serviceId The service ID + * @param factory The factory method that creates the AI service instances of type T. + * @param setAsDefault Optional: set as the default AI service for type T + * @param clazz The class of the instance. + */ + public Kernel.Builder withAIServiceFactory( + @Nullable String serviceId, + Function factory, + boolean setAsDefault, + Class clazz) { + this.aiServices.setService( + serviceId, (Supplier) () -> factory.apply(this.config), setAsDefault, clazz); + return this; + } + + /** + * Add a semantic text memory entity to the kernel to be built. + * + * @param memory Semantic text memory entity to add. + * @return Updated kernel builder including the semantic text memory entity. + */ + public Kernel.Builder withMemory(SemanticTextMemory memory) { + Verify.notNull(memory); + this.memoryFactory = () -> memory; + return this; + } + + /** + * Add memory storage and an embedding generator to the kernel to be built. + * + * @param storage Storage to add. + * @param embeddingGenerator Embedding generator to add. + * @return Updated kernel builder including the memory storage and embedding generator. + */ + public Kernel.Builder withMemoryStorageAndTextEmbeddingGeneration( + MemoryStore storage, TextEmbeddingGeneration embeddingGenerator) { + Verify.notNull(storage); + Verify.notNull(embeddingGenerator); + this.memoryFactory = + () -> + SKBuilders.semanticTextMemory() + .setEmbeddingGenerator(embeddingGenerator) + .setStorage(storage) + .build(); + return this; + } + + /** + * Build the kernel + * + * @return Kernel + */ + public Kernel build() { + if (config == null) { + config = SKBuilders.kernelConfig().build(); + } + + return build( + config, + promptTemplateEngine, + memoryFactory.get(), + memoryStorageFactory == null ? null : memoryStorageFactory.get(), + aiServices.build()); + } - @Override - public Kernel build( + private Kernel build( KernelConfig kernelConfig, @Nullable PromptTemplateEngine promptTemplateEngine, @Nullable SemanticTextMemory memory, diff --git a/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/SkFunctionBuilders.java b/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/SkFunctionBuilders.java index 8fbe4e005c43..3969ffd306f8 100644 --- a/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/SkFunctionBuilders.java +++ b/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/SkFunctionBuilders.java @@ -1,106 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. package com.microsoft.semantickernel; -import com.microsoft.semantickernel.ai.AIException; -import com.microsoft.semantickernel.builders.FunctionBuilders; -import com.microsoft.semantickernel.orchestration.DefaultCompletionSKFunction; -import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig; -import com.microsoft.semantickernel.semanticfunctions.SemanticFunctionConfig; -import com.microsoft.semantickernel.textcompletion.CompletionSKFunction; -import javax.annotation.Nullable; - /** Provides various builders for completion functions */ -public class SkFunctionBuilders implements FunctionBuilders { +public class SkFunctionBuilders { public SkFunctionBuilders() {} - - private static class InternalCompletionBuilder extends CompletionSKFunction.Builder { - private final Kernel kernel; - - private InternalCompletionBuilder(Kernel kernel) { - this.kernel = kernel; - } - - private DefaultCompletionSKFunction register(DefaultCompletionSKFunction function) { - if (kernel != null) { - kernel.registerSemanticFunction(function); - } - return function; - } - - @Override - public CompletionSKFunction createFunction( - String promptTemplate, - PromptTemplateConfig config, - String functionName, - @Nullable String skillName) { - return register( - DefaultCompletionSKFunction.createFunction( - promptTemplate, - config, - functionName, - skillName, - kernel.getPromptTemplateEngine())); - } - - @Override - public CompletionSKFunction createFunction( - String prompt, PromptTemplateConfig.CompletionConfig functionConfig) { - return createFunction(prompt, null, null, null, functionConfig); - } - - @Override - public CompletionSKFunction createFunction( - @Nullable String skillNameFinal, - String functionName, - SemanticFunctionConfig functionConfig) { - return register( - DefaultCompletionSKFunction.createFunction( - skillNameFinal, - functionName, - functionConfig, - kernel.getPromptTemplateEngine())); - } - - @Override - public CompletionSKFunction createFunction( - String promptTemplate, - @Nullable String functionName, - @Nullable String skillName, - @Nullable String description) { - return createFunction( - promptTemplate, - functionName, - skillName, - description, - new PromptTemplateConfig.CompletionConfig()); - } - - @Override - public CompletionSKFunction createFunction( - String prompt, - @Nullable String functionName, - @Nullable String skillName, - @Nullable String description, - PromptTemplateConfig.CompletionConfig completionConfig) { - if (kernel == null) { - throw new AIException( - AIException.ErrorCodes.InvalidConfiguration, - "Called builder to create a function that"); - } - - return register( - DefaultCompletionSKFunction.createFunction( - prompt, - functionName, - skillName, - description, - completionConfig, - kernel.getPromptTemplateEngine())); - } - } - - @Override - public CompletionSKFunction.Builder completionBuilders(Kernel kernel) { - return new InternalCompletionBuilder(kernel); - } } diff --git a/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/orchestration/DefaultCompletionSKFunction.java b/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/orchestration/DefaultCompletionSKFunction.java index f3a22725ac5e..927aac0fff2b 100644 --- a/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/orchestration/DefaultCompletionSKFunction.java +++ b/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/orchestration/DefaultCompletionSKFunction.java @@ -3,7 +3,8 @@ import com.azure.core.exception.HttpResponseException; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; +import com.microsoft.semantickernel.ai.AIException; import com.microsoft.semantickernel.semanticfunctions.PromptTemplate; import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig; import com.microsoft.semantickernel.semanticfunctions.SemanticFunctionConfig; @@ -11,7 +12,6 @@ import com.microsoft.semantickernel.skilldefinition.KernelSkillsSupplier; import com.microsoft.semantickernel.skilldefinition.ParameterView; import com.microsoft.semantickernel.skilldefinition.ReadOnlySkillCollection; -import com.microsoft.semantickernel.templateengine.PromptTemplateEngine; import com.microsoft.semantickernel.textcompletion.CompletionRequestSettings; import com.microsoft.semantickernel.textcompletion.CompletionSKFunction; import com.microsoft.semantickernel.textcompletion.TextCompletion; @@ -97,8 +97,8 @@ public DefaultCompletionSKFunction( private SKContext buildContext() { assertSkillSupplierRegistered(); return SKBuilders.context() - .with(SKBuilders.variables().build()) - .with(getSkillsSupplier() == null ? null : getSkillsSupplier().get()) + .setVariables(SKBuilders.variables().build()) + .setSkills(getSkillsSupplier() == null ? null : getSkillsSupplier().get()) .build(); } @@ -245,107 +245,142 @@ private static String randomFunctionName() { return "func" + UUID.randomUUID(); } - public static DefaultCompletionSKFunction createFunction( - String promptTemplate, - @Nullable String functionName, - @Nullable String skillName, - @Nullable String description, - PromptTemplateConfig.CompletionConfig completion, - PromptTemplateEngine promptTemplateEngine) { + @Override + public FunctionView describe() { + return new FunctionView( + super.getName(), + super.getSkillName(), + super.getDescription(), + super.getParametersView(), + true, + false); + } - if (functionName == null) { - functionName = randomFunctionName(); + public static class Builder implements CompletionSKFunction.Builder { + private Kernel kernel; + @Nullable private String promptTemplate = null; + @Nullable private String functionName = null; + @Nullable private String skillName = null; + @Nullable private String description = null; + private PromptTemplateConfig.CompletionConfig completionConfig = + new PromptTemplateConfig.CompletionConfig(); + @Nullable private SemanticFunctionConfig functionConfig = null; + @Nullable private PromptTemplateConfig promptTemplateConfig = null; + + @Override + public CompletionSKFunction build() { + if (kernel == null) { + throw new AIException( + AIException.ErrorCodes.InvalidConfiguration, + "Called builder to create a function without setting the kernel"); + } + + if (functionName == null) { + functionName = randomFunctionName(); + } + + // Verify.NotNull(functionConfig, "Function configuration is empty"); + if (skillName == null) { + skillName = ReadOnlySkillCollection.GlobalSkill; + } + + if (functionConfig == null) { + + if (description == null) { + description = "Generic function, unknown purpose"; + } + + if (promptTemplate == null) { + throw new AIException( + AIException.ErrorCodes.InvalidConfiguration, + "Must set prompt template before building"); + } + + if (promptTemplateConfig == null) { + promptTemplateConfig = + new PromptTemplateConfig(description, "completion", completionConfig); + } + + PromptTemplate template = + SKBuilders.promptTemplate() + .setPromptTemplateConfig(promptTemplateConfig) + .setPromptTemplate(promptTemplate) + .setPromptTemplateEngine(kernel.getPromptTemplateEngine()) + .build(); + + // Prepare lambda wrapping AI logic + functionConfig = new SemanticFunctionConfig(promptTemplateConfig, template); + } + + CompletionRequestSettings requestSettings = + CompletionRequestSettings.fromCompletionConfig( + functionConfig.getConfig().getCompletionConfig()); + + PromptTemplate promptTemplate = functionConfig.getTemplate(); + + DefaultCompletionSKFunction function = + new DefaultCompletionSKFunction( + promptTemplate.getParameters(), + skillName, + functionName, + functionConfig.getConfig().getDescription(), + requestSettings, + functionConfig, + null); + + kernel.registerSemanticFunction(function); + return function; } - if (description == null) { - description = "Generic function, unknown purpose"; + @Override + public CompletionSKFunction.Builder withKernel(Kernel kernel) { + this.kernel = kernel; + return this; } - PromptTemplateConfig config = - new PromptTemplateConfig(description, "completion", completion); - - return createFunction( - promptTemplate, config, functionName, skillName, promptTemplateEngine); - } - - public static DefaultCompletionSKFunction createFunction( - String promptTemplate, - PromptTemplateConfig config, - String functionName, - @Nullable String skillName, - PromptTemplateEngine promptTemplateEngine) { - if (functionName == null) { - functionName = randomFunctionName(); + @Override + public CompletionSKFunction.Builder setPromptTemplate(String promptTemplate) { + this.promptTemplate = promptTemplate; + return this; } - // TODO - // Verify.ValidFunctionName(functionName); - // if (!string.IsNullOrEmpty(skillName)) { - // Verify.ValidSkillName(skillName); - // } - - PromptTemplate template = - SKBuilders.promptTemplate() - .withPromptTemplateConfig(config) - .withPromptTemplate(promptTemplate) - .build(promptTemplateEngine); - - // Prepare lambda wrapping AI logic - SemanticFunctionConfig functionConfig = new SemanticFunctionConfig(config, template); - - return createFunction(skillName, functionName, functionConfig, promptTemplateEngine); - } - - public static DefaultCompletionSKFunction createFunction( - String functionName, - SemanticFunctionConfig functionConfig, - PromptTemplateEngine promptTemplateEngine) { - return createFunction(null, functionName, functionConfig, promptTemplateEngine); - } + @Override + public CompletionSKFunction.Builder setPromptTemplateConfig( + PromptTemplateConfig promptTemplateConfig) { + this.promptTemplateConfig = promptTemplateConfig; + return this; + } - /// - /// Create a native function instance, given a semantic function configuration. - /// - /// Name of the skill to which the function to create belongs. - /// Name of the function to create. - /// Semantic function configuration. - /// Optional logger for the function. - /// SK function instance. - public static DefaultCompletionSKFunction createFunction( - @Nullable String skillNameFinal, - String functionName, - SemanticFunctionConfig functionConfig, - PromptTemplateEngine promptTemplateEngine) { - String skillName = skillNameFinal; - // Verify.NotNull(functionConfig, "Function configuration is empty"); - if (skillName == null) { - skillName = ReadOnlySkillCollection.GlobalSkill; + @Override + public CompletionSKFunction.Builder setCompletionConfig( + PromptTemplateConfig.CompletionConfig completionConfig) { + this.completionConfig = completionConfig; + return this; } - CompletionRequestSettings requestSettings = - CompletionRequestSettings.fromCompletionConfig( - functionConfig.getConfig().getCompletionConfig()); + @Override + public CompletionSKFunction.Builder setSemanticFunctionConfig( + SemanticFunctionConfig functionConfig) { + this.functionConfig = functionConfig; + return this; + } - PromptTemplate promptTemplate = functionConfig.getTemplate(); + @Override + public CompletionSKFunction.Builder setSkillName(@Nullable String skillName) { + this.skillName = skillName; + return this; + } - return new DefaultCompletionSKFunction( - promptTemplate.getParameters(), - skillName, - functionName, - functionConfig.getConfig().getDescription(), - requestSettings, - functionConfig, - null); - } + @Override + public CompletionSKFunction.Builder setFunctionName(@Nullable String functionName) { + this.functionName = functionName; + return this; + } - @Override - public FunctionView describe() { - return new FunctionView( - super.getName(), - super.getSkillName(), - super.getDescription(), - super.getParametersView(), - true, - false); + @Override + public CompletionSKFunction.Builder setDescription(String description) { + this.description = description; + return this; + } } } diff --git a/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/orchestration/DefaultSKContext.java b/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/orchestration/DefaultSKContext.java index eccb00789448..2e313e02a74a 100644 --- a/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/orchestration/DefaultSKContext.java +++ b/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/orchestration/DefaultSKContext.java @@ -2,7 +2,7 @@ package com.microsoft.semantickernel.orchestration; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.memory.NullMemory; import com.microsoft.semantickernel.memory.SemanticTextMemory; import com.microsoft.semantickernel.skilldefinition.ReadOnlySkillCollection; @@ -39,12 +39,6 @@ public static class Builder implements SKContext.Builder { private ReadOnlySkillCollection skills; private SemanticTextMemory memory = NullMemory.getInstance(); - @Override - public SKContext build(ReadOnlySkillCollection skills) { - return new DefaultSKContext( - SKBuilders.variables().build(), NullMemory.getInstance(), skills); - } - @Override public SKContext build() { if (variables == null) { @@ -54,18 +48,13 @@ public SKContext build() { } @Override - public SKContext build(Class clazz) { - return null; - } - - @Override - public SKContext.Builder with(ContextVariables variables) { + public SKContext.Builder setVariables(ContextVariables variables) { this.variables = variables; return this; } @Override - public SKContext.Builder with(@Nullable ReadOnlySkillCollection skills) { + public SKContext.Builder setSkills(@Nullable ReadOnlySkillCollection skills) { if (skills != null) { this.skills = skills; } @@ -73,7 +62,7 @@ public SKContext.Builder with(@Nullable ReadOnlySkillCollection skills) { } @Override - public SKContext.Builder with(@Nullable SemanticTextMemory memory) { + public SKContext.Builder setMemory(@Nullable SemanticTextMemory memory) { if (memory != null) { this.memory = memory.copy(); } @@ -82,13 +71,13 @@ public SKContext.Builder with(@Nullable SemanticTextMemory memory) { @Override public SKContext.Builder clone(SKContext context) { - return with(context.getVariables()) - .with(context.getSkills()) - .with(context.getSemanticMemory()); + return setVariables(context.getVariables()) + .setSkills(context.getSkills()) + .setMemory(context.getSemanticMemory()); } @Override - public SKContext build(Kernel kernel) { + public SKContext.Builder withKernel(Kernel kernel) { if (memory == null) { memory = kernel.getMemory(); } @@ -98,7 +87,7 @@ public SKContext build(Kernel kernel) { if (variables == null) { variables = SKBuilders.variables().build(); } - return build(); + return this; } } } diff --git a/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/orchestration/DefaultSemanticSKFunction.java b/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/orchestration/DefaultSemanticSKFunction.java index 7d065054c287..06a7736a99c5 100644 --- a/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/orchestration/DefaultSemanticSKFunction.java +++ b/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/orchestration/DefaultSemanticSKFunction.java @@ -1,7 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. package com.microsoft.semantickernel.orchestration; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.memory.NullMemory; import com.microsoft.semantickernel.skilldefinition.KernelSkillsSupplier; import com.microsoft.semantickernel.skilldefinition.ParameterView; @@ -37,9 +37,9 @@ public Mono invokeAsync( assertSkillSupplierRegistered(); context = SKBuilders.context() - .with(SKBuilders.variables().build()) - .with(NullMemory.getInstance()) - .with(super.getSkillsSupplier().get()) + .setVariables(SKBuilders.variables().build()) + .setMemory(NullMemory.getInstance()) + .setSkills(super.getSkillsSupplier().get()) .build(); } else { context = context.copy(); diff --git a/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/semanticfunctions/DefaultPromptTemplate.java b/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/semanticfunctions/DefaultPromptTemplate.java index 1d4eb566daff..19546dada83c 100644 --- a/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/semanticfunctions/DefaultPromptTemplate.java +++ b/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/semanticfunctions/DefaultPromptTemplate.java @@ -68,28 +68,38 @@ public Mono renderAsync(SKContext executionContext) { return templateEngine.renderAsync(this.promptTemplate, executionContext); } - public static final class Builder extends PromptTemplate.Builder { + public static final class Builder implements PromptTemplate.Builder { @Nullable private String promptTemplate = null; @Nullable private PromptTemplateConfig config = null; + @Nullable private PromptTemplateEngine promptTemplateEngine = null; @Override - public PromptTemplate build(PromptTemplateEngine promptTemplateEngine) { - if (promptTemplate == null || config == null) - throw new IllegalStateException( - "PromptTemplate and PromptTemplateConfig must be set"); - return new DefaultPromptTemplate(promptTemplate, config, promptTemplateEngine); + public PromptTemplate.Builder setPromptTemplate(String promptTemplate) { + this.promptTemplate = promptTemplate; + return this; } @Override - public Builder withPromptTemplate(String promptTemplate) { - this.promptTemplate = promptTemplate; + public PromptTemplate.Builder setPromptTemplateConfig(PromptTemplateConfig config) { + this.config = config; return this; } @Override - public Builder withPromptTemplateConfig(PromptTemplateConfig config) { - this.config = config; + public PromptTemplate.Builder setPromptTemplateEngine( + PromptTemplateEngine promptTemplateEngine) { + this.promptTemplateEngine = promptTemplateEngine; return this; } + + @Override + public PromptTemplate build() { + if (promptTemplate == null || config == null || promptTemplateEngine == null) { + throw new IllegalStateException( + "PromptTemplate, PromptTemplateConfig and promptTemplateEngine must be" + + " set"); + } + return new DefaultPromptTemplate(promptTemplate, config, promptTemplateEngine); + } } } diff --git a/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/templateengine/DefaultPromptTemplateEngine.java b/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/templateengine/DefaultPromptTemplateEngine.java index 8af35cce8968..d3174c23de4b 100644 --- a/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/templateengine/DefaultPromptTemplateEngine.java +++ b/java/semantickernel-core/src/main/java/com/microsoft/semantickernel/templateengine/DefaultPromptTemplateEngine.java @@ -119,7 +119,7 @@ public Mono renderAsync(List blocks, SKContext context) { */ } - public static final class Builder extends PromptTemplateEngine.Builder { + public static final class Builder implements PromptTemplateEngine.Builder { @Override public PromptTemplateEngine build() { return new DefaultPromptTemplateEngine(); diff --git a/java/semantickernel-core/src/main/resources/META-INF/services/com.microsoft.semantickernel.Kernel$InternalBuilder b/java/semantickernel-core/src/main/resources/META-INF/services/com.microsoft.semantickernel.Kernel$Builder similarity index 100% rename from java/semantickernel-core/src/main/resources/META-INF/services/com.microsoft.semantickernel.Kernel$InternalBuilder rename to java/semantickernel-core/src/main/resources/META-INF/services/com.microsoft.semantickernel.Kernel$Builder diff --git a/java/semantickernel-core/src/main/resources/META-INF/services/com.microsoft.semantickernel.textcompletion.CompletionSKFunction$Builder b/java/semantickernel-core/src/main/resources/META-INF/services/com.microsoft.semantickernel.textcompletion.CompletionSKFunction$Builder new file mode 100644 index 000000000000..354182690ac8 --- /dev/null +++ b/java/semantickernel-core/src/main/resources/META-INF/services/com.microsoft.semantickernel.textcompletion.CompletionSKFunction$Builder @@ -0,0 +1 @@ +com.microsoft.semantickernel.orchestration.DefaultCompletionSKFunction$Builder \ No newline at end of file diff --git a/java/semantickernel-core/src/main/resources/META-INF/services/com.microsoft.semantickernel.textcompletion.TextCompletion$Builder b/java/semantickernel-core/src/main/resources/META-INF/services/com.microsoft.semantickernel.textcompletion.TextCompletion$Builder deleted file mode 100644 index 39bdf4b28755..000000000000 --- a/java/semantickernel-core/src/main/resources/META-INF/services/com.microsoft.semantickernel.textcompletion.TextCompletion$Builder +++ /dev/null @@ -1 +0,0 @@ -com.microsoft.semantickernel.connectors.ai.openai.textcompletion.OpenAITextCompletion$Builder \ No newline at end of file diff --git a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/AiServiceRegistryTest.java b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/AiServiceRegistryTest.java index 15b5325910bb..5fd8109e4292 100644 --- a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/AiServiceRegistryTest.java +++ b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/AiServiceRegistryTest.java @@ -1,7 +1,6 @@ // Copyright (c) Microsoft. All rights reserved. package com.microsoft.semantickernel; -import com.microsoft.semantickernel.builders.SKBuilders; import com.microsoft.semantickernel.textcompletion.CompletionSKFunction; import com.microsoft.semantickernel.textcompletion.TextCompletion; import java.util.Collections; diff --git a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/DefaultKernelTest.java b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/DefaultKernelTest.java index 600a87ebfd49..c9cf90eb1db7 100644 --- a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/DefaultKernelTest.java +++ b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/DefaultKernelTest.java @@ -9,7 +9,6 @@ import com.azure.ai.openai.models.Choice; import com.azure.ai.openai.models.Completions; import com.azure.ai.openai.models.CompletionsOptions; -import com.microsoft.semantickernel.builders.SKBuilders; import com.microsoft.semantickernel.connectors.ai.openai.textcompletion.OpenAITextCompletion; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig; @@ -47,16 +46,17 @@ void contextVariableTest() { CompletionSKFunction chat = kernel.getSemanticFunctionBuilder() - .createFunction( - prompt, - "ChatBot", - null, - null, - new PromptTemplateConfig.CompletionConfig(0.7, 0.5, 0, 0, 2000)); + .withKernel(kernel) + .setPromptTemplate(prompt) + .setFunctionName("ChatBot") + .setCompletionConfig( + new PromptTemplateConfig.CompletionConfig(0.7, 0.5, 0, 0, 2000)) + .build(); SKContext readOnlySkContext = SKBuilders.context() - .build(kernel) + .withKernel(kernel) + .build() .setVariable("history", "") .setVariable("user_input", "A"); @@ -314,13 +314,13 @@ void inlineFunctionTest() { String prompt = "{{$input}}\n" + "Summarize the content above."; CompletionSKFunction summarize = - SKBuilders.completionFunctions(kernel) - .createFunction( - prompt, - "summarize", - null, - null, - new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000)); + SKBuilders.completionFunctions() + .withKernel(kernel) + .setPromptTemplate(prompt) + .setFunctionName("summarize") + .setCompletionConfig( + new PromptTemplateConfig.CompletionConfig(0.2, 0.5, 0, 0, 2000)) + .build(); Mono mono = summarize.invokeAsync(text); SKContext result = mono.block(); diff --git a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/connectors/ai/openai/chatcompletion/OpenAIChatCompletionTest.java b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/connectors/ai/openai/chatcompletion/OpenAIChatCompletionTest.java index 65852889ac8d..c8871479acd4 100644 --- a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/connectors/ai/openai/chatcompletion/OpenAIChatCompletionTest.java +++ b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/connectors/ai/openai/chatcompletion/OpenAIChatCompletionTest.java @@ -7,8 +7,8 @@ import com.azure.ai.openai.models.ChatCompletions; import com.azure.ai.openai.models.ChatCompletionsOptions; import com.microsoft.semantickernel.Kernel; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.ai.AIException; -import com.microsoft.semantickernel.builders.SKBuilders; import com.microsoft.semantickernel.chatcompletion.ChatCompletion; import com.microsoft.semantickernel.chatcompletion.ChatHistory; import com.microsoft.semantickernel.syntaxexamples.Example17ChatGPTTest; @@ -31,7 +31,10 @@ public void chatCompletionCanBeUsedAsATextCompletion() { Kernel kernel = SKBuilders.kernel() .withDefaultAIService( - SKBuilders.chatCompletion().build(client, "gpt-3.5-turbo-0301")) + SKBuilders.chatCompletion() + .withOpenAIClient(client) + .setModelId("gpt-3.5-turbo-0301") + .build()) .build(); TextCompletion textCompletion = kernel.getService(null, TextCompletion.class); @@ -61,7 +64,10 @@ private static String getRunExample(OpenAIAsyncClient client, String message) { Kernel kernel = SKBuilders.kernel() .withDefaultAIService( - SKBuilders.chatCompletion().build(client, "gpt-3.5-turbo-0301")) + SKBuilders.chatCompletion() + .withOpenAIClient(client) + .setModelId("gpt-3.5-turbo-0301") + .build()) .build(); ChatCompletion chatGPT = kernel.getService(null, ChatCompletion.class); diff --git a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/connectors/memory/azurecognitivesearch/AzureCognitiveSearchMemoryTests.java b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/connectors/memory/azurecognitivesearch/AzureCognitiveSearchMemoryTests.java index 876ec36a35a6..7c7a95b4168c 100644 --- a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/connectors/memory/azurecognitivesearch/AzureCognitiveSearchMemoryTests.java +++ b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/connectors/memory/azurecognitivesearch/AzureCognitiveSearchMemoryTests.java @@ -16,7 +16,7 @@ import com.azure.search.documents.indexes.SearchIndexAsyncClient; import com.azure.search.documents.indexes.SearchIndexClientBuilder; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.memory.MemoryQueryResult; import java.nio.charset.StandardCharsets; import java.util.Base64; diff --git a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/orchestration/NativeSKFunctionTest.java b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/orchestration/NativeSKFunctionTest.java index c93614f5327c..c724b5dcce18 100644 --- a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/orchestration/NativeSKFunctionTest.java +++ b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/orchestration/NativeSKFunctionTest.java @@ -1,8 +1,8 @@ // Copyright (c) Microsoft. All rights reserved. package com.microsoft.semantickernel.orchestration; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.ai.AIException; -import com.microsoft.semantickernel.builders.SKBuilders; import com.microsoft.semantickernel.coreskills.SkillImporter; import com.microsoft.semantickernel.skilldefinition.DefaultSkillCollection; import com.microsoft.semantickernel.skilldefinition.FunctionCollection; diff --git a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/planner/actionplanner/ActionPlannerTest.java b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/planner/actionplanner/ActionPlannerTest.java index 936b80fc03d1..20df64aac0ee 100644 --- a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/planner/actionplanner/ActionPlannerTest.java +++ b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/planner/actionplanner/ActionPlannerTest.java @@ -5,7 +5,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.skilldefinition.annotations.DefineSKFunction; import com.microsoft.semantickernel.skilldefinition.annotations.SKFunctionParameters; import org.junit.jupiter.api.Assertions; @@ -51,7 +51,9 @@ public void inputsAreCorrectlyPassed() { SKBuilders.kernel() .withDefaultAIService( SKBuilders.textCompletionService() - .build(client, "text-davinci-002")) + .setModelId("text-davinci-002") + .withOpenAIClient(client) + .build()) .build(); SkillWithSomeArgs skill = Mockito.spy(new SkillWithSomeArgs()); diff --git a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example03VariablesTest.java b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example03VariablesTest.java index ed88aa8f6b75..a137a4356cd5 100644 --- a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example03VariablesTest.java +++ b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example03VariablesTest.java @@ -4,7 +4,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.DefaultKernelTest; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.orchestration.ContextVariables; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.skilldefinition.ReadOnlyFunctionCollection; diff --git a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example04CombineLLMPromptsAndNativeCodeTest.java b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example04CombineLLMPromptsAndNativeCodeTest.java index a000700e4a50..f54ed7f884fc 100644 --- a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example04CombineLLMPromptsAndNativeCodeTest.java +++ b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example04CombineLLMPromptsAndNativeCodeTest.java @@ -5,7 +5,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.connectors.ai.openai.textcompletion.OpenAITextCompletion; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.syntaxexamples.skills.SearchEngineSkill; diff --git a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example05UsingThePlannerTest.java b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example05UsingThePlannerTest.java index d19818657b73..df27ffdcb5b2 100644 --- a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example05UsingThePlannerTest.java +++ b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example05UsingThePlannerTest.java @@ -5,7 +5,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.planner.sequentialplanner.SequentialPlanner; import org.junit.jupiter.api.Test; import org.mockito.ArgumentMatcher; @@ -35,7 +35,9 @@ public void run() { SKBuilders.kernel() .withDefaultAIService( SKBuilders.textCompletionService() - .build(client, "text-davinci-003")) + .setModelId("text-davinci-003") + .withOpenAIClient(client) + .build()) .build(); SequentialPlanner planner = getPlanner(kernel); diff --git a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example06TemplateLanguageTest.java b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example06TemplateLanguageTest.java index 0e91d34daf74..79260b438cbc 100644 --- a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example06TemplateLanguageTest.java +++ b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example06TemplateLanguageTest.java @@ -5,7 +5,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.coreskills.TimeSkill; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.semanticfunctions.PromptTemplate; @@ -27,7 +27,9 @@ public void run() { SKBuilders.kernel() .withDefaultAIService( SKBuilders.textCompletionService() - .build(client, "text-davinci-003")) + .setModelId("text-davinci-003") + .withOpenAIClient(client) + .build()) .build(); // Load native skill into the kernel skill collection, sharing its functions @@ -50,11 +52,12 @@ public void run() { PromptTemplate promptRenderer = SKBuilders.promptTemplate() - .withPromptTemplateConfig(new PromptTemplateConfig()) - .withPromptTemplate(functionDefinition) - .build(kernel.getPromptTemplateEngine()); + .setPromptTemplateConfig(new PromptTemplateConfig()) + .setPromptTemplate(functionDefinition) + .setPromptTemplateEngine(kernel.getPromptTemplateEngine()) + .build(); - SKContext skContext = SKBuilders.context().build(kernel.getSkills()); + SKContext skContext = SKBuilders.context().setSkills(kernel.getSkills()).build(); Mono renderedPrompt = promptRenderer.renderAsync(skContext); @@ -66,12 +69,11 @@ public void run() { // Run the prompt / semantic function CompletionSKFunction kindOfDay = kernel.getSemanticFunctionBuilder() - .createFunction( - functionDefinition, - null, - null, - null, - new PromptTemplateConfig.CompletionConfig(0, 0, 0, 0, 256)); + .withKernel(kernel) + .setPromptTemplate(functionDefinition) + .setCompletionConfig( + new PromptTemplateConfig.CompletionConfig(0, 0, 0, 0, 256)) + .build(); Assertions.assertEquals("A-RESULT", kindOfDay.invokeAsync("").block().getResult()); } diff --git a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example12SequentialPlannerTest.java b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example12SequentialPlannerTest.java index 0e2fa8d80016..67fb11ec2ed9 100644 --- a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example12SequentialPlannerTest.java +++ b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example12SequentialPlannerTest.java @@ -7,7 +7,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.coreskills.TextSkill; import com.microsoft.semantickernel.orchestration.SKContext; import com.microsoft.semantickernel.planner.PlanningException; @@ -79,7 +79,9 @@ private static Kernel getKernel(OpenAIAsyncClient client) { SKBuilders.kernel() .withDefaultAIService( SKBuilders.textCompletionService() - .build(client, "text-davinci-002")) + .setModelId("text-davinci-002") + .withOpenAIClient(client) + .build()) .build(); kernel.importSkillFromDirectory("SummarizeSkill", "../../samples/skills", "SummarizeSkill"); diff --git a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example17ChatGPTTest.java b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example17ChatGPTTest.java index 615705ff3eee..9791d36f29a1 100644 --- a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example17ChatGPTTest.java +++ b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example17ChatGPTTest.java @@ -8,7 +8,7 @@ import com.azure.ai.openai.models.ChatCompletions; import com.azure.ai.openai.models.ChatMessage; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.chatcompletion.ChatCompletion; import com.microsoft.semantickernel.chatcompletion.ChatHistory; import com.microsoft.semantickernel.connectors.ai.openai.chatcompletion.OpenAIChatHistory; @@ -38,7 +38,10 @@ public void azureOpenAIChatSampleAsync() { Kernel kernel = SKBuilders.kernel() .withDefaultAIService( - SKBuilders.chatCompletion().build(client, "gpt-3.5-turbo-0301")) + SKBuilders.chatCompletion() + .withOpenAIClient(client) + .setModelId("gpt-3.5-turbo-0301") + .build()) .build(); ChatCompletion chatGPT = kernel.getService(null, ChatCompletion.class); diff --git a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example28ActionPlannerTest.java b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example28ActionPlannerTest.java index 1c5575eefe75..f3a4264515d0 100644 --- a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example28ActionPlannerTest.java +++ b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example28ActionPlannerTest.java @@ -6,7 +6,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.DefaultKernelTest; import com.microsoft.semantickernel.Kernel; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.planner.PlanningException; import com.microsoft.semantickernel.planner.actionplanner.ActionPlanner; import com.microsoft.semantickernel.planner.actionplanner.Plan; @@ -123,7 +123,9 @@ public static ActionPlanner createPlanner(OpenAIAsyncClient client) { SKBuilders.kernel() .withDefaultAIService( SKBuilders.textCompletionService() - .build(client, "text-davinci-002")) + .setModelId("text-davinci-002") + .withOpenAIClient(client) + .build()) .build(); kernel.importSkillFromDirectory("SummarizeSkill", "../../samples/skills", "SummarizeSkill"); diff --git a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example33StreamingChatTest.java b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example33StreamingChatTest.java index ce58a43bfceb..f2ba2f33b5a2 100644 --- a/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example33StreamingChatTest.java +++ b/java/semantickernel-core/src/test/java/com/microsoft/semantickernel/syntaxexamples/Example33StreamingChatTest.java @@ -5,7 +5,7 @@ import com.azure.ai.openai.OpenAIAsyncClient; import com.microsoft.semantickernel.DefaultKernelTest; -import com.microsoft.semantickernel.builders.SKBuilders; +import com.microsoft.semantickernel.SKBuilders; import com.microsoft.semantickernel.chatcompletion.ChatCompletion; import com.microsoft.semantickernel.chatcompletion.ChatHistory; import java.util.function.BiFunction; @@ -34,7 +34,10 @@ public void streamingChatAsync() { "Second response")); ChatCompletion chatCompletion = - SKBuilders.chatCompletion().build(client, "gpt-35-turbo"); + SKBuilders.chatCompletion() + .withOpenAIClient(client) + .setModelId("gpt-35-turbo") + .build(); ChatHistory chatHistory = chatCompletion.createNewChat("You are a librarian, expert about books");