82 lines
2.2 KiB
PHP
82 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace KupShop\LLMBundle\TextObjects;
|
|
|
|
use KupShop\LLMBundle\Dto\TextPromptInput;
|
|
use KupShop\LLMBundle\Enum\PromptPlaceholder;
|
|
use KupShop\LLMBundle\Enum\SystemPromptExtension;
|
|
use KupShop\LLMBundle\Util\PlaceholdersCreator;
|
|
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
|
|
#[AutoconfigureTag('kupshop.llm.textObject')]
|
|
abstract class AbstractTextObject
|
|
{
|
|
public const LABEL = '_override';
|
|
protected const DESCRIPTION = '_override';
|
|
|
|
/**
|
|
* @var SystemPromptExtension[] Allows to extend system prompt by additional instructions
|
|
*/
|
|
protected array $SYSTEM_PROMPT_EXTENSIONS = [];
|
|
|
|
/**
|
|
* @var string[] Prompts from specified textObjects will be also shown for this object
|
|
*/
|
|
protected array $INCLUDE_PROMPTS_FROM_OBJECTS = [];
|
|
|
|
#[Required]
|
|
public PlaceholdersCreator $placeholdersCreator;
|
|
|
|
/**
|
|
* @return PromptPlaceholder[]
|
|
*/
|
|
public function providesPlaceholders(): array
|
|
{
|
|
return [PromptPlaceholder::TEXT];
|
|
}
|
|
|
|
final public static function getLabel(): string
|
|
{
|
|
return static::LABEL;
|
|
}
|
|
|
|
public function getDescription(): string
|
|
{
|
|
return static::DESCRIPTION;
|
|
}
|
|
|
|
final public function modifyPrompt(TextPromptInput $prompt, ?string $entityType, ?int $entityId): void
|
|
{
|
|
$this->setPromptPlaceholders($prompt, $entityType, $entityId);
|
|
|
|
$extensions = array_map(fn ($val) => $val->value, $this->getSystemPromptExtensions($entityType));
|
|
$prompt->replacePlaceholder(PromptPlaceholder::TEXT_OBJECT_EXTENSIONS, $extensions ? implode(PHP_EOL, $extensions) : '');
|
|
}
|
|
|
|
protected function setPromptPlaceholders(TextPromptInput $prompt, ?string $entityType, ?int $entityId): void
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @return SystemPromptExtension[]
|
|
*/
|
|
protected function getSystemPromptExtensions(?string $entityType): array
|
|
{
|
|
return $this->SYSTEM_PROMPT_EXTENSIONS;
|
|
}
|
|
|
|
/**
|
|
* @return string[]
|
|
*/
|
|
public function getIncludedPromptsObjects(): array
|
|
{
|
|
return $this->INCLUDE_PROMPTS_FROM_OBJECTS;
|
|
}
|
|
|
|
public function isEnabled(): bool
|
|
{
|
|
return true;
|
|
}
|
|
}
|