50 lines
1.5 KiB
PHP
50 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\ContentManagement;
|
|
|
|
use App\Domain\AI\AIClient;
|
|
use App\Domain\Model\AI\Embedding;
|
|
use App\Domain\Model\Brand;
|
|
use App\Domain\Model\CarProperty;
|
|
use App\Domain\Model\CarRevision;
|
|
use App\Domain\Model\Persistence\PersistedEmbedding;
|
|
use App\Domain\Model\Value\EmbeddingId;
|
|
use App\Domain\Repository\EmbeddingRepository;
|
|
use Stringable;
|
|
|
|
class CarPropertyEmbedder
|
|
{
|
|
public function __construct(
|
|
private readonly AIClient $aiClient,
|
|
private readonly EmbeddingRepository $embeddingRepository,
|
|
) {
|
|
}
|
|
|
|
public function createPersistedEmbedding(CarProperty $carProperty, ?CarRevision $carRevision, ?Brand $brand): ?Embedding
|
|
{
|
|
if (!($carProperty->value instanceof Stringable)) {
|
|
return null;
|
|
}
|
|
|
|
$text = $carProperty->type->humanReadable() . ': ' . (string) $carProperty->value;
|
|
if ($carRevision !== null) {
|
|
$text .= ' - ' . $carRevision->name;
|
|
}
|
|
if ($brand !== null) {
|
|
$text .= ', ' . $brand->name;
|
|
}
|
|
|
|
$persistedEmbedding = $this->embeddingRepository->findByPhrase($text);
|
|
if ($persistedEmbedding) {
|
|
return $persistedEmbedding;
|
|
}
|
|
|
|
$largeEmbedding = $this->aiClient->embedTextLarge($text);
|
|
$smallEmbedding = $this->aiClient->embedTextSmall($text);
|
|
|
|
$embedding = new Embedding(EmbeddingId::generate(), $text, $largeEmbedding, $smallEmbedding);
|
|
$this->embeddingRepository->save($embedding);
|
|
|
|
return $embedding;
|
|
}
|
|
} |