Backup #1233

ID1233
Dateipfad/var/www/dev.campus.systemische-tools.de/src/Controller/Api/ChatController.php
Version11
Typ modified
Größe4.2 KB
Hash18f4a1d3189b7e67677582247397f6d86d140c42f3ef08516e9d13c1041d5e74
Datum2025-12-25 12:33:55
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Edit-Operation
Datei existiert Ja

Dateiinhalt

<?php

declare(strict_types=1);

namespace Controller\Api;

// @responsibility: REST-API für KI-Chat und Vektor-Suche

use Framework\Controller;
use Infrastructure\AI\ChatService;
use Infrastructure\AI\OllamaService;
use Infrastructure\AI\QdrantService;
use Infrastructure\Persistence\SystemExplorerRepository;

class ChatController extends Controller
{
    private ChatService $chatService;
    private OllamaService $ollamaService;
    private QdrantService $qdrantService;
    private SystemExplorerRepository $explorerRepository;

    public function __construct(
        ChatService $chatService,
        OllamaService $ollamaService,
        QdrantService $qdrantService,
        SystemExplorerRepository $explorerRepository
    ) {
        $this->chatService = $chatService;
        $this->ollamaService = $ollamaService;
        $this->qdrantService = $qdrantService;
        $this->explorerRepository = $explorerRepository;
    }

    /**
     * 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;
        }

        try {
            $result = $this->askChat($question);
            $this->json($result);
        } catch (\Exception $e) {
            $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;
        }

        try {
            $results = $this->searchChunks($query, $limit);
            $this->json(['results' => $results]);
        } catch (\Exception $e) {
            $this->json(['error' => $e->getMessage()], 500);
        }
    }

    /**
     * GET /api/v1/chat/stats
     * Get chat/document statistics (Doc2Vector Pipeline)
     */
    public function stats(): void
    {
        try {
            $chunkStats = $this->explorerRepository->getChunkStats();

            $stats = [
                'dokumente' => $this->explorerRepository->countDokumente(),
                'seiten' => $this->explorerRepository->countSeiten(),
                'chunks' => $chunkStats['total'],
                'tokens' => $chunkStats['tokens'],
                'analyzed' => $chunkStats['analyzed'],
                'synced' => $chunkStats['synced'],
            ];

            $this->json($stats);
        } catch (\Exception $e) {
            $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);
        }
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

Andere Versionen dieser Datei

ID Version Typ Größe Datum
2122 17 modified 5.0 KB 2025-12-29 09:18
2121 16 modified 4.9 KB 2025-12-29 09:18
2033 15 modified 4.9 KB 2025-12-28 23:24
2032 14 modified 4.8 KB 2025-12-28 23:24
2031 13 modified 4.7 KB 2025-12-28 23:23
1243 12 modified 4.7 KB 2025-12-25 12:34
1233 11 modified 4.2 KB 2025-12-25 12:33
704 10 modified 4.1 KB 2025-12-23 07:53
620 9 modified 4.6 KB 2025-12-23 04:43
593 8 modified 4.1 KB 2025-12-23 04:25
535 7 modified 4.3 KB 2025-12-22 22:19
534 6 modified 4.7 KB 2025-12-22 22:19
533 5 modified 4.5 KB 2025-12-22 22:19
300 4 modified 4.5 KB 2025-12-22 08:04
299 3 modified 4.5 KB 2025-12-22 08:04
199 2 modified 4.5 KB 2025-12-21 14:40
41 1 modified 5.3 KB 2025-12-20 17:24

← Zurück zur Übersicht