Backup #837

ID837
Dateipfad/var/www/dev.campus.systemische-tools.de/src/UseCases/Docs/DocumentationChatUseCase.php
Version2
Typ modified
Größe2.9 KB
Hash7bf65c7885686116cfcb993dcd2ea46fb94c474116cf5f22d31005e2e3c69507
Datum2025-12-23 08:17:03
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Edit-Operation
Datei existiert Ja

Dateiinhalt

<?php

declare(strict_types=1);

namespace UseCases\Docs;

// @responsibility: RAG-basierter Chat über Dokumentation

use Infrastructure\AI\OllamaService;
use Infrastructure\Docs\ChunkSyncService;

class DocumentationChatUseCase
{
    private ChunkSyncService $syncService;
    private OllamaService $ollamaService;

    public function __construct(
        ?ChunkSyncService $syncService = null,
        ?OllamaService $ollamaService = null
    ) {
        $this->syncService = $syncService ?? new ChunkSyncService();
        $this->ollamaService = $ollamaService ?? new OllamaService();
    }

    /**
     * Execute documentation chat.
     *
     * @param string $question User question
     * @param string $model LLM model to use
     * @param int $limit Number of context chunks
     * @return array{answer: string, sources: array<int, array{id: int, path: string, title: string, score: float}>}
     */
    public function execute(string $question, string $model = 'mistral', int $limit = 5): array
    {
        // Get relevant chunks via semantic search
        $chunks = $this->syncService->search($question, $limit);

        if (empty($chunks)) {
            return [
                'answer' => 'Leider konnte ich keine relevanten Informationen in der Dokumentation finden.',
                'sources' => [],
            ];
        }

        // Build context from chunks
        $context = $this->buildContext($chunks);

        // Generate answer using LLM
        $prompt = $this->buildPrompt($question, $context);
        $answer = $this->ollamaService->generate($prompt, $model, 0.3);

        // Map sources
        $sources = array_map(static fn (array $chunk): array => [
            'id' => $chunk['doc_id'],
            'path' => $chunk['path'],
            'title' => $chunk['title'],
            'score' => round($chunk['score'], 3),
        ], $chunks);

        return [
            'answer' => $answer,
            'sources' => $sources,
        ];
    }

    /**
     * Build context from chunks.
     */
    private function buildContext(array $chunks): string
    {
        $parts = [];

        foreach ($chunks as $chunk) {
            $part = "## {$chunk['title']}\n";
            $part .= "Pfad: {$chunk['path']}\n";
            $part .= $chunk['content'];
            $parts[] = $part;
        }

        return implode("\n\n---\n\n", $parts);
    }

    /**
     * Build chat prompt.
     */
    private function buildPrompt(string $question, string $context): string
    {
        return <<<PROMPT
            Du bist ein Dokumentations-Assistent. Beantworte die Frage basierend auf dem bereitgestellten Kontext.

            KONTEXT:
            {$context}

            FRAGE:
            {$question}

            ANLEITUNG:
            - Antworte auf Deutsch
            - Sei präzise und hilfreich
            - Wenn der Kontext die Frage nicht beantwortet, sage das ehrlich
            - Verweise auf die relevanten Abschnitte der Dokumentation
            PROMPT;
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

Andere Versionen dieser Datei

ID Version Typ Größe Datum
1285 4 modified 2.7 KB 2025-12-25 13:01
1284 3 modified 2.7 KB 2025-12-25 13:01
837 2 modified 2.9 KB 2025-12-23 08:17
721 1 modified 3.1 KB 2025-12-23 07:56

← Zurück zur Übersicht