Backup #721
| ID | 721 |
| Dateipfad | /var/www/dev.campus.systemische-tools.de/src/UseCases/Docs/DocumentationChatUseCase.php |
| Version | 1 |
| Typ |
modified |
| Größe | 3.1 KB |
| Hash | a4a480a1c3ca2d4cfdeb875ddbb65e8277b29a8069b2c5672a8f8761506fb0ce |
| Datum | 2025-12-23 07:56:25 |
| Geändert von | claude-code-hook |
| Grund | Claude Code Pre-Hook Backup vor Edit-Operation |
| Datei existiert |
Ja
|
Dateiinhalt
<?php
declare(strict_types=1);
namespace UseCases\Docs;
use Infrastructure\AI\OllamaService;
use Infrastructure\Docs\ChunkSyncService;
/**
* DocumentationChatUseCase - RAG-based documentation chat.
*
* Handles semantic search over documentation and generates
* contextual answers using an LLM.
*/
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
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