作者 李宇航

合并分支 'lyh-server' 到 'master'

Lyh server



查看合并请求 !1690
... ... @@ -31,42 +31,53 @@ 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) {
$fullResponse = ''; // 存储完整 AI 回复
$stream = $gptService->get_ai_chat($data); // 获取流
while (!$stream->eof()) {
$chunk = $stream->read(1024); // 逐步读取数据块
echo $gptService->en_sse_data($chunk); // **封装 SSE 数据**
ob_flush();
flush();
$fullResponse .= $chunk; // 拼接完整 AI 回复
}
// **流结束后,保存完整的 AI 回复**
$this->saveChatItem($chatId, $fullResponse ?: '服务器繁忙,请重试', 1);
}, 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,26 @@ class GptService
}
return json_decode($result,true);
}else {
//发送流式请求
// **流式请求**
$payload->stream = true;
$response = $client->chat($payload);
$body = $response->getBody();
return $body;
$stream = $response->getBody();
return $stream;
}
}
/**
* @remark :返回格式
* @name :en_sse_data
* @author :lyh
* @method :post
* @time :2025/4/2 16:56
*/
public function en_sse_data($body, string $type='text'){
return 'data:'.json_encode([
'id' => md5(is_array($body) ? json_encode($body) : $body),
'data' => $body,
'type' => $type
],JSON_UNESCAPED_UNICODE)."\n\n";
}
}
... ...