优化:AI聊天机器人知道对方的名字,并且连接超时不再抛出底层的cURL长代码错误
This commit is contained in:
@@ -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 服务暂时不可用,网络开小差啦,请稍后再试。');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user