- RagSearchService: docs 폴더 키워드 검색 + Gemini API 컨텍스트 기반 답변 - RagSearchController: 검색 페이지 및 HTMX 비동기 검색 API - 검색 UI: 통계 바, 예시 질문, Markdown 렌더링, 참조 문서 표시 - AiTokenHelper 연동으로 토큰 사용량 자동 추적
61 lines
1.5 KiB
PHP
61 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Additional;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\RagSearchService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\View\View;
|
|
|
|
/**
|
|
* 추가기능 > RAG 검색 컨트롤러
|
|
* SAM 프로젝트 문서를 Gemini AI로 검색/답변
|
|
*/
|
|
class RagSearchController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly RagSearchService $ragService,
|
|
) {}
|
|
|
|
/**
|
|
* RAG 검색 메인 페이지
|
|
*/
|
|
public function index(Request $request): View|Response
|
|
{
|
|
if ($request->header('HX-Request')) {
|
|
return response('', 200)->header('HX-Redirect', route('additional.rag.index'));
|
|
}
|
|
|
|
$stats = $this->ragService->getDocStats();
|
|
|
|
return view('additional.rag.index', compact('stats'));
|
|
}
|
|
|
|
/**
|
|
* RAG 검색 실행 (HTMX)
|
|
*/
|
|
public function search(Request $request)
|
|
{
|
|
$query = trim($request->input('query', ''));
|
|
|
|
if (empty($query)) {
|
|
return response()->json([
|
|
'ok' => false,
|
|
'error' => '검색어를 입력해주세요.',
|
|
]);
|
|
}
|
|
|
|
if (mb_strlen($query) < 2) {
|
|
return response()->json([
|
|
'ok' => false,
|
|
'error' => '검색어는 2자 이상 입력해주세요.',
|
|
]);
|
|
}
|
|
|
|
$result = $this->ragService->search($query);
|
|
|
|
return response()->json($result);
|
|
}
|
|
}
|