Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added helper APIs for chat function/tool calls #42

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

KrzysztofCwalina
Copy link
Collaborator

@KrzysztofCwalina KrzysztofCwalina commented Jun 11, 2024

  1. Added ChatTool[] ChatTool.CreateFunctionTools(Type functionHolder) method. The method reflects on the functionHolder and builds an array of ChatTool instances corresponding to the reflected on methods.
  2. Added ChatToolCall.GetFunctionArgument, which helps with parsing function call arguments.

Users can now do:

ChatCompletionOptions options = new() {
    Tools = ChatTool.CreateFunctionTools(typeof(MyFunctions))
};

internal static class MyFunctions {
    [Description("Get the user's current location")]
    public static string GetCurrentLocation() => "San Francisco";

    [Description("Get the current weather in a given location")]
    public static string GetCurrentWeather([Description("The city and state, e.g. Boston, MA")] string location, TemperatureUnit unit) => "31 {unit}";
}

...
case nameof(MyFunctions.GetCurrentWeather): 
        var location = toolCall.GetFunctionArgument<string>("location");
        var unit = toolCall.GetFunctionArgument("unit", defaultValue: MyFunctions.TemperatureUnit.Celsius);
        string toolResult = MyFunctions.GetCurrentWeather(location, unit);
        messages.Add(new ToolChatMessage(toolCall.Id, toolResult));
        break;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant