improve interacting with ollama api and models

This commit is contained in:
2026-05-10 17:40:24 +03:30
parent b89ae98524
commit 4c910cb8e8
11 changed files with 265 additions and 225 deletions

View File

@@ -1,25 +1,22 @@
<?php
use App\Actions\Embeddings\RecordEmbeddingUsageEvent;
use App\Concerns\InteractsWithEmbeddingWorkbench;
use App\Models\EmbeddingEntry;
use App\Services\EmbeddingVector;
use App\Services\EmbeddingWorkbench;
use Flux\Flux;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Schema;
use Livewire\Attributes\Title;
use Livewire\Component;
new #[Title('Embedding comparison')] class extends Component
{
use InteractsWithEmbeddingWorkbench;
/** @var array<int, string> */
public array $comparisonPhrases = ['', '', ''];
public string $embeddingModel = '';
/** @var array<int, array{name: string, dimensions: int, default: bool}> */
public array $modelOptions = [];
/** @var array<int, array<string, mixed>> */
public array $comparisonEntries = [];
@@ -32,12 +29,6 @@ new #[Title('Embedding comparison')] class extends Component
/** @var array<string, mixed>|null */
public ?array $closestPair = null;
public string $providerSummary = '';
public int $storedCount = 0;
public bool $databaseReady = true;
public function mount(EmbeddingWorkbench $workbench): void
{
$this->initializeWorkbench($workbench);
@@ -45,6 +36,10 @@ new #[Title('Embedding comparison')] class extends Component
public function comparePhrases(EmbeddingWorkbench $workbench, RecordEmbeddingUsageEvent $recordUsageEvent): void
{
if (! $this->ensureWorkbenchAvailable($workbench, 'comparisonPhrases')) {
return;
}
$this->validate([
'comparisonPhrases' => ['required', 'array', 'min:2', 'max:12'],
'comparisonPhrases.*' => ['nullable', 'string', 'max:1000'],
@@ -59,12 +54,6 @@ new #[Title('Embedding comparison')] class extends Component
return;
}
if (! $this->databaseReady) {
$this->addError('comparisonPhrases', __('The embedding table is not ready.'));
return;
}
try {
$comparison = $workbench->compare($phrases, $this->embeddingModel);
@@ -125,36 +114,6 @@ new #[Title('Embedding comparison')] class extends Component
$this->resetComparisonResults();
}
protected function initializeWorkbench(EmbeddingWorkbench $workbench): void
{
$this->modelOptions = $workbench->modelOptions();
$this->embeddingModel = $this->embeddingModel ?: $workbench->modelName();
$this->refreshSummary($workbench);
}
protected function refreshSummary(EmbeddingWorkbench $workbench): void
{
$provider = $workbench->providerName();
$model = $workbench->modelName($this->embeddingModel);
$dimensions = $workbench->dimensions($model);
$this->providerSummary = "{$provider} / {$model} / {$dimensions}d";
$this->databaseReady = Schema::hasTable('embedding_entries');
if (! $this->databaseReady) {
$this->storedCount = 0;
return;
}
$this->storedCount = EmbeddingEntry::query()
->where('provider', $provider)
->where('model', $model)
->where('embedding_dimensions', $dimensions)
->count();
}
/**
* @return list<string>
*/
@@ -230,7 +189,7 @@ new #[Title('Embedding comparison')] class extends Component
@endif
</div>
<flux:button type="submit" variant="primary" icon="arrows-right-left" :disabled="! $databaseReady" class="data-loading:opacity-60">
<flux:button type="submit" variant="primary" icon="arrows-right-left" :disabled="! $databaseReady || ! $ollamaAvailable" class="data-loading:opacity-60">
<span wire:loading.remove wire:target="comparePhrases">{{ __('Compare') }}</span>
<span wire:loading wire:target="comparePhrases">{{ __('Comparing') }}</span>
</flux:button>