Skip to content

Commit

Permalink
OpenAI-DotNet 8.0.0 (#309)
Browse files Browse the repository at this point in the history
- Updated Assistants Beta v2
- Added support for specifying project id
- Added BatchEndpoint
- Added VectorStoresEndpoint
- Added Message.ctr to specify specific tool call id, function name, and content
- Renamed OpenAI.Images.ResponseFormat to OpenAI.Images.ImageResponseFormat
- Changed ThreadEndpoint.CancelRunAsync return type from RunResponse to bool
- Fixed Json defined Tools/Functions being improperly added to tool cache
- Added Tool.TryUnregisterTool to remove a tool from the cache
  • Loading branch information
StephenHodgson authored Jun 10, 2024
1 parent 03bdded commit 9675957
Show file tree
Hide file tree
Showing 172 changed files with 6,228 additions and 1,787 deletions.
61 changes: 61 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/code-style-rule-options

# Remove the line below if you want to inherit .editorconfig settings from higher directories
root = true

[*.cs]

#### Core EditorConfig Options ####

# Indentation and spacing
indent_size = 4
indent_style = space
tab_width = 4

# New line preferences
end_of_line = crlf
insert_final_newline = true
trim_trailing_whitespace = true
csharp_new_line_before_catch = true
csharp_new_line_before_else = true
csharp_new_line_before_finally = true
csharp_new_line_before_open_brace = all

# Modifier preferences
dotnet_style_require_accessibility_modifiers = for_non_interface_members:error

# Code-block preferences
csharp_prefer_braces = true:error

# Use language keywords for types
dotnet_style_predefined_type_for_member_access = true
dotnet_style_predefined_type_for_locals_parameters_members = true

# Code Style
csharp_style_var_when_type_is_apparent = true
csharp_place_field_attribute_on_same_line=false
csharp_place_accessorholder_attribute_on_same_line=false
csharp_trailing_comma_in_multiline_lists=false
csharp_trailing_comma_in_singleline_lists=false
csharp_keep_existing_attribute_arrangement=false
csharp_blank_lines_around_region=1
csharp_blank_lines_inside_region=1
csharp_keep_blank_lines_in_code=false
csharp_remove_blank_lines_near_braces_in_code=true
csharp_blank_lines_before_control_transfer_statements=1
csharp_blank_lines_after_control_transfer_statements=1
csharp_blank_lines_before_block_statements=1
csharp_blank_lines_after_block_statements=1
csharp_blank_lines_before_multiline_statements=1
csharp_blank_lines_after_multiline_statements=1
csharp_blank_lines_around_block_case_section=0
csharp_blank_lines_around_multiline_case_section=0
csharp_blank_lines_before_case=0
csharp_blank_lines_after_case=0

#### Resharper/Rider Rules ####
# https://www.jetbrains.com/help/resharper/EditorConfig_Properties.html

resharper_csharp_force_attribute_style=separate
resharper_use_name_of_instead_of_type_of_highlighting=error
resharper_wrong_public_modifier_specification_highlighting=error
2 changes: 1 addition & 1 deletion OpenAI-DotNet-Proxy/Proxy/AbstractAuthenticationFilter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;

namespace OpenAI.Proxy
{
Expand Down
2 changes: 1 addition & 1 deletion OpenAI-DotNet-Tests-Proxy/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ public static void Main(string[] args)
OpenAIProxyStartup.CreateWebApplication<AuthenticationFilter>(args, openAIClient).Run();
}
}
}
}
2 changes: 1 addition & 1 deletion OpenAI-DotNet-Tests/AbstractTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ protected AbstractTestFixture()
};
}
}
}
}
22 changes: 13 additions & 9 deletions OpenAI-DotNet-Tests/TestFixture_00_01_Authentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ internal class TestFixture_00_01_Authentication
[SetUp]
public void Setup()
{
var authJson = new AuthInfo("sk-test12", "org-testOrg");
var authJson = new AuthInfo("sk-test12", "org-testOrg", "proj_testProject");
var authText = JsonSerializer.Serialize(authJson);
File.WriteAllText(".openai", authText);
Assert.IsTrue(File.Exists(".openai"));
File.WriteAllText(OpenAIAuthentication.CONFIG_FILE, authText);
Assert.IsTrue(File.Exists(OpenAIAuthentication.CONFIG_FILE));
}

[Test]
Expand All @@ -38,12 +38,14 @@ public void Test_01_GetAuthFromEnv()
[Test]
public void Test_02_GetAuthFromFile()
{
var auth = OpenAIAuthentication.LoadFromPath(Path.GetFullPath(".openai"));
var auth = OpenAIAuthentication.LoadFromPath(Path.GetFullPath(OpenAIAuthentication.CONFIG_FILE));
Assert.IsNotNull(auth);
Assert.IsNotNull(auth.ApiKey);
Assert.AreEqual("sk-test12", auth.ApiKey);
Assert.IsNotNull(auth.OrganizationId);
Assert.AreEqual("org-testOrg", auth.OrganizationId);
Assert.IsNotNull(auth.ProjectId);
Assert.AreEqual("proj_testProject", auth.ProjectId);
}

