Skip to content

.Net: Function calling: Add function author role and name property - #3421

Merged
Mark Wallace (markwallace-microsoft) merged 13 commits into
microsoft:mainfrom
markwallace-microsoft:users/markwallace/authr-role-function
Nov 9, 2023
Merged

Mark Wallace (markwallace-microsoft) merged 13 commits into
microsoft:mainfrom
markwallace-microsoft:users/markwallace/authr-role-function

Conversation

@markwallace-microsoft

@markwallace-microsoft Mark Wallace (markwallace-microsoft) commented Nov 8, 2023 •

Copy link
Copy Markdown
Contributor

Motivation and Context

Replaces #3302

Resolves #3017 and enables the full function calling scenario as described in the OpenAI function calling documentation.

Description

Adds the "function" author role to allow adding function messages to chat history. Function messages require a "name" parameter, so this change also adds that to chat message model.

Contribution Checklist

@shawncal Shawn Callegari (shawncal) added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel kernel.core labels Nov 8, 2023
@markwallace-microsoft
Mark Wallace (markwallace-microsoft) marked this pull request as ready for review November 9, 2023 00:43

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CLG

Merged via the queue into microsoft:main with commit e83570d Nov 9, 2023
@markwallace-microsoft
Mark Wallace (markwallace-microsoft) deleted the users/markwallace/authr-role-function branch November 9, 2023 16:18
@leungkimming

Copy link
Copy Markdown

Mark Wallace (@markwallace-microsoft) I found a bug in beta6 that will cause OpenAI to return 500 Internal Server Error. To reproduce, change and execute Example59 as below:

        IKernel kernel = await InitializeKernelAsync();
        var chatCompletion = kernel.GetService<IChatCompletion>();
        var chatHistory = chatCompletion.CreateNewChat();

        OpenAIRequestSettings requestSettings = new()
        {
            Functions = kernel.Functions.GetFunctionViews().Select(f => f.ToOpenAIFunction()).ToList(),
        };
        requestSettings.FunctionCall = OpenAIRequestSettings.FunctionCallAuto;
        await CompleteChatWithFunctionsAsync("What day is today?", chatHistory, chatCompletion, kernel, requestSettings);
        await CompleteChatWithFunctionsAsync("What day is one month later?", chatHistory, chatCompletion, kernel, requestSettings);

I am using "gpt-3.5-turbo-0613" LLM model.

In fact, I have reported this issue in PR 3302 before. When LLM returns the function calling assistant message to us (record 2 below), the Name field is null and the FunctionCall field contains the function to be called. I am using native OpenAI API call here.
Screenshot 2023-11-10 211552
The name field is used only when returning the function result to LLM via the function message (record 3).
Hence, when we insert the function calling assistant message to chat history and pass it back to LLM, the Name field should also be null. Please consider my version of the foreach loop inside the CreateChatCompletionsOptions method in the ClientBase.cs file. This will fix the bug and LLM would not return 500 server error.

foreach (var message in chatHistory)
{
    var azureMessage = new ChatMessage(new ChatRole(message.Role.Label), message.Content);
    if (message.AdditionalProperties?.TryGetValue(NameProperty, out string? name) is true)
    {
        if (message.AdditionalProperties?.TryGetValue(ArgumentsProperty, out string? arguments) is true)
        {
            azureMessage.FunctionCall = new FunctionCall(name, arguments);
        }
        else
        {
            azureMessage.Name = name;
        }
    }
    options.Messages.Add(azureMessage);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.Net: Function calling: Update ChatMessage to support 'name'

6 participants