1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-10-17 21:31:24 +03:00

Vectors: Got basic LLM querying working using vector search context

This commit is contained in:
Dan Brown
2025-03-24 19:51:48 +00:00
parent 8452099a5b
commit 0ffcb3d4aa
8 changed files with 114 additions and 2 deletions

View File

@@ -0,0 +1,33 @@
<?php
namespace BookStack\Search\Vectors;
class VectorSearchRunner
{
public function __construct(
protected VectorQueryServiceProvider $vectorQueryServiceProvider
) {
}
public function run(string $query): array
{
$queryService = $this->vectorQueryServiceProvider->get();
$queryVector = $queryService->generateEmbeddings($query);
// TODO - Apply permissions
// TODO - Join models
$topMatches = SearchVector::query()->select('text', 'entity_type', 'entity_id')
->selectRaw('VEC_DISTANCE_COSINE(VEC_FROMTEXT("[' . implode(',', $queryVector) . ']"), embedding) as distance')
->orderBy('distance', 'asc')
->limit(10)
->get();
$matchesText = array_values(array_map(fn (SearchVector $match) => $match->text, $topMatches->all()));
$llmResult = $queryService->query($query, $matchesText);
return [
'llm_result' => $llmResult,
'entity_matches' => $topMatches->toArray()
];
}
}