Skip to content

Commit

Permalink
Static Code Analysis, Refactor, Optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
drejmanMacopedia committed May 8, 2023
1 parent 45a6855 commit 2c015bb
Show file tree
Hide file tree
Showing 34 changed files with 190 additions and 153 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Akeneo OpenAI Translator
Akeneo OpenAi Translator
=====================================


Expand All @@ -16,7 +16,7 @@ register bundle in `config/bundles.php`

```php
return [
'Macopedia\Translator\MacopediaTranslatorBundle' => ['all' => true]
'Macopedia\OpenAiTranslator\MacopediaTranslatorBundle' => ['all' => true]
];
```

Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand All @@ -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) {
Expand All @@ -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();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Macopedia\Translator\Client\OpenAiClient;
namespace Macopedia\OpenAiTranslator\Client\OpenAiClient;

class Choice
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Macopedia\Translator\Client\OpenAiClient;
namespace Macopedia\OpenAiTranslator\Client\OpenAiClient;

use Webmozart\Assert\Assert;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Macopedia\Translator\Client\OpenAiClient;
namespace Macopedia\OpenAiTranslator\Client\OpenAiClient;

class Message
{
Expand Down
40 changes: 40 additions & 0 deletions src/Macopedia/OpenAiTranslator/Client/OpenAiClient/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Macopedia\OpenAiTranslator\Client\OpenAiClient;

use Macopedia\OpenAiTranslator\Client\OpenAiClient\Response\ChoicesResponse;
use Macopedia\OpenAiTranslator\Client\OpenAiClient\Response\ErrorResponse;
use Webmozart\Assert\Assert;
use DateTime;
use RuntimeException;

class Response
{
public static function fromArray(array $data): self
{
if (array_key_exists('id', $data)) {
Assert::keyExists($data, 'object');
Assert::keyExists($data, 'created');
Assert::keyExists($data, 'model');
Assert::keyExists($data, 'choices');
Assert::keyExists($data, 'usage');

return new ChoicesResponse(
$data['id'],
$data['object'],
(new DateTime())->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');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Macopedia\OpenAiTranslator\Client\OpenAiClient\Response;

use Macopedia\OpenAiTranslator\Client\OpenAiClient\Choices;
use Macopedia\OpenAiTranslator\Client\OpenAiClient\Response;
use DateTime;

class ChoicesResponse extends Response
{
public function __construct(
private string $id,
private string $object,
private DateTime $created,
private string $model,
private Choices $choices,
array $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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Macopedia\OpenAiTranslator\Client\OpenAiClient\Response;

use Macopedia\OpenAiTranslator\Client\OpenAiClient\Response;

class ErrorResponse extends Response
{
public function __construct(
private string $error
) {
}

public function getError(): string
{
return $this->error;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Macopedia\Translator\DependencyInjection;
namespace Macopedia\OpenAiTranslator\DependencyInjection;

use Exception;
use Symfony\Component\Config\FileLocator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Macopedia\Translator\Exception;
namespace Macopedia\OpenAiTranslator\Exception;

use Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Macopedia\Translator;
namespace Macopedia\OpenAiTranslator;

use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
Macopedia\Translator\Connector\Processor\MassEdit\TranslateAttributesProcessor:
Macopedia\OpenAiTranslator\Connector\Processor\MassEdit\TranslateAttributesProcessor:
arguments:
- '@Macopedia\Translator\Service\TranslateAttributesService'
- '@Macopedia\OpenAiTranslator\Service\TranslateAttributesService'

macopedia.job.update_product_translations:
class: '%pim_connector.job.simple_job.class%'
Expand Down Expand Up @@ -34,6 +34,6 @@ services:
- '@event_dispatcher'
- '@akeneo_batch.job_repository'
- '@pim_enrich.reader.database.product_and_product_model'
- '@Macopedia\Translator\Connector\Processor\MassEdit\TranslateAttributesProcessor'
- '@Macopedia\OpenAiTranslator\Connector\Processor\MassEdit\TranslateAttributesProcessor'
- '@pim_enrich.writer.database.product_and_product_model_writer'
- '%pim_job_product_batch_size%'
19 changes: 19 additions & 0 deletions src/Macopedia/OpenAiTranslator/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
parameters:
open_ai_key: '%env(default::OPEN_AI_KEY)%'
services:
Macopedia\OpenAiTranslator\Client\OpenAiClient:
arguments:
- '@logger'
- 'gpt-3.5-turbo'
- '%open_ai_key%'

Macopedia\OpenAiTranslator\Translator\OpenAiTranslator:
arguments:
- '@Macopedia\OpenAiTranslator\Client\OpenAiClient'

Macopedia\OpenAiTranslator\Service\TranslateAttributesService:
arguments:
- '@Macopedia\OpenAiTranslator\Translator\OpenAiTranslator'
- '@pim_catalog.repository.attribute'
- '@pim_catalog.entity_with_family_variant.check_attribute_editable'
- '@pim_catalog.updater.property_setter'
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -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,
Expand All @@ -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']
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Macopedia\Translator\Translator;
namespace Macopedia\OpenAiTranslator\Translator;

use Webmozart\Assert\Assert;

Expand Down
24 changes: 24 additions & 0 deletions src/Macopedia/OpenAiTranslator/Translator/OpenAiTranslator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Macopedia\OpenAiTranslator\Translator;

use Macopedia\OpenAiTranslator\Client\OpenAiClient;

class OpenAiTranslator implements TranslatorInterface
{
private const MESSAGE = 'Translate the text after a semicolon to %s;';

public function __construct(
private OpenAiClient $openAiClient
) {
}

public function translate(string $text, Language $targetLanguageCode): ?string
{
return $this
->openAiClient
->ask('user', sprintf(self::MESSAGE, $targetLanguageCode->asText()) . $text);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading

0 comments on commit 2c015bb

Please sign in to comment.