ChatController.php
- Pfad:
src/Controller/Api/ChatController.php - Namespace: Controller\Api
- Zeilen: 162 | Größe: 5,253 Bytes
- Geändert: 2025-12-29 09:18:57 | Gescannt: 2025-12-31 10:22:15
Code Hygiene Score: 95
- Dependencies: 80 (25%)
- LOC: 100 (20%)
- Methods: 100 (20%)
- Secrets: 100 (15%)
- Classes: 100 (10%)
- Magic Numbers: 100 (10%)
Keine Issues gefunden.
Dependencies 14
- extends Framework\Controller
- constructor Infrastructure\AI\ChatService
- constructor Infrastructure\AI\OllamaService
- constructor Infrastructure\AI\QdrantService
- constructor Domain\Repository\DokumentExplorerRepositoryInterface
- constructor Domain\Repository\SeiteExplorerRepositoryInterface
- constructor Domain\Repository\ChunkExplorerRepositoryInterface
- use Domain\Repository\ChunkExplorerRepositoryInterface
- use Domain\Repository\DokumentExplorerRepositoryInterface
- use Domain\Repository\SeiteExplorerRepositoryInterface
- use Framework\Controller
- use Infrastructure\AI\ChatService
- use Infrastructure\AI\OllamaService
- use Infrastructure\AI\QdrantService
Klassen 1
-
ChatControllerclass Zeile 17
Funktionen 6
-
__construct()public Zeile 26 -
send()public Zeile 46 -
search()public Zeile 73 -
stats()public Zeile 100 -
askChat()private Zeile 130 -
searchChunks()private Zeile 150
Versionen 17
-
v17
2025-12-29 09:18 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v16
2025-12-29 09:18 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v15
2025-12-28 23:24 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v14
2025-12-28 23:24 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v13
2025-12-28 23:23 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v12
2025-12-25 12:34 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v11
2025-12-25 12:33 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v10
2025-12-23 07:53 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v9
2025-12-23 04:43 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v8
2025-12-23 04:25 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v7
2025-12-22 22:19 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v6
2025-12-22 22:19 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v5
2025-12-22 22:19 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v4
2025-12-22 08:04 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v3
2025-12-22 08:04 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v2
2025-12-21 14:40 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation -
v1
2025-12-20 17:24 | claude-code-hook | modified
Claude Code Pre-Hook Backup vor Edit-Operation
Code
<?php
declare(strict_types=1);
namespace Controller\Api;
// @responsibility: REST-API für KI-Chat und Vektor-Suche
use Domain\Repository\ChunkExplorerRepositoryInterface;
use Domain\Repository\DokumentExplorerRepositoryInterface;
use Domain\Repository\SeiteExplorerRepositoryInterface;
use Framework\Controller;
use Infrastructure\AI\ChatService;
use Infrastructure\AI\OllamaService;
use Infrastructure\AI\QdrantService;
class ChatController extends Controller
{
private ChatService $chatService;
private OllamaService $ollamaService;
private QdrantService $qdrantService;
private DokumentExplorerRepositoryInterface $dokumentRepository;
private SeiteExplorerRepositoryInterface $seiteRepository;
private ChunkExplorerRepositoryInterface $chunkRepository;
public function __construct(
ChatService $chatService,
OllamaService $ollamaService,
QdrantService $qdrantService,
DokumentExplorerRepositoryInterface $dokumentRepository,
SeiteExplorerRepositoryInterface $seiteRepository,
ChunkExplorerRepositoryInterface $chunkRepository
) {
$this->chatService = $chatService;
$this->ollamaService = $ollamaService;
$this->qdrantService = $qdrantService;
$this->dokumentRepository = $dokumentRepository;
$this->seiteRepository = $seiteRepository;
$this->chunkRepository = $chunkRepository;
}
/**
* POST /api/v1/chat
* Handle chat message
*/
public function send(): void
{
$input = $this->getJsonInput();
$question = trim($input['message'] ?? '');
if ($question === '') {
$this->json(['error' => 'Keine Frage angegeben'], 400);
return;
}
// Release session lock before long-running LLM call
session_write_close();
try {
$result = $this->askChat($question);
$this->json($result);
} catch (\Exception $e) {
error_log(sprintf('[API.Chat.send] %s: %s', get_class($e), $e->getMessage()));
$this->json(['error' => $e->getMessage()], 500);
}
}
/**
* GET /api/v1/chat/search
* Search for relevant chunks
*/
public function search(): void
{
$query = $this->getString('q');
$limit = $this->getInt('limit', 5);
if ($query === '') {
$this->json(['error' => 'Keine Suchanfrage'], 400);
return;
}
// Release session lock before embedding generation
session_write_close();
try {
$results = $this->searchChunks($query, $limit);
$this->json(['results' => $results]);
} catch (\Exception $e) {
error_log(sprintf('[API.Chat.search] %s: %s', get_class($e), $e->getMessage()));
$this->json(['error' => $e->getMessage()], 500);
}
}
/**
* GET /api/v1/chat/stats
* Get chat/document statistics (Doc2Vector Pipeline)
*/
public function stats(): void
{
try {
$chunkStats = $this->chunkRepository->getChunkStats();
$stats = [
'dokumente' => $this->dokumentRepository->countDokumente(),
'seiten' => $this->seiteRepository->countSeiten(),
'chunks' => $chunkStats['total'],
'tokens' => $chunkStats['tokens'],
'analyzed' => $chunkStats['analyzed'],
'synced' => $chunkStats['synced'],
];
$this->json($stats);
} catch (\Exception $e) {
error_log(sprintf('[API.Chat.stats] %s: %s', get_class($e), $e->getMessage()));
$this->json(['error' => $e->getMessage()], 500);
}
}
/**
* Ask chat question using ChatService
*
* @param string $question User's question
*
* @return array<string, mixed> Chat response with answer and sources
*
* @throws \RuntimeException If chat service fails
*/
private function askChat(string $question): array
{
try {
// Use dokumentation_chunks collection for RAG
return $this->chatService->chat($question, 'claude-opus-4-5-20251101', ['dokumentation_chunks'], 5);
} catch (\RuntimeException $e) {
throw new \RuntimeException('Chat-Service konnte nicht ausgeführt werden: ' . $e->getMessage(), 0, $e);
}
}
/**
* Search for similar chunks using vector search
*
* @param string $query Search query
* @param int $limit Maximum number of results
*
* @return array<int, array{id: int|string, score: float, payload: array<string, mixed>}> Search results
*
* @throws \RuntimeException If search fails
*/
private function searchChunks(string $query, int $limit): array
{
try {
$queryEmbedding = $this->ollamaService->getEmbedding($query);
// Use dokumentation_chunks collection
return $this->qdrantService->search($queryEmbedding, 'dokumentation_chunks', $limit);
} catch (\RuntimeException $e) {
throw new \RuntimeException('Suche konnte nicht ausgeführt werden: ' . $e->getMessage(), 0, $e);
}
}
}