mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
Java: Initial implementation of Example09_FunctionTypes (#4493)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
This commit is contained in:
+29
-6
@@ -3,6 +3,9 @@ package com.microsoft.semantickernel.samples.syntaxexamples;
|
||||
|
||||
|
||||
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.DefaultKernel;
|
||||
import com.microsoft.semantickernel.Kernel;
|
||||
import com.microsoft.semantickernel.aiservices.azureopenai.AzureOpenAITextGenerationService;
|
||||
@@ -11,7 +14,6 @@ import com.microsoft.semantickernel.orchestration.PromptExecutionSettings;
|
||||
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariable;
|
||||
import com.microsoft.semantickernel.orchestration.contextvariables.KernelArguments;
|
||||
import com.microsoft.semantickernel.plugin.KernelFunctionFactory;
|
||||
import com.microsoft.semantickernel.samples.SamplesConfig;
|
||||
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionFromPrompt;
|
||||
import com.microsoft.semantickernel.textcompletion.TextGenerationService;
|
||||
import java.time.Instant;
|
||||
@@ -20,14 +22,35 @@ import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class Example05_InlineFunctionDefinition {
|
||||
|
||||
private static final boolean USE_AZURE_CLIENT = Boolean.parseBoolean(
|
||||
System.getenv("USE_AZURE_CLIENT"));
|
||||
private static final String CLIENT_KEY = System.getenv("CLIENT_KEY");
|
||||
|
||||
// Only required if USE_AZURE_CLIENT is true
|
||||
private static final String CLIENT_ENDPOINT = System.getenv("CLIENT_ENDPOINT");
|
||||
|
||||
public static void main(String[] args) throws ConfigurationException {
|
||||
|
||||
OpenAIAsyncClient client = SamplesConfig.getClient();
|
||||
TextGenerationService textGenerationService;
|
||||
|
||||
AzureOpenAITextGenerationService textGenerationService = AzureOpenAITextGenerationService.builder()
|
||||
.withOpenAIAsyncClient(client)
|
||||
.withModelId("text-davinci-003")
|
||||
.build();
|
||||
if (USE_AZURE_CLIENT) {
|
||||
OpenAIAsyncClient client = new OpenAIClientBuilder()
|
||||
.credential(new AzureKeyCredential(CLIENT_KEY))
|
||||
.endpoint(CLIENT_ENDPOINT)
|
||||
.buildAsyncClient();
|
||||
|
||||
textGenerationService = AzureOpenAITextGenerationService.builder()
|
||||
.withOpenAIAsyncClient(client)
|
||||
.withModelId("text-davinci-003")
|
||||
.build();
|
||||
} else {
|
||||
OpenAIAsyncClient client = new OpenAIClientBuilder()
|
||||
.credential(new KeyCredential(CLIENT_KEY))
|
||||
.buildAsyncClient();
|
||||
|
||||
// TODO: Add support for OpenAI API
|
||||
textGenerationService = null;
|
||||
}
|
||||
|
||||
Kernel kernel = new DefaultKernel.Builder()
|
||||
.withDefaultAIService(TextGenerationService.class, textGenerationService)
|
||||
|
||||
+1
-2
@@ -8,7 +8,6 @@ import com.azure.core.http.policy.RetryOptions;
|
||||
import com.microsoft.semantickernel.DefaultKernel;
|
||||
import com.microsoft.semantickernel.Kernel;
|
||||
import com.microsoft.semantickernel.aiservices.azureopenai.AzureOpenAITextGenerationService;
|
||||
import com.microsoft.semantickernel.exceptions.ConfigurationException;
|
||||
import com.microsoft.semantickernel.orchestration.KernelFunction;
|
||||
import com.microsoft.semantickernel.semanticfunctions.KernelFunctionFromPrompt;
|
||||
import com.microsoft.semantickernel.textcompletion.TextGenerationService;
|
||||
@@ -16,7 +15,7 @@ import java.time.Duration;
|
||||
|
||||
public class Example08_RetryHandler {
|
||||
|
||||
public static void main(String[] args) throws ConfigurationException {
|
||||
public static void main(String[] args) {
|
||||
// Create a Kernel with the HttpClient
|
||||
RetryOptions retryOptions = new RetryOptions(new ExponentialBackoffOptions()
|
||||
.setMaxDelay(Duration.ofSeconds(10))
|
||||
|
||||
+477
@@ -0,0 +1,477 @@
|
||||
package com.microsoft.semantickernel.samples.syntaxexamples;
|
||||
|
||||
import static com.microsoft.semantickernel.orchestration.contextvariables.ContextVariableTypes.convert;
|
||||
|
||||
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.DefaultKernel;
|
||||
import com.microsoft.semantickernel.Kernel;
|
||||
import com.microsoft.semantickernel.aiservices.azureopenai.AzureOpenAITextGenerationService;
|
||||
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariable;
|
||||
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariableTypeConverter;
|
||||
import com.microsoft.semantickernel.orchestration.contextvariables.ContextVariableTypeConverter.NoopConverter;
|
||||
import com.microsoft.semantickernel.orchestration.contextvariables.KernelArguments;
|
||||
import com.microsoft.semantickernel.plugin.KernelPlugin;
|
||||
import com.microsoft.semantickernel.plugin.KernelPluginFactory;
|
||||
import com.microsoft.semantickernel.plugin.annotations.DefineKernelFunction;
|
||||
import com.microsoft.semantickernel.plugin.annotations.KernelFunctionParameter;
|
||||
import com.microsoft.semantickernel.textcompletion.TextGenerationService;
|
||||
import java.nio.file.Path;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
public class Example09_FunctionTypes {
|
||||
|
||||
private static final boolean USE_AZURE_CLIENT = Boolean.parseBoolean(
|
||||
System.getenv("USE_AZURE_CLIENT"));
|
||||
private static final String CLIENT_KEY = System.getenv("CLIENT_KEY");
|
||||
|
||||
// Only required if USE_AZURE_CLIENT is true
|
||||
private static final String CLIENT_ENDPOINT = System.getenv("CLIENT_ENDPOINT");
|
||||
|
||||
|
||||
private static class DateTimeContextVariableTypeConverter extends
|
||||
ContextVariableTypeConverter<ZonedDateTime> {
|
||||
|
||||
private static final List<Converter<ZonedDateTime, ?>> converters = List.of(
|
||||
new DefaultConverter<>(ZonedDateTime.class, Date.class) {
|
||||
@Override
|
||||
public Date toObject(ZonedDateTime zonedDateTime) {
|
||||
return new Date(zonedDateTime.toInstant().toEpochMilli());
|
||||
}
|
||||
},
|
||||
new DefaultConverter<>(ZonedDateTime.class, String.class) {
|
||||
@Override
|
||||
public String toObject(ZonedDateTime zonedDateTime) {
|
||||
return zonedDateTime.format(DateTimeFormatter.ISO_DATE_TIME);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
public DateTimeContextVariableTypeConverter() {
|
||||
super(
|
||||
ZonedDateTime.class,
|
||||
(x) -> convert(x, ZonedDateTime.class),
|
||||
zonedDateTime -> zonedDateTime.format(DateTimeFormatter.ISO_DATE_TIME),
|
||||
promptString -> ZonedDateTime.parse(promptString, DateTimeFormatter.ISO_DATE_TIME),
|
||||
converters
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
System.out.println("======== Method Function types ========");
|
||||
|
||||
TextGenerationService textGenerationService;
|
||||
|
||||
if (USE_AZURE_CLIENT) {
|
||||
OpenAIAsyncClient client = new OpenAIClientBuilder()
|
||||
.credential(new AzureKeyCredential(CLIENT_KEY))
|
||||
.endpoint(CLIENT_ENDPOINT)
|
||||
.buildAsyncClient();
|
||||
|
||||
textGenerationService = AzureOpenAITextGenerationService.builder()
|
||||
.withOpenAIAsyncClient(client)
|
||||
.withModelId("text-davinci-003")
|
||||
.build();
|
||||
} else {
|
||||
OpenAIAsyncClient client = new OpenAIClientBuilder()
|
||||
.credential(new KeyCredential(CLIENT_KEY))
|
||||
.buildAsyncClient();
|
||||
|
||||
// TODO: Add support for OpenAI API
|
||||
textGenerationService = null;
|
||||
}
|
||||
|
||||
// Load native plugin into the kernel function collection, sharing its functions with prompt templates
|
||||
KernelPlugin plugin = KernelPluginFactory
|
||||
.createFromObject(new LocalExamplePlugin(), "Examples");
|
||||
|
||||
KernelPlugin summarize = KernelPluginFactory
|
||||
.importPluginFromDirectory(
|
||||
Path.of("java/samples/sample-code/src/main/resources/Plugins"),
|
||||
"SummarizePlugin",
|
||||
null);
|
||||
|
||||
KernelPlugin examplePlugin = KernelPluginFactory
|
||||
.importPluginFromResourcesDirectory(
|
||||
"Plugins",
|
||||
"ExamplePlugins",
|
||||
"ExampleFunction",
|
||||
null,
|
||||
Example09_FunctionTypes.class
|
||||
);
|
||||
|
||||
Kernel kernel = new DefaultKernel.Builder()
|
||||
.withDefaultAIService(TextGenerationService.class, textGenerationService)
|
||||
.withPlugin(plugin)
|
||||
.withPlugin(summarize)
|
||||
.withPlugin(examplePlugin)
|
||||
.build();
|
||||
|
||||
// Different ways to invoke a function (not limited to these examples)
|
||||
kernel.invokeAsync(plugin.get("NoInputWithVoidResult"), null, String.class).block();
|
||||
kernel.invokeAsync(plugin.get("NoInputTaskWithVoidResult"), null, String.class).block();
|
||||
|
||||
kernel
|
||||
.invokeAsync(
|
||||
plugin.get("InputDateTimeWithStringResult"),
|
||||
KernelArguments
|
||||
.builder()
|
||||
.withVariable("currentDate",
|
||||
ContextVariable.of(
|
||||
ZonedDateTime.now(),
|
||||
new DateTimeContextVariableTypeConverter()
|
||||
)
|
||||
)
|
||||
.build(),
|
||||
String.class
|
||||
).block();
|
||||
|
||||
kernel.invokeAsync(plugin.get("NoInputTaskWithStringResult"), null, String.class).block();
|
||||
|
||||
kernel.invokeAsync(plugin.get("MultipleInputsWithVoidResult"),
|
||||
KernelArguments
|
||||
.builder()
|
||||
.withVariable("x", "x string")
|
||||
.withVariable("y", 100)
|
||||
.withVariable("z", 1.5)
|
||||
.build(),
|
||||
String.class)
|
||||
.block();
|
||||
|
||||
kernel
|
||||
.invokeAsync(plugin.get("ComplexInputWithStringResult"),
|
||||
KernelArguments
|
||||
.builder()
|
||||
.withVariable(
|
||||
"complexObject",
|
||||
ContextVariable.of(
|
||||
new Object() {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "A complex object";
|
||||
}
|
||||
},
|
||||
new NoopConverter<>(Object.class)
|
||||
)
|
||||
)
|
||||
.build(),
|
||||
String.class)
|
||||
.block();
|
||||
|
||||
kernel
|
||||
.invokeAsync(plugin.get("InputStringTaskWithStringResult"),
|
||||
KernelArguments
|
||||
.builder()
|
||||
.withVariable("echoInput", "return this")
|
||||
.build(),
|
||||
String.class)
|
||||
.block();
|
||||
|
||||
kernel
|
||||
.invokeAsync(plugin.get("InputStringTaskWithVoidResult"),
|
||||
KernelArguments
|
||||
.builder()
|
||||
.withVariable("x", "x input")
|
||||
.build(),
|
||||
Void.class)
|
||||
.block();
|
||||
|
||||
/*
|
||||
TODO: support FunctionResult
|
||||
kernel
|
||||
.invokeAsync(plugin.get("NoInputWithFunctionResult"),
|
||||
null,
|
||||
Void.class)
|
||||
.block();
|
||||
|
||||
*/
|
||||
|
||||
/*
|
||||
TODO: support injection
|
||||
// Injecting Parameters Examples
|
||||
await kernel.InvokeAsync(plugin[nameof(LocalExamplePlugin.TaskInjectingKernelFunctionWithStringResult)]);
|
||||
await kernel.InvokeAsync(plugin[nameof(LocalExamplePlugin.TaskInjectingLoggerWithNoResult)]);
|
||||
await kernel.InvokeAsync(plugin[nameof(LocalExamplePlugin.TaskInjectingLoggerFactoryWithNoResult)]);
|
||||
await kernel.InvokeAsync(plugin[nameof(LocalExamplePlugin.TaskInjectingCultureInfoOrIFormatProviderWithStringResult)]);
|
||||
await kernel.InvokeAsync(plugin[nameof(LocalExamplePlugin.TaskInjectingCancellationTokenWithStringResult)]);
|
||||
await kernel.InvokeAsync(plugin[nameof(LocalExamplePlugin.TaskInjectingServiceSelectorWithStringResult)]);
|
||||
await kernel.InvokeAsync(plugin[nameof(LocalExamplePlugin.TaskInjectingKernelWithInputTextAndStringResult)],
|
||||
new()
|
||||
{
|
||||
["textToSummarize"] = @"C# is a modern, versatile language by Microsoft, blending the efficiency of C++
|
||||
with Visual Basic's simplicity. It's ideal for a wide range of applications,
|
||||
emphasizing type safety, modularity, and modern programming paradigms."
|
||||
});
|
||||
|
||||
*/
|
||||
|
||||
// You can also use the kernel.Plugins collection to invoke a function
|
||||
kernel
|
||||
.invokeAsync(
|
||||
kernel.getPlugins()
|
||||
.getFunction("Examples", "NoInputWithVoidResult"),
|
||||
null,
|
||||
Void.class)
|
||||
.block();
|
||||
}
|
||||
|
||||
|
||||
public static class LocalExamplePlugin {
|
||||
|
||||
/// <summary>
|
||||
/// Example of using a void function with no input
|
||||
/// </summary>
|
||||
@DefineKernelFunction(name = "NoInputWithVoidResult")
|
||||
public void NoInputWithVoidResult() {
|
||||
System.out.println("Running this.NoInputWithVoidResult) -> No input");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example of using a void task function with no input
|
||||
/// </summary>
|
||||
@DefineKernelFunction(name = "NoInputTaskWithVoidResult")
|
||||
public Mono<Void> NoInputTaskWithVoidResult() {
|
||||
return Mono.fromRunnable(
|
||||
() -> System.out.println("Running this.NoInputTaskWithVoidResult) -> No input"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example of using a function with a DateTime input and a string result
|
||||
/// </summary>
|
||||
@DefineKernelFunction(name = "InputDateTimeWithStringResult")
|
||||
public String InputDateTimeWithStringResult(
|
||||
@KernelFunctionParameter(
|
||||
name = "currentDate",
|
||||
description = "Current date time",
|
||||
type = ZonedDateTime.class
|
||||
)
|
||||
ZonedDateTime currentDate) {
|
||||
var result = currentDate.format(DateTimeFormatter.ISO_DATE_TIME);
|
||||
System.out.println(
|
||||
"Running {nameof(this.InputDateTimeWithStringResult)} -> [currentDate = {"
|
||||
+ currentDate + "}] -> result: {" + result + "}");
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example of using a Task function with no input and a string result
|
||||
/// </summary>
|
||||
@DefineKernelFunction(name = "NoInputTaskWithStringResult")
|
||||
public Mono<String> NoInputTaskWithStringResult() {
|
||||
return Mono.fromCallable(() -> {
|
||||
var result = "string result";
|
||||
System.out.println(
|
||||
"Running {nameof(this.NoInputTaskWithStringResult)} -> No input -> result: {"
|
||||
+ result + "}");
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example passing multiple parameters with multiple types
|
||||
/// </summary>
|
||||
@DefineKernelFunction(name = "MultipleInputsWithVoidResult")
|
||||
public void MultipleInputsWithVoidResult(
|
||||
@KernelFunctionParameter(
|
||||
name = "x"
|
||||
)
|
||||
String x,
|
||||
|
||||
@KernelFunctionParameter(
|
||||
name = "y",
|
||||
type = int.class
|
||||
)
|
||||
int y,
|
||||
|
||||
@KernelFunctionParameter(
|
||||
name = "z",
|
||||
type = double.class
|
||||
)
|
||||
double z) {
|
||||
System.out.println(
|
||||
"Running {nameof(this.MultipleInputsWithVoidResult)} -> input: [x = {" + x
|
||||
+ "}, y = {" + y + "}, z = {" + z + "}]");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example passing a complex object and returning a string result
|
||||
/// </summary>
|
||||
@DefineKernelFunction(name = "ComplexInputWithStringResult")
|
||||
public String ComplexInputWithStringResult(
|
||||
@KernelFunctionParameter(
|
||||
name = "complexObject",
|
||||
type = Object.class
|
||||
)
|
||||
Object complexObject
|
||||
) {
|
||||
String result = complexObject.toString();
|
||||
System.out.println(
|
||||
"Running {nameof(this.ComplexInputWithStringResult)} -> input: [complexObject = {complexObject}] -> result: {"
|
||||
+ result + "}");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Example using an async task function echoing the input
|
||||
/// </summary>
|
||||
@DefineKernelFunction(name = "InputStringTaskWithStringResult")
|
||||
public Mono<String> InputStringTaskWithStringResult(
|
||||
|
||||
@KernelFunctionParameter(
|
||||
name = "echoInput"
|
||||
)
|
||||
String echoInput) {
|
||||
return Mono.fromCallable(() -> {
|
||||
System.out.println(
|
||||
"Running {nameof(this.InputStringTaskWithStringResult)} -> input: [echoInput = {echoInput}] -> result: {"
|
||||
+ echoInput + "}");
|
||||
return echoInput;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example using an async void task with string input
|
||||
/// </summary>
|
||||
@DefineKernelFunction(name = "InputStringTaskWithVoidResult")
|
||||
public Mono<Void> InputStringTaskWithVoidResult(
|
||||
@KernelFunctionParameter(
|
||||
name = "x"
|
||||
)
|
||||
String x) {
|
||||
return Mono.fromRunnable(() -> {
|
||||
System.out.println(
|
||||
"Running {nameof(this.InputStringTaskWithVoidResult)} -> input: [x = {" + x
|
||||
+ "}]");
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
/// <summary>
|
||||
/// Example using a function to return the result of another inner function
|
||||
/// </summary>
|
||||
@DefineKernelFunction(name = "InputStringTaskWithVoidResult")
|
||||
public FunctionResult NoInputWithFunctionResult()
|
||||
{
|
||||
var myInternalFunction = KernelFunctionFactory.CreateFromMethod(() => { });
|
||||
var result = new FunctionResult(myInternalFunction);
|
||||
Console.WriteLine($"Running {nameof(this.NoInputWithFunctionResult)} -> No input -> result: {result.GetType().Name}");
|
||||
return result;
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
|
||||
/// <summary>
|
||||
/// Example using a task function to return the result of another kernel function
|
||||
/// </summary>
|
||||
[KernelFunction]
|
||||
public async Task<FunctionResult> NoInputTaskWithFunctionResult(Kernel kernel)
|
||||
{
|
||||
var result = await kernel.InvokeAsync(kernel.Plugins["Examples"][nameof(this.NoInputWithVoidResult)]);
|
||||
Console.WriteLine($"Running {nameof(this.NoInputTaskWithFunctionResult)} -> Injected kernel -> result: {result.GetType().Name}");
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example how to inject Kernel in your function
|
||||
/// This example uses the injected kernel to invoke a plugin from within another function
|
||||
/// </summary>
|
||||
[KernelFunction]
|
||||
public async Task<string> TaskInjectingKernelWithInputTextAndStringResult(Kernel kernel, string textToSummarize)
|
||||
{
|
||||
var summary = await kernel.InvokeAsync<string>(kernel.Plugins["SummarizePlugin"]["Summarize"], new() { ["input"] = textToSummarize });
|
||||
Console.WriteLine($"Running {nameof(this.TaskInjectingKernelWithInputTextAndStringResult)} -> Injected kernel + input: [textToSummarize: {textToSummarize[..15]}...{textToSummarize[^15..]}] -> result: {summary}");
|
||||
return summary!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example how to inject the executing KernelFunction as a parameter
|
||||
/// </summary>
|
||||
[KernelFunction, Description("Example function injecting itself as a parameter")]
|
||||
public async Task<string> TaskInjectingKernelFunctionWithStringResult(KernelFunction executingFunction)
|
||||
{
|
||||
var result = $"Name: {executingFunction.Name}, Description: {executingFunction.Description}";
|
||||
Console.WriteLine($"Running {nameof(this.TaskInjectingKernelWithInputTextAndStringResult)} -> Injected Function -> result: {result}");
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example how to inject ILogger in your function
|
||||
/// </summary>
|
||||
[KernelFunction]
|
||||
public Task TaskInjectingLoggerWithNoResult(ILogger logger)
|
||||
{
|
||||
logger.LogWarning("Running {FunctionName} -> Injected Logger", nameof(this.TaskInjectingLoggerWithNoResult));
|
||||
Console.WriteLine($"Running {nameof(this.TaskInjectingKernelWithInputTextAndStringResult)} -> Injected Logger");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example how to inject ILoggerFactory in your function
|
||||
/// </summary>
|
||||
[KernelFunction]
|
||||
public Task TaskInjectingLoggerFactoryWithNoResult(ILoggerFactory loggerFactory)
|
||||
{
|
||||
loggerFactory
|
||||
.CreateLogger<LocalExamplePlugin>()
|
||||
.LogWarning("Running {FunctionName} -> Injected Logger", nameof(this.TaskInjectingLoggerWithNoResult));
|
||||
|
||||
Console.WriteLine($"Running {nameof(this.TaskInjectingKernelWithInputTextAndStringResult)} -> Injected Logger");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example how to inject a service selector in your function and use a specific service
|
||||
/// </summary>
|
||||
[KernelFunction]
|
||||
public async Task<string> TaskInjectingServiceSelectorWithStringResult(Kernel kernel, KernelFunction function, KernelArguments arguments, IAIServiceSelector serviceSelector)
|
||||
{
|
||||
ChatMessageContent? chatMessageContent = null;
|
||||
if (serviceSelector.TrySelectAIService<IChatCompletionService>(kernel, function, arguments, out var chatCompletion, out var executionSettings))
|
||||
{
|
||||
chatMessageContent = await chatCompletion.GetChatMessageContentAsync(new ChatHistory("How much is 5 + 5 ?"), executionSettings);
|
||||
}
|
||||
|
||||
var result = chatMessageContent?.Content;
|
||||
Console.WriteLine($"Running {nameof(this.TaskInjectingKernelWithInputTextAndStringResult)} -> Injected Kernel, KernelFunction, KernelArguments, Service Selector -> result: {result}");
|
||||
return result ?? string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example how to inject CultureInfo or IFormatProvider in your function
|
||||
/// </summary>
|
||||
public async Task<string> TaskInjectingCultureInfoOrIFormatProviderWithStringResult(CultureInfo cultureInfo, IFormatProvider formatProvider)
|
||||
{
|
||||
var result = $"Culture Name: {cultureInfo.Name}, FormatProvider Equals CultureInfo?: {formatProvider.Equals(cultureInfo)}";
|
||||
Console.WriteLine($"Running {nameof(this.TaskInjectingCultureInfoOrIFormatProviderWithStringResult)} -> Injected CultureInfo, IFormatProvider -> result: {result}");
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Example how to inject current CancellationToken in your function
|
||||
/// </summary>
|
||||
[KernelFunction]
|
||||
public async Task<string> TaskInjectingCancellationTokenWithStringResult(CancellationToken cancellationToken)
|
||||
{
|
||||
var result = $"Cancellation resquested: {cancellationToken.IsCancellationRequested}";
|
||||
Console.WriteLine($"Running {nameof(this.TaskInjectingCultureInfoOrIFormatProviderWithStringResult)} -> Injected Cancellation Token -> result: {result}");
|
||||
return result;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return "Complex type result ToString override";
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"schema": 1,
|
||||
"description": "Given a scientific white paper abstract, rewrite it to make it more readable",
|
||||
"execution_settings": {
|
||||
"default": {
|
||||
"max_tokens": 4000,
|
||||
"temperature": 0.0,
|
||||
"top_p": 1.0,
|
||||
"presence_penalty": 0.0,
|
||||
"frequency_penalty": 2.0
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{{$input}}
|
||||
|
||||
==
|
||||
Summarize, using a user friendly, using simple grammar. Don't use subjects like "we" "our" "us" "your".
|
||||
==
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"schema": 1,
|
||||
"description": "Automatically generate compact notes for any text or text document.",
|
||||
"execution_settings": {
|
||||
"default": {
|
||||
"max_tokens": 256,
|
||||
"temperature": 0.0,
|
||||
"top_p": 0.0,
|
||||
"presence_penalty": 0.0,
|
||||
"frequency_penalty": 0.0
|
||||
}
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
Analyze the following extract taken from a document.
|
||||
- Produce key points for memory.
|
||||
- Give memory a name.
|
||||
- Extract only points worth remembering.
|
||||
- Be brief. Conciseness is very important.
|
||||
- Use broken English.
|
||||
You will use this memory to analyze the rest of this document, and for other relevant tasks.
|
||||
|
||||
[Input]
|
||||
My name is Macbeth. I used to be King of Scotland, but I died. My wife's name is Lady Macbeth and we were married for 15 years. We had no children. Our beloved dog Toby McDuff was a famous hunter of rats in the forest.
|
||||
My story was immortalized by Shakespeare in a play.
|
||||
+++++
|
||||
Family History
|
||||
- Macbeth, King Scotland
|
||||
- Wife Lady Macbeth, No Kids
|
||||
- Dog Toby McDuff. Hunter, dead.
|
||||
- Shakespeare play
|
||||
|
||||
[Input]
|
||||
[[{{$input}}]]
|
||||
+++++
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"schema": 1,
|
||||
"description": "Summarize given text or any text document",
|
||||
"execution_settings": {
|
||||
"default": {
|
||||
"max_tokens": 512,
|
||||
"temperature": 0.0,
|
||||
"top_p": 0.0,
|
||||
"presence_penalty": 0.0,
|
||||
"frequency_penalty": 0.0
|
||||
}
|
||||
},
|
||||
"input_variables": [
|
||||
{
|
||||
"name": "input",
|
||||
"description": "Text to summarize",
|
||||
"default": "",
|
||||
"is_required": true
|
||||
}
|
||||
]
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
[SUMMARIZATION RULES]
|
||||
DONT WASTE WORDS
|
||||
USE SHORT, CLEAR, COMPLETE SENTENCES.
|
||||
DO NOT USE BULLET POINTS OR DASHES.
|
||||
USE ACTIVE VOICE.
|
||||
MAXIMIZE DETAIL, MEANING
|
||||
FOCUS ON THE CONTENT
|
||||
|
||||
[BANNED PHRASES]
|
||||
This article
|
||||
This document
|
||||
This page
|
||||
This material
|
||||
[END LIST]
|
||||
|
||||
Summarize:
|
||||
Hello how are you?
|
||||
+++++
|
||||
Hello
|
||||
|
||||
Summarize this
|
||||
{{$input}}
|
||||
+++++
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"schema": 1,
|
||||
"description": "Analyze given text or document and extract key topics worth remembering",
|
||||
"execution_settings": {
|
||||
"default": {
|
||||
"max_tokens": 128,
|
||||
"temperature": 0.0,
|
||||
"top_p": 0.0,
|
||||
"presence_penalty": 0.0,
|
||||
"frequency_penalty": 0.0
|
||||
}
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
Analyze the following extract taken from a document and extract key topics.
|
||||
- Topics only worth remembering.
|
||||
- Be brief. Short phrases.
|
||||
- Can use broken English.
|
||||
- Conciseness is very important.
|
||||
- Topics can include names of memories you want to recall.
|
||||
- NO LONG SENTENCES. SHORT PHRASES.
|
||||
- Return in JSON
|
||||
[Input]
|
||||
My name is Macbeth. I used to be King of Scotland, but I died. My wife's name is Lady Macbeth and we were married for 15 years. We had no children. Our beloved dog Toby McDuff was a famous hunter of rats in the forest.
|
||||
My tragic story was immortalized by Shakespeare in a play.
|
||||
[Output]
|
||||
{
|
||||
"topics": [
|
||||
"Macbeth",
|
||||
"King of Scotland",
|
||||
"Lady Macbeth",
|
||||
"Dog",
|
||||
"Toby McDuff",
|
||||
"Shakespeare",
|
||||
"Play",
|
||||
"Tragedy"
|
||||
]
|
||||
}
|
||||
+++++
|
||||
[Input]
|
||||
{{$input}}
|
||||
[Output]
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"schema": 1,
|
||||
"description": "Sample plugin loaded via resources",
|
||||
"execution_settings": {
|
||||
"default": {
|
||||
"max_tokens": 150,
|
||||
"temperature": 0.9,
|
||||
"top_p": 0.0,
|
||||
"presence_penalty": 0.6,
|
||||
"frequency_penalty": 0.0
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
This is an example
|
||||
+8
-7
@@ -1,12 +1,13 @@
|
||||
{
|
||||
"schema": 1,
|
||||
"description": "Sample plugin loaded via resources",
|
||||
"type": "completion",
|
||||
"completion": {
|
||||
"max_tokens": 150,
|
||||
"temperature": 0.9,
|
||||
"top_p": 0.0,
|
||||
"presence_penalty": 0.6,
|
||||
"frequency_penalty": 0.0
|
||||
"execution_settings": {
|
||||
"default": {
|
||||
"max_tokens": 150,
|
||||
"temperature": 0.9,
|
||||
"top_p": 0.0,
|
||||
"presence_penalty": 0.6,
|
||||
"frequency_penalty": 0.0
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user