63 lines
1.9 KiB
PHP
63 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Exceptions\OllamaUnavailableException;
|
|
use App\Models\EmbeddingCache;
|
|
use App\Services\Llm\LlmClientInterface;
|
|
|
|
class EmbeddingService
|
|
{
|
|
public function __construct(
|
|
private readonly LlmClientInterface $llmClient,
|
|
private readonly AppSettingsService $settings,
|
|
) {}
|
|
|
|
public function embed(string $text): array
|
|
{
|
|
$hash = hash('sha256', $text);
|
|
$context = $this->context();
|
|
$cached = EmbeddingCache::query()
|
|
->where('provider_instance_id', $context['provider_instance_id'])
|
|
->where('embedding_model', $context['embedding_model'])
|
|
->where('text_hash', $hash)
|
|
->first();
|
|
|
|
if ($cached !== null) {
|
|
return $cached->embedding;
|
|
}
|
|
|
|
$embedding = $this->llmClient->embed($text);
|
|
if ($embedding === []) {
|
|
throw new OllamaUnavailableException('llm', 'embedding', 'LLM embedding response did not include a valid embedding');
|
|
}
|
|
|
|
EmbeddingCache::query()->updateOrCreate(
|
|
[
|
|
'provider_instance_id' => $context['provider_instance_id'],
|
|
'embedding_model' => $context['embedding_model'],
|
|
'text_hash' => $hash,
|
|
],
|
|
['text' => $text, 'embedding' => $embedding]
|
|
);
|
|
|
|
return $embedding;
|
|
}
|
|
|
|
public function context(): array
|
|
{
|
|
$instance = $this->settings->activeProviderInstance();
|
|
$instanceId = (string) ($instance['id'] ?? $this->settings->activeProviderInstanceId());
|
|
$model = trim((string) $this->settings->get('llm.models.embedding', ''));
|
|
|
|
if ($model === '') {
|
|
$model = (string) ($instance['embedding_model'] ?? '');
|
|
}
|
|
|
|
return [
|
|
'provider_instance_id' => $instanceId,
|
|
'embedding_model' => $model,
|
|
];
|
|
}
|
|
}
|