[Test]
Expand All @@ -68,23 +70,25 @@ public void Test_04_GetDefault()
public void Test_05_Authentication()
{
var defaultAuth = OpenAIAuthentication.Default;
var manualAuth = new OpenAIAuthentication("sk-testAA", "org-testAA");
var manualAuth = new OpenAIAuthentication("sk-testAA", "org-testAA", "proj_testProject");
var api = new OpenAIClient();
var shouldBeDefaultAuth = api.OpenAIAuthentication;
Assert.IsNotNull(shouldBeDefaultAuth);
Assert.IsNotNull(shouldBeDefaultAuth.ApiKey);
Assert.IsNotNull(shouldBeDefaultAuth.OrganizationId);
Assert.AreEqual(defaultAuth.ApiKey, shouldBeDefaultAuth.ApiKey);
Assert.AreEqual(defaultAuth.OrganizationId, shouldBeDefaultAuth.OrganizationId);
Assert.AreEqual(defaultAuth.ProjectId, shouldBeDefaultAuth.ProjectId);

OpenAIAuthentication.Default = new OpenAIAuthentication("sk-testAA", "org-testAA");
OpenAIAuthentication.Default = new OpenAIAuthentication("sk-testAA", "org-testAA", "proj_testProject");
api = new OpenAIClient();
var shouldBeManualAuth = api.OpenAIAuthentication;
Assert.IsNotNull(shouldBeManualAuth);
Assert.IsNotNull(shouldBeManualAuth.ApiKey);
Assert.IsNotNull(shouldBeManualAuth.OrganizationId);
Assert.AreEqual(manualAuth.ApiKey, shouldBeManualAuth.ApiKey);
Assert.AreEqual(manualAuth.OrganizationId, shouldBeManualAuth.OrganizationId);
Assert.AreEqual(manualAuth.ProjectId, shouldBeDefaultAuth.ProjectId);

OpenAIAuthentication.Default = defaultAuth;
}
Expand Down Expand Up @@ -181,12 +185,12 @@ public void Test_12_CustomDomainConfigurationSettings()
[TearDown]
public void TearDown()
{
if (File.Exists(".openai"))
if (File.Exists(OpenAIAuthentication.CONFIG_FILE))
{
File.Delete(".openai");
File.Delete(OpenAIAuthentication.CONFIG_FILE);
}

Assert.IsFalse(File.Exists(".openai"));
Assert.IsFalse(File.Exists(OpenAIAuthentication.CONFIG_FILE));
}
}
}
21 changes: 19 additions & 2 deletions OpenAI-DotNet-Tests/TestFixture_00_02_Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public async Task Test_02_Tool_Funcs()
{
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))
Tool.FromFunc("test_func_weather", () => WeatherService.GetCurrentWeatherAsync("my location", WeatherService.WeatherUnit.Celsius)),
Tool.FromFunc<List<int>, string>("test_func_with_array_args", FunctionWithArrayArgs)
};

var json = JsonSerializer.Serialize(tools, new JsonSerializerOptions(OpenAIClient.JsonSerializationOptions)
Expand All @@ -48,6 +49,7 @@ public async Task Test_02_Tool_Funcs()
Assert.IsNotNull(tool);
var result = tool.InvokeFunction<string>();
Assert.AreEqual("success", result);

var toolWithArgs = tools[1];
Assert.IsNotNull(toolWithArgs);
toolWithArgs.Function.Arguments = new JsonObject
Expand All @@ -63,6 +65,16 @@ public async Task Test_02_Tool_Funcs()
var resultWeather = await toolWeather.InvokeFunctionAsync();
Assert.IsFalse(string.IsNullOrWhiteSpace(resultWeather));
Console.WriteLine(resultWeather);

var toolWithArrayArgs = tools[3];
Assert.IsNotNull(toolWithArrayArgs);
toolWithArrayArgs.Function.Arguments = new JsonObject
{
["args"] = new JsonArray { 1, 2, 3, 4, 5 }
};
var resultWithArrayArgs = toolWithArrayArgs.InvokeFunction<string>();
Assert.AreEqual("1, 2, 3, 4, 5", resultWithArrayArgs);
Console.WriteLine(resultWithArrayArgs);
}

private string Function()
Expand All @@ -75,6 +87,11 @@ private string FunctionWithArgs(string arg1, string arg2)
return $"{arg1} {arg2}";
}

private string FunctionWithArrayArgs(List<int> args)
{
return string.Join(", ", args);
}

[Test]
public void Test_03_Tool_works_when_called_concurrently()
{
Expand All @@ -100,4 +117,4 @@ async Task Test(int id)
}
}
}
}
}
Loading

0 comments on commit 9675957

Please sign in to comment.