semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

.Net: KernelFunctionHelpers.ParseResult report exception when the result is NaN or Infinity

Open hanhsia opened this issue 2 years ago • 0 comments

Describe the bug KernelFunctionHelpers.ParseResult report exception when the result is NaN or Infinity An unhandled exception has occurred while executing the request. System.ArgumentException: .NET number values such as positive and negative infinity cannot be written as valid JSON. To make it work when using 'JsonSerializer', consider specifying 'JsonNumberHandling.AllowNamedFloatingPointLiterals' (see https://docs.microsoft.com/dotnet/api/system.text.json.serialization.jsonnumberhandling). at System.Text.Json.ThrowHelper.ThrowArgumentException_ValueNotSupported() at System.Text.Json.Utf8JsonWriter.WriteNumberValue(Double value) at System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.Metadata.JsonTypeInfo1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed) at System.Text.Json.Serialization.Metadata.JsonTypeInfo1.SerializeAsObject(Utf8JsonWriter writer, Object rootValue) at System.Text.Json.Serialization.Metadata.JsonTypeInfo1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed) at System.Text.Json.JsonSerializer.WriteString[TValue](TValue& value, JsonTypeInfo1 jsonTypeInfo) at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options) at Microsoft.SemanticKernel.PromptTemplates.Handlebars.Helpers.KernelFunctionHelpers.ParseResult(FunctionResult result) at Microsoft.SemanticKernel.PromptTemplates.Handlebars.Helpers.KernelFunctionHelpers.InvokeKernelFunction(Kernel kernel, KernelFunction function, KernelArguments executionContext, CancellationToken cancellationToken) at Microsoft.SemanticKernel.PromptTemplates.Handlebars.Helpers.KernelFunctionHelpers.<>c__DisplayClass1_0.b__0(Context context, Arguments handlebarsArguments) at HandlebarsDotNet.Helpers.DelegateReturnHelperDescriptor.Invoke(HelperOptions& options, Context& context, Arguments& arguments) at HandlebarsDotNet.Helpers.DelegateReturnHelperDescriptor.HandlebarsDotNet.Helpers.IHelperDescriptor<HandlebarsDotNet.HelperOptions>.Invoke(HelperOptions& options, Context& context, Arguments& arguments) at lambda_method348(Closure, EncodedTextWriter&, BindingContext) at HandlebarsDotNet.HandlebarsEnvironment.<>c__DisplayClass19_0.b__0(TextWriter writer, Object context, Object data) at HandlebarsDotNet.HandlebarsEnvironment.<>c__DisplayClass20_0.b__0(Object context, Object data) at Microsoft.SemanticKernel.PromptTemplates.Handlebars.HandlebarsPromptTemplate.RenderAsync(Kernel kernel, KernelArguments arguments, CancellationToken cancellationToken) at Microsoft.SemanticKernel.Planning.Handlebars.HandlebarsPlan.InvokeCoreAsync(Kernel kernel, KernelArguments arguments, CancellationToken cancellationToken) at Microsoft.SemanticKernel.Planning.PlannerInstrumentation.InvokePlanAsync[TPlan,TPlanInput,TPlanResult](Func`5 InvokePlanAsync, TPlan plan, Kernel kernel, TPlanInput input, ILogger logger, CancellationToken cancellationToken) at Backend.Plugins.MathSolverPlugin.SolveAsync(Kernel kernel, String problem) in C:\Users\hanxia\Repos\Hsialab\Next\Backend\Plugins\MathSolverPlugin\MathSolverPlugin.cs:line 43 at Microsoft.SemanticKernel.KernelFunctionFromMethod.<>c.<b__12_4>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.SemanticKernel.KernelFunction.InvokeAsync(Kernel kernel, KernelArguments arguments, CancellationToken cancellationToken)

To Reproduce Steps to reproduce the behavior: kernelWithMath.Plugins.AddFromType(); var planner = new HandlebarsPlanner(new HandlebarsPlannerOptions() { AllowLoops = true }); // Create a plan var plan = await planner.CreatePlanAsync(kernelWithMath, problem); // Execute the plan var result = (await plan.InvokeAsync(kernelWithMath)).Trim(); Input a quadratic equation with one unknown to resolve. And the equation has no solution.

Expected behavior add allowNamedFloatingPointLiterals options when serializing the return value. Or provide option to set the json serialize/deserialize settings. In my case. output that the equation has no solution without any exception.

Platform

  • OS: [e.g. Windows, Mac] Windows
  • IDE: [e.g. Visual Studio, VS Code] Visual Sutudio
  • Language: [e.g. C#, Python] C#
  • Source: [e.g. NuGet package version 0.1.0, pip package version 0.1.0, main branch of repository] SK 1.3

Additional context Use MathPlugin in DocumentationExamples. But add a function Negate otherwise the equation can not be solved.

hanhsia avatar Feb 01 '24 09:02 hanhsia