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,34 +1,24 @@
<?php
use App\Actions\Embeddings\RecordEmbeddingUsageEvent;
use App\Models\EmbeddingEntry;
use App\Concerns\InteractsWithEmbeddingWorkbench;
use App\Services\EmbeddingWorkbench;
use Illuminate\Support\Facades\Schema;
use Livewire\Attributes\Title;
use Livewire\Component;
new #[Title('Embedding search')] class extends Component
{
use InteractsWithEmbeddingWorkbench;
public string $similarityQuery = '';
public int $similarityLimit = 10;
public string $minimumSimilarity = '0.30';
public string $embeddingModel = '';
/** @var array<int, array{name: string, dimensions: int, default: bool}> */
public array $modelOptions = [];
/** @var array<int, array<string, mixed>> */
public array $similarityResults = [];
public string $providerSummary = '';
public int $storedCount = 0;
public bool $databaseReady = true;
public function mount(EmbeddingWorkbench $workbench): void
{
$this->initializeWorkbench($workbench);
@@ -36,6 +26,10 @@ new #[Title('Embedding search')] class extends Component
public function searchSimilar(EmbeddingWorkbench $workbench, RecordEmbeddingUsageEvent $recordUsageEvent): void
{
if (! $this->ensureWorkbenchAvailable($workbench, 'similarityQuery')) {
return;
}
$this->validate([
'similarityQuery' => ['required', 'string', 'max:1000'],
'similarityLimit' => ['required', 'integer', 'min:1', 'max:50'],
@@ -43,12 +37,6 @@ new #[Title('Embedding search')] class extends Component
'embeddingModel' => ['required', 'string'],
]);
if (! $this->databaseReady) {
$this->addError('similarityQuery', __('The embedding table is not ready.'));
return;
}
try {
$this->similarityResults = $workbench
->search($this->similarityQuery, $this->similarityLimit, (float) $this->minimumSimilarity, $this->embeddingModel)
@@ -75,37 +63,6 @@ new #[Title('Embedding search')] class extends Component
$this->addError('embeddingModel', $exception->getMessage());
}
}
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();
}
};
?>
@@ -126,7 +83,7 @@ new #[Title('Embedding search')] class extends Component
</div>
<div>
<flux:button type="submit" variant="primary" icon="magnifying-glass" :disabled="! $databaseReady" class="data-loading:opacity-60">
<flux:button type="submit" variant="primary" icon="magnifying-glass" :disabled="! $databaseReady || ! $ollamaAvailable" class="data-loading:opacity-60">
<span wire:loading.remove wire:target="searchSimilar">{{ __('Search') }}</span>
<span wire:loading wire:target="searchSimilar">{{ __('Searching') }}</span>
</flux:button>