*/ public array $modelOptions = []; /** @var array> */ public array $embeddingResults = []; public string $providerSummary = ''; public int $storedCount = 0; public bool $databaseReady = true; public function mount(EmbeddingWorkbench $workbench): void { $this->initializeWorkbench($workbench); } public function generateEmbeddings(EmbeddingWorkbench $workbench, RecordEmbeddingUsageEvent $recordUsageEvent): void { $this->validate([ 'embeddingInput' => ['required', 'string', 'max:12000'], 'embeddingModel' => ['required', 'string'], ]); $phrases = EmbeddingVector::phrasesFromText($this->embeddingInput); if ($phrases === []) { $this->addError('embeddingInput', __('Enter at least one phrase.')); return; } if (! $this->databaseReady) { $this->addError('embeddingInput', __('The embedding table is not ready.')); return; } try { $entries = $workbench->embed($phrases, $this->embeddingModel); $tokens = (int) $entries->sum(fn (EmbeddingEntry $entry): int => (int) ($entry->tokens ?? 0)); $this->embeddingResults = $entries ->map(fn (EmbeddingEntry $entry): array => $workbench->presentEntry($entry)) ->all(); $recordUsageEvent->handle( user: auth()->user(), workbench: $workbench, selectedModel: $this->embeddingModel, tool: 'embed', inputCount: count($phrases), resultCount: $entries->count(), tokens: $tokens > 0 ? $tokens : null, ); $this->refreshSummary($workbench); Flux::toast(variant: 'success', text: __('Embeddings generated.')); } catch (\Throwable $exception) { $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(); } }; ?>
@include('pages.embeddings.partials.navigation', ['heading' => __('Embed')])
{{ __('Phrases') }}
{{ __('Generate') }} {{ __('Generating') }}
@if ($embeddingResults !== [])
@foreach ($embeddingResults as $result) @endforeach
{{ __('Phrase') }} {{ __('Vector') }} {{ __('Meta') }}
{{ $result['source_text'] }} [{{ implode(', ', $result['vector_preview']) }}...]
{{ __('Full vector') }}
{{ $result['vector_json'] }}
{{ $result['dimensions'] }}d
{{ $result['provider'] }} / {{ $result['model'] }}
@endif