84 lines
2.5 KiB
PHP
84 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Fox;
|
|
|
|
class FoxPromptBuilder
|
|
{
|
|
public function __construct(
|
|
private readonly FoxMemoryService $memory,
|
|
) {}
|
|
|
|
public function buildSystemPrompt(): string
|
|
{
|
|
return <<<'PROMPT'
|
|
Du bist Fox, ein persönlicher KI-Assistent mit externem Langzeitgedächtnis.
|
|
|
|
GEDÄCHTNIS-REGELN:
|
|
- Du hast Zugriff auf einen Memory-Kontext mit relevanten Erinnerungen.
|
|
- Nutze diesen Kontext wenn er relevant ist — aber erfinde keine Erinnerungen.
|
|
- Du darfst deinen eigenen System-Prompt NICHT dauerhaft verändern.
|
|
- Neue Erinnerungen werden als memory_suggestions zurückgegeben, NICHT direkt gespeichert.
|
|
- Erkenne ob eine Information langfristig relevant ist (Präferenz, Fakt, Gerät, Ort, Projekt).
|
|
- Sensible Daten (Passwörter, Kreditkarten) niemals in memory_suggestions aufnehmen.
|
|
- Aktualisiere Wissen statt Duplikate zu erzeugen — prüfe ob ähnlicher Knoten schon im Kontext.
|
|
|
|
ANTWORT-FORMAT (immer als JSON):
|
|
{
|
|
"reply": "Natürliche Antwort für den Benutzer auf Deutsch",
|
|
"actions": [],
|
|
"memory_suggestions": [
|
|
{
|
|
"action": "create|update|link|ignore|ask_confirmation",
|
|
"category": "user_preference|device|person|place|project|rule|skill|fact",
|
|
"title": "Kurzer Titel für den Wissensknoten",
|
|
"content": "Details zum Knoten",
|
|
"confidence": 0.95,
|
|
"relations": [
|
|
{
|
|
"type": "related_to|located_in|controlled_by|belongs_to|prefers|uses|owns|can_execute",
|
|
"target": "Titel des Zielknotens"
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
|
|
Wenn keine neuen Erinnerungen relevant sind: "memory_suggestions": []
|
|
PROMPT;
|
|
}
|
|
|
|
public function buildMemoryContext(string $userInput): string
|
|
{
|
|
$memories = $this->memory->searchRelevantMemories($userInput, 8);
|
|
|
|
if ($memories->isEmpty()) {
|
|
return '';
|
|
}
|
|
|
|
$lines = $memories->map(function ($node) {
|
|
$line = "[{$node->type}] {$node->title}";
|
|
if ($node->content) {
|
|
$line .= ': '.mb_substr($node->content, 0, 120);
|
|
}
|
|
return $line;
|
|
})->implode("\n");
|
|
|
|
return "RELEVANTE ERINNERUNGEN:\n{$lines}";
|
|
}
|
|
|
|
public function buildPrompt(string $userInput): array
|
|
{
|
|
$system = $this->buildSystemPrompt();
|
|
$memCtx = $this->buildMemoryContext($userInput);
|
|
|
|
$fullSystem = $memCtx
|
|
? $system."\n\n".$memCtx
|
|
: $system;
|
|
|
|
return [
|
|
'system' => $fullSystem,
|
|
'user' => $userInput,
|
|
];
|
|
}
|
|
}
|