mirror of
https://github.com/BookStackApp/BookStack.git
synced 2026-02-25 19:07:25 +03:00
Allowing the vector query results and the LLM response to each come back over the same HTTP request at two different times via a somewhat standard. Uses a package for JS SSE client, since native browser client does not support over POST, which is probably important for this endpoint as we don't want crawlers or other bots abusing this via accidentally.
54 lines
1.5 KiB
PHP
54 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace BookStack\Search\Queries;
|
|
|
|
use BookStack\Http\Controller;
|
|
use BookStack\Search\SearchOptions;
|
|
use BookStack\Search\SearchRunner;
|
|
use Illuminate\Http\Request;
|
|
|
|
class QueryController extends Controller
|
|
{
|
|
public function __construct(
|
|
protected SearchRunner $searchRunner,
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* Show the view to start a vector/LLM-based query search.
|
|
*/
|
|
public function show(Request $request)
|
|
{
|
|
// TODO - Validate if query system is active
|
|
$query = $request->get('ask', '');
|
|
|
|
// TODO - Placeholder
|
|
$entities = $this->searchRunner->searchEntities(SearchOptions::fromString("cat"), 'all', 1, 20)['results'];
|
|
|
|
// TODO - Set page title
|
|
|
|
return view('search.query', [
|
|
'query' => $query,
|
|
'entities' => $entities,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Perform a vector/LLM-based query search.
|
|
*/
|
|
public function run(Request $request, VectorSearchRunner $searchRunner, LlmQueryRunner $llmRunner)
|
|
{
|
|
// TODO - Validate if query system is active
|
|
$query = $request->get('query', '');
|
|
|
|
return response()->eventStream(function () use ($query, $searchRunner, $llmRunner) {
|
|
$results = $query ? $searchRunner->run($query) : [];
|
|
|
|
$count = count($results);
|
|
yield "Found {$count} results for query: {$query}!";
|
|
$llmResult = $llmRunner->run($query, $results);
|
|
yield "LLM result: {$llmResult}";
|
|
});
|
|
}
|
|
}
|