|
@@ -31,42 +31,51 @@ class ChatLogic extends BaseLogic |
|
@@ -31,42 +31,51 @@ class ChatLogic extends BaseLogic |
|
31
|
* @method :post
|
31
|
* @method :post
|
|
32
|
* @time :2025/4/2 10:01
|
32
|
* @time :2025/4/2 10:01
|
|
33
|
*/
|
33
|
*/
|
|
34
|
- public function sendMessage(){
|
34
|
+ public function sendMessage()
|
|
|
|
35
|
+ {
|
|
35
|
$gptService = new GptService();
|
36
|
$gptService = new GptService();
|
|
36
|
- if(isset($this->param['chat_id'])){
|
|
|
|
37
|
- $chatInfo = $this->model->read(['id'=>$this->param['chat_id']]);
|
|
|
|
38
|
- if($chatInfo !== false){
|
|
|
|
39
|
- $this->saveChatItem($chatInfo['id'],$this->param['message']);
|
|
|
|
40
|
- $list = $this->itemModel->list(['chat_id'=>$chatInfo['id']],'id',['*'],'desc',2);
|
37
|
+ $message = [];
|
|
|
|
38
|
+ if (isset($this->param['chat_id'])) {
|
|
|
|
39
|
+ $chatInfo = $this->model->read(['id' => $this->param['chat_id']]);
|
|
|
|
40
|
+ if ($chatInfo !== false) {
|
|
|
|
41
|
+ $this->saveChatItem($chatInfo['id'], $this->param['message']);
|
|
|
|
42
|
+ // 获取最近 2 条对话记录
|
|
|
|
43
|
+ $list = $this->itemModel->list(['chat_id' => $chatInfo['id']], 'id', ['*'], 'desc', 2);
|
|
41
|
$message[] = ['role' => 'system', 'content' => "You are now the marketing customer service of 深圳创贸集团"];
|
44
|
$message[] = ['role' => 'system', 'content' => "You are now the marketing customer service of 深圳创贸集团"];
|
|
42
|
- foreach ($list as $val){
|
|
|
|
43
|
- if($val['is_reply'] == 2){
|
45
|
+ foreach ($list as $val) {
|
|
|
|
46
|
+ if ($val['is_reply'] == 2) {
|
|
44
|
$message[] = ['role' => 'user', 'content' => $val['content']];
|
47
|
$message[] = ['role' => 'user', 'content' => $val['content']];
|
|
45
|
- }else{
|
48
|
+ } else {
|
|
46
|
$message[] = ['role' => 'assistant', 'content' => $val['content']];
|
49
|
$message[] = ['role' => 'assistant', 'content' => $val['content']];
|
|
47
|
}
|
50
|
}
|
|
48
|
}
|
51
|
}
|
|
49
|
$message[] = ['role' => 'user', 'content' => $this->param['message']];
|
52
|
$message[] = ['role' => 'user', 'content' => $this->param['message']];
|
|
50
|
- $data = [
|
|
|
|
51
|
- 'message'=>$message,
|
|
|
|
52
|
- ];
|
|
|
|
53
|
- $result = $gptService->get_ai_chat($data);
|
|
|
|
54
|
- $streamContent = $result->getBody()->getContents(); // 读取流内容
|
|
|
|
55
|
-// $this->saveChatItem($chatInfo['id'],$result['text'] ?? '服务器繁忙,请重试',1);
|
|
|
|
56
|
-// return $this->success(['text'=>$result['text']]);
|
53
|
+ $chatId = $chatInfo['id'];
|
|
57
|
}
|
54
|
}
|
|
58
|
- }
|
|
|
|
59
|
- $id = $this->saveChat($this->param['message']);
|
|
|
|
60
|
- $this->saveChatItem($id,$this->param['message']);
|
|
|
|
61
|
- $data = [
|
|
|
|
62
|
- 'message'=>[
|
55
|
+ } else {
|
|
|
|
56
|
+ $chatId = $this->saveChat($this->param['message']);
|
|
|
|
57
|
+ $this->saveChatItem($chatId, $this->param['message']);
|
|
|
|
58
|
+ $message = [
|
|
63
|
['role' => 'system', 'content' => "You are now the marketing customer service of 深圳创贸集团"],
|
59
|
['role' => 'system', 'content' => "You are now the marketing customer service of 深圳创贸集团"],
|
|
64
|
- ['role' => 'user', 'content' => $this->param['message']]
|
|
|
|
65
|
- ],
|
|
|
|
66
|
- ];
|
|
|
|
67
|
- $result = $gptService->get_ai_chat($data);
|
|
|
|
68
|
- $this->saveChatItem($id,$result['text'] ?? '服务器繁忙,请重试',1);
|
|
|
|
69
|
- return $this->success(['text'=>$result['text']]);
|
60
|
+ ['role' => 'user', 'content' => $this->param['message']],
|
|
|
|
61
|
+ ];
|
|
|
|
62
|
+ }
|
|
|
|
63
|
+ $data = ['message' => $message];
|
|
|
|
64
|
+ return response()->stream(function () use ($gptService, $data, $chatId) {
|
|
|
|
65
|
+ $streamFunction = $gptService->get_ai_chat($data);
|
|
|
|
66
|
+ ob_start();
|
|
|
|
67
|
+ $streamFunction();
|
|
|
|
68
|
+ $fullResponse = ob_get_clean(); // 获取完整内容
|
|
|
|
69
|
+ // **流结束后,保存完整的 AI 回复**
|
|
|
|
70
|
+ $this->saveChatItem($chatId, $fullResponse ?: '服务器繁忙,请重试', 1);
|
|
|
|
71
|
+ echo $fullResponse;
|
|
|
|
72
|
+ ob_flush();
|
|
|
|
73
|
+ flush();
|
|
|
|
74
|
+ }, 200, [
|
|
|
|
75
|
+ "Content-Type" => "text/event-stream",
|
|
|
|
76
|
+ "Cache-Control" => "no-cache",
|
|
|
|
77
|
+ "Connection" => "keep-alive",
|
|
|
|
78
|
+ ]);
|
|
70
|
}
|
79
|
}
|
|
71
|
|
80
|
|
|
72
|
/**
|
81
|
/**
|