.Net: kernel.InvokeAsync<customclass> sometimes fails with the exception it can't case a string to whatever custom model you are trying to return from a plugin function.
` OpenAIPromptExecutionSettings openAIPromptExecutionSettingsManual = new() { ToolCallBehavior = ToolCallBehavior.EnableKernelFunctions, FrequencyPenalty = 0, PresencePenalty = 0, TopP = .95, Temperature = .10, MaxTokens = 1000,
};
OpenAIPromptExecutionSettings openAIPromptExecutionSettingsAuto = new()
{
ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions,
FrequencyPenalty = 0,
PresencePenalty = 0,
TopP = .95,
Temperature = .10,
MaxTokens = 1000,
};
var result1 = await chatCompletion35Turbo.GetChatMessageContentAsync(
questionHistory,
executionSettings: openAIPromptExecutionSettingsManual,
kernel: kernel);
// Check to see if result1 type is OpenAIChatMessageContent
var openAIContent = result1 as OpenAIChatMessageContent;
// If a valid function was found, execute it
/*
var functionCall = openAIContent.ToolCalls.OfType<ChatCompletionsFunctionToolCall>().First();
if (functionCall != null)
{
kernel.Plugins.TryGetFunctionAndArguments(functionCall, out KernelFunction? pluginFunction, out KernelArguments? arguments);
var answer = await pluginFunction.InvokeAsync<KernelReturnModel>(kernel, arguments);
}
*/
List<SupportingContentRecord> supportingContentRecords = new List<SupportingContentRecord>();
foreach (var toolCall in openAIContent.ToolCalls.OfType<ChatCompletionsFunctionToolCall>())
{
if (toolCall is ChatCompletionsFunctionToolCall functionCall)
{
kernel.Plugins.TryGetFunctionAndArguments(functionCall, out KernelFunction? pluginFunction, out KernelArguments? arguments);
if (pluginFunction != null)
{
var answer = await pluginFunction.InvokeAsync<KernelReturnModel>(kernel, arguments);
// Concatenate content and AI summarized question
//contentList.Add(answer.Content);
// aiSummarizedQuestionList.Add(answer.AISummarizedQuestion);
history.AddUserMessage(answer.AISummarizedQuestion);
// Add content returned from plugin ai search query.
history.AddUserMessage($@"##Source ##
{answer.Content}
End ##");
if(answer.MetaData is SupportingContentRecord)
{
supportingContentRecords.Add((SupportingContentRecord) answer.MetaData);
}
}
}
}
' We are creating plugins and inside the plugins we are making calls using the question passed in to AI search to get related content.
This includes content, as well as meta data such as URL's we need to pass back to clients to use for citations.
So we're using the code above, however,
var answer = await pluginFunction.InvokeAsync
Visual Studio .Net 8 core C#.
In another simpler example where I'm using the SK source code, I'm guessing maybe it's somewhere in the KernelFunction InvokeAsync method and possibly one of the conditions checking for invokingEventArgs such as this.
var invokedContext = kernel.OnFunctionInvokedFilter(arguments, functionResult);
if (invokedEventArgs is not null)
{
// Apply any changes from the event handlers to final result.
functionResult = new FunctionResult(this, invokedEventArgs.ResultValue, functionResult.Culture, invokedEventArgs.Metadata ?? functionResult.Metadata);
}
if (invokedContext is not null)
{
// Apply any changes from the function filters to final result.
functionResult = new FunctionResult(this, invokedContext.ResultValue, functionResult.Culture, invokedContext.Metadata ?? functionResult.Metadata);
}
But I'm not sure as the project I have source code in, it doesn't happen. Just the one where I'm using the latest 1.4 nuget package.
I'm internal to MS if you need to see the code.
On further investigation after filing this, I verified the actual plugin to make sure the one that should be running was and looks like it wasn't so it's an issue we're going to have to address with the plugin function description. Looks like a plugin I wasn't planning on using that was left in the plugins still did have a string as the return type and it was loading instead of what we were expected. Closing for now, will reopen if we see it happen again outside this scenario.