From df4f7bd3ae55039000a16d7306d01ae142876bfd Mon Sep 17 00:00:00 2001 From: drejmanMacopedia Date: Mon, 8 May 2023 19:32:31 +0200 Subject: [PATCH] Static Code Analysis, Refactor, Optimization --- README.md | 6 +- .../Client/OpenAiClient.php | 22 +++---- .../Client/OpenAiClient/Choice.php | 2 +- .../Client/OpenAiClient/Choices.php | 2 +- .../Client/OpenAiClient/Message.php | 2 +- .../Client/OpenAiClient/Response.php | 40 +++++++++++++ .../OpenAiClient/Response/ChoicesResponse.php | 40 +++++++++++++ .../OpenAiClient/Response/ErrorResponse.php | 20 +++++++ .../MassEdit/TranslateAttributesProcessor.php | 4 +- .../MacopediaTranslatorExtension.php | 2 +- .../InvalidOpenAiResponseException.php | 2 +- .../MacopediaTranslatorBundle.php | 2 +- .../Resources/config/acl.yml | 0 .../Resources/config/connector.yml | 0 .../form_extensions/mass_edit/product.yml | 0 .../Resources/config/requirejs.yml | 0 .../Resources/config/services.yml | 2 +- .../public/js/mass-edit/translate.js | 0 .../public/templates/mass-edit/translate.html | 0 .../Resources/translations/jsmessages.en.yml | 0 .../Resources/translations/jsmessages.fr.yml | 0 .../Resources/translations/jsmessages.pl.yml | 0 .../Resources/translations/messages.en.yml | 0 .../Resources/translations/messages.fr.yml | 0 .../Resources/translations/messages.pl.yml | 0 .../Service/TranslateAttributesService.php | 25 ++++---- .../Translator/Language.php | 2 +- .../Translator/OpenAiTranslator.php | 24 ++++++++ .../Translator/TranslatorInterface.php | 4 +- .../Client/OpenAiClient/Response.php | 59 ------------------- .../Translator/OpenAiTranslator.php | 33 ----------- tests/Translator/LanguageTest.php | 4 +- tests/bootstrap.php | 4 +- 33 files changed, 169 insertions(+), 132 deletions(-) rename src/Macopedia/{Translator => OpenAiTranslator}/Client/OpenAiClient.php (66%) rename src/Macopedia/{Translator => OpenAiTranslator}/Client/OpenAiClient/Choice.php (82%) rename src/Macopedia/{Translator => OpenAiTranslator}/Client/OpenAiClient/Choices.php (91%) rename src/Macopedia/{Translator => OpenAiTranslator}/Client/OpenAiClient/Message.php (80%) create mode 100644 src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response.php create mode 100644 src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response/ChoicesResponse.php create mode 100644 src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response/ErrorResponse.php rename src/Macopedia/{Translator => OpenAiTranslator}/Connector/Processor/MassEdit/TranslateAttributesProcessor.php (92%) rename src/Macopedia/{Translator => OpenAiTranslator}/DependencyInjection/MacopediaTranslatorExtension.php (93%) rename src/Macopedia/{Translator => OpenAiTranslator}/Exception/InvalidOpenAiResponseException.php (85%) rename src/Macopedia/{Translator => OpenAiTranslator}/MacopediaTranslatorBundle.php (84%) rename src/Macopedia/{Translator => OpenAiTranslator}/Resources/config/acl.yml (100%) rename src/Macopedia/{Translator => OpenAiTranslator}/Resources/config/connector.yml (100%) rename src/Macopedia/{Translator => OpenAiTranslator}/Resources/config/form_extensions/mass_edit/product.yml (100%) rename src/Macopedia/{Translator => OpenAiTranslator}/Resources/config/requirejs.yml (100%) rename src/Macopedia/{Translator => OpenAiTranslator}/Resources/config/services.yml (92%) rename src/Macopedia/{Translator => OpenAiTranslator}/Resources/public/js/mass-edit/translate.js (100%) rename src/Macopedia/{Translator => OpenAiTranslator}/Resources/public/templates/mass-edit/translate.html (100%) rename src/Macopedia/{Translator => OpenAiTranslator}/Resources/translations/jsmessages.en.yml (100%) rename src/Macopedia/{Translator => OpenAiTranslator}/Resources/translations/jsmessages.fr.yml (100%) rename src/Macopedia/{Translator => OpenAiTranslator}/Resources/translations/jsmessages.pl.yml (100%) rename src/Macopedia/{Translator => OpenAiTranslator}/Resources/translations/messages.en.yml (100%) rename src/Macopedia/{Translator => OpenAiTranslator}/Resources/translations/messages.fr.yml (100%) rename src/Macopedia/{Translator => OpenAiTranslator}/Resources/translations/messages.pl.yml (100%) rename src/Macopedia/{Translator => OpenAiTranslator}/Service/TranslateAttributesService.php (79%) rename src/Macopedia/{Translator => OpenAiTranslator}/Translator/Language.php (99%) create mode 100644 src/Macopedia/OpenAiTranslator/Translator/OpenAiTranslator.php rename src/Macopedia/{Translator => OpenAiTranslator}/Translator/TranslatorInterface.php (65%) delete mode 100644 src/Macopedia/Translator/Client/OpenAiClient/Response.php delete mode 100644 src/Macopedia/Translator/Translator/OpenAiTranslator.php diff --git a/README.md b/README.md index aed0356..7626c41 100755 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Akeneo OpenAI Translator +Akeneo OpenAi Translator ===================================== @@ -16,7 +16,7 @@ register bundle in `config/bundles.php` ```php return [ - 'Macopedia\Translator\MacopediaTranslatorBundle' => ['all' => true] + 'Macopedia\OpenAiTranslator\MacopediaTranslatorBundle' => ['all' => true] ]; ``` @@ -45,6 +45,6 @@ Extension uses [Open AI API](https://openai.com/product) - ChatGPT v3.5 * Akeneo PIM >= 6.x ## Contact -`Akeneo OpenAI translator` is brought to you by [Macopedia](https://macopedia.com/). +`Akeneo OpenAi translator` is brought to you by [Macopedia](https://macopedia.com/). [Contact us](https://macopedia.com/contact) \ No newline at end of file diff --git a/src/Macopedia/Translator/Client/OpenAiClient.php b/src/Macopedia/OpenAiTranslator/Client/OpenAiClient.php similarity index 66% rename from src/Macopedia/Translator/Client/OpenAiClient.php rename to src/Macopedia/OpenAiTranslator/Client/OpenAiClient.php index 5ed3fb8..0bb6db1 100644 --- a/src/Macopedia/Translator/Client/OpenAiClient.php +++ b/src/Macopedia/OpenAiTranslator/Client/OpenAiClient.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace Macopedia\Translator\Client; +namespace Macopedia\OpenAiTranslator\Client; use JetBrains\PhpStorm\ArrayShape; -use Macopedia\Translator\Client\OpenAiClient\Response; -use Macopedia\Translator\Exception\InvalidOpenAiResponseException; +use Macopedia\OpenAiTranslator\Client\OpenAiClient\Response; +use Macopedia\OpenAiTranslator\Exception\InvalidOpenAiResponseException; use Orhanerday\OpenAi\OpenAi; use Psr\Log\LoggerInterface; @@ -18,8 +18,7 @@ public function __construct( private LoggerInterface $logger, private string $model, ?string $apiKey = '', - ?string $organization = null, - private bool $logging = false + ?string $organization = null ) { $this->client = new OpenAi($apiKey); $this->client->listModels(); @@ -29,14 +28,10 @@ public function __construct( } } - public function ask(string $role, string $content): ?Response + public function ask(string $role, string $content): ?string { $message = $this->generateMessage($role, $content); - if ($this->logging) { - $this->logger->notice('OpenAI Message prepared', $message); - } - $response = $this->client->chat($message); if ($response === false) { @@ -45,11 +40,12 @@ public function ask(string $role, string $content): ?Response $response = Response::fromArray(json_decode($response, true)); - if ($this->logging) { - $this->logger->notice('OpenAI answer received', ['message' => $response->getFirstChoiceMessage() ?? 'EMPTY']); + if ($response instanceof Response\ErrorResponse) { + $this->logger->error('OpenAI connection Error: ' . $response->getError()); + return null; } - return $response; + return $response->getFirstChoiceMessage(); } diff --git a/src/Macopedia/Translator/Client/OpenAiClient/Choice.php b/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Choice.php similarity index 82% rename from src/Macopedia/Translator/Client/OpenAiClient/Choice.php rename to src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Choice.php index b35755d..22ccbb2 100644 --- a/src/Macopedia/Translator/Client/OpenAiClient/Choice.php +++ b/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Choice.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Macopedia\Translator\Client\OpenAiClient; +namespace Macopedia\OpenAiTranslator\Client\OpenAiClient; class Choice { diff --git a/src/Macopedia/Translator/Client/OpenAiClient/Choices.php b/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Choices.php similarity index 91% rename from src/Macopedia/Translator/Client/OpenAiClient/Choices.php rename to src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Choices.php index 9d3d5f6..a1996d5 100644 --- a/src/Macopedia/Translator/Client/OpenAiClient/Choices.php +++ b/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Choices.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Macopedia\Translator\Client\OpenAiClient; +namespace Macopedia\OpenAiTranslator\Client\OpenAiClient; use Webmozart\Assert\Assert; diff --git a/src/Macopedia/Translator/Client/OpenAiClient/Message.php b/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Message.php similarity index 80% rename from src/Macopedia/Translator/Client/OpenAiClient/Message.php rename to src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Message.php index ad4448b..d0dd175 100644 --- a/src/Macopedia/Translator/Client/OpenAiClient/Message.php +++ b/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Message.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Macopedia\Translator\Client\OpenAiClient; +namespace Macopedia\OpenAiTranslator\Client\OpenAiClient; class Message { diff --git a/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response.php b/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response.php new file mode 100644 index 0000000..0434326 --- /dev/null +++ b/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response.php @@ -0,0 +1,40 @@ +setTimestamp($data['created']), + $data['model'], + new Choices($data['choices']), + $data['usage'] + ); + } + + if (array_key_exists('error', $data)) { + return new ErrorResponse($data['error']); + } + + throw new RuntimeException('Can not create Response'); + } +} diff --git a/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response/ChoicesResponse.php b/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response/ChoicesResponse.php new file mode 100644 index 0000000..ea99a5d --- /dev/null +++ b/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response/ChoicesResponse.php @@ -0,0 +1,40 @@ +choices; + } + + public function getFirstChoiceMessage(): ?string + { + $firstMessage = $this->choices->getAnswers()[0] ?? null; + + if ($firstMessage === null) { + return null; + } + return $firstMessage->getMessage()->getContent(); + } +} diff --git a/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response/ErrorResponse.php b/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response/ErrorResponse.php new file mode 100644 index 0000000..9596353 --- /dev/null +++ b/src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response/ErrorResponse.php @@ -0,0 +1,20 @@ +error; + } +} diff --git a/src/Macopedia/Translator/Connector/Processor/MassEdit/TranslateAttributesProcessor.php b/src/Macopedia/OpenAiTranslator/Connector/Processor/MassEdit/TranslateAttributesProcessor.php similarity index 92% rename from src/Macopedia/Translator/Connector/Processor/MassEdit/TranslateAttributesProcessor.php rename to src/Macopedia/OpenAiTranslator/Connector/Processor/MassEdit/TranslateAttributesProcessor.php index ac730d8..01eb9d2 100644 --- a/src/Macopedia/Translator/Connector/Processor/MassEdit/TranslateAttributesProcessor.php +++ b/src/Macopedia/OpenAiTranslator/Connector/Processor/MassEdit/TranslateAttributesProcessor.php @@ -2,14 +2,14 @@ declare(strict_types=1); -namespace Macopedia\Translator\Connector\Processor\MassEdit; +namespace Macopedia\OpenAiTranslator\Connector\Processor\MassEdit; use Akeneo\Pim\Enrichment\Component\Product\Connector\Processor\MassEdit\AbstractProcessor; use Akeneo\Pim\Enrichment\Component\Product\Model\ProductInterface; use Akeneo\Pim\Enrichment\Component\Product\Model\ProductModelInterface; use Exception; use InvalidArgumentException; -use Macopedia\Translator\Service\TranslateAttributesService; +use Macopedia\OpenAiTranslator\Service\TranslateAttributesService; final class TranslateAttributesProcessor extends AbstractProcessor { diff --git a/src/Macopedia/Translator/DependencyInjection/MacopediaTranslatorExtension.php b/src/Macopedia/OpenAiTranslator/DependencyInjection/MacopediaTranslatorExtension.php similarity index 93% rename from src/Macopedia/Translator/DependencyInjection/MacopediaTranslatorExtension.php rename to src/Macopedia/OpenAiTranslator/DependencyInjection/MacopediaTranslatorExtension.php index 1ef4b02..ffdfca4 100644 --- a/src/Macopedia/Translator/DependencyInjection/MacopediaTranslatorExtension.php +++ b/src/Macopedia/OpenAiTranslator/DependencyInjection/MacopediaTranslatorExtension.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Macopedia\Translator\DependencyInjection; +namespace Macopedia\OpenAiTranslator\DependencyInjection; use Exception; use Symfony\Component\Config\FileLocator; diff --git a/src/Macopedia/Translator/Exception/InvalidOpenAiResponseException.php b/src/Macopedia/OpenAiTranslator/Exception/InvalidOpenAiResponseException.php similarity index 85% rename from src/Macopedia/Translator/Exception/InvalidOpenAiResponseException.php rename to src/Macopedia/OpenAiTranslator/Exception/InvalidOpenAiResponseException.php index ed975a0..17ee290 100644 --- a/src/Macopedia/Translator/Exception/InvalidOpenAiResponseException.php +++ b/src/Macopedia/OpenAiTranslator/Exception/InvalidOpenAiResponseException.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Macopedia\Translator\Exception; +namespace Macopedia\OpenAiTranslator\Exception; use Exception; diff --git a/src/Macopedia/Translator/MacopediaTranslatorBundle.php b/src/Macopedia/OpenAiTranslator/MacopediaTranslatorBundle.php similarity index 84% rename from src/Macopedia/Translator/MacopediaTranslatorBundle.php rename to src/Macopedia/OpenAiTranslator/MacopediaTranslatorBundle.php index 716ee02..4f222d1 100644 --- a/src/Macopedia/Translator/MacopediaTranslatorBundle.php +++ b/src/Macopedia/OpenAiTranslator/MacopediaTranslatorBundle.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Macopedia\Translator; +namespace Macopedia\OpenAiTranslator; use Symfony\Component\HttpKernel\Bundle\Bundle; diff --git a/src/Macopedia/Translator/Resources/config/acl.yml b/src/Macopedia/OpenAiTranslator/Resources/config/acl.yml similarity index 100% rename from src/Macopedia/Translator/Resources/config/acl.yml rename to src/Macopedia/OpenAiTranslator/Resources/config/acl.yml diff --git a/src/Macopedia/Translator/Resources/config/connector.yml b/src/Macopedia/OpenAiTranslator/Resources/config/connector.yml similarity index 100% rename from src/Macopedia/Translator/Resources/config/connector.yml rename to src/Macopedia/OpenAiTranslator/Resources/config/connector.yml diff --git a/src/Macopedia/Translator/Resources/config/form_extensions/mass_edit/product.yml b/src/Macopedia/OpenAiTranslator/Resources/config/form_extensions/mass_edit/product.yml similarity index 100% rename from src/Macopedia/Translator/Resources/config/form_extensions/mass_edit/product.yml rename to src/Macopedia/OpenAiTranslator/Resources/config/form_extensions/mass_edit/product.yml diff --git a/src/Macopedia/Translator/Resources/config/requirejs.yml b/src/Macopedia/OpenAiTranslator/Resources/config/requirejs.yml similarity index 100% rename from src/Macopedia/Translator/Resources/config/requirejs.yml rename to src/Macopedia/OpenAiTranslator/Resources/config/requirejs.yml diff --git a/src/Macopedia/Translator/Resources/config/services.yml b/src/Macopedia/OpenAiTranslator/Resources/config/services.yml similarity index 92% rename from src/Macopedia/Translator/Resources/config/services.yml rename to src/Macopedia/OpenAiTranslator/Resources/config/services.yml index 2b8a65f..d870b88 100644 --- a/src/Macopedia/Translator/Resources/config/services.yml +++ b/src/Macopedia/OpenAiTranslator/Resources/config/services.yml @@ -1,5 +1,5 @@ parameters: - open_ai_key: env(default::OPEN_AI_KEY) + open_ai_key: '%env(default::OPEN_AI_KEY)%' services: Macopedia\Translator\Client\OpenAiClient: arguments: diff --git a/src/Macopedia/Translator/Resources/public/js/mass-edit/translate.js b/src/Macopedia/OpenAiTranslator/Resources/public/js/mass-edit/translate.js similarity index 100% rename from src/Macopedia/Translator/Resources/public/js/mass-edit/translate.js rename to src/Macopedia/OpenAiTranslator/Resources/public/js/mass-edit/translate.js diff --git a/src/Macopedia/Translator/Resources/public/templates/mass-edit/translate.html b/src/Macopedia/OpenAiTranslator/Resources/public/templates/mass-edit/translate.html similarity index 100% rename from src/Macopedia/Translator/Resources/public/templates/mass-edit/translate.html rename to src/Macopedia/OpenAiTranslator/Resources/public/templates/mass-edit/translate.html diff --git a/src/Macopedia/Translator/Resources/translations/jsmessages.en.yml b/src/Macopedia/OpenAiTranslator/Resources/translations/jsmessages.en.yml similarity index 100% rename from src/Macopedia/Translator/Resources/translations/jsmessages.en.yml rename to src/Macopedia/OpenAiTranslator/Resources/translations/jsmessages.en.yml diff --git a/src/Macopedia/Translator/Resources/translations/jsmessages.fr.yml b/src/Macopedia/OpenAiTranslator/Resources/translations/jsmessages.fr.yml similarity index 100% rename from src/Macopedia/Translator/Resources/translations/jsmessages.fr.yml rename to src/Macopedia/OpenAiTranslator/Resources/translations/jsmessages.fr.yml diff --git a/src/Macopedia/Translator/Resources/translations/jsmessages.pl.yml b/src/Macopedia/OpenAiTranslator/Resources/translations/jsmessages.pl.yml similarity index 100% rename from src/Macopedia/Translator/Resources/translations/jsmessages.pl.yml rename to src/Macopedia/OpenAiTranslator/Resources/translations/jsmessages.pl.yml diff --git a/src/Macopedia/Translator/Resources/translations/messages.en.yml b/src/Macopedia/OpenAiTranslator/Resources/translations/messages.en.yml similarity index 100% rename from src/Macopedia/Translator/Resources/translations/messages.en.yml rename to src/Macopedia/OpenAiTranslator/Resources/translations/messages.en.yml diff --git a/src/Macopedia/Translator/Resources/translations/messages.fr.yml b/src/Macopedia/OpenAiTranslator/Resources/translations/messages.fr.yml similarity index 100% rename from src/Macopedia/Translator/Resources/translations/messages.fr.yml rename to src/Macopedia/OpenAiTranslator/Resources/translations/messages.fr.yml diff --git a/src/Macopedia/Translator/Resources/translations/messages.pl.yml b/src/Macopedia/OpenAiTranslator/Resources/translations/messages.pl.yml similarity index 100% rename from src/Macopedia/Translator/Resources/translations/messages.pl.yml rename to src/Macopedia/OpenAiTranslator/Resources/translations/messages.pl.yml diff --git a/src/Macopedia/Translator/Service/TranslateAttributesService.php b/src/Macopedia/OpenAiTranslator/Service/TranslateAttributesService.php similarity index 79% rename from src/Macopedia/Translator/Service/TranslateAttributesService.php rename to src/Macopedia/OpenAiTranslator/Service/TranslateAttributesService.php index 292bbb3..39dd98f 100644 --- a/src/Macopedia/Translator/Service/TranslateAttributesService.php +++ b/src/Macopedia/OpenAiTranslator/Service/TranslateAttributesService.php @@ -2,16 +2,17 @@ declare(strict_types=1); -namespace Macopedia\Translator\Service; +namespace Macopedia\OpenAiTranslator\Service; use Akeneo\Pim\Enrichment\Component\Product\EntityWithFamilyVariant\CheckAttributeEditable; use Akeneo\Pim\Enrichment\Component\Product\Model\ProductInterface; use Akeneo\Pim\Enrichment\Component\Product\Model\ProductModelInterface; use Akeneo\Pim\Enrichment\Component\Product\Value\ScalarValue; +use Akeneo\Pim\Structure\Component\AttributeTypes; use Akeneo\Pim\Structure\Component\Repository\AttributeRepositoryInterface; use Akeneo\Tool\Component\StorageUtils\Updater\PropertySetterInterface; -use Macopedia\Translator\Translator\Language; -use Macopedia\Translator\Translator\TranslatorInterface; +use Macopedia\OpenAiTranslator\Translator\Language; +use Macopedia\OpenAiTranslator\Translator\TranslatorInterface; use Webmozart\Assert\Assert; class TranslateAttributesService @@ -38,7 +39,7 @@ public function translateAttributes(mixed $product, array $action): ProductInter continue; } - if (!($attribute instanceof ScalarValue)) { + if (!($attribute->getType() === AttributeTypes::TEXT || $attribute->getType() === AttributeTypes::TEXTAREA)) { continue; } @@ -57,6 +58,10 @@ public function translateAttributes(mixed $product, array $action): ProductInter $targetLocale ); + if ($translatedText === null) { + continue; + } + $this->propertySetter->setData($product, $attributeCode, $translatedText, [ 'locale' => $targetLocaleAkeneo, 'scope' => $targetScope, @@ -75,12 +80,12 @@ private function extractVariables(array $action): array Assert::keyExists($action, 'attributesToTranslate'); return [ - 'sourceScope' => $action['sourceChannel'], - 'targetScope' => $action['targetChannel'], - 'sourceLocaleAkeneo' => $action['sourceLocale'], - 'targetLocaleAkeneo' => $action['targetLocale'], - 'targetLocale' => Language::fromCode(explode('_', $action['targetLocale'])[0]), - 'attributesToTranslate' => $action['attributesToTranslate'] + $action['sourceChannel'], + $action['targetChannel'], + $action['sourceLocale'], + $action['targetLocale'], + Language::fromCode(explode('_', $action['targetLocale'])[0]), + $action['attributesToTranslate'] ]; } } diff --git a/src/Macopedia/Translator/Translator/Language.php b/src/Macopedia/OpenAiTranslator/Translator/Language.php similarity index 99% rename from src/Macopedia/Translator/Translator/Language.php rename to src/Macopedia/OpenAiTranslator/Translator/Language.php index 9bf3fd9..6120482 100644 --- a/src/Macopedia/Translator/Translator/Language.php +++ b/src/Macopedia/OpenAiTranslator/Translator/Language.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Macopedia\Translator\Translator; +namespace Macopedia\OpenAiTranslator\Translator; use Webmozart\Assert\Assert; diff --git a/src/Macopedia/OpenAiTranslator/Translator/OpenAiTranslator.php b/src/Macopedia/OpenAiTranslator/Translator/OpenAiTranslator.php new file mode 100644 index 0000000..b6c9c04 --- /dev/null +++ b/src/Macopedia/OpenAiTranslator/Translator/OpenAiTranslator.php @@ -0,0 +1,24 @@ +openAiClient + ->ask('user', sprintf(self::MESSAGE, $targetLanguageCode->asText()) . $text); + } +} diff --git a/src/Macopedia/Translator/Translator/TranslatorInterface.php b/src/Macopedia/OpenAiTranslator/Translator/TranslatorInterface.php similarity index 65% rename from src/Macopedia/Translator/Translator/TranslatorInterface.php rename to src/Macopedia/OpenAiTranslator/Translator/TranslatorInterface.php index c4aaebc..5d1218f 100644 --- a/src/Macopedia/Translator/Translator/TranslatorInterface.php +++ b/src/Macopedia/OpenAiTranslator/Translator/TranslatorInterface.php @@ -2,9 +2,9 @@ declare(strict_types=1); -namespace Macopedia\Translator\Translator; +namespace Macopedia\OpenAiTranslator\Translator; interface TranslatorInterface { - public function translate(string $text, Language $targetLanguageCode): string; + public function translate(string $text, Language $targetLanguageCode): ?string; } diff --git a/src/Macopedia/Translator/Client/OpenAiClient/Response.php b/src/Macopedia/Translator/Client/OpenAiClient/Response.php deleted file mode 100644 index b089f20..0000000 --- a/src/Macopedia/Translator/Client/OpenAiClient/Response.php +++ /dev/null @@ -1,59 +0,0 @@ -setTimestamp($data['created']), - $data['model'], - new Choices($data['choices']), - $data['usage'] - ); - } - - /** - * @return Choices - */ - public function getChoices(): Choices - { - return $this->choices; - } - - public function getFirstChoiceMessage(): ?string - { - $firstMessage = $this->choices->getAnswers()[0] ?? null; - - if ($firstMessage === null) { - return null; - } - return $firstMessage->getMessage()->getContent(); - } -} diff --git a/src/Macopedia/Translator/Translator/OpenAiTranslator.php b/src/Macopedia/Translator/Translator/OpenAiTranslator.php deleted file mode 100644 index 7442515..0000000 --- a/src/Macopedia/Translator/Translator/OpenAiTranslator.php +++ /dev/null @@ -1,33 +0,0 @@ -openAiClient - ->ask('user', sprintf(self::MESSAGE, $targetLanguageCode->asText()) . $text) - ->getFirstChoiceMessage(); - - if ($answer === null || str_contains($answer, self::NO_TRANSLATION)) { - return ''; - } - - return $answer; - } -} diff --git a/tests/Translator/LanguageTest.php b/tests/Translator/LanguageTest.php index 49ed4ca..0e51b32 100644 --- a/tests/Translator/LanguageTest.php +++ b/tests/Translator/LanguageTest.php @@ -1,9 +1,11 @@