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 服务暂时不可用,网络开小差啦,请稍后再试。'); } /**