|
...
|
...
|
@@ -31,42 +31,51 @@ class ChatLogic extends BaseLogic |
|
|
|
* @method :post
|
|
|
|
* @time :2025/4/2 10:01
|
|
|
|
*/
|
|
|
|
public function sendMessage(){
|
|
|
|
public function sendMessage()
|
|
|
|
{
|
|
|
|
$gptService = new GptService();
|
|
|
|
if(isset($this->param['chat_id'])){
|
|
|
|
$chatInfo = $this->model->read(['id'=>$this->param['chat_id']]);
|
|
|
|
if($chatInfo !== false){
|
|
|
|
$this->saveChatItem($chatInfo['id'],$this->param['message']);
|
|
|
|
$list = $this->itemModel->list(['chat_id'=>$chatInfo['id']],'id',['*'],'desc',2);
|
|
|
|
$message = [];
|
|
|
|
if (isset($this->param['chat_id'])) {
|
|
|
|
$chatInfo = $this->model->read(['id' => $this->param['chat_id']]);
|
|
|
|
if ($chatInfo !== false) {
|
|
|
|
$this->saveChatItem($chatInfo['id'], $this->param['message']);
|
|
|
|
// 获取最近 2 条对话记录
|
|
|
|
$list = $this->itemModel->list(['chat_id' => $chatInfo['id']], 'id', ['*'], 'desc', 2);
|
|
|
|
$message[] = ['role' => 'system', 'content' => "You are now the marketing customer service of 深圳创贸集团"];
|
|
|
|
foreach ($list as $val){
|
|
|
|
if($val['is_reply'] == 2){
|
|
|
|
foreach ($list as $val) {
|
|
|
|
if ($val['is_reply'] == 2) {
|
|
|
|
$message[] = ['role' => 'user', 'content' => $val['content']];
|
|
|
|
}else{
|
|
|
|
} else {
|
|
|
|
$message[] = ['role' => 'assistant', 'content' => $val['content']];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$message[] = ['role' => 'user', 'content' => $this->param['message']];
|
|
|
|
$data = [
|
|
|
|
'message'=>$message,
|
|
|
|
];
|
|
|
|
$result = $gptService->get_ai_chat($data);
|
|
|
|
$streamContent = $result->getBody()->getContents(); // 读取流内容
|
|
|
|
// $this->saveChatItem($chatInfo['id'],$result['text'] ?? '服务器繁忙,请重试',1);
|
|
|
|
// return $this->success(['text'=>$result['text']]);
|
|
|
|
$chatId = $chatInfo['id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$id = $this->saveChat($this->param['message']);
|
|
|
|
$this->saveChatItem($id,$this->param['message']);
|
|
|
|
$data = [
|
|
|
|
'message'=>[
|
|
|
|
} else {
|
|
|
|
$chatId = $this->saveChat($this->param['message']);
|
|
|
|
$this->saveChatItem($chatId, $this->param['message']);
|
|
|
|
$message = [
|
|
|
|
['role' => 'system', 'content' => "You are now the marketing customer service of 深圳创贸集团"],
|
|
|
|
['role' => 'user', 'content' => $this->param['message']]
|
|
|
|
],
|
|
|
|
];
|
|
|
|
$result = $gptService->get_ai_chat($data);
|
|
|
|
$this->saveChatItem($id,$result['text'] ?? '服务器繁忙,请重试',1);
|
|
|
|
return $this->success(['text'=>$result['text']]);
|
|
|
|
['role' => 'user', 'content' => $this->param['message']],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$data = ['message' => $message];
|
|
|
|
return response()->stream(function () use ($gptService, $data, $chatId) {
|
|
|
|
$streamFunction = $gptService->get_ai_chat($data);
|
|
|
|
ob_start();
|
|
|
|
$streamFunction();
|
|
|
|
$fullResponse = ob_get_clean(); // 获取完整内容
|
|
|
|
// **流结束后,保存完整的 AI 回复**
|
|
|
|
$this->saveChatItem($chatId, $fullResponse ?: '服务器繁忙,请重试', 1);
|
|
|
|
echo $fullResponse;
|
|
|
|
ob_flush();
|
|
|
|
flush();
|
|
|
|
}, 200, [
|
|
|
|
"Content-Type" => "text/event-stream",
|
|
|
|
"Cache-Control" => "no-cache",
|
|
|
|
"Connection" => "keep-alive",
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
...
|
...
|
|