feat: Enhance Support Reply Service with tone instructions and article details

- Added tone instruction retrieval to SupportReplyService.
- Improved user feedback when no relevant article is found.
- Included article URL and tone instruction in LLM prompt.
- Updated response format to include source information.
- Enhanced article management UI with search functionality and editing capabilities.
- Introduced a new API endpoint for nearest articles based on vector search.
- Added confidence badge component to display article confidence levels.
- Implemented tests for article searching, editing, and nearest article API.
- Removed obsolete .htaccess file.
This commit is contained in:
your name
2026-05-13 22:25:45 +02:00
parent c94d3f85e8
commit 9244899f9b
22 changed files with 813 additions and 123 deletions

View File

@@ -9,10 +9,29 @@ use Illuminate\Support\Facades\DB;
class AdminArticleService
{
public function paginate(int $perPage = 10): LengthAwarePaginator
public function paginate(int $perPage = 10, ?string $search = null): LengthAwarePaginator
{
$search = $search !== null ? trim($search) : '';
$numericSearch = ltrim($search, '#');
return Article::query()
->with('quickReplies')
->when($search !== '', function ($query) use ($numericSearch, $search): void {
$like = "%{$search}%";
$query->where(function ($query) use ($numericSearch, $like): void {
$query
->where('title', 'ilike', $like)
->orWhere('content', 'ilike', $like)
->orWhere('source_url', 'ilike', $like)
->orWhere('source_article_id', 'ilike', $like);
if (ctype_digit($numericSearch)) {
$query->orWhere('id', (int) $numericSearch);
}
});
})
->latest()
->paginate($perPage);
}
@@ -34,6 +53,25 @@ class AdminArticleService
return (bool) Article::query()->whereKey($articleId)->delete();
}
public function findById(int $articleId): ?Article
{
return Article::query()->find($articleId);
}
public function updateContent(int $articleId, string $title, string $content): bool
{
$article = Article::query()->find($articleId);
if ($article === null) {
return false;
}
$article->title = trim($title);
$article->content = trim($content);
$article->save();
return true;
}
public function updateMetadata(int $articleId, ?string $note, array $allowedActions, array $quickReplyIds = []): bool
{
$article = Article::query()->find($articleId);

View File

@@ -130,6 +130,20 @@ class AppSettingsService
return $default;
}
public function toneAddressing(): string
{
return $this->get('tone_addressing', 'je') === 'u' ? 'u' : 'je';
}
public function toneInstruction(): string
{
if ($this->toneAddressing() === 'u') {
return 'Als de klanttaal Nederlands is: spreek de klant consequent formeel aan met u/uw. Gebruik geen je/jij/jouw.';
}
return 'Als de klanttaal Nederlands is: spreek de klant consequent informeel aan met je/jij/jouw. Gebruik geen u/uw.';
}
public function promptSettings(): array
{
$settings = $this->all();

View File

@@ -8,6 +8,8 @@ use Illuminate\Support\Str;
class KnowledgeGapService
{
public const CONFIDENCE_THRESHOLD = 0.45;
public function __construct(
private readonly LlmClientInterface $llmClient,
private readonly AppSettingsService $settings,
@@ -16,7 +18,7 @@ class KnowledgeGapService
public function shouldCreateDraft(Ticket $ticket, array $result): bool
{
$confidence = (float) ($result['confidence'] ?? 0);
if ($confidence < 0.45) {
if ($confidence < self::CONFIDENCE_THRESHOLD) {
return true;
}
@@ -41,6 +43,7 @@ class KnowledgeGapService
$prompt = $basePrompt."\n\n".
"Klantvraag:\n{$question}\n\n".
"Originele taal: {$language}. Schrijf titel en inhoud in deze taal.\n\n".
'Aanspreekvorm: '.$this->settings->toneInstruction()."\n\n".
"Huidige kandidaten (mogelijk onvoldoende):\n{$topCandidates}\n\n".
'Content moet praktisch zijn met duidelijke stappen.';

View File

@@ -18,18 +18,22 @@ class SupportReplyService
public function build(Ticket $ticket, ?Article $bestArticle, string $explanation, ?array $toolCall = null): string
{
$basePrompt = $this->settings->getPrompt('support_reply', 'Give only direct advice in the requested output language.');
$toneInstruction = $this->settings->toneInstruction();
if ($bestArticle === null) {
return "Geen passend kennisbankartikel gevonden.\n\n1. Controleer of je vraag voldoende details bevat (product, domein, foutmelding).\n2. Escaleer naar support voor handmatige opvolging.\n3. Voeg een nieuw kennisbankartikel toe na afhandeling van deze case.";
return "Geen passend kennisbankartikel gevonden.\n\n1. Controleer of de klantvraag voldoende details bevat (product, domein, foutmelding).\n2. Escaleer naar support voor handmatige opvolging.\n3. Voeg een nieuw kennisbankartikel toe na afhandeling van deze case.";
}
if ((bool) config('services.llm.ranking_enabled', true)) {
$userInput = $ticket->normalized_message ?: $ticket->message;
$language = (string) ($ticket->redaction_report['language'] ?? 'nl');
$articleUrl = $bestArticle->source_url ?: 'niet beschikbaar';
$llmPrompt = $basePrompt."\n\n".
"Customer language: {$language}. Write the advice in this language.\n".
"Aanspreekvorm: {$toneInstruction}\n".
'Gebruikersvraag (genormaliseerd): '.$userInput."\n".
'Beste artikel titel: '.$bestArticle->title."\n".
'Beste artikel URL: '.$articleUrl."\n".
'Beste artikel content: '.$bestArticle->content."\n".
'Interne artikelnotitie: '.($bestArticle->note ?: '-')."\n".
'Waarom dit artikel gekozen is: '.$explanation."\n\n".
@@ -39,7 +43,9 @@ class SupportReplyService
"- GEEN aanhef, GEEN afsluiting, GEEN bedanktekst.\n".
"- Begin direct met de oplossing.\n".
"- Geef 3-6 genummerde actiepunten.\n".
"- Volg de aanspreekvorm exact.\n".
"- Voeg een korte controle-stap toe als laatste punt.\n".
"- Eindig altijd met precies 1 bronregel: 'Bron: <url>' of 'Bron: niet beschikbaar'.\n".
'- Geen markdown, geen codeblokken.';
try {
@@ -69,7 +75,8 @@ class SupportReplyService
"3. Voer de herstel- of controleacties uit die in het artikel worden genoemd.\n".
"4. Controleer daarna of het oorspronkelijke probleem niet meer optreedt.\n\n".
'Relevantie: '.$safeExplanation."\n".
'Bronsamenvatting: '.$snippet;
'Bronsamenvatting: '.$snippet."\n".
'Bron: '.($bestArticle->source_url ?: 'niet beschikbaar');
}
private function formatToolCallForPrompt(?array $toolCall): string
@@ -100,7 +107,7 @@ class SupportReplyService
|| str_contains($lower, 'curl error');
if ($hasTechnicalFailure) {
return 'Dit artikel sluit het beste aan op je vraag en bevat de meest relevante stappen om dit in te stellen.';
return 'Dit artikel sluit het beste aan op de klantvraag en bevat de meest relevante stappen om dit in te stellen.';
}
return $explanation;