harden information getter services and optimize user msg

This commit is contained in:
team2
2026-04-27 22:21:21 +02:00
parent 316a5b5cc2
commit 79adf8f1df
7 changed files with 506 additions and 25 deletions

View File

@@ -40,15 +40,24 @@ final readonly class PromptBuilder
array $knowledgeChunks,
array $shopResults = [],
?bool $fullContext = false,
?string $swagFullOutPut = ''
?string $swagFullOutPut = '',
bool $commerceSearchAttempted = false,
bool $shopSearchHadSystemFailure = false
): string {
$prompt = $this->normalizeBlockText($prompt);
$urlContent = $this->normalizeBlockText($urlContent);
$swagFullOutPut = $this->normalizeNullableBlockText($swagFullOutPut);
$hasShopResults = $shopResults !== [];
$hasKnowledge = $knowledgeChunks !== [] || $urlContent !== '';
$isTechnicalProductQuestion = $this->isLikelyTechnicalProductQuestion($prompt);
$asksForAccessoryOrBundle = $this->asksForAccessoryOrBundle($prompt);
$reliabilityState = $this->resolveReliabilityState(
hasKnowledge: $hasKnowledge,
hasShopResults: $hasShopResults,
commerceSearchAttempted: $commerceSearchAttempted,
shopSearchHadSystemFailure: $shopSearchHadSystemFailure
);
$systemBlock = $this->buildSystemBlock();
$shopBlock = $this->buildShopBlock($shopResults, $swagFullOutPut);
@@ -56,6 +65,13 @@ final readonly class PromptBuilder
hasShopResults: $hasShopResults,
isTechnicalProductQuestion: $isTechnicalProductQuestion
);
$fallbackEscalationBlock = $this->buildFallbackEscalationBlock(
reliabilityState: $reliabilityState,
hasShopResults: $hasShopResults,
commerceSearchAttempted: $commerceSearchAttempted,
shopSearchHadSystemFailure: $shopSearchHadSystemFailure,
isTechnicalProductQuestion: $isTechnicalProductQuestion
);
$responseFormatBlock = $this->buildResponseFormatBlock(
hasShopResults: $hasShopResults,
isTechnicalProductQuestion: $isTechnicalProductQuestion,
@@ -73,6 +89,7 @@ final readonly class PromptBuilder
$systemBlock,
$shopBlock,
$outputPriorityBlock,
$fallbackEscalationBlock,
$responseFormatBlock,
$knowledgeBlock,
$userBlock,
@@ -88,6 +105,7 @@ final readonly class PromptBuilder
$systemBlock,
$shopBlock,
$outputPriorityBlock,
$fallbackEscalationBlock,
$responseFormatBlock,
$knowledgeBlock,
$contextBlock,
@@ -239,6 +257,66 @@ final readonly class PromptBuilder
);
}
private function resolveReliabilityState(
bool $hasKnowledge,
bool $hasShopResults,
bool $commerceSearchAttempted,
bool $shopSearchHadSystemFailure
): string {
if ($shopSearchHadSystemFailure && !$hasKnowledge) {
return 'shopdaten_nicht_verfuegbar';
}
if ($hasKnowledge && !$hasShopResults) {
return 'sicher_beantwortbar';
}
if ($hasKnowledge && $hasShopResults) {
return 'wahrscheinlich_beantwortbar';
}
if (!$hasKnowledge && $hasShopResults) {
return 'nur_shop_treffer_kein_belastbares_fachwissen';
}
return 'keine_belastbaren_daten';
}
private function buildFallbackEscalationBlock(
string $reliabilityState,
bool $hasShopResults,
bool $commerceSearchAttempted,
bool $shopSearchHadSystemFailure,
bool $isTechnicalProductQuestion
): string {
$rules = [];
$stateLineTemplate = $this->config->getFallbackEscalationStateLineTemplate();
if ($stateLineTemplate !== '') {
$rules[] = str_replace('{state}', $reliabilityState, $stateLineTemplate);
}
$rules = array_merge($rules, $this->config->getFallbackEscalationBaseRules());
$rules = array_merge($rules, $this->config->getFallbackEscalationStateRules($reliabilityState));
if ($isTechnicalProductQuestion && !$commerceSearchAttempted && !$shopSearchHadSystemFailure) {
$rules = array_merge($rules, $this->config->getFallbackEscalationWithoutShopCheckRules());
}
if ($hasShopResults && !$commerceSearchAttempted) {
$rules[] = '- Treat shop results as provided context only; do not imply that a live shop check was performed in this run.';
}
if ($rules === []) {
return '';
}
return $this->buildRuleBlock(
$this->config->getFallbackEscalationSectionLabel(),
$rules
);
}
private function buildResponseFormatBlock(
bool $hasShopResults,
bool $isTechnicalProductQuestion,