*/ public array $modelOptions = []; /** @var array> */ public array $similarityResults = []; public string $providerSummary = ''; public int $storedCount = 0; public bool $databaseReady = true; public function mount(EmbeddingWorkbench $workbench): void { $this->initializeWorkbench($workbench); } public function searchSimilar(EmbeddingWorkbench $workbench, RecordEmbeddingUsageEvent $recordUsageEvent): void { $this->validate([ 'similarityQuery' => ['required', 'string', 'max:1000'], 'similarityLimit' => ['required', 'integer', 'min:1', 'max:50'], 'minimumSimilarity' => ['required', 'numeric', 'min:0', 'max:1'], '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) ->map(function (array $result) use ($workbench): array { return [ ...$workbench->presentEntry($result['entry']), 'similarity' => $result['similarity'], 'distance' => $result['distance'], ]; }) ->all(); $recordUsageEvent->handle( user: auth()->user(), workbench: $workbench, selectedModel: $this->embeddingModel, tool: 'search', inputCount: 1, resultCount: count($this->similarityResults), ); $this->refreshSummary($workbench); } 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' => __('Similarity search')])
{{ __('Query') }}
{{ __('Search') }} {{ __('Searching') }}
@if ($similarityResults !== [])
@foreach ($similarityResults as $result) @endforeach
{{ __('Phrase') }} {{ __('Similarity') }} {{ __('Distance') }}
{{ $result['source_text'] }} {{ number_format($result['similarity'], 3) }} {{ number_format($result['distance'], 3) }}
@endif