Files
embedd-hub/app/Concerns/InteractsWithEmbeddingWorkbench.php

97 lines
2.8 KiB
PHP

<?php
namespace App\Concerns;
use App\Models\EmbeddingEntry;
use App\Services\EmbeddingWorkbench;
use Illuminate\Support\Facades\Schema;
trait InteractsWithEmbeddingWorkbench
{
public string $embeddingModel = '';
/** @var array<int, array{name: string, dimensions: int}> */
public array $modelOptions = [];
public string $providerSummary = '';
public int $storedCount = 0;
public bool $databaseReady = true;
public bool $ollamaAvailable = false;
public string $ollamaStatusMessage = '';
public string $ollamaUrl = '';
public function refreshOllama(EmbeddingWorkbench $workbench): void
{
$this->initializeWorkbench($workbench);
}
protected function initializeWorkbench(EmbeddingWorkbench $workbench): void
{
$status = $workbench->ollamaStatus();
$this->ollamaAvailable = $status['available'];
$this->ollamaStatusMessage = $status['message'];
$this->ollamaUrl = $status['url'];
$this->modelOptions = $workbench->modelOptions();
if (! $this->ollamaAvailable) {
$this->embeddingModel = '';
} elseif ($this->embeddingModel === '' || ! collect($this->modelOptions)->pluck('name')->contains($this->embeddingModel)) {
$this->embeddingModel = $workbench->modelName();
}
$this->refreshSummary($workbench);
}
protected function ensureWorkbenchAvailable(EmbeddingWorkbench $workbench, string $databaseErrorField): bool
{
$this->initializeWorkbench($workbench);
if (! $this->ollamaAvailable) {
$this->addError('embeddingModel', $this->ollamaStatusMessage);
}
if (! $this->databaseReady) {
$this->addError($databaseErrorField, __('The embedding table is not ready.'));
}
return $this->ollamaAvailable && $this->databaseReady && $this->embeddingModel !== '';
}
protected function refreshSummary(EmbeddingWorkbench $workbench): void
{
$provider = $workbench->providerName();
$this->databaseReady = Schema::hasTable('embedding_entries');
if (! $this->ollamaAvailable || $this->embeddingModel === '') {
$this->providerSummary = "{$provider} / unavailable";
$this->storedCount = 0;
return;
}
$model = $workbench->modelName($this->embeddingModel);
$dimensions = $workbench->dimensions($model);
$this->providerSummary = "{$provider} / {$model} / {$dimensions}d";
if (! $this->databaseReady) {
$this->storedCount = 0;
return;
}
$this->storedCount = EmbeddingEntry::query()
->where('provider', $provider)
->where('model', $model)
->where('embedding_dimensions', $dimensions)
->count();
}
}