microsoft / semantic-kernel

Integrate cutting-edge LLM technology quickly and easily into your apps
https://aka.ms/semantic-kernel
MIT License
21.36k stars 3.14k forks source link

.Net: Accessing SKChatMessage.FunctionCall throws exception when the OpenAI model does not return a function call #3492

Closed alexminza closed 10 months ago

alexminza commented 10 months ago

Describe the bug When streaming the chat response and checking if the OpenAI model returned a function call accessing SKChatMessage.FunctionCall throws NotSupportedException.

Exception has occurred: CLR/System.NotSupportedException
Exception thrown: 'System.NotSupportedException' in System.Private.CoreLib.dll: 'Function call is not supported'
   at Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk.SKChatMessage.get_FunctionCall()
   at Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk.ChatStreamingResultExtensions.<GetOpenAIStreamingFunctionResponseAsync>d__0.MoveNext()
   at Microsoft.SemanticKernel.Connectors.AI.OpenAI.AzureSdk.ChatStreamingResultExtensions.<GetOpenAIStreamingFunctionResponseAsync>d__0.MoveNext()
   ...

Reference: https://github.com/microsoft/semantic-kernel/pull/3246

To Reproduce Code similar to Example59_OpenAIFunctionCalling.cs

var chatCompletionResults = this.Chat.GetStreamingChatCompletionsAsync(
    chat: chatHistory,
    requestSettings: requestSettings,
    cancellationToken: cancellationToken);

await foreach (var chatCompletionResult in chatCompletionResults)
{
    await foreach (var chatMessageStream in chatCompletionResult.GetStreamingChatMessageAsync(cancellationToken))
    {
        if (!string.IsNullOrEmpty(chatMessageStream.Content))
            yield return chatMessageStream.Content;
    }

    var functionResponse = await chatCompletionResult.GetOpenAIStreamingFunctionResponseAsync();
    yield break;
}

Expected behavior SKChatMessage.FunctionCall should return null instead.

Platform

RogerBarreto commented 10 months ago

The current PR with update to Azure Beta 9, resolves that problem.

SKChatMessage Nullable FunctionCall property