68 lines
2.1 KiB
PHP
68 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\ChatLog;
|
|
use App\Models\ChatSession;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
/**
|
|
* LogChatInteractionJob
|
|
*
|
|
* Log pertanyaan + jawapan chatbot secara asynchronous.
|
|
* Hantar ke queue supaya response chatbot tidak ditangguh oleh operasi DB.
|
|
*/
|
|
class LogChatInteractionJob implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public int $tries = 3;
|
|
public int $timeout = 30;
|
|
|
|
public function __construct(
|
|
private readonly string $sessionToken,
|
|
private readonly ?int $userId,
|
|
private readonly ?int $categoryId,
|
|
private readonly string $question,
|
|
private readonly string $answer,
|
|
private readonly array $sources,
|
|
private readonly array $contextChunks,
|
|
private readonly string $modelUsed,
|
|
private readonly ?int $tokensUsed,
|
|
private readonly float $responseTime,
|
|
private readonly bool $hasAnswer,
|
|
) {
|
|
$this->onQueue(config('knowledgebase.queue.chat_log', 'default'));
|
|
}
|
|
|
|
public function handle(): void
|
|
{
|
|
$session = ChatSession::where('session_token', $this->sessionToken)->first();
|
|
|
|
if (!$session) {
|
|
return;
|
|
}
|
|
|
|
ChatLog::create([
|
|
'chat_session_id' => $session->id,
|
|
'user_id' => $this->userId,
|
|
'category_id' => $this->categoryId,
|
|
'question' => $this->question,
|
|
'answer' => $this->answer,
|
|
'sources_used' => $this->sources,
|
|
'context_chunks' => $this->contextChunks,
|
|
'model_used' => $this->modelUsed,
|
|
'tokens_used' => $this->tokensUsed,
|
|
'response_time' => $this->responseTime,
|
|
'has_answer' => $this->hasAnswer,
|
|
'is_flagged' => false,
|
|
]);
|
|
|
|
$session->update(['last_activity_at' => now()]);
|
|
}
|
|
}
|