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

Add new tool choices to the ChatCompletionCreateRequest and ChatMessa… #432

Closed shanepowell closed 7 months ago

shanepowell commented 7 months ago

Add new tool choices to the ChatCompletionCreateRequest and ChatMessage response messages. Mark the old function api as Obsolete. Added playground tests to test the new function calling and also get the streaming function calling example working (in a messy way).

kayhantolga commented 7 months ago

Hi, I made some changes. I removed support for legacy functions as they can easily be converted to tools, which will make the SDK cleaner. I also added support for AsObject to the functionDefination in ToolDefinition, as some may need it. I have only one comment, please take a look. I think you may have forgotten to remove that class by mistake.If yes, please remove it. If no, could you please clarify what it is?

shanepowell commented 7 months ago

Your changes looks good to me.

What class are you referring to by 'may have forgotten to remove that class by mistake'?

Do you mean the class ToolChoiceFunction?

This is meant to be used with the ChatCompletionCreateRequest.ToolChoice field. That field can be a string, e.g.

request.ToolChoice = StaticValues.CompletionStatics.ToolChoice.Auto;

or

request.ToolChoice = StaticValues.CompletionStatics.ToolChoice.None;

or a ToolChoiceFunction instance. e.g.

request.ToolChoice = new ToolChoiceFunction() { Function = new FunctionTool() { Name = "get_current_weather" }};

That is why I setup the ToolChoice as a object.

You can see I tested it by commenting out the code bits in this block of the RunChatFunctionCallTest method:

            ConsoleExtensions.WriteLine("Chat Function Call Test:", ConsoleColor.DarkCyan);
            var completionResult = await sdk.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest
            {
                Messages = new List<ChatMessage>
                {
                    ChatMessage.FromSystem("Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous."),
                    ChatMessage.FromUser("Give me a weather report for Chicago, USA, for the next 5 days.")
                },
                Tools = new List<ToolDefinition> { new() { Function = fn1 }, new() { Function = fn2 }, new() { Function = fn3 }, new() { Function = fn4 }, new() { Function = fn4 } },
                // optionally, to force a specific function:
                // ToolChoice = new ToolChoiceFunction() { Function = new FunctionTool() { Name = "get_current_weather" }},
                // or auto tool choice:
                // ToolChoice = StaticValues.CompletionStatics.ToolChoice.Auto,
                MaxTokens = 50,
                Model = Models.Gpt_3_5_Turbo
            });
kayhantolga commented 7 months ago

Oh, I see. That makes sense. I have made some changes to the usage. I don't like the given object as a default field because it may leave developers in the dark about which objects are acceptable. I will be merging these changes. Please leave a comment if you disagree with any of my changes so that we can further improve this pull request and merge again.

(I removed default tool values, etc., because in the future, I think OpenAI will introduce different types, and using functions as defaults will lose its purpose and may create confusion for both new and existing developers.)