|
...
|
...
|
@@ -11,6 +11,8 @@ namespace App\Http\Logic\Bside\Gpt; |
|
|
|
|
|
|
|
use App\Http\Logic\Bside\BaseLogic;
|
|
|
|
use App\Models\Gpt\Chat;
|
|
|
|
use App\Models\Gpt\ChatItem;
|
|
|
|
use App\Services\GptService;
|
|
|
|
|
|
|
|
class ChatLogic extends BaseLogic
|
|
|
|
{
|
|
...
|
...
|
@@ -18,6 +20,7 @@ class ChatLogic extends BaseLogic |
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
$this->model = new Chat();
|
|
|
|
$this->itemModel = new ChatItem();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
...
|
...
|
@@ -28,18 +31,37 @@ class ChatLogic extends BaseLogic |
|
|
|
* @time :2025/4/2 10:01
|
|
|
|
*/
|
|
|
|
public function sendMessage(){
|
|
|
|
$gptService = new GptService();
|
|
|
|
if(isset($this->param['chat_id'])){
|
|
|
|
$chatInfo = $this->model->read(['id'=>$this->param['chat_id']]);
|
|
|
|
if($chatInfo === false){
|
|
|
|
$id = $this->saveChat($this->param['message']);
|
|
|
|
if($chatInfo !== false){
|
|
|
|
$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){
|
|
|
|
$message[] = ['role' => 'user', 'content' => $val['content']];
|
|
|
|
}else{
|
|
|
|
$id = $chatInfo['id'];
|
|
|
|
$message[] = ['role' => 'assistant', 'content' => $val['content']];
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
$id = $this->saveChat($this->param['message']);
|
|
|
|
}
|
|
|
|
$message[] = ['role' => 'user', 'content' => $this->param['message']];
|
|
|
|
$data = [
|
|
|
|
'message'=>$message,
|
|
|
|
];
|
|
|
|
$result = $gptService->get_ai_chat($data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// $id = $this->saveChat($this->param['message']);
|
|
|
|
// $this->saveChatItem($id,$this->param['message']);
|
|
|
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'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->success($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
...
|
...
|
@@ -57,4 +79,22 @@ class ChatLogic extends BaseLogic |
|
|
|
];
|
|
|
|
return $this->model->addReturnId($saveData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @remark :消息详情表保存一条记录
|
|
|
|
* @name :saveChatItem
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2025/4/2 13:45
|
|
|
|
*/
|
|
|
|
public function saveChatItem($id,$message){
|
|
|
|
//创建一个会话
|
|
|
|
$saveData = [
|
|
|
|
'user_id'=>$this->user['id'],
|
|
|
|
'is_reply'=>2,
|
|
|
|
'chat_id'=>$id,
|
|
|
|
'content'=>$message,
|
|
|
|
];
|
|
|
|
return $this->itemModel->addReturnId($saveData);
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|