Skip to content

Commit

Permalink
add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenHodgson committed Jun 15, 2024
1 parent 9ae666f commit b0fb1c2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
24 changes: 23 additions & 1 deletion OpenAI-DotNet-Tests/TestFixture_00_02_Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ 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<List<int>, string>("test_func_with_array_args", FunctionWithArrayArgs)
Tool.FromFunc<List<int>, string>("test_func_with_array_args", FunctionWithArrayArgs),
Tool.FromFunc<string, string>("test_single_return_arg", arg1 => arg1),
Tool.FromFunc("test_no_specifiers", (string arg1) => arg1)
};

var json = JsonSerializer.Serialize(tools, new JsonSerializerOptions(OpenAIClient.JsonSerializationOptions)
Expand Down Expand Up @@ -75,6 +77,26 @@ public async Task Test_02_Tool_Funcs()
var resultWithArrayArgs = toolWithArrayArgs.InvokeFunction<string>();
Assert.AreEqual("1, 2, 3, 4, 5", resultWithArrayArgs);
Console.WriteLine(resultWithArrayArgs);

var singleReturnArg = tools[4];
Assert.IsNotNull(singleReturnArg);
singleReturnArg.Function.Arguments = new JsonObject
{
["arg1"] = "arg1"
};
var resultSingleReturnArg = singleReturnArg.InvokeFunction<string>();
Assert.AreEqual("arg1", resultSingleReturnArg);
Console.WriteLine(resultSingleReturnArg);

var toolNoSpecifiers = tools[5];
Assert.IsNotNull(toolNoSpecifiers);
toolNoSpecifiers.Function.Arguments = new JsonObject
{
["arg1"] = "arg1"
};
var resultNoSpecifiers = toolNoSpecifiers.InvokeFunction<string>();
Assert.AreEqual("arg1", resultNoSpecifiers);
Console.WriteLine(resultNoSpecifiers);
}

private string Function()
Expand Down
33 changes: 33 additions & 0 deletions OpenAI-DotNet-Tests/TestFixture_04_Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,5 +487,38 @@ public async Task Test_04_02_GetChatLogProbsStreaming()
Console.WriteLine(response.ToString());
response.GetUsage();
}

[Test]
public async Task Test_05_02_GetChat_Enumerable_TestToolCalls_Streaming()
{
Assert.IsNotNull(OpenAIClient.ChatEndpoint);

var messages = new List<Message>
{
new(Role.System, "You must extract the name from the input"),
new(Role.User, "My name is Joe")
};

var tools = new List<Tool>
{
Tool.FromFunc("extract_first_name", (string name) => name)
};

var request = new ChatRequest(messages, tools);

await foreach (var streamResponse in OpenAIClient.ChatEndpoint.StreamCompletionEnumerableAsync(request))
{
Console.WriteLine(streamResponse.ToJsonString());

if (streamResponse.FirstChoice.Message is { } message)
{
foreach (var tool in message.ToolCalls)
{
var output = tool.InvokeFunction<string>();
Console.WriteLine($"Output from StreamCompletionEnumerableAsync: {output}");
}
}
}
}
}
}

0 comments on commit b0fb1c2

Please sign in to comment.