From d945e1901b2d66307c5f5e5c5e65be321ab41b5a Mon Sep 17 00:00:00 2001 From: Tolga Kayhan Date: Mon, 18 Dec 2023 14:02:18 +0000 Subject: [PATCH] Updated function defination as tool defination --- .../FunctionCallingHelperTests.cs | 26 +++++++++---------- .../FunctionCalling/FunctionCallingHelper.cs | 21 ++++++++++----- OpenAI.Utilities/OpenAI.Utilities.csproj | 6 ++--- .../TestHelpers/FunctionCallingTestHelpers.cs | 2 +- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/OpenAI.Utilities.Tests/FunctionCallingHelperTests.cs b/OpenAI.Utilities.Tests/FunctionCallingHelperTests.cs index 2a85814e..aa50259e 100644 --- a/OpenAI.Utilities.Tests/FunctionCallingHelperTests.cs +++ b/OpenAI.Utilities.Tests/FunctionCallingHelperTests.cs @@ -61,24 +61,24 @@ public void VerifyTypeOverride() [Fact] public void VerifyGetFunctionDefinitions() { - var functionDefinitions = FunctionCallingHelper.GetFunctionDefinitions(); + var functionDefinitions = FunctionCallingHelper.GetToolDefinitions(); functionDefinitions.Count.ShouldBe(3); - var functionDefinition = functionDefinitions.First(x => x.Name == "TestFunction"); - functionDefinition.Description.ShouldBe("Test Function"); - functionDefinition.Parameters.ShouldNotBeNull(); - functionDefinition.Parameters.Properties!.Count.ShouldBe(9); + var functionDefinition = functionDefinitions.First(x => x.Function!.Name == "TestFunction"); + functionDefinition.Function!.Description.ShouldBe("Test Function"); + functionDefinition.Function!.Parameters.ShouldNotBeNull(); + functionDefinition.Function!.Parameters.Properties!.Count.ShouldBe(9); - var functionDefinition2 = functionDefinitions.First(x => x.Name == "SecondFunction"); - functionDefinition2.Description.ShouldBe("Second Function"); - functionDefinition2.Parameters.ShouldNotBeNull(); - functionDefinition2.Parameters.Properties!.Count.ShouldBe(0); + var functionDefinition2 = functionDefinitions.First(x => x.Function!.Name == "SecondFunction"); + functionDefinition2.Function!.Description.ShouldBe("Second Function"); + functionDefinition2.Function!.Parameters.ShouldNotBeNull(); + functionDefinition2.Function!.Parameters.Properties!.Count.ShouldBe(0); - var functionDefinition3 = functionDefinitions.First(x => x.Name == "ThirdFunction"); - functionDefinition3.Description.ShouldBe("Third Function"); - functionDefinition3.Parameters.ShouldNotBeNull(); - functionDefinition3.Parameters.Properties!.Count.ShouldBe(1); + var functionDefinition3 = functionDefinitions.First(x => x.Function!.Name == "ThirdFunction"); + functionDefinition3.Function!.Description.ShouldBe("Third Function"); + functionDefinition3.Function!.Parameters.ShouldNotBeNull(); + functionDefinition3.Function!.Parameters.Properties!.Count.ShouldBe(1); } [Fact] diff --git a/OpenAI.Utilities/FunctionCalling/FunctionCallingHelper.cs b/OpenAI.Utilities/FunctionCalling/FunctionCallingHelper.cs index 019cb228..012ae1dc 100644 --- a/OpenAI.Utilities/FunctionCalling/FunctionCallingHelper.cs +++ b/OpenAI.Utilities/FunctionCalling/FunctionCallingHelper.cs @@ -78,16 +78,24 @@ public static FunctionDefinition GetFunctionDefinition(MethodInfo methodInfo) return result.Build(); } + public static ToolDefinition GetToolDefinition(MethodInfo methodInfo) + { + return new ToolDefinition() + { + Type = "function", + Function = GetFunctionDefinition(methodInfo) + }; + } /// /// Enumerates the methods in the provided object, and a returns a of /// for all methods /// marked with a /// /// the object to analyze - public static List GetFunctionDefinitions(object obj) + public static List GetToolDefinitions(object obj) { var type = obj.GetType(); - return GetFunctionDefinitions(type); + return GetToolDefinitions(type); } /// @@ -96,9 +104,9 @@ public static List GetFunctionDefinitions(object obj) /// /// The type to analyze /// - public static List GetFunctionDefinitions() + public static List GetToolDefinitions() { - return GetFunctionDefinitions(typeof(T)); + return GetToolDefinitions(typeof(T)); } /// @@ -106,7 +114,7 @@ public static List GetFunctionDefinitions() /// for all methods /// /// The type to analyze - public static List GetFunctionDefinitions(Type type) + public static List GetToolDefinitions(Type type) { var methods = type.GetMethods(); @@ -117,10 +125,11 @@ public static List GetFunctionDefinitions(Type type) methodDescriptionAttribute = method.GetCustomAttribute() }) .Where(t => t.methodDescriptionAttribute != null) - .Select(t => GetFunctionDefinition(t.method)).ToList(); + .Select(t => GetToolDefinition(t.method)).ToList(); return result; } + /// /// Calls the function on the provided object, using the provided and returns the result of diff --git a/OpenAI.Utilities/OpenAI.Utilities.csproj b/OpenAI.Utilities/OpenAI.Utilities.csproj index 6d2648d7..6bf0f538 100644 --- a/OpenAI.Utilities/OpenAI.Utilities.csproj +++ b/OpenAI.Utilities/OpenAI.Utilities.csproj @@ -10,7 +10,7 @@ https://openai.com/ OpenAI-Betalgo.png true - 7.0.3 + 7.0.4 Tolga Kayhan, Betalgo Betalgo Up Ltd. Utility tools for Betalgo.OpenAI @@ -41,9 +41,9 @@ - + - + \ No newline at end of file diff --git a/OpenAI.UtilitiesPlayground/TestHelpers/FunctionCallingTestHelpers.cs b/OpenAI.UtilitiesPlayground/TestHelpers/FunctionCallingTestHelpers.cs index aed7c958..4ad10d90 100644 --- a/OpenAI.UtilitiesPlayground/TestHelpers/FunctionCallingTestHelpers.cs +++ b/OpenAI.UtilitiesPlayground/TestHelpers/FunctionCallingTestHelpers.cs @@ -15,7 +15,7 @@ public static async Task ExerciseFunctionCalling(IOpenAIService openAIService) { //Functions = FunctionCallingHelper.GetFunctionDefinitions(calculator), //Functions = FunctionCallingHelper.GetFunctionDefinitions(typeof(Calculator)), - Functions = FunctionCallingHelper.GetFunctionDefinitions(), + Tools = FunctionCallingHelper.GetToolDefinitions(), Messages = new List { ChatMessage.FromSystem("You are a helpful assistant."),