mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
Merge branch 'fluent-function-factory' into format-3
This commit is contained in:
+20
-22
@@ -5,7 +5,6 @@ import com.microsoft.semantickernel.orchestration.KernelFunction;
|
||||
import com.microsoft.semantickernel.orchestration.KernelFunctionArguments;
|
||||
import com.microsoft.semantickernel.orchestration.PromptExecutionSettings;
|
||||
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariableTypes;
|
||||
import com.microsoft.semantickernel.plugin.KernelFunctionFactory;
|
||||
import com.microsoft.semantickernel.plugin.annotations.DefineKernelFunction;
|
||||
import com.microsoft.semantickernel.plugin.annotations.KernelFunctionParameter;
|
||||
import com.microsoft.semantickernel.text.TextChunker;
|
||||
@@ -37,29 +36,28 @@ public class ConversationSummaryPlugin {
|
||||
.withTopP(0.5)
|
||||
.build();
|
||||
|
||||
this.summarizeConversationFunction = KernelFunctionFactory.createFromPrompt(
|
||||
PromptFunctionConstants.SummarizeConversationDefinition,
|
||||
settings,
|
||||
"summarizeConversation",
|
||||
"Given a section of a conversation transcript, summarize the part of the conversation.",
|
||||
null,
|
||||
null);
|
||||
this.summarizeConversationFunction = KernelFunction
|
||||
.<String>createFromPrompt(PromptFunctionConstants.SummarizeConversationDefinition)
|
||||
.withDefaultExecutionSettings(settings)
|
||||
.withName("summarizeConversation")
|
||||
.withDescription(
|
||||
"Given a section of a conversation transcript, summarize the part of the conversation.")
|
||||
.build();
|
||||
|
||||
this.conversationActionItemsFunction = KernelFunctionFactory.createFromPrompt(
|
||||
PromptFunctionConstants.GetConversationActionItemsDefinition,
|
||||
settings,
|
||||
"conversationActionItems",
|
||||
"Given a section of a conversation transcript, identify action items.",
|
||||
null,
|
||||
null);
|
||||
this.conversationActionItemsFunction = KernelFunction
|
||||
.<String>createFromPrompt(PromptFunctionConstants.GetConversationActionItemsDefinition)
|
||||
.withDefaultExecutionSettings(settings)
|
||||
.withName("conversationActionItems")
|
||||
.withDescription("Given a section of a conversation transcript, identify action items.")
|
||||
.build();
|
||||
|
||||
this.conversationTopicsFunction = KernelFunctionFactory.createFromPrompt(
|
||||
PromptFunctionConstants.GetConversationTopicsDefinition,
|
||||
settings,
|
||||
"conversationTopics",
|
||||
"Analyze a conversation transcript and extract key topics worth remembering.",
|
||||
null,
|
||||
null);
|
||||
this.conversationTopicsFunction = KernelFunction
|
||||
.<String>createFromPrompt(PromptFunctionConstants.GetConversationTopicsDefinition)
|
||||
.withDefaultExecutionSettings(settings)
|
||||
.withName("conversationTopics")
|
||||
.withDescription(
|
||||
"Analyze a conversation transcript and extract key topics worth remembering.")
|
||||
.build();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
+10
-13
@@ -8,9 +8,9 @@ import com.azure.core.credential.KeyCredential;
|
||||
import com.microsoft.semantickernel.Kernel;
|
||||
import com.microsoft.semantickernel.exceptions.ConfigurationException;
|
||||
import com.microsoft.semantickernel.orchestration.FunctionResult;
|
||||
import com.microsoft.semantickernel.orchestration.KernelFunction;
|
||||
import com.microsoft.semantickernel.orchestration.KernelFunctionArguments;
|
||||
import com.microsoft.semantickernel.orchestration.PromptExecutionSettings;
|
||||
import com.microsoft.semantickernel.plugin.KernelFunctionFactory;
|
||||
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionFromPrompt;
|
||||
import com.microsoft.semantickernel.textcompletion.TextGenerationService;
|
||||
import java.time.Instant;
|
||||
@@ -94,18 +94,15 @@ public class Example05_InlineFunctionDefinition {
|
||||
.block();
|
||||
System.out.println(result.getResult());
|
||||
|
||||
var fixedFunction = KernelFunctionFactory.createFromPrompt(
|
||||
"Translate this date " + DateTimeFormatter.ISO_LOCAL_DATE
|
||||
.withZone(ZoneOffset.UTC)
|
||||
.format(Instant.ofEpochSecond(1))
|
||||
+ " to French format",
|
||||
PromptExecutionSettings.builder()
|
||||
.withMaxTokens(100)
|
||||
.build(),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
var date = DateTimeFormatter.ISO_LOCAL_DATE.withZone(ZoneOffset.UTC)
|
||||
.format(Instant.ofEpochSecond(1));
|
||||
var message = "Translate this date " + date + " to French format";
|
||||
var fixedFunction = KernelFunction.createFromPrompt(message)
|
||||
.withDefaultExecutionSettings(
|
||||
PromptExecutionSettings.builder()
|
||||
.withMaxTokens(100)
|
||||
.build())
|
||||
.build();
|
||||
|
||||
FunctionResult<?> fixedFunctionResult = kernel
|
||||
.invokeAsync(fixedFunction)
|
||||
|
||||
+9
-12
@@ -7,8 +7,8 @@ import com.azure.core.credential.KeyCredential;
|
||||
import com.microsoft.semantickernel.Kernel;
|
||||
import com.microsoft.semantickernel.aiservices.openai.textcompletion.OpenAITextGenerationService;
|
||||
import com.microsoft.semantickernel.exceptions.ConfigurationException;
|
||||
import com.microsoft.semantickernel.orchestration.PromptExecutionSettings.Builder;
|
||||
import com.microsoft.semantickernel.plugin.KernelFunctionFactory;
|
||||
import com.microsoft.semantickernel.orchestration.KernelFunction;
|
||||
import com.microsoft.semantickernel.orchestration.PromptExecutionSettings;
|
||||
import com.microsoft.semantickernel.plugin.KernelPlugin;
|
||||
import com.microsoft.semantickernel.plugin.KernelPluginFactory;
|
||||
import com.microsoft.semantickernel.plugin.annotations.DefineKernelFunction;
|
||||
@@ -70,7 +70,7 @@ public class Example06_TemplateLanguage {
|
||||
KernelPlugin time = KernelPluginFactory.createFromObject(
|
||||
new Time(), "time");
|
||||
|
||||
kernel.getPlugins().add(time);
|
||||
kernel.addPlugin(time);
|
||||
|
||||
// Prompt Function invoking time.Date and time.Time method functions
|
||||
String functionDefinition = """
|
||||
@@ -91,16 +91,13 @@ public class Example06_TemplateLanguage {
|
||||
var renderedPrompt = promptTemplate.renderAsync(kernel, null, null).block();
|
||||
System.out.println(renderedPrompt);
|
||||
|
||||
var kindOfDay = KernelFunctionFactory
|
||||
.<String>createFromPrompt(
|
||||
functionDefinition,
|
||||
new Builder()
|
||||
var kindOfDay = KernelFunction.createFromPrompt(functionDefinition)
|
||||
.withDefaultExecutionSettings(
|
||||
PromptExecutionSettings.builder()
|
||||
.withMaxTokens(100)
|
||||
.build(),
|
||||
null,
|
||||
null,
|
||||
"semantic-kernel",
|
||||
null);
|
||||
.build())
|
||||
.withTemplateFormat(PromptTemplateConfig.SEMANTIC_KERNEL_TEMPLATE_FORMAT)
|
||||
.build();
|
||||
|
||||
// Show the result
|
||||
System.out.println("--- Prompt Function result");
|
||||
|
||||
+3
-3
@@ -78,16 +78,16 @@ public class Example10_DescribeAllPluginsAndFunctions {
|
||||
.withAIService(TextGenerationService.class, textGenerationService)
|
||||
.build();
|
||||
|
||||
kernel.getPlugins().add(
|
||||
kernel.addPlugin(
|
||||
KernelPluginFactory.createFromObject(
|
||||
new StaticTextPlugin(), "StaticTextPlugin"));
|
||||
|
||||
// Import another native plugin
|
||||
kernel.getPlugins().add(
|
||||
kernel.addPlugin(
|
||||
KernelPluginFactory.createFromObject(
|
||||
new TextPlugin(), "AnotherTextPlugin"));
|
||||
|
||||
kernel.getPlugins().add(
|
||||
kernel.addPlugin(
|
||||
KernelPluginFactory
|
||||
.importPluginFromDirectory(
|
||||
Path.of(PLUGIN_DIR, "java/samples/sample-code/src/main/resources/Plugins"),
|
||||
|
||||
+4
-3
@@ -6,9 +6,9 @@ import com.azure.core.credential.AzureKeyCredential;
|
||||
import com.azure.core.credential.KeyCredential;
|
||||
import com.microsoft.semantickernel.Kernel;
|
||||
import com.microsoft.semantickernel.chatcompletion.ChatCompletionService;
|
||||
import com.microsoft.semantickernel.orchestration.KernelFunction;
|
||||
import com.microsoft.semantickernel.orchestration.KernelFunctionArguments;
|
||||
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariableTypes;
|
||||
import com.microsoft.semantickernel.plugin.KernelFunctionFactory;
|
||||
|
||||
public class Example27_PromptFunctionsUsingChatGPT {
|
||||
|
||||
@@ -46,8 +46,9 @@ public class Example27_PromptFunctionsUsingChatGPT {
|
||||
.withAIService(ChatCompletionService.class, openAIChatCompletion)
|
||||
.build();
|
||||
|
||||
var func = KernelFunctionFactory.<String>createFromPrompt(
|
||||
"List the two planets closest to '{{$input}}', excluding moons, using bullet points.");
|
||||
var func = KernelFunction.<String>createFromPrompt(
|
||||
"List the two planets closest to '{{$input}}', excluding moons, using bullet points.")
|
||||
.build();
|
||||
|
||||
var result = func.invokeAsync(kernel)
|
||||
.withArguments(
|
||||
|
||||
+2
-2
@@ -14,7 +14,7 @@ import com.microsoft.semantickernel.samples.plugins.TimePlugin;
|
||||
import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig;
|
||||
import com.microsoft.semantickernel.semanticfunctions.PromptTemplateFactory;
|
||||
import com.microsoft.semantickernel.services.ServiceNotFoundException;
|
||||
import com.microsoft.semantickernel.util.EmbeddedResourceLoader;
|
||||
import com.microsoft.semantickernel.implementation.EmbeddedResourceLoader;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.time.ZoneId;
|
||||
@@ -77,7 +77,7 @@ public class Example30_ChatWithPrompts {
|
||||
// We could also use a variable, this is just to show that the prompt can invoke functions.
|
||||
KernelPlugin timePlugin = KernelPluginFactory.createFromObject(
|
||||
new TimePlugin(), "time");
|
||||
kernel.getPlugins().add(timePlugin);
|
||||
kernel.addPlugin(timePlugin);
|
||||
|
||||
// Adding required arguments referenced by the prompt templates.
|
||||
|
||||
|
||||
+1
-1
@@ -58,7 +58,7 @@ public class Example56_TemplateMethodFunctionsWithMultipleArguments {
|
||||
|
||||
// Load native plugin into the kernel function collection, sharing its functions with prompt templates
|
||||
// Functions loaded here are available as "text.*"
|
||||
kernel.getPlugins().add(KernelPluginFactory.createFromObject(new TextPlugin(), "text"));
|
||||
kernel.addPlugin(KernelPluginFactory.createFromObject(new TextPlugin(), "text"));
|
||||
|
||||
// Prompt Function invoking text.Concat method function with named arguments input and input2 where input is a string and input2 is set to a variable from context called word2.
|
||||
String functionDefinition = "Write a haiku about the following: {{text.Concat input='Harry' input2=$word2}}";
|
||||
|
||||
+1
@@ -163,6 +163,7 @@ public class Example57_KernelHooks {
|
||||
PromptRenderingHook myRenderingHandler = event -> {
|
||||
System.out.println(
|
||||
event.getFunction().getName() + " : Prompt Rendering Handler - Triggered");
|
||||
|
||||
event.getArguments().put("style", ContextVariable.of("Seinfeld"));
|
||||
return event;
|
||||
};
|
||||
|
||||
+7
-4
@@ -7,8 +7,8 @@ import com.azure.core.credential.KeyCredential;
|
||||
import com.microsoft.semantickernel.Kernel;
|
||||
import com.microsoft.semantickernel.aiservices.openai.chatcompletion.OpenAIChatCompletion;
|
||||
import com.microsoft.semantickernel.chatcompletion.ChatCompletionService;
|
||||
import com.microsoft.semantickernel.orchestration.KernelFunction;
|
||||
import com.microsoft.semantickernel.orchestration.PromptExecutionSettings;
|
||||
import com.microsoft.semantickernel.plugin.KernelFunctionFactory;
|
||||
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionFromPrompt;
|
||||
import com.microsoft.semantickernel.semanticfunctions.OutputVariable;
|
||||
import com.microsoft.semantickernel.semanticfunctions.PromptTemplateConfig;
|
||||
@@ -86,10 +86,13 @@ public class Example58_ConfigureExecutionSettings {
|
||||
}
|
||||
}""".stripIndent();
|
||||
|
||||
var promptConfig = PromptTemplateConfig.parseFromJson(configPayload);
|
||||
promptConfig.setTemplate(prompt);
|
||||
var promptConfig = PromptTemplateConfig
|
||||
.parseFromJson(configPayload)
|
||||
.setTemplate(prompt);
|
||||
|
||||
var func = KernelFunctionFactory.createFromPrompt(promptConfig, null);
|
||||
var func = KernelFunction
|
||||
.createFromPrompt(promptConfig)
|
||||
.build();
|
||||
|
||||
result = kernel.invokeAsync(func).block();
|
||||
System.out.println(result.getResult());
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ public class Example60_AdvancedMethodFunctions {
|
||||
new FunctionsChainingPlugin(),
|
||||
FunctionsChainingPlugin.PluginName);
|
||||
|
||||
kernel.getPlugins().add(functions);
|
||||
kernel.addPlugin(functions);
|
||||
|
||||
ContextVariableTypeConverter<MyCustomType> type = new ContextVariableTypeConverter<>(
|
||||
MyCustomType.class,
|
||||
|
||||
+3
-2
@@ -4,7 +4,8 @@ import com.azure.ai.openai.OpenAIAsyncClient;
|
||||
import com.azure.ai.openai.OpenAIClientBuilder;
|
||||
import com.azure.core.credential.AzureKeyCredential;
|
||||
import com.azure.core.credential.KeyCredential;
|
||||
import com.microsoft.semantickernel.AIService;
|
||||
import com.microsoft.semantickernel.services.AIService;
|
||||
import com.microsoft.semantickernel.services.AiServiceCollection;
|
||||
import com.microsoft.semantickernel.Kernel;
|
||||
import com.microsoft.semantickernel.chatcompletion.ChatCompletionService;
|
||||
import com.microsoft.semantickernel.orchestration.KernelFunction;
|
||||
@@ -83,7 +84,7 @@ public class Example62_CustomAIServiceSelector {
|
||||
// A dumb AIServiceSelector that just returns the first service and execution settings it finds
|
||||
static class CustomAIServiceSelector extends BaseAIServiceSelector {
|
||||
|
||||
public CustomAIServiceSelector(Map<Class<? extends AIService>, AIService> services) {
|
||||
public CustomAIServiceSelector(AiServiceCollection services) {
|
||||
super(services);
|
||||
}
|
||||
|
||||
|
||||
+5
-3
@@ -1,7 +1,7 @@
|
||||
package com.microsoft.semantickernel.samples.syntaxexamples;
|
||||
|
||||
import com.microsoft.semantickernel.Kernel;
|
||||
import com.microsoft.semantickernel.plugin.KernelFunctionFactory;
|
||||
import com.microsoft.semantickernel.orchestration.KernelFunction;
|
||||
import com.microsoft.semantickernel.plugin.KernelPlugin;
|
||||
import com.microsoft.semantickernel.plugin.annotations.DefineKernelFunction;
|
||||
|
||||
@@ -25,8 +25,10 @@ public class Example69_MutableKernelPlugin {
|
||||
System.out.println("======== Example69_MutableKernelPlugin ========");
|
||||
|
||||
KernelPlugin plugin = new KernelPlugin("Plugin", "Mutable plugin", null);
|
||||
plugin.addFunction(KernelFunctionFactory.createFromMethod(
|
||||
Time.class.getMethod("date"), new Time(), "dateFunction", null, null, null));
|
||||
plugin.addFunction(KernelFunction.createFromMethod(
|
||||
Time.class.getMethod("date"), new Time())
|
||||
.withFunctionName("dateFunction")
|
||||
.build());
|
||||
|
||||
Kernel kernel = Kernel.builder().build();
|
||||
kernel.addPlugin(plugin);
|
||||
|
||||
Reference in New Issue
Block a user