betalgo / openai

OpenAI .NET sdk - Azure OpenAI, ChatGPT, Whisper, and DALL-E
https://betalgo.github.io/openai/
MIT License
2.85k stars 513 forks source link

FunctionCallingHelper.CallFunction does not working properly under specific circumstances #468

Closed dogdie233 closed 5 months ago

dogdie233 commented 6 months ago

Describe the bug When the Name of FunctionDescriptionAttribute is manually specified, FunctionCallingHelper.CallFunction is not working properly

Your code piece

public class Tools
{
    [FunctionDescription("A awesome method", Name = "awesome_method")]
    pubilc string AwesomeMethod() => "I will crash your program";
}

var tools = new Tools();
var res = await openAiService.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest()
{
    Messages = [ChatMessage.FromSystem("free to use any tools"), ChatMessage.FromUser("Call some awesome function and tell me the result")],
    Tools = FunctionCallingHelper.GetToolDefinitions(tools);
}, "gpt-3.5-turbo-1106");

if (resMsg.ToolCalls != null)
{
    foreach (var toolCall in resMsg.ToolCalls)
    {
        var toolResponse= FunctionCallingHelper.CallFunction<string>(toolCall.FunctionCall!, tools);
        /* It will throw an exception
         * OpenAI.Utilities.FunctionCalling.InvalidFunctionCallException: Method 'awesome_method' on type 'Test.Tools' not found
         */
    }
}

Result It will throw an exception __OpenAI.Utilities.FunctionCalling.InvalidFunctionCallException: Method 'awesome_method' on type 'Test.Tools' not found__ When the code running to CallFunction;

Expected behavior Call AwesomeMethod and get the result

Screenshots I think I explained it very clearly

Desktop (please complete the following information):