- 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.
71 lines
2.6 KiB
PHP
71 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Ticket;
|
|
use App\Services\Llm\LlmClientInterface;
|
|
use Illuminate\Support\Str;
|
|
|
|
class KnowledgeGapService
|
|
{
|
|
public const CONFIDENCE_THRESHOLD = 0.45;
|
|
|
|
public function __construct(
|
|
private readonly LlmClientInterface $llmClient,
|
|
private readonly AppSettingsService $settings,
|
|
) {}
|
|
|
|
public function shouldCreateDraft(Ticket $ticket, array $result): bool
|
|
{
|
|
$confidence = (float) ($result['confidence'] ?? 0);
|
|
if ($confidence < self::CONFIDENCE_THRESHOLD) {
|
|
return true;
|
|
}
|
|
|
|
$explanation = mb_strtolower((string) ($result['explanation'] ?? ''));
|
|
$signals = ['does not contain', 'niet relevant', 'mismatch', 'no article', 'onvoldoende', 'server outage'];
|
|
foreach ($signals as $signal) {
|
|
if (str_contains($explanation, $signal)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function suggestArticleDraft(Ticket $ticket, array $result): array
|
|
{
|
|
$question = $ticket->normalized_message ?: $ticket->message;
|
|
$language = (string) ($ticket->redaction_report['language'] ?? 'nl');
|
|
$topCandidates = json_encode($result['top_3_candidates'] ?? [], JSON_UNESCAPED_UNICODE);
|
|
|
|
$basePrompt = $this->settings->getPrompt('knowledge_gap', 'Create a draft knowledge base article suggestion. Return JSON only with keys: title, content.');
|
|
$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.';
|
|
|
|
$title = 'Concept: '.Str::limit($question, 80, '');
|
|
$content = "Deze vraag kon nog niet goed worden beantwoord vanuit de huidige kennisbank.\n\n".
|
|
"Klantvraag:\n".$question."\n\n".
|
|
'Actie: supportmedewerker vult dit artikel aan met definitieve stappen en context.';
|
|
|
|
try {
|
|
$raw = trim($this->llmClient->generate($prompt, ['expect_json' => true, 'task' => 'knowledge_gap']));
|
|
$decoded = json_decode($raw, true);
|
|
if (is_array($decoded)) {
|
|
$title = trim((string) ($decoded['title'] ?? $title));
|
|
$content = trim((string) ($decoded['content'] ?? $content));
|
|
}
|
|
} catch (\Throwable) {
|
|
}
|
|
|
|
return [
|
|
'title' => $title,
|
|
'content' => $content,
|
|
];
|
|
}
|
|
}
|