3 namespace BookStack\Search\Vectors;
5 class VectorSearchRunner
7 public function __construct(
8 protected VectorQueryServiceProvider $vectorQueryServiceProvider
12 public function run(string $query): array
14 $queryService = $this->vectorQueryServiceProvider->get();
15 $queryVector = $queryService->generateEmbeddings($query);
17 // TODO - Apply permissions
19 $topMatches = SearchVector::query()->select('text', 'entity_type', 'entity_id')
20 ->selectRaw('VEC_DISTANCE_COSINE(VEC_FROMTEXT("[' . implode(',', $queryVector) . ']"), embedding) as distance')
21 ->orderBy('distance', 'asc')
25 $matchesText = array_values(array_map(fn (SearchVector $match) => $match->text, $topMatches->all()));
26 $llmResult = $queryService->query($query, $matchesText);
29 'llm_result' => $llmResult,
30 'entity_matches' => $topMatches->toArray()