Skip to content

Commit

Permalink
Merge pull request betalgo#461 from betalgo/dev
Browse files Browse the repository at this point in the history
Utilities 7.0.4
  • Loading branch information
kayhantolga authored Dec 18, 2023
2 parents 649f31f + a13ac9d commit 9d34b6c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
26 changes: 13 additions & 13 deletions OpenAI.Utilities.Tests/FunctionCallingHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,24 @@ public void VerifyTypeOverride()
[Fact]
public void VerifyGetFunctionDefinitions()
{
var functionDefinitions = FunctionCallingHelper.GetFunctionDefinitions<FunctionCallingTestClass>();
var functionDefinitions = FunctionCallingHelper.GetToolDefinitions<FunctionCallingTestClass>();

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]
Expand Down
21 changes: 15 additions & 6 deletions OpenAI.Utilities/FunctionCalling/FunctionCallingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
}
/// <summary>
/// Enumerates the methods in the provided object, and a returns a <see cref="List{FunctionDefinition}" /> of
/// <see cref="FunctionDefinition" /> for all methods
/// marked with a <see cref="FunctionDescriptionAttribute" />
/// </summary>
/// <param name="obj">the object to analyze</param>
public static List<FunctionDefinition> GetFunctionDefinitions(object obj)
public static List<ToolDefinition> GetToolDefinitions(object obj)
{
var type = obj.GetType();
return GetFunctionDefinitions(type);
return GetToolDefinitions(type);
}

/// <summary>
Expand All @@ -96,17 +104,17 @@ public static List<FunctionDefinition> GetFunctionDefinitions(object obj)
/// </summary>
/// <typeparam name="T">The type to analyze</typeparam>
/// <returns></returns>
public static List<FunctionDefinition> GetFunctionDefinitions<T>()
public static List<ToolDefinition> GetToolDefinitions<T>()
{
return GetFunctionDefinitions(typeof(T));
return GetToolDefinitions(typeof(T));
}

/// <summary>
/// Enumerates the methods in the provided type, and a returns a <see cref="List{FunctionDefinition}" /> of
/// <see cref="FunctionDefinition" /> for all methods
/// </summary>
/// <param name="type">The type to analyze</param>
public static List<FunctionDefinition> GetFunctionDefinitions(Type type)
public static List<ToolDefinition> GetToolDefinitions(Type type)
{
var methods = type.GetMethods();

Expand All @@ -117,10 +125,11 @@ public static List<FunctionDefinition> GetFunctionDefinitions(Type type)
methodDescriptionAttribute = method.GetCustomAttribute<FunctionDescriptionAttribute>()
})
.Where(t => t.methodDescriptionAttribute != null)
.Select(t => GetFunctionDefinition(t.method)).ToList();
.Select(t => GetToolDefinition(t.method)).ToList();

return result;
}


/// <summary>
/// Calls the function on the provided object, using the provided <see cref="FunctionCall" /> and returns the result of
Expand Down
6 changes: 3 additions & 3 deletions OpenAI.Utilities/OpenAI.Utilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageProjectUrl>https://openai.com/</PackageProjectUrl>
<PackageIcon>OpenAI-Betalgo.png</PackageIcon>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>7.0.3</Version>
<Version>7.0.4</Version>
<Authors>Tolga Kayhan, Betalgo</Authors>
<Company>Betalgo Up Ltd.</Company>
<Product>Utility tools for Betalgo.OpenAI</Product>
Expand Down Expand Up @@ -41,9 +41,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Betalgo.OpenAI" Version="7.2.0" />
<PackageReference Include="Betalgo.OpenAI" Version="7.4.3" />
<PackageReference Include="CsvHelper" Version="30.0.1" />
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
<PackageReference Include="Microsoft.Data.Analysis" Version="0.20.1" />
<PackageReference Include="Microsoft.Data.Analysis" Version="0.21.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static async Task ExerciseFunctionCalling(IOpenAIService openAIService)
{
//Functions = FunctionCallingHelper.GetFunctionDefinitions(calculator),
//Functions = FunctionCallingHelper.GetFunctionDefinitions(typeof(Calculator)),
Functions = FunctionCallingHelper.GetFunctionDefinitions<Calculator>(),
Tools = FunctionCallingHelper.GetToolDefinitions<Calculator>(),
Messages = new List<ChatMessage>
{
ChatMessage.FromSystem("You are a helpful assistant."),
Expand Down

0 comments on commit 9d34b6c

Please sign in to comment.