public function send(): ResponseInterface
{
// 1. 유저 메시지 DB 저장
$userId = $db->table('chat_messages')->insert([
'nickname' => '나', 'content' => $content, ...
], true);
// 2. 최근 20개 대화 → Groq 컨텍스트 구성
$history = $db->table('chat_messages')->orderBy('id','DESC')->limit(20)->get();
$groqMessages = [
['role' => 'system', 'content' => '...'],
// history: nickname === 'AI봇' → assistant, 나머지 → user
];
// 3. Groq API 호출
$response = Services::curlrequest()->post(
'https://api.groq.com/openai/v1/chat/completions',
['body' => json_encode(['model' => $model, 'messages' => $groqMessages])]
);
// 4. 봇 메시지 저장 후 둘 다 반환
return $this->response->setJSON([
'user' => $userMsg,
'bot' => $botMsg,
]);
}