59 lines
1.9 KiB
PHP
59 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\LLMBundle\Util;
|
|
|
|
use KupShop\LLMBundle\Dto\TextPromptInput;
|
|
use KupShop\LLMBundle\Entity\TextPrompt;
|
|
use KupShop\LLMBundle\SystemPrompts\AbstractSystemPrompt;
|
|
use Query\Operator;
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
|
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
|
|
use Symfony\Component\DependencyInjection\ServiceLocator;
|
|
|
|
class TextPromptUtil
|
|
{
|
|
public function __construct(
|
|
/** @param ServiceLocator<AbstractSystemPrompt> $systemPromptLocator */
|
|
#[TaggedLocator(tag: 'kupshop.llm.systemPrompt', defaultIndexMethod: 'getLabel')] private readonly ServiceLocator $systemPromptLocator,
|
|
#[Autowire('%kupshop.llm.prompts%')] protected array $llmPrompts,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* @return AbstractSystemPrompt[]
|
|
*/
|
|
public function getAllSystemPrompts(): iterable
|
|
{
|
|
foreach ($this->systemPromptLocator->getProvidedServices() as $service) {
|
|
yield $this->systemPromptLocator->get($service::getLabel());
|
|
}
|
|
}
|
|
|
|
public function getPromptEntityById(int $id): TextPrompt
|
|
{
|
|
$promptRow = sqlQueryBuilder()->select('*')->from('llm_prompts')
|
|
->where(Operator::equals(['id' => $id]))
|
|
->execute()->fetchAssociative();
|
|
|
|
return TextPrompt::fromArray($promptRow);
|
|
}
|
|
|
|
public function getSystemPromptText(): string
|
|
{
|
|
$settings = \Settings::getDefault();
|
|
$userSystemPrompt = $settings['llm']['system_prompt'] ?? null ?: $this->llmPrompts['defaultUserSystem'];
|
|
|
|
return str_replace('{USER_SYSTEM_PROMPT}', $userSystemPrompt, $this->llmPrompts['wpjSystem']);
|
|
}
|
|
|
|
public function getTextPromptInputById(int $id): TextPromptInput
|
|
{
|
|
$entity = $this->getPromptEntityById($id);
|
|
$input = TextPromptInput::createFromEntity($entity);
|
|
|
|
return $input->setSystemText($this->getSystemPromptText());
|
|
}
|
|
}
|