77 lines
2.2 KiB
PHP
77 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\LLMBundle\Util;
|
|
|
|
use KupShop\LLMBundle\Entity\TextPrompt;
|
|
use KupShop\LLMBundle\TextObjects\AbstractTextObject;
|
|
use Query\Operator;
|
|
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;
|
|
use Symfony\Component\DependencyInjection\ServiceLocator;
|
|
|
|
class TextObjectUtil
|
|
{
|
|
public function __construct(
|
|
/** @param ServiceLocator<AbstractTextObject> $serviceLocator */
|
|
#[TaggedLocator(tag: 'kupshop.llm.textObject', defaultIndexMethod: 'getLabel')] private ServiceLocator $serviceLocator,
|
|
) {
|
|
}
|
|
|
|
public function getByLabel(string $label): AbstractTextObject
|
|
{
|
|
return $this->serviceLocator->get($label);
|
|
}
|
|
|
|
/**
|
|
* @return AbstractTextObject[]
|
|
*/
|
|
public function getAll(): array
|
|
{
|
|
return array_filter(
|
|
array_map(fn ($path) => $this->serviceLocator->get($path::getLabel()), $this->serviceLocator->getProvidedServices()),
|
|
fn ($service) => $service->isEnabled()
|
|
);
|
|
}
|
|
|
|
public function getAllLabels(): array
|
|
{
|
|
return array_map(fn ($path) => $path::getLabel(), $this->serviceLocator->getProvidedServices());
|
|
}
|
|
|
|
public function getLabelsMap(): array
|
|
{
|
|
$map = [];
|
|
|
|
/*
|
|
* @var AbstractTextObject $path
|
|
*/
|
|
foreach ($this->getAll() as $service) {
|
|
$map[$service::getLabel()] = $service->getDescription();
|
|
}
|
|
asort($map);
|
|
|
|
return $map;
|
|
}
|
|
|
|
/**
|
|
* Get all prompts assigned to the TextObject with $label.
|
|
*
|
|
* @return TextPrompt[]
|
|
*/
|
|
public function getObjectLabelPrompts(string $label): array
|
|
{
|
|
$object = $this->getByLabel($label);
|
|
$labels = array_merge([$label], $object->getIncludedPromptsObjects());
|
|
|
|
$promptRows = sqlFetchAll(sqlQueryBuilder()->select('*')->from('llm_prompts', 'lp')
|
|
->innerJoin('lp', 'llm_prompts_objects', 'lpo', 'lp.id = lpo.id_prompt')
|
|
->where(Operator::inStringArray($labels, 'lpo.object_type'))
|
|
->andWhere("lp.figure = 'Y'")
|
|
->groupBy('lp.id')
|
|
->execute(), 'id');
|
|
|
|
return array_map(fn ($row) => TextPrompt::fromArray($row), $promptRows);
|
|
}
|
|
}
|