Closed qmatteoq closed 11 months ago
This appears to be an issue with how the Handlebars planner handles async methods. Given a plugin that looks like the following...
public class UnitedStatesPlugin
{
[KernelFunction, Description("Get the United States population for a specific year")]
public async Task<string> GetPopulation([Description("The year")] int year)
{
return "124,000";
}
[KernelFunction, Description("Get the United States population who identifies as male for a specific year")]
public async Task<string> GetMalePopulation([Description("The year")] int year)
{
return "61,000";
}
}
An error is thrown because somewhere the returned string
is trying to be converted into a Task<string>
This should be fixed with this PR: https://github.com/microsoft/semantic-kernel/pull/4017
Tagging an issue @lemillermicrosoft reported offline as another test case.
{{!-- Step 1: Set the search query --}}
{{set "query" "latest rivian news"}}
{{!-- Step 2: Use the bing-Search helper to perform the search --}}
{{set "searchResults" (bing-Search query=(get "query") count=5)}}
{{!-- Step 3: Output the search results --}}
{{json (get "searchResults")}}
Unhandled exception: System.Text.Json.JsonException: The JSON value could not be converted to System.Threading.Tasks.Task`1[System.String]. Path: $ | LineNumber: 0 | BytePositionInLine: 209.
at System.Text.Json.ThrowHelper.ThrowJsonException_DeserializeUnableToConvertValue(Type propertyType)
at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Deserialize(Utf8JsonReader& reader, ReadStack& state)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.DeserializeAsObject(Utf8JsonReader& reader, ReadStack& state)
at System.Text.Json.JsonSerializer.ReadFromSpanAsObject(ReadOnlySpan`1 utf8Json, JsonTypeInfo jsonTypeInfo, Nullable`1 actualByteCount)
at System.Text.Json.JsonSerializer.ReadFromSpanAsObject(ReadOnlySpan`1 json, JsonTypeInfo jsonTypeInfo)
at System.Text.Json.JsonSerializer.Deserialize(String json, Type returnType, JsonSerializerOptions options)
at Microsoft.SemanticKernel.Planning.Handlebars.HandlebarsTemplateEngineExtensions.InvokeSKFunction(Kernel kernel, KernelFunction function, KernelArguments state, CancellationToken cancellationToken)
Closing this as a dupe of https://github.com/microsoft/semantic-kernel/issues/4157: .Net: Handlebars fail to process correctly result of asynchronous functions (when return type is Task<...>)
Describe the bug I have a sample project which uses Semantic Kernel which includes 3 plugins:
I'm trying to use the HandlebarsPlanner to generate a plan that can satisfy the following ask:
Write a mail to share the population of the United States in 2015. Make sure to include the number of people who identify themselves as male.
This is my code:
By exploring the result, I can see that the generated plan looks correct. In the custom helpers section, in fact, I can see that it picked up my 3 functions: GetPopulation, GetMalePopulation and WriteBusinessMail.
However, when I try to execute the plan by calling
InvokeAsync()
, I'm getting the following exception:To Reproduce You can use the following sample code to reproduce the problem: https://github.com/qmatteoq/SemanticKernel-Demos/blob/rc/SemanticKernel.Planner/Program.cs
Expected behavior The plan is executed successfully.
Platform