作者 lyh

gx

... ... @@ -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",
]);
}
/**
... ...
... ... @@ -24,13 +24,6 @@ class GptService
*
*/
public function get_ai_chat($data,$type = 0){
// 组装请求体参数
// $data['message'] = [
// ['role' => 'system', 'content' => "You are now the marketing customer service of 深圳创贸集团"],
// ['role' => 'user', 'content' => '创贸集团有多少技术?'],
// ['role' => 'assistant', 'content' => '创贸集团有200+技术。'],
// ['role' => 'user', 'content' => '今天天气怎么样']
// ];
$apikey = env('AI_CREATE_KEY')??'7yn!We6$&NnVA38bpGy*A@4TQ5iYLJcW';
$client = new CmerClient($apikey);
// 修改超时时间,默认60秒
... ... @@ -50,11 +43,18 @@ class GptService
}
return json_decode($result,true);
}else {
//发送流式请求
// **流式模式**
$payload->stream = true;
$response = $client->chat($payload);
$body = $response->getBody();
return $body;
$stream = $response->getBody();
return function () use ($stream) {
while (!$stream->eof()) {
$chunk = $stream->read(1024);
echo $chunk;
ob_flush();
flush();
}
};
}
}
}
... ...