improve interacting with ollama api and models
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user