diff --git a/Directory.Build.props b/Directory.Build.props index e4de2ee..9b172ac 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -3,4 +3,10 @@ 2.7.1 true + + + + + + \ No newline at end of file diff --git a/OpenAI.ChatGpt.Modules.Translator/ChatGPTTranslatorService.cs b/OpenAI.ChatGpt.Modules.Translator/ChatGPTTranslatorService.cs index 5dd1b49..e66ac1d 100644 --- a/OpenAI.ChatGpt.Modules.Translator/ChatGPTTranslatorService.cs +++ b/OpenAI.ChatGpt.Modules.Translator/ChatGPTTranslatorService.cs @@ -88,19 +88,19 @@ public async Task TranslateText( rawResponseGetter, cancellationToken); return response; - - string CreateTextTranslationPrompt(string sourceLanguage, string targetLanguage) - { - ArgumentNullException.ThrowIfNull(sourceLanguage); - ArgumentNullException.ThrowIfNull(targetLanguage); - return $"I want you to act as a translator from {sourceLanguage} to {targetLanguage}. " + - "The user provides with a sentence and you translate it. " + - "In the response write ONLY translated text." + - (_extraPrompt is not null ? "\n" + _extraPrompt : ""); - } } - public async Task TranslateObject( + internal virtual string CreateTextTranslationPrompt(string sourceLanguage, string targetLanguage) + { + ArgumentNullException.ThrowIfNull(sourceLanguage); + ArgumentNullException.ThrowIfNull(targetLanguage); + return $"I want you to act as a translator from {sourceLanguage} to {targetLanguage}. " + + "The user provides with a sentence and you translate it. " + + "In the response write ONLY translated text." + + (_extraPrompt is not null ? "\n" + _extraPrompt : ""); + } + + public virtual async Task TranslateObject( TObject objectToTranslate, string? sourceLanguage = null, string? targetLanguage = null, @@ -144,14 +144,14 @@ public async Task TranslateObject( cancellationToken ); return response; - - string CreateObjectTranslationPrompt(string sourceLanguage, string targetLanguage) - { - ArgumentNullException.ThrowIfNull(sourceLanguage); - ArgumentNullException.ThrowIfNull(targetLanguage); - return $"I want you to act as a translator from {sourceLanguage} to {targetLanguage}. " + - "The user provides you with an object in json. You translate only the text fields that need to be translated. " + - (_extraPrompt is not null ? "\n" + _extraPrompt : ""); - } + } + + internal string CreateObjectTranslationPrompt(string sourceLanguage, string targetLanguage) + { + ArgumentNullException.ThrowIfNull(sourceLanguage); + ArgumentNullException.ThrowIfNull(targetLanguage); + return $"I want you to act as a translator from {sourceLanguage} to {targetLanguage}. " + + "The user provides you with an object in json. You translate only the text fields that need to be translated. " + + (_extraPrompt is not null ? "\n" + _extraPrompt : ""); } } \ No newline at end of file diff --git a/OpenAI_DotNet.sln b/OpenAI_DotNet.sln index a325f09..c4dea23 100644 --- a/OpenAI_DotNet.sln +++ b/OpenAI_DotNet.sln @@ -27,10 +27,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatGpt.TelegramBotExample" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.ChatGpt.Modules.Translator", "OpenAI.ChatGpt.Modules.Translator\OpenAI.ChatGpt.Modules.Translator.csproj", "{E155D31C-0061-40A3-AD54-93B5DD08836B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.ChatGpt.Modules.Translator.UnitTests", "tests\OpenAI.ChatGpt.Modules.Translator.UnitTests\OpenAI.ChatGpt.Modules.Translator.UnitTests.csproj", "{49F18714-F5F9-4FFC-A674-39CE166466A6}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.ChatGpt.Modules.Translator.IntegrationTests", "tests\OpenAI.ChatGpt.Modules.Translator.IntegrationTests\OpenAI.ChatGpt.Modules.Translator.IntegrationTests.csproj", "{600195A2-6E93-46AF-87A7-EA2E48E5AE24}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.Tests.Shared", "tests\OpenAI.Tests.Shared\OpenAI.Tests.Shared.csproj", "{E303F270-6091-47DE-9260-DAD6122005A7}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.ChatGpt.Modules.StructuredResponse", "OpenAI.ChatGpt.Modules.StructuredResponse\OpenAI.ChatGpt.Modules.StructuredResponse.csproj", "{F2968A66-5672-439E-823E-D35100CA067D}" @@ -81,14 +77,6 @@ Global {E155D31C-0061-40A3-AD54-93B5DD08836B}.Debug|Any CPU.Build.0 = Debug|Any CPU {E155D31C-0061-40A3-AD54-93B5DD08836B}.Release|Any CPU.ActiveCfg = Release|Any CPU {E155D31C-0061-40A3-AD54-93B5DD08836B}.Release|Any CPU.Build.0 = Release|Any CPU - {49F18714-F5F9-4FFC-A674-39CE166466A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {49F18714-F5F9-4FFC-A674-39CE166466A6}.Debug|Any CPU.Build.0 = Debug|Any CPU - {49F18714-F5F9-4FFC-A674-39CE166466A6}.Release|Any CPU.ActiveCfg = Release|Any CPU - {49F18714-F5F9-4FFC-A674-39CE166466A6}.Release|Any CPU.Build.0 = Release|Any CPU - {600195A2-6E93-46AF-87A7-EA2E48E5AE24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {600195A2-6E93-46AF-87A7-EA2E48E5AE24}.Debug|Any CPU.Build.0 = Debug|Any CPU - {600195A2-6E93-46AF-87A7-EA2E48E5AE24}.Release|Any CPU.ActiveCfg = Release|Any CPU - {600195A2-6E93-46AF-87A7-EA2E48E5AE24}.Release|Any CPU.Build.0 = Release|Any CPU {E303F270-6091-47DE-9260-DAD6122005A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {E303F270-6091-47DE-9260-DAD6122005A7}.Debug|Any CPU.Build.0 = Debug|Any CPU {E303F270-6091-47DE-9260-DAD6122005A7}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -108,8 +96,6 @@ Global {983A565C-5AE2-4F76-8B7B-7C5C051072C7} = {926D63B6-9F6A-45A1-B5B7-5F36352C23AB} {88213D5C-AADC-4F03-ACA3-7ADDCCE87DF4} = {80DB142C-5A1B-431C-BD85-19C83C6FC0A3} {8C86E60A-77C2-4204-AF36-F4B845474016} = {80DB142C-5A1B-431C-BD85-19C83C6FC0A3} - {49F18714-F5F9-4FFC-A674-39CE166466A6} = {926D63B6-9F6A-45A1-B5B7-5F36352C23AB} - {600195A2-6E93-46AF-87A7-EA2E48E5AE24} = {926D63B6-9F6A-45A1-B5B7-5F36352C23AB} {E303F270-6091-47DE-9260-DAD6122005A7} = {926D63B6-9F6A-45A1-B5B7-5F36352C23AB} EndGlobalSection EndGlobal diff --git a/tests/OpenAI.ChatGpt.Modules.Translator.IntegrationTests/ChatGptTranslatorServiceTests.cs b/tests/OpenAI.ChatGpt.IntegrationTests/ChatGptTranslatorServiceTests.cs similarity index 96% rename from tests/OpenAI.ChatGpt.Modules.Translator.IntegrationTests/ChatGptTranslatorServiceTests.cs rename to tests/OpenAI.ChatGpt.IntegrationTests/ChatGptTranslatorServiceTests.cs index a747dd3..9d08d68 100644 --- a/tests/OpenAI.ChatGpt.Modules.Translator.IntegrationTests/ChatGptTranslatorServiceTests.cs +++ b/tests/OpenAI.ChatGpt.IntegrationTests/ChatGptTranslatorServiceTests.cs @@ -1,7 +1,6 @@ -using FluentAssertions; -using OpenAI.Tests.Shared; +using OpenAI.ChatGpt.Modules.Translator; -namespace OpenAI.ChatGpt.Modules.Translator.IntegrationTests; +namespace OpenAI.ChatGpt.IntegrationTests; public class ChatGptTranslatorServiceTests { diff --git a/tests/OpenAI.ChatGpt.Modules.Translator.UnitTests/ChatGptTranslatorServiceTests.cs b/tests/OpenAI.ChatGpt.UnitTests/ChatGptTranslatorServiceTests.cs similarity index 53% rename from tests/OpenAI.ChatGpt.Modules.Translator.UnitTests/ChatGptTranslatorServiceTests.cs rename to tests/OpenAI.ChatGpt.UnitTests/ChatGptTranslatorServiceTests.cs index 24270bf..555cfc8 100644 --- a/tests/OpenAI.ChatGpt.Modules.Translator.UnitTests/ChatGptTranslatorServiceTests.cs +++ b/tests/OpenAI.ChatGpt.UnitTests/ChatGptTranslatorServiceTests.cs @@ -1,9 +1,7 @@ -using FluentAssertions; using Moq; -using OpenAI.ChatGpt.Models.ChatCompletion; -using OpenAI.ChatGpt.Models.ChatCompletion.Messaging; +using OpenAI.ChatGpt.Modules.Translator; -namespace OpenAI.ChatGpt.Modules.Translator.UnitTests; +namespace OpenAI.ChatGpt.UnitTests; public class ChatGptTranslatorServiceTests { @@ -46,31 +44,29 @@ public async Task Translate_without_source_and_target_languages_uses_default_lan It.IsAny>(), It.IsAny>(), It.IsAny())) - .ReturnsAsync("Привет, мир!") + .ReturnsAsync("Привет, мир!"); + + var translatorServiceMock = new Mock( + (IOpenAiClient) clientMock.Object, + expectedSourceLanguage, + expectedTargetLanguage, + null); + + translatorServiceMock.Setup(service => service.CreateTextTranslationPrompt( + It.IsAny(), It.IsAny())) + .Returns($"{expectedSourceLanguage} => {expectedTargetLanguage}") .Verifiable(); - var translatorService = new ChatGPTTranslatorService( - clientMock.Object, - defaultSourceLanguage: expectedSourceLanguage, - defaultTargetLanguage: expectedTargetLanguage); + + var translatorService = translatorServiceMock.Object; // Act - var translatedText = await translatorService.TranslateText(textToTranslate); + _ = await translatorService.TranslateText(textToTranslate); // Assert - clientMock.Verify(client => client.GetChatCompletions( - It.Is(dialog => - dialog.GetMessages().Any( - message => message.Role == "system" && - message.Content.Contains($"I want you to act as a translator from {expectedSourceLanguage} to {expectedTargetLanguage}"))), - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny>(), - It.IsAny>(), - It.IsAny()), + translatorServiceMock.Verify(service => + service.CreateTextTranslationPrompt( + expectedSourceLanguage, expectedTargetLanguage), Times.Once); - translatedText.Should().Be("Привет, мир!"); } diff --git a/tests/OpenAI.ChatGpt.UnitTests/OpenAI.ChatGpt.UnitTests.csproj b/tests/OpenAI.ChatGpt.UnitTests/OpenAI.ChatGpt.UnitTests.csproj index 2904eb4..cb7e104 100644 --- a/tests/OpenAI.ChatGpt.UnitTests/OpenAI.ChatGpt.UnitTests.csproj +++ b/tests/OpenAI.ChatGpt.UnitTests/OpenAI.ChatGpt.UnitTests.csproj @@ -15,6 +15,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive