-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- More Function utilities and invoking methods - Added FunctionPropertyAttribute to help better inform the feature how to format the Function json - Added FromFunc<,> overloads for convenance - Fixed invoke args sometimes being casting to wrong type - Added additional protections for static and instanced function calls - Added additional tool utilities: - Tool.ClearRegisteredTools - Tool.IsToolRegistered(Tool) - Tool.TryRegisterTool(Tool) - Improved memory usage and performance by propertly disposing http content and response objects - Updated debug output to be formatted to json for easier reading and debugging
- Loading branch information
1 parent
192c765
commit d3e59d1
Showing
38 changed files
with
1,170 additions
and
479 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
using OpenAI.Images; | ||
using OpenAI.Tests.Weather; | ||
|
||
namespace OpenAI.Tests | ||
{ | ||
internal class TestFixture_00_02_Tools : AbstractTestFixture | ||
{ | ||
[Test] | ||
public void Test_01_GetTools() | ||
{ | ||
var tools = Tool.GetAllAvailableTools(forceUpdate: true, clearCache: true).ToList(); | ||
Assert.IsNotNull(tools); | ||
Assert.IsNotEmpty(tools); | ||
tools.Add(Tool.GetOrCreateTool(OpenAIClient.ImagesEndPoint, nameof(ImagesEndpoint.GenerateImageAsync))); | ||
var json = JsonSerializer.Serialize(tools, new JsonSerializerOptions(OpenAIClient.JsonSerializationOptions) | ||
{ | ||
WriteIndented = true | ||
}); | ||
Console.WriteLine(json); | ||
} | ||
|
||
[Test] | ||
public async Task Test_02_Tool_Funcs() | ||
{ | ||
var tools = new List<Tool> | ||
{ | ||
Tool.FromFunc("test_func", Function), | ||
Tool.FromFunc<string, string, string>("test_func_with_args", FunctionWithArgs), | ||
Tool.FromFunc("test_func_weather", () => WeatherService.GetCurrentWeatherAsync("my location", WeatherService.WeatherUnit.Celsius)) | ||
}; | ||
|
||
var json = JsonSerializer.Serialize(tools, new JsonSerializerOptions(OpenAIClient.JsonSerializationOptions) | ||
{ | ||
WriteIndented = true | ||
}); | ||
Console.WriteLine(json); | ||
Assert.IsNotNull(tools); | ||
var tool = tools[0]; | ||
Assert.IsNotNull(tool); | ||
var result = tool.InvokeFunction<string>(); | ||
Assert.AreEqual("success", result); | ||
var toolWithArgs = tools[1]; | ||
Assert.IsNotNull(toolWithArgs); | ||
toolWithArgs.Function.Arguments = new JsonObject | ||
{ | ||
["arg1"] = "arg1", | ||
["arg2"] = "arg2" | ||
}; | ||
var resultWithArgs = toolWithArgs.InvokeFunction<string>(); | ||
Assert.AreEqual("arg1 arg2", resultWithArgs); | ||
|
||
var toolWeather = tools[2]; | ||
Assert.IsNotNull(toolWeather); | ||
var resultWeather = await toolWeather.InvokeFunctionAsync(); | ||
Assert.IsFalse(string.IsNullOrWhiteSpace(resultWeather)); | ||
Console.WriteLine(resultWeather); | ||
} | ||
|
||
private string Function() | ||
{ | ||
return "success"; | ||
} | ||
|
||
private string FunctionWithArgs(string arg1, string arg2) | ||
{ | ||
return $"{arg1} {arg2}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.