From 7bbc4c18d7436ea021f1bbb76d1da7043da82365 Mon Sep 17 00:00:00 2001 From: lkddi Date: Fri, 27 Feb 2026 17:50:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9AAI=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E6=9C=BA=E5=99=A8=E4=BA=BA=E7=9F=A5=E9=81=93=E5=AF=B9=E6=96=B9?= =?UTF-8?q?=E7=9A=84=E5=90=8D=E5=AD=97=EF=BC=8C=E5=B9=B6=E4=B8=94=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E8=B6=85=E6=97=B6=E4=B8=8D=E5=86=8D=E6=8A=9B=E5=87=BA?= =?UTF-8?q?=E5=BA=95=E5=B1=82=E7=9A=84cURL=E9=95=BF=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/AiChatService.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/Services/AiChatService.php b/app/Services/AiChatService.php index 1256303..dbaa7b4 100644 --- a/app/Services/AiChatService.php +++ b/app/Services/AiChatService.php @@ -118,8 +118,14 @@ PROMPT; $contextKey = self::CONTEXT_PREFIX.$userId; $context = $this->getContext($contextKey); - // 将用户消息加入上下文 - $context[] = ['role' => 'user', 'content' => $message]; + // 获取用户名,以便让 AI 知道是谁在说话 + $username = \App\Models\User::find($userId)?->username ?? '未知用户'; + + // 将用户消息加入上下文(包含发送者信息) + $context[] = [ + 'role' => 'user', + 'content' => "【当前发言人:{$username}】\n" . $message + ]; // 构建完整的 messages 数组(系统提示 + 对话上下文) $messages = array_merge( @@ -150,7 +156,7 @@ PROMPT; } // 所有厂商都失败了 - throw new \Exception('AI 服务暂时不可用,请稍后再试。('.($lastError?->getMessage() ?? '未知错误').')'); + throw new \Exception('AI 服务暂时不可用,网络开小差啦,请稍后再试。'); } /**