mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
Tidy up example
This commit is contained in:
+2
-112
@@ -1,12 +1,6 @@
|
||||
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.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;
|
||||
@@ -16,39 +10,10 @@ import reactor.core.publisher.Mono;
|
||||
|
||||
public class Example60_AdvancedMethodFunctions {
|
||||
|
||||
private static final String CLIENT_KEY = System.getenv("CLIENT_KEY");
|
||||
private static final String AZURE_CLIENT_KEY = System.getenv("AZURE_CLIENT_KEY");
|
||||
|
||||
// Only required if AZURE_CLIENT_KEY is set
|
||||
private static final String CLIENT_ENDPOINT = System.getenv("CLIENT_ENDPOINT");
|
||||
private static final String MODEL_ID = System.getenv()
|
||||
.getOrDefault("MODEL_ID", "gpt-35-turbo");
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("======== Example58_ConfigureExecutionSettings ========");
|
||||
System.out.println("======== Example60_AdvancedMethodFunctions ========");
|
||||
|
||||
OpenAIAsyncClient client;
|
||||
|
||||
if (AZURE_CLIENT_KEY != null) {
|
||||
client = new OpenAIClientBuilder()
|
||||
.credential(new AzureKeyCredential(AZURE_CLIENT_KEY))
|
||||
.endpoint(CLIENT_ENDPOINT)
|
||||
.buildAsyncClient();
|
||||
|
||||
} else {
|
||||
client = new OpenAIClientBuilder()
|
||||
.credential(new KeyCredential(CLIENT_KEY))
|
||||
.buildAsyncClient();
|
||||
}
|
||||
|
||||
ChatCompletionService openAIChatCompletion = OpenAIChatCompletion.builder()
|
||||
.withModelId(MODEL_ID)
|
||||
.withOpenAIAsyncClient(client)
|
||||
.build();
|
||||
|
||||
var kernel = Kernel.builder()
|
||||
.withAIService(ChatCompletionService.class, openAIChatCompletion)
|
||||
.build();
|
||||
var kernel = Kernel.builder().build();
|
||||
|
||||
System.out.println("Running Method Function Chaining example...");
|
||||
|
||||
@@ -138,79 +103,4 @@ public class Example60_AdvancedMethodFunctions {
|
||||
return new MyCustomType(1, "From Function2");
|
||||
}
|
||||
}
|
||||
/*
|
||||
|
||||
public static async Task RunAsync()
|
||||
{
|
||||
await MethodFunctionsChainingAsync();
|
||||
}
|
||||
|
||||
#region Method Functions Chaining
|
||||
|
||||
/// <summary>
|
||||
/// This example executes Function1, which in turn executes Function2.
|
||||
/// </summary>
|
||||
private static async Task MethodFunctionsChainingAsync()
|
||||
{
|
||||
Console.WriteLine("Running Method Function Chaining example...");
|
||||
|
||||
var kernel = new Kernel();
|
||||
|
||||
var functions = kernel.ImportPluginFromType<FunctionsChainingPlugin>();
|
||||
|
||||
var customType = await kernel.InvokeAsync<MyCustomType>(functions["Function1"]);
|
||||
|
||||
Console.WriteLine($"CustomType.Number: {customType!.Number}"); // 2
|
||||
Console.WriteLine($"CustomType.Text: {customType.Text}"); // From Function1 + From Function2
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Custom Type
|
||||
|
||||
/// <summary>
|
||||
/// In order to use custom types, <see cref="TypeConverter"/> should be specified,
|
||||
/// that will convert object instance to string representation.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <see cref="TypeConverter"/> is used to represent complex object as meaningful string, so
|
||||
/// it can be passed to AI for further processing using prompt functions.
|
||||
/// It's possible to choose any format (e.g. XML, JSON, YAML) to represent your object.
|
||||
/// </remarks>
|
||||
[TypeConverter(typeof(MyCustomTypeConverter))]
|
||||
private sealed class MyCustomType
|
||||
{
|
||||
public int Number { get; set; }
|
||||
|
||||
public string? Text { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implementation of <see cref="TypeConverter"/> for <see cref="MyCustomType"/>.
|
||||
/// In this example, object instance is serialized with <see cref="JsonSerializer"/> from System.Text.Json,
|
||||
/// but it's possible to convert object to string using any other serialization logic.
|
||||
/// </summary>
|
||||
private sealed class MyCustomTypeConverter : TypeConverter
|
||||
{
|
||||
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) => true;
|
||||
|
||||
/// <summary>
|
||||
/// This method is used to convert object from string to actual type. This will allow to pass object to
|
||||
/// method function which requires it.
|
||||
/// </summary>
|
||||
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
|
||||
{
|
||||
return JsonSerializer.Deserialize<MyCustomType>((string)value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method is used to convert actual type to string representation, so it can be passed to AI
|
||||
/// for further processing.
|
||||
/// </summary>
|
||||
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
|
||||
{
|
||||
return JsonSerializer.Serialize(value);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user