7 changed files with 184 additions and 3 deletions
@ -0,0 +1,139 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\enterprise\controller; |
||||
|
|
||||
|
use app\BaseController; |
||||
|
use app\enterprise\model\Message; |
||||
|
use utils\Curl; |
||||
|
|
||||
|
class Chat extends BaseController |
||||
|
{ |
||||
|
protected $error = ''; |
||||
|
|
||||
|
public function sendChat() |
||||
|
{ |
||||
|
$content = $this->request->param('content'); |
||||
|
// 限制文字内容长度 |
||||
|
$text = strip_tags($content); |
||||
|
$textLen = mb_strlen($text); |
||||
|
if ($textLen > 2048) { |
||||
|
return warning(lang('im.msgContentLimit') . $textLen); |
||||
|
} |
||||
|
// 接入聊天内容检测服务 |
||||
|
event('GreenText',['content'=>$content,'service'=>"chat_detection"]); |
||||
|
|
||||
|
$postData = $this->curlChat($content); |
||||
|
|
||||
|
$userInfo = $this->userInfo; |
||||
|
$uid = $userInfo['id']; |
||||
|
|
||||
|
$newContent = $postData['choices'][0]['message']['content']; |
||||
|
$newContent = '<p>' . $newContent . "</p>"; |
||||
|
|
||||
|
if ($this->error) { |
||||
|
return warning($this->error); |
||||
|
} |
||||
|
$chat_identify = '-2-'.$uid; |
||||
|
$data = [ |
||||
|
'from_user'=> 1, |
||||
|
'to_user'=> $uid, |
||||
|
'content'=>str_encipher($newContent,true), |
||||
|
'chat_identify'=>'-2-'.$uid, |
||||
|
'create_time'=>time(), |
||||
|
'type'=>'text', |
||||
|
'is_group'=>4, |
||||
|
'is_read'=>1, |
||||
|
'is_top'=>0, |
||||
|
'is_notice'=>1, |
||||
|
'is_last'=>1, |
||||
|
'at'=>[], |
||||
|
'pid'=>0, |
||||
|
'extends'=>['chat'=>$newContent], |
||||
|
]; |
||||
|
$data['id']=\utils\Str::getUuid(); |
||||
|
|
||||
|
Message::update(['is_last'=>0],['chat_identify'=>$chat_identify]); |
||||
|
$message=new Message(); |
||||
|
$message->save($data); |
||||
|
$msgId=$message->msg_id; |
||||
|
|
||||
|
$msgInfo=$data; |
||||
|
$msgInfo['status']='successd'; |
||||
|
$msgInfo['msg_id']=$msgId; |
||||
|
$msgInfo['user_id']=1; |
||||
|
$msgInfo['sendTime']=time()*1000; |
||||
|
$msgInfo['toContactId']='ai_chat'; |
||||
|
$msgInfo['to_user']='ai_chat'; |
||||
|
$msgInfo['content']=$newContent; |
||||
|
$msgInfo['fromUser']=[ |
||||
|
'id'=>'-2', |
||||
|
'avatar'=>getMainHost().'/static/common/img/ai.png', |
||||
|
'displayName'=>lang('system.ai') |
||||
|
]; |
||||
|
wsSendMsg($uid,'simple',$msgInfo); |
||||
|
|
||||
|
return success('', $msgInfo); |
||||
|
} |
||||
|
|
||||
|
protected function curlChat($content) |
||||
|
{ |
||||
|
$url = "https://api.siliconflow.cn/v1/chat/completions"; |
||||
|
$data = [ |
||||
|
'model' => 'deepseek-ai/Deepseek-V3', |
||||
|
'max_tokens' => 512, |
||||
|
'enable_thinking' => true, |
||||
|
'thinking_budget' => 4096, |
||||
|
'min_p' => 0.05, |
||||
|
'temperature' => 0.7, |
||||
|
'top_p' => 0.7, |
||||
|
'frequency_penalty' => 0.5, |
||||
|
'n' => 1, |
||||
|
'stream' => false, |
||||
|
'stop' => [], |
||||
|
'messages' => [ |
||||
|
[ |
||||
|
'role' => 'system', |
||||
|
'content' => $content |
||||
|
] |
||||
|
], |
||||
|
]; |
||||
|
$key = 'sk-tfvzhlgatdoczhfcwgfruetxogmrhcyhhqdirwefadmvfibs'; |
||||
|
$header = [ |
||||
|
"Authorization: Bearer {$key}", |
||||
|
'Content-Type: application/json' |
||||
|
]; |
||||
|
$json_data = json_encode($data); |
||||
|
$curl = curl_init(); |
||||
|
|
||||
|
curl_setopt_array($curl, [ |
||||
|
CURLOPT_URL => $url, |
||||
|
CURLOPT_RETURNTRANSFER => true, |
||||
|
CURLOPT_ENCODING => "", |
||||
|
CURLOPT_MAXREDIRS => 10, |
||||
|
CURLOPT_TIMEOUT => 30, |
||||
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||||
|
CURLOPT_CUSTOMREQUEST => "POST", |
||||
|
CURLOPT_POSTFIELDS => $json_data, |
||||
|
CURLOPT_HTTPHEADER => $header, |
||||
|
]); |
||||
|
|
||||
|
// 跳过证书验证(不推荐在生产环境使用) |
||||
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
||||
|
|
||||
|
$response = curl_exec($curl); |
||||
|
$err = curl_error($curl); |
||||
|
|
||||
|
curl_close($curl); |
||||
|
|
||||
|
if ($err) { |
||||
|
$this->error = "cURL Error #:" . $err; |
||||
|
return false; |
||||
|
} |
||||
|
$response = json_decode($response,true); |
||||
|
if (!is_array($response)) { |
||||
|
$this->error = "cURL Error #:" . $response; |
||||
|
return false; |
||||
|
} |
||||
|
return $response; |
||||
|
} |
||||
|
} |
||||
|
After Width: | Height: | Size: 100 KiB |
Loading…
Reference in new issue