|
@@ -11,6 +11,8 @@ namespace App\Http\Logic\Bside\Gpt; |
|
@@ -11,6 +11,8 @@ namespace App\Http\Logic\Bside\Gpt; |
|
11
|
|
11
|
|
|
12
|
use App\Http\Logic\Bside\BaseLogic;
|
12
|
use App\Http\Logic\Bside\BaseLogic;
|
|
13
|
use App\Models\Gpt\Chat;
|
13
|
use App\Models\Gpt\Chat;
|
|
|
|
14
|
+use App\Models\Gpt\ChatItem;
|
|
|
|
15
|
+use App\Services\GptService;
|
|
14
|
|
16
|
|
|
15
|
class ChatLogic extends BaseLogic
|
17
|
class ChatLogic extends BaseLogic
|
|
16
|
{
|
18
|
{
|
|
@@ -18,6 +20,7 @@ class ChatLogic extends BaseLogic |
|
@@ -18,6 +20,7 @@ class ChatLogic extends BaseLogic |
|
18
|
{
|
20
|
{
|
|
19
|
parent::__construct();
|
21
|
parent::__construct();
|
|
20
|
$this->model = new Chat();
|
22
|
$this->model = new Chat();
|
|
|
|
23
|
+ $this->itemModel = new ChatItem();
|
|
21
|
}
|
24
|
}
|
|
22
|
|
25
|
|
|
23
|
/**
|
26
|
/**
|
|
@@ -28,18 +31,37 @@ class ChatLogic extends BaseLogic |
|
@@ -28,18 +31,37 @@ class ChatLogic extends BaseLogic |
|
28
|
* @time :2025/4/2 10:01
|
31
|
* @time :2025/4/2 10:01
|
|
29
|
*/
|
32
|
*/
|
|
30
|
public function sendMessage(){
|
33
|
public function sendMessage(){
|
|
|
|
34
|
+ $gptService = new GptService();
|
|
31
|
if(isset($this->param['chat_id'])){
|
35
|
if(isset($this->param['chat_id'])){
|
|
32
|
$chatInfo = $this->model->read(['id'=>$this->param['chat_id']]);
|
36
|
$chatInfo = $this->model->read(['id'=>$this->param['chat_id']]);
|
|
33
|
- if($chatInfo === false){
|
|
|
|
34
|
- $id = $this->saveChat($this->param['message']);
|
37
|
+ if($chatInfo !== false){
|
|
|
|
38
|
+ $list = $this->itemModel->list(['chat_id'=>$chatInfo['id'],'id',['*'],'desc',2]);
|
|
|
|
39
|
+ $message[] = ['role' => 'system', 'content' => "You are now the marketing customer service of 深圳创贸集团"];
|
|
|
|
40
|
+ foreach ($list as $val){
|
|
|
|
41
|
+ if($val['is_reply'] == 2){
|
|
|
|
42
|
+ $message[] = ['role' => 'user', 'content' => $val['content']];
|
|
35
|
}else{
|
43
|
}else{
|
|
36
|
- $id = $chatInfo['id'];
|
44
|
+ $message[] = ['role' => 'assistant', 'content' => $val['content']];
|
|
37
|
}
|
45
|
}
|
|
38
|
- }else{
|
|
|
|
39
|
- $id = $this->saveChat($this->param['message']);
|
|
|
|
40
|
}
|
46
|
}
|
|
|
|
47
|
+ $message[] = ['role' => 'user', 'content' => $this->param['message']];
|
|
|
|
48
|
+ $data = [
|
|
|
|
49
|
+ 'message'=>$message,
|
|
|
|
50
|
+ ];
|
|
|
|
51
|
+ $result = $gptService->get_ai_chat($data);
|
|
|
|
52
|
+ }
|
|
|
|
53
|
+ }
|
|
|
|
54
|
+// $id = $this->saveChat($this->param['message']);
|
|
|
|
55
|
+// $this->saveChatItem($id,$this->param['message']);
|
|
41
|
|
56
|
|
|
42
|
-
|
57
|
+ $data = [
|
|
|
|
58
|
+ 'message'=>[
|
|
|
|
59
|
+ ['role' => 'system', 'content' => "You are now the marketing customer service of 深圳创贸集团"],
|
|
|
|
60
|
+ ['role' => 'user', 'content' => $this->param['message']]
|
|
|
|
61
|
+ ],
|
|
|
|
62
|
+ ];
|
|
|
|
63
|
+ $result = $gptService->get_ai_chat($data);
|
|
|
|
64
|
+ $this->success($result);
|
|
43
|
}
|
65
|
}
|
|
44
|
|
66
|
|
|
45
|
/**
|
67
|
/**
|
|
@@ -57,4 +79,22 @@ class ChatLogic extends BaseLogic |
|
@@ -57,4 +79,22 @@ class ChatLogic extends BaseLogic |
|
57
|
];
|
79
|
];
|
|
58
|
return $this->model->addReturnId($saveData);
|
80
|
return $this->model->addReturnId($saveData);
|
|
59
|
}
|
81
|
}
|
|
|
|
82
|
+
|
|
|
|
83
|
+ /**
|
|
|
|
84
|
+ * @remark :消息详情表保存一条记录
|
|
|
|
85
|
+ * @name :saveChatItem
|
|
|
|
86
|
+ * @author :lyh
|
|
|
|
87
|
+ * @method :post
|
|
|
|
88
|
+ * @time :2025/4/2 13:45
|
|
|
|
89
|
+ */
|
|
|
|
90
|
+ public function saveChatItem($id,$message){
|
|
|
|
91
|
+ //创建一个会话
|
|
|
|
92
|
+ $saveData = [
|
|
|
|
93
|
+ 'user_id'=>$this->user['id'],
|
|
|
|
94
|
+ 'is_reply'=>2,
|
|
|
|
95
|
+ 'chat_id'=>$id,
|
|
|
|
96
|
+ 'content'=>$message,
|
|
|
|
97
|
+ ];
|
|
|
|
98
|
+ return $this->itemModel->addReturnId($saveData);
|
|
|
|
99
|
+ }
|
|
60
|
} |
100
|
} |