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 df4f7bd
Show file tree
Hide file tree
Showing 33 changed files with 169 additions and 132 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,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:
Expand Down
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 df4f7bd

Please sign in to comment.