From f50d386f0b65a4ba8c1041a28bab2a1a475c2296 Mon Sep 17 00:00:00 2001 From: rodion-m Date: Sun, 6 Aug 2023 03:33:12 +0600 Subject: [PATCH] Improve OpenAiClientGetStructuredResponseTests --- OpenAI.ChatGPT.Modules.ChatBot/ChatGptBot.cs | 55 ------------------- OpenAI_DotNet.sln | 7 --- .../OpenAiClient_GetStructuredResponse.cs | 51 +++++++---------- 3 files changed, 20 insertions(+), 93 deletions(-) delete mode 100644 OpenAI.ChatGPT.Modules.ChatBot/ChatGptBot.cs diff --git a/OpenAI.ChatGPT.Modules.ChatBot/ChatGptBot.cs b/OpenAI.ChatGPT.Modules.ChatBot/ChatGptBot.cs deleted file mode 100644 index 66072b7..0000000 --- a/OpenAI.ChatGPT.Modules.ChatBot/ChatGptBot.cs +++ /dev/null @@ -1,55 +0,0 @@ -using OpenAI.ChatGpt; -using OpenAI.ChatGpt.Interfaces; -using OpenAI.ChatGpt.Internal; -using OpenAI.ChatGpt.Models; - -namespace OpenAI.ChatGPT.Modules.ChatBot; - -internal class ChatGptBot -{ - private readonly string? _initialMessage; - private readonly int? _maxTokens; - private readonly string? _model; - private readonly float? _temperature; - private readonly ITimeProvider _timeProvider; - private readonly IChatHistoryStorage _chatHistoryStorage; - private readonly OpenAiClient _client; - - public ChatGptBot( - string openAiKey, - string? initialMessage = null, - int? maxTokens = null, - string? model = null, - float? temperature = null, - string? host = null, - IChatHistoryStorage? chatHistoryStorage = null, - ITimeProvider? timeProvider = null) - { - _initialMessage = initialMessage; - _maxTokens = maxTokens; - _model = model; - _temperature = temperature; - _timeProvider = timeProvider ?? new TimeProviderUtc(); - _chatHistoryStorage = chatHistoryStorage ?? new InMemoryChatHistoryStorage(); - _client = new OpenAiClient(openAiKey, host); - } - - public async Task GetResponse(string message, string userId, CancellationToken cancellationToken = default) - { - ArgumentNullException.ThrowIfNull(message); - ArgumentNullException.ThrowIfNull(userId); - var chatGpt = new ChatGpt.ChatGPT(_client, _chatHistoryStorage, _timeProvider, userId, new ChatGPTConfig() - { - InitialSystemMessage = _initialMessage, - MaxTokens = _maxTokens, - Model = _model, - Temperature = _temperature - }); - - var service = await chatGpt.ContinueOrStartNewTopic(cancellationToken); - //var messages = await service.GetMessages(cancellationToken); - //messages.First().CalculateApproxTotalTokenCount(); - var response = await service.GetNextMessageResponse(message, cancellationToken); - return response; - } -} \ No newline at end of file diff --git a/OpenAI_DotNet.sln b/OpenAI_DotNet.sln index 52a7c62..7a7456e 100644 --- a/OpenAI_DotNet.sln +++ b/OpenAI_DotNet.sln @@ -33,8 +33,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.ChatGpt.Modules.Stru EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "modules", "modules", "{068E9E67-C2FC-4F8C-B27C-CB3A8FA44BD8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.ChatGPT.Modules.ChatBot", "OpenAI.ChatGPT.Modules.ChatBot\OpenAI.ChatGPT.Modules.ChatBot.csproj", "{18DF620B-CEE4-4DDB-8C70-A2BF95026E14}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -89,10 +87,6 @@ Global {F2968A66-5672-439E-823E-D35100CA067D}.Debug|Any CPU.Build.0 = Debug|Any CPU {F2968A66-5672-439E-823E-D35100CA067D}.Release|Any CPU.ActiveCfg = Release|Any CPU {F2968A66-5672-439E-823E-D35100CA067D}.Release|Any CPU.Build.0 = Release|Any CPU - {18DF620B-CEE4-4DDB-8C70-A2BF95026E14}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {18DF620B-CEE4-4DDB-8C70-A2BF95026E14}.Debug|Any CPU.Build.0 = Debug|Any CPU - {18DF620B-CEE4-4DDB-8C70-A2BF95026E14}.Release|Any CPU.ActiveCfg = Release|Any CPU - {18DF620B-CEE4-4DDB-8C70-A2BF95026E14}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -107,6 +101,5 @@ Global {E303F270-6091-47DE-9260-DAD6122005A7} = {926D63B6-9F6A-45A1-B5B7-5F36352C23AB} {F2968A66-5672-439E-823E-D35100CA067D} = {068E9E67-C2FC-4F8C-B27C-CB3A8FA44BD8} {E155D31C-0061-40A3-AD54-93B5DD08836B} = {068E9E67-C2FC-4F8C-B27C-CB3A8FA44BD8} - {18DF620B-CEE4-4DDB-8C70-A2BF95026E14} = {068E9E67-C2FC-4F8C-B27C-CB3A8FA44BD8} EndGlobalSection EndGlobal diff --git a/tests/OpenAI.ChatGpt.IntegrationTests/OpenAiClientTests/OpenAiClient_GetStructuredResponse.cs b/tests/OpenAI.ChatGpt.IntegrationTests/OpenAiClientTests/OpenAiClient_GetStructuredResponse.cs index c126376..3afc17a 100644 --- a/tests/OpenAI.ChatGpt.IntegrationTests/OpenAiClientTests/OpenAiClient_GetStructuredResponse.cs +++ b/tests/OpenAI.ChatGpt.IntegrationTests/OpenAiClientTests/OpenAiClient_GetStructuredResponse.cs @@ -14,7 +14,7 @@ public OpenAiClientGetStructuredResponseTests() [Fact] public async void Get_simple_structured_response_from_ChatGPT() { - var message = + var message = Dialog.StartAsSystem("What did user input?") .ThenUser("My name is John, my age is 30, my email is john@gmail.com"); var response = await _client.GetStructuredResponse(message); @@ -23,62 +23,52 @@ public async void Get_simple_structured_response_from_ChatGPT() response.Age.Should().Be(30); response.Email.Should().Be("john@gmail.com"); } - + [Fact] public async void Get_structured_response_with_ARRAY_from_ChatGPT() { - var message = - Dialog.StartAsSystem("What did user input?") - .ThenUser("My name is John, my age is 30, my email is john@gmail.com. I want to buy 2 apple and 3 orange."); + var message = Dialog + .StartAsSystem("What did user input?") + .ThenUser("My name is John, my age is 30, my email is john@gmail.com. " + + "I want to buy 2 apple and 3 orange."); var response = await _client.GetStructuredResponse(message); response.Should().NotBeNull(); response.UserInfo.Should().NotBeNull(); response.UserInfo!.Name.Should().Be("John"); response.UserInfo.Age.Should().Be(30); response.UserInfo.Email.Should().Be("john@gmail.com"); - + response.Items.Should().HaveCount(2); response.Items![0].Name.Should().Be("apple"); response.Items[0].Quantity.Should().Be(2); response.Items[1].Name.Should().Be("orange"); response.Items[1].Quantity.Should().Be(3); } - + [Fact] public async void Get_structured_response_with_ENUM_from_ChatGPT() { - var message = - Dialog.StartAsSystem("What did user input?") - .ThenUser("Мой любимый цвет - красный"); + var message = Dialog + .StartAsSystem("What did user input?") + .ThenUser("Мой любимый цвет - красный"); var response = await _client.GetStructuredResponse(message); response.Should().NotBeNull(); response.Color.Should().Be(Thing.Colors.Red); } - + [Fact] public async void Get_structured_response_with_extra_data_from_ChatGPT() { - var message = - Dialog.StartAsSystem("Return requested data.") - .ThenUser("In what year was the city of Almaty originally founded?"); + var message = Dialog + .StartAsSystem("Return requested data.") + .ThenUser("I need info about Almaty city"); var response = await _client.GetStructuredResponse(message); response.Should().NotBeNull(); - //response.Name.Should().Be("Almaty"); + response.Name.Should().Be("Almaty"); + response.Country.Should().Be("Kazakhstan"); response.YearOfFoundation.Should().Be(1854); - //response.Country.Should().Be("Kazakhstan"); } - - [Fact] - public async void Get_structured_response_for_tic_tak_toe_from_ChatGPT_GPT4() - { - var message = - Dialog.StartAsSystem("This is a game of tic tac toe. X goes first. Your turn is O. What is your next move? Board: [{\"Row\":0,\"Column\":0},{\"Row\":0,\"Column\":1},{\"Row\":0,\"Column\":2},{\"Row\":1,\"Column\":0},{\"Row\":1,\"Column\":1},{\"Row\":1,\"Column\":2},{\"Row\":2,\"Column\":0},{\"Row\":2,\"Column\":1},{\"Row\":2,\"Column\":2}]"); - var response = await _client.GetStructuredResponse(message, model: ChatCompletionModels.Gpt4); - response.Should().NotBeNull(); - } - - private record CellPosition(int Row, int Column); - + private class Order { public UserInfo? UserInfo { get; set; } @@ -86,7 +76,7 @@ private class Order public record Item(string Name, int Quantity); } - + private class UserInfo { public string? Name { get; init; } @@ -106,5 +96,4 @@ public enum Colors } private record City(string Name, int YearOfFoundation, string Country); -} - +} \ No newline at end of file