Add fluent invocations

This commit is contained in:
John Oliver
2024-02-06 21:26:07 +00:00
parent 52f2ef6588
commit 757474e252
107 changed files with 1458 additions and 1147 deletions
@@ -57,9 +57,8 @@ public class Example03_Arguments {
.withVariable("day", "Monday")
.build();
FunctionResult<?> resultValue = kernel.invokeAsync(
functionCollection.get("AppendDay"),
arguments)
FunctionResult<?> resultValue = kernel.invokeAsync(functionCollection.get("AppendDay"))
.withArguments(arguments)
.block();
System.out.println(resultValue.getResult());
@@ -8,9 +8,9 @@ import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.credential.KeyCredential;
import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.exceptions.ConfigurationException;
import com.microsoft.semantickernel.orchestration.PromptExecutionSettings;
import com.microsoft.semantickernel.orchestration.FunctionResult;
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;
@@ -79,14 +79,17 @@ public class Example05_InlineFunctionDefinition {
)
.build();
var result = kernel.invokeAsync(excuseFunction,
var result = kernel
.invokeAsync(excuseFunction)
.withArguments(
KernelFunctionArguments.builder()
.withInput("I missed the F1 final race")
.build())
.block();
System.out.println(result.getResult());
result = kernel.invokeAsync(excuseFunction,
result = kernel.invokeAsync(excuseFunction)
.withArguments(
KernelFunctionArguments.builder()
.withInput("sorry I forgot your birthday")
.build())
@@ -108,7 +111,7 @@ public class Example05_InlineFunctionDefinition {
null);
FunctionResult<?> fixedFunctionResult = kernel
.invokeAsync(fixedFunction, null)
.invokeAsync(fixedFunction)
.block();
System.out.println(fixedFunctionResult.getResult());
@@ -104,7 +104,7 @@ public class Example06_TemplateLanguage {
// Show the result
System.out.println("--- Prompt Function result");
var result = kernel.invokeAsync(kindOfDay, null).block();
var result = kernel.invokeAsync(kindOfDay).block();
System.out.println(result.getResult());
}
}
@@ -34,7 +34,7 @@ public class Example07_BingAndGooglePlugins {
private static final String CLIENT_ENDPOINT = System.getenv("CLIENT_ENDPOINT");
private static final String MODEL_ID = System.getenv()
.getOrDefault("MODEL_ID", "gpt-3.5-turbo");
private static final String BING_API_KEY = System.getenv("BING_API_KEY");
private static final String GOOGLE_API_KEY = System.getenv("GOOGLE_API_KEY");
private static final String GOOGLE_SEARCH_ENGINE_ID = System.getenv("GOOGLE_SEARCH_ENGINE_ID");
@@ -57,12 +57,13 @@ public class Example07_BingAndGooglePlugins {
// Load Bing plugin
var bingConnector = new BingConnector(BING_API_KEY);
var bing = KernelPluginFactory.createFromObject(new WebSearchEnginePlugin(bingConnector), "bing");
var bing = KernelPluginFactory.createFromObject(new WebSearchEnginePlugin(bingConnector),
"bing");
var chatCompletionService = ChatCompletionService.builder()
.withOpenAIAsyncClient(client)
.withModelId(MODEL_ID)
.build();
.withOpenAIAsyncClient(client)
.withModelId(MODEL_ID)
.build();
var kernel = Kernel.builder()
.withPlugin(bing)
@@ -78,8 +79,7 @@ public class Example07_BingAndGooglePlugins {
// kernel.importPluginFromObject(new WebSearchEnginePlugin(googleConnector), "google");
}
private static void example1Async(Kernel kernel, String searchPluginName)
{
private static void example1Async(Kernel kernel, String searchPluginName) {
System.out.println("======== Bing and Google Search Plugins ========");
// Run
@@ -87,9 +87,9 @@ public class Example07_BingAndGooglePlugins {
var kernelArguments = KernelFunctionArguments.builder()
.withVariable("query", question)
.build();
var function = kernel.getPlugins().getFunction(searchPluginName, "search");
var result = kernel.invokeAsync(function, kernelArguments).block();
var result = kernel.invokeAsync(function).withArguments(kernelArguments).block();
System.out.println(question);
System.out.printf("----%s----%n", searchPluginName);
@@ -108,45 +108,44 @@ public class Example07_BingAndGooglePlugins {
*/
}
private static void example2Async(Kernel kernel)
{
private static void example2Async(Kernel kernel) {
System.out.println("======== Use Search Plugin to answer user questions ========");
var semanticFunction = """
Answer questions only when you know the facts or the information is provided.
When you don't have sufficient information you reply with a list of commands to find the information needed.
When answering multiple questions, use a bullet point list.
Note: make sure single and double quotes are escaped using a backslash char.
Answer questions only when you know the facts or the information is provided.
When you don't have sufficient information you reply with a list of commands to find the information needed.
When answering multiple questions, use a bullet point list.
Note: make sure single and double quotes are escaped using a backslash char.
[COMMANDS AVAILABLE]
- bing.search
[COMMANDS AVAILABLE]
- bing.search
[INFORMATION PROVIDED]
{{ $externalInformation }}
[INFORMATION PROVIDED]
{{ $externalInformation }}
[EXAMPLE 1]
Question: what's the biggest lake in Italy?
Answer: Lake Garda, also known as Lago di Garda.
[EXAMPLE 1]
Question: what's the biggest lake in Italy?
Answer: Lake Garda, also known as Lago di Garda.
[EXAMPLE 2]
Question: what's the biggest lake in Italy? What's the smallest positive number?
Answer:
* Lake Garda, also known as Lago di Garda.
* The smallest positive number is 1.
[EXAMPLE 2]
Question: what's the biggest lake in Italy? What's the smallest positive number?
Answer:
* Lake Garda, also known as Lago di Garda.
* The smallest positive number is 1.
[EXAMPLE 3]
Question: what's Ferrari stock price? Who is the current number one female tennis player in the world?
Answer:
{{ '{{' }} bing.search "what's Ferrari stock price?" {{ '}}' }}.
{{ '{{' }} bing.search "Who is the current number one female tennis player in the world?" {{ '}}' }}.
[EXAMPLE 3]
Question: what's Ferrari stock price? Who is the current number one female tennis player in the world?
Answer:
{{ '{{' }} bing.search "what's Ferrari stock price?" {{ '}}' }}.
{{ '{{' }} bing.search "Who is the current number one female tennis player in the world?" {{ '}}' }}.
[END OF EXAMPLES]
[END OF EXAMPLES]
[TASK]
Question: {{ $question }}.
Answer:
""".stripIndent();
[TASK]
Question: {{ $question }}.
Answer:
""".stripIndent();
// The prompt function will append the answer here
var question = "Who is the most followed person on TikTok right now? What's the exchange rate EUR:USD?";
System.out.println(question);
@@ -167,13 +166,13 @@ public class Example07_BingAndGooglePlugins {
.withVariable("externalInformation", "")
.build();
FunctionResult<String> answer = kernel.invokeAsync(oracle, kernelArguments).block();
FunctionResult<String> answer = kernel.invokeAsync(oracle).withArguments(kernelArguments)
.block();
var result = answer.getResult();
// If the answer contains commands, execute them using the prompt renderer.
if (result.contains("bing.search"))
{
if (result.contains("bing.search")) {
PromptTemplate promptTemplate = new KernelPromptTemplateFactory()
.tryCreate(new PromptTemplateConfig(result));
@@ -189,10 +188,8 @@ public class Example07_BingAndGooglePlugins {
.build();
// Run the prompt function again, now including information from Bing
answer = kernel.invokeAsync(oracle, kernelArguments).block();
}
else
{
answer = kernel.invokeAsync(oracle).withArguments(kernelArguments).block();
} else {
System.out.println("AI had all the information, no need to query Bing.");
}
@@ -13,8 +13,10 @@ import com.microsoft.semantickernel.textcompletion.TextGenerationService;
import java.time.Duration;
public class Example08_RetryHandler {
private static final String MODEL_ID = System.getenv()
.getOrDefault("MODEL_ID", "text-davinci-003");
public static void main(String[] args) throws ConfigurationException {
// Create a Kernel with the HttpClient
RetryOptions retryOptions = new RetryOptions(new ExponentialBackoffOptions()
@@ -43,7 +45,7 @@ public class Example08_RetryHandler {
try {
// Will retry 3 times with exponential backoff
kernel.invokeAsync(fuction, null).block();
kernel.invokeAsync(fuction).block();
} catch (Exception e) {
System.out.println("Hit max retries");
}
@@ -10,7 +10,9 @@ import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.aiservices.openai.textcompletion.OpenAITextGenerationService;
import com.microsoft.semantickernel.orchestration.KernelFunctionArguments;
import com.microsoft.semantickernel.orchestration.FunctionResult;
import com.microsoft.semantickernel.orchestration.KernelFunctionArguments;
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariable;
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariableType;
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariableTypeConverter;
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariableTypeConverter.NoopConverter;
import com.microsoft.semantickernel.plugin.KernelPlugin;
@@ -63,7 +65,12 @@ public class Example09_FunctionTypes {
public DateTimeContextVariableTypeConverter() {
super(
ZonedDateTime.class,
(x) -> convert(x, ZonedDateTime.class),
(x) -> {
if (x instanceof OffsetDateTime) {
return ((OffsetDateTime) x).toZonedDateTime();
}
return convert(x, ZonedDateTime.class);
},
zonedDateTime -> zonedDateTime.format(DateTimeFormatter.ISO_DATE_TIME),
promptString -> ZonedDateTime.parse(promptString, DateTimeFormatter.ISO_DATE_TIME),
converters
@@ -120,17 +127,18 @@ public class Example09_FunctionTypes {
.build();
// Different ways to invoke a function (not limited to these examples)
FunctionResult<?> result = kernel.invokeAsync(plugin.get("NoInputWithVoidResult"), null).block();
FunctionResult<?> result = kernel.invokeAsync(plugin.get("NoInputWithVoidResult"))
.block();
assert result == null;
System.out.println(result != null ? result.getResult() : "null");
result = kernel.invokeAsync(plugin.get("NoInputTaskWithVoidResult"), null)
result = kernel.invokeAsync(plugin.get("NoInputTaskWithVoidResult"))
.block();
assert result == null;
System.out.println(result != null ? result.getResult() : "null");
result = kernel
.invokeAsync(
plugin.<String>get("InputDateTimeWithStringResult"),
.invokeAsync(plugin.<String>get("InputDateTimeWithStringResult"))
.withArguments(
KernelFunctionArguments
.builder()
.withVariable("currentDate",
@@ -143,11 +151,12 @@ public class Example09_FunctionTypes {
).block();
System.out.println(result.getResult());
result = kernel.invokeAsync(plugin.<String>get("NoInputTaskWithStringResult"), null)
result = kernel.invokeAsync(plugin.<String>get("NoInputTaskWithStringResult"))
.block();
System.out.println(result.getResult());
kernel.invokeAsync(plugin.<String>get("MultipleInputsWithVoidResult"),
kernel.invokeAsync(plugin.<String>get("MultipleInputsWithVoidResult"))
.withArguments(
KernelFunctionArguments
.builder()
.withVariable("x", "x string")
@@ -158,7 +167,8 @@ public class Example09_FunctionTypes {
System.out.println(result.getResult());
result = kernel
.invokeAsync(plugin.<String>get("ComplexInputWithStringResult"),
.invokeAsync(plugin.<String>get("ComplexInputWithStringResult"))
.withArguments(
KernelFunctionArguments
.builder()
.withVariable(
@@ -178,7 +188,8 @@ public class Example09_FunctionTypes {
System.out.println(result.getResult());
result = kernel
.invokeAsync(plugin.<String>get("InputStringTaskWithStringResult"),
.invokeAsync(plugin.<String>get("InputStringTaskWithStringResult"))
.withArguments(
KernelFunctionArguments
.builder()
.withVariable("echoInput", "return this")
@@ -187,25 +198,24 @@ public class Example09_FunctionTypes {
System.out.println(result.getResult());
result = kernel
.invokeAsync(plugin.<String>get("InputStringTaskWithVoidResult"),
.invokeAsync(plugin.<String>get("InputStringTaskWithVoidResult"))
.withArguments(
KernelFunctionArguments
.builder()
.withVariable("x", "x input")
.build()
)
)
.block();
assert result == null;
System.out.println(result != null ? result.getResult() : "null");
result = kernel
.invokeAsync(plugin.<String>get("noInputComplexReturnTypeAsync"),
null)
.invokeAsync(plugin.<String>get("noInputComplexReturnTypeAsync"))
.block();
System.out.println(result.getResult());
var temporalResult = kernel
.invokeAsync(plugin.<Temporal>get("noInputComplexReturnType"),
null)
.invokeAsync(plugin.<Temporal>get("noInputComplexReturnType"))
.block();
System.out.println(temporalResult.getResult());
@@ -220,48 +230,60 @@ public class Example09_FunctionTypes {
// | T | U extends T | V converted from U |
var result1 = kernel
.invokeAsync(plugin.<OffsetDateTime>get("conversionScenarioA"),
null)
.invokeAsync(plugin.<OffsetDateTime>get("conversionScenarioA"))
.block();
System.out.println(result1.getResult());
var result2 = kernel
.invokeAsync(plugin.<Instant>get("conversionScenarioA"),
null)
.invokeAsync(plugin.<Instant>get("conversionScenarioA"))
.block();
System.out.println(result2.getResult());
var result3 = kernel
.invokeAsync(plugin.<OffsetDateTime>get("conversionScenarioB"),
null)
.invokeAsync(plugin.<OffsetDateTime>get("conversionScenarioB"))
.block();
System.out.println(result3.getResult());
var result4 = kernel
.invokeAsync(plugin.<Temporal>get("conversionScenarioB"),
null)
.invokeAsync(plugin.<Temporal>get("conversionScenarioB"))
.block();
System.out.println(result4.getResult());
var result5 = kernel
.invokeAsync(plugin.<Instant>get("conversionScenarioB"),
null
)
.invokeAsync(plugin.<Instant>get("conversionScenarioB"))
.block();
System.out.println(result5.getResult());
var result6 = kernel
.invokeAsync(plugin.get("noInputComplexReturnType"),
null)
.invokeAsync(plugin.get("noInputComplexReturnType"))
.block();
System.out.println(result6.getResult());
var result7 = kernel
.invokeAsync(plugin.get("withDefaultValue"),
null)
.invokeAsync(plugin.get("withDefaultValue"))
.block();
System.out.println(result7.getResult());
var result8 = kernel
.invokeAsync(plugin.get("noInputComplexReturnType"))
.withResultType(
new ContextVariableType<>(
new DateTimeContextVariableTypeConverter(),
ZonedDateTime.class)
)
.block();
System.out.println(result8.getResult());
var result9 = kernel
.invokeAsync(plugin.get("noInputComplexReturnType"))
.withResultType(
new ContextVariableType<>(
new DateTimeContextVariableTypeConverter(),
ZonedDateTime.class)
)
.block();
System.out.println(result9.getResult());
/*
TODO: support FunctionResult
kernel
@@ -295,8 +317,7 @@ public class Example09_FunctionTypes {
kernel
.invokeAsync(
kernel.getPlugins()
.getFunction("Examples", "NoInputWithVoidResult"),
null)
.getFunction("Examples", "NoInputWithVoidResult"))
.block();
}
@@ -306,7 +327,7 @@ public class Example09_FunctionTypes {
/// <summary>
/// Example of using a void function with no input
/// </summary>
@DefineKernelFunction(name = "NoInputWithVoidResult", returnType="void")
@DefineKernelFunction(name = "NoInputWithVoidResult", returnType = "void")
public void NoInputWithVoidResult() {
System.out.println("Running this.NoInputWithVoidResult) -> No input");
}
@@ -12,27 +12,28 @@ public class Example11_WebSearchQueries {
var kernel = Kernel.builder().build();
// Load native plugins
var searchUrlPlugin = KernelPluginFactory.createFromObject(new SearchUrlPlugin(), "SearchUrlPlugin");
var searchUrlPlugin = KernelPluginFactory.createFromObject(new SearchUrlPlugin(),
"SearchUrlPlugin");
var bingSearchFunction = searchUrlPlugin.get("BingSearchUrl");
// Run
// Run
var ask = "What's the largest building in Europe?";
var kernelArguments = KernelFunctionArguments.builder()
.withVariable("query", ask)
.build();
var result = kernel.invokeAsync(bingSearchFunction, kernelArguments).block();
var result = kernel.invokeAsync(bingSearchFunction).withArguments(kernelArguments).block();
System.out.println(ask);
System.out.println(result.getResult());
/* Expected output:
* ======== WebSearchQueries ========
* What's the tallest building in Europe?
*
* https://www.bing.com/search?q=What%27s%20the%20tallest%20building%20in%20Europe%3F
* == DONE ==
*/
/* Expected output:
* ======== WebSearchQueries ========
* What's the tallest building in Europe?
*
* https://www.bing.com/search?q=What%27s%20the%20tallest%20building%20in%20Europe%3F
* == DONE ==
*/
}
}
@@ -9,7 +9,6 @@ import com.microsoft.semantickernel.Kernel;
import com.microsoft.semantickernel.aiservices.openai.textcompletion.OpenAITextGenerationService;
import com.microsoft.semantickernel.exceptions.ConfigurationException;
import com.microsoft.semantickernel.orchestration.FunctionResult;
import com.microsoft.semantickernel.orchestration.KernelFunction;
import com.microsoft.semantickernel.orchestration.KernelFunctionArguments.Builder;
import com.microsoft.semantickernel.plugin.KernelPlugin;
import com.microsoft.semantickernel.plugin.KernelPluginFactory;
@@ -164,7 +163,8 @@ public class Example13_ConversationSummaryPlugin {
Mono<FunctionResult<String>> summary = kernel
.invokeAsync(
conversationSummaryPlugin
.<String>get("GetConversationActionItems"),
.<String>get("GetConversationActionItems"))
.withArguments(
new Builder()
.withInput(chatTranscript)
.build()
@@ -181,9 +181,8 @@ public class Example13_ConversationSummaryPlugin {
new ConversationSummaryPlugin(), null);
Mono<FunctionResult<String>> summary = kernel
.invokeAsync(
conversationSummaryPlugin
.<String>get("GetConversationTopics"),
.invokeAsync(conversationSummaryPlugin.<String>get("GetConversationTopics"))
.withArguments(
new Builder()
.withInput(chatTranscript)
.build()
@@ -206,7 +205,8 @@ public class Example13_ConversationSummaryPlugin {
.invokeAsync(
conversationSummaryPlugin
.getFunctions()
.get("SummarizeConversation"),
.get("SummarizeConversation"))
.withArguments(
new Builder()
.withInput(chatTranscript)
.build()
@@ -58,7 +58,8 @@ public class Example43_GetModelResult {
// Invoke function through kernel
FunctionResult<String> result = kernel.invokeAsync(
myFunction,
myFunction)
.withArguments(
KernelFunctionArguments.builder()
.withVariable("input", "travel")
.build())
@@ -84,7 +84,7 @@ public class Example56_TemplateMethodFunctionsWithMultipleArguments {
// Show the result
System.out.println("--- Prompt Function result");
var result = kernel.invokeAsync(haiku, arguments).block();
var result = kernel.invokeAsync(haiku).withArguments(arguments).block();
System.out.println(result.getResult());
}
@@ -31,6 +31,9 @@ import com.microsoft.semantickernel.orchestration.PromptExecutionSettings;
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariable;
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionFromPrompt;
import com.microsoft.semantickernel.semanticfunctions.OutputVariable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class Example57_KernelHooks {
@@ -131,8 +134,8 @@ public class Example57_KernelHooks {
// Invoke prompt to trigger execution hooks.
String input = "I missed the F1 final race";
var result = kernel.invokeAsync(
excuseFunction,
var result = kernel.invokeAsync(excuseFunction)
.withArguments(
KernelFunctionArguments
.builder()
.withVariable("input", input)
@@ -188,7 +191,8 @@ public class Example57_KernelHooks {
// Invoke prompt to trigger execution hooks.
String input = "I missed the F1 final race";
var result = kernel.invokeAsync(excuseFunction,
var result = kernel.invokeAsync(excuseFunction)
.withArguments(
KernelFunctionArguments
.builder()
.withVariable("input", input)
@@ -235,8 +239,8 @@ public class Example57_KernelHooks {
kernel.getGlobalKernelHooks().addHook(hook);
// Invoke prompt to trigger execution hooks.
var result = kernel.invokeAsync(
writerFunction,
var result = kernel.invokeAsync(writerFunction)
.withArguments(
KernelFunctionArguments.builder().build())
.block();
System.out.println("Function Result: " + result.getResult());
@@ -276,8 +280,8 @@ public class Example57_KernelHooks {
try {
// Invoke prompt to trigger execution hooks.
var result = kernel.invokeAsync(
writerFunction,
var result = kernel.invokeAsync(writerFunction)
.withArguments(
KernelFunctionArguments.builder().build())
.block();
System.out.println("Function Result: " + result.getResult());
@@ -313,9 +317,8 @@ public class Example57_KernelHooks {
// Invoke prompt to trigger execution hooks.
try {
var result = kernel.invokeAsync(
secondFunction,
KernelFunctionArguments.builder().build())
var result = kernel.invokeAsync(secondFunction)
.withArguments(KernelFunctionArguments.builder().build())
.block();
System.out.println("Function Result: " + result.getResult());
} catch (Exception e) {
@@ -355,8 +358,8 @@ public class Example57_KernelHooks {
try {
// Invoke prompt to trigger execution hooks.
var result = kernel.invokeAsync(
writerFunction,
var result = kernel.invokeAsync(writerFunction)
.withArguments(
KernelFunctionArguments.builder().build())
.block();
System.out.println("Function Result: " + result.getResult());
@@ -54,18 +54,17 @@ public class Example58_ConfigureExecutionSettings {
// Option 1:
// Invoke the prompt function and pass an OpenAI specific instance containing the execution settings
var result = kernel.invokeAsync(
KernelFunctionFromPrompt.builder()
.withTemplate(prompt)
.withDefaultExecutionSettings(
PromptExecutionSettings.builder()
.withMaxTokens(60)
.withTemperature(0.7)
.build()
)
.withOutputVariable(new OutputVariable("result", "java.lang.String"))
.build(),
null
).block();
KernelFunctionFromPrompt.builder()
.withTemplate(prompt)
.withDefaultExecutionSettings(
PromptExecutionSettings.builder()
.withMaxTokens(60)
.withTemperature(0.7)
.build()
)
.withOutputVariable(new OutputVariable("result", "java.lang.String"))
.build())
.block();
System.out.println(result.getResult());
@@ -93,7 +92,7 @@ public class Example58_ConfigureExecutionSettings {
var func = KernelFunctionFactory.createFromPrompt(promptConfig, null);
result = kernel.invokeAsync(func, null).block();
result = kernel.invokeAsync(func).block();
System.out.println(result.getResult());
}
}
@@ -7,9 +7,9 @@ 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.KernelFunctionArguments;
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariableTypeConverter;
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariableTypes;
import com.microsoft.semantickernel.orchestration.KernelFunctionArguments;
import com.microsoft.semantickernel.plugin.KernelPluginFactory;
import com.microsoft.semantickernel.plugin.annotations.DefineKernelFunction;
import reactor.core.publisher.Mono;
@@ -68,14 +68,12 @@ public class Example60_AdvancedMethodFunctions {
ContextVariableTypes.DEFAULT_TYPES.putConverter(type);
var result = kernel
.invokeAsync(
FunctionsChainingPlugin.PluginName,
"Function1",
.invokeAsync(FunctionsChainingPlugin.PluginName, "Function1")
.withArguments(
KernelFunctionArguments
.builder()
.build(),
ContextVariableTypes.getDefaultVariableTypeForClass(MyCustomType.class)
)
.build())
.withResultType(ContextVariableTypes.getDefaultVariableTypeForClass(MyCustomType.class))
.block();
System.out.println("CustomType.Number: " + result.getResult().number); // 2
@@ -120,12 +118,10 @@ public class Example60_AdvancedMethodFunctions {
public Mono<MyCustomType> function1Async(Kernel kernel) {
// Execute another function
return kernel
.invokeAsync(
PluginName,
"Function2",
KernelFunctionArguments.builder().build(),
ContextVariableTypes.getDefaultVariableTypeForClass(
Example60_AdvancedMethodFunctions.MyCustomType.class))
.invokeAsync(PluginName, "Function2")
.withArguments(KernelFunctionArguments.builder().build())
.withResultType(ContextVariableTypes.getDefaultVariableTypeForClass(
Example60_AdvancedMethodFunctions.MyCustomType.class))
.flatMap(value -> {
// Process the result
return Mono.just(
@@ -86,7 +86,7 @@ public class Example61_MultipleLLMs {
.withOutputVariable("result", "java.lang.String")
.build();
var result = kernel.invokeAsync(func, arguments).block();
var result = kernel.invokeAsync(func).withArguments(arguments).block();
System.out.println(result.getResult());
}
@@ -97,18 +97,18 @@ public class Example61_MultipleLLMs {
var prompt = "Hello AI, what can you do for me?";
var result = kernel.invokeAsync(
KernelFunctionFromPrompt
.builder()
.withTemplate(prompt)
.withDefaultExecutionSettings(
PromptExecutionSettings.builder()
.withModelId(modelId)
.build()
)
.withOutputVariable("result", "java.lang.String")
.build(),
KernelFunctionArguments.builder().build()
).block();
KernelFunctionFromPrompt
.builder()
.withTemplate(prompt)
.withDefaultExecutionSettings(
PromptExecutionSettings.builder()
.withModelId(modelId)
.build()
)
.withOutputVariable("result", "java.lang.String")
.build())
.withArguments(KernelFunctionArguments.builder().build())
.block();
System.out.println(result.getResult());
}
@@ -137,9 +137,8 @@ public class Example61_MultipleLLMs {
var function = KernelFunctionFromPrompt.create(promptConfig);
var result = kernel.invokeAsync(
function,
KernelFunctionArguments.builder().build())
var result = kernel.invokeAsync(function)
.withArguments(KernelFunctionArguments.builder().build())
.block();
System.out.println(result.getResult());
@@ -77,7 +77,7 @@ public class Example62_CustomAIServiceSelector {
.withOutputVariable("result", "java.lang.String")
.build();
var result = kernel.invokeAsync(func, arguments).block();
var result = kernel.invokeAsync(func).withArguments(arguments).block();
System.out.println(result.getResult());
}
@@ -96,16 +96,16 @@ public class Example62_CustomAIServiceSelector {
@Nullable
KernelFunction<?> function,
@Nullable
KernelFunctionArguments arguments,
Map<Class<? extends AIService>, AIService> services) {
Map<Class<? extends AIService>, AIService> services) {
// Just get the first one
PromptExecutionSettings executionSettings = function.getExecutionSettings()
.values().stream().findFirst().get();
// unchecked cast
T service = (T)services.values().stream().findFirst().get();
T service = (T) services.values().stream().findFirst().get();
return new AIServiceSelection<>(
service,
@@ -50,7 +50,7 @@ public class Example63_ChatCompletionPrompts {
""".stripIndent();
var chatSemanticFunction = KernelFunctionFromPrompt.create(chatPrompt);
var chatPromptResult = kernel.invokeAsync(chatSemanticFunction, null).block();
var chatPromptResult = kernel.invokeAsync(chatSemanticFunction).block();
System.out.println("Chat Prompt:");
System.out.println(chatPrompt);
@@ -60,7 +60,7 @@ public class Example63_ChatCompletionPrompts {
CountDownLatch cdl = new CountDownLatch(1);
System.out.println("Chat Prompt Result:");
kernel
.invokeAsync(chatSemanticFunction, null)
.invokeAsync(chatSemanticFunction)
.doFinally(x -> cdl.countDown())
.subscribe(result -> System.out.println(result.getResult()));
@@ -81,7 +81,7 @@ public class Example64_MultiplePromptTemplates {
.withVariable("name", "Bob")
.build();
var result = kernel.invokeAsync(function, arguments).block();
var result = kernel.invokeAsync(function).withArguments(arguments).block();
System.out.println(result.getResult());
}
}
@@ -31,8 +31,8 @@ public class Example69_MutableKernelPlugin {
Kernel kernel = Kernel.builder().build();
kernel.getPlugins().add(plugin);
var result = kernel.invokeAsync(kernel.getPlugins().getFunction("Plugin", "dateFunction"),
null).block();
var result = kernel.invokeAsync(kernel.getPlugins().getFunction("Plugin", "dateFunction"))
.block();
System.out.println("Result: " + result.getResult());
}