Skip to content

Commit

Permalink
add product description length configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
boxblinkracer committed Jun 6, 2023
1 parent 928da81 commit 65e679c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.

### Added

- Added new option to define the approximate length of product descriptions.
- Add new plugin configuration hint for OpenAI key that it starts with **sk_**. (thx @jissereitsma)
- Added better error output for OpenAI requests. Sometimes the message is empty, but the error code can still be displayed. (thx @jissereitsma)

Expand Down
9 changes: 8 additions & 1 deletion src/Command/ProductGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$withImages = $input->getOption('images');
$imgSize = $input->getOption('images-size');

$descriptionLength = $this->configService->getProductDescriptionLength();

if ($keyWords === null) {
throw new \Exception('No AI keywords given. Please specify what you want to be generated by providing the --keywords option.');
}
Expand Down Expand Up @@ -123,6 +125,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->io->note('Images will not be generated for the products.');
}


$this->io->note('Product description length: ' . $descriptionLength . ' characters');


# -----------------------------------------------------------------------------------------------------------------------

# configure our generator
Expand All @@ -136,7 +142,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->productGenerator->generate(
$keyWords,
$count,
$category
$category,
$descriptionLength
);

if ($this->errorCount <= 0) {
Expand Down
7 changes: 7 additions & 0 deletions src/Resources/config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
<helpText>If enabled product images will be automatically generated if no CLI argument is provided.</helpText>
</input-field>

<input-field type="int">
<name>productDescriptionLength</name>
<label>Description Length</label>
<defaultValue>400</defaultValue>
<helpText>The approximate target length of a product description text.</helpText>
</input-field>

<input-field type="single-select">
<name>productImageSize</name>
<label>Size (px)</label>
Expand Down
19 changes: 19 additions & 0 deletions src/Service/Config/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class ConfigService
*/
private $mediaImageSize;

/**
* @var int
*/
private $productDescriptionLength;


/**
* @param SystemConfigService $configService
Expand All @@ -39,6 +44,8 @@ public function __construct(SystemConfigService $configService)
$this->productImageSize = $configService->getString('AIDemoData.config.productImageSize');

$this->mediaImageSize = $configService->getString('AIDemoData.config.mediaImageSize');

$this->productDescriptionLength = $configService->getInt('AIDemoData.config.productDescriptionLength');
}

/**
Expand Down Expand Up @@ -69,6 +76,18 @@ public function getProductImageSize(): string
return $this->productImageSize;
}

/**
* @return int
*/
public function getProductDescriptionLength(): int
{
if (empty($this->productDescriptionLength)) {
return 400;
}

return (int)$this->productDescriptionLength;
}

/**
* @return string
*/
Expand Down
7 changes: 4 additions & 3 deletions src/Service/Generator/ProductGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ public function setGenerateImages(bool $generateImages, string $imageSize): void
* @param string $keywords
* @param int $maxCount
* @param string $category
* @throws \Exception
* @param int $descriptionLength
* @throws \JsonException
* @return void
*/
public function generate(string $keywords, int $maxCount, string $category)
public function generate(string $keywords, int $maxCount, string $category, int $descriptionLength)
{
$prompt = 'Create a list of demo products with these properties, separated values with ";". Only write down values and no property names ' . PHP_EOL;
$prompt .= PHP_EOL;
Expand All @@ -127,7 +128,7 @@ public function generate(string $keywords, int $maxCount, string $category)
$prompt .= 'product count.' . PHP_EOL;
$prompt .= 'product number code. should be 16 unique random alphanumeric.' . PHP_EOL;
$prompt .= 'name of the product.' . PHP_EOL;
$prompt .= 'description (about 400 characters).' . PHP_EOL;
$prompt .= 'description (about ' . $descriptionLength . ' characters).' . PHP_EOL;
$prompt .= 'price value (no currency just number).' . PHP_EOL;
$prompt .= 'EAN code.' . PHP_EOL;
$prompt .= 'SEO description (max 100 characters).' . PHP_EOL;
Expand Down

0 comments on commit 65e679c

Please sign in to comment.