From fe9eb7cb411128f589c1400d096e0a4274e4ac3c Mon Sep 17 00:00:00 2001 From: yaooo <272523191@qq.com> Date: Sat, 14 Oct 2023 11:56:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=98=9F=E7=81=AB=E8=AE=A4?= =?UTF-8?q?=E7=9F=A5=E6=A8=A1=E5=9E=8B=E8=81=8A=E5=A4=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/XunFeiController.php | 87 +++++++------------ .../model/informationg/UserInformationg.php | 1 + 2 files changed, 32 insertions(+), 56 deletions(-) diff --git a/app/api/controller/XunFeiController.php b/app/api/controller/XunFeiController.php index 3e017dc79..4fc3cf4c0 100644 --- a/app/api/controller/XunFeiController.php +++ b/app/api/controller/XunFeiController.php @@ -41,90 +41,65 @@ class XunFeiController extends BaseApiController public function chat() { header('X-Accel-Buffering: no'); - $parmas=$this->request->param('content'); - if(empty($parmas)){ - return $this->success('success'); + $content = $this->request->param('content'); + if(empty($content)){ + return $this->data(['answer' => '']); } $chat=new ChatClient($this->app_id,$this->api_key,$this->api_secret); $client = new Client($chat->assembleAuthUrl('wss://spark-api.xf-yun.com/v2.1/chat')); // 连接到 WebSocket 服务器 if ($client) { - // 发送数据到 WebSocket 服务器 - $data = $this->getBody($this->app_id,$parmas); + $header = [ + "app_id" => $this->app_id, + "uid" => "1" + ]; + $parameter = [ + "chat" => [ + "domain" => "generalv2", + "temperature" => 0.5, + "max_tokens" => 1024 + ] + ]; + $payload = [ + "message" => [ + "text" => [ + ["role" => "user", "content" => $content] + ] + ] + ]; + $data = json_encode([ + "header" => $header, + "parameter" => $parameter, + "payload" => $payload + ]); $client->send($data); - // 从 WebSocket 服务器接收数据 $answer = ""; while(true){ $response = $client->receive(); - $resp = json_decode($response,true); - $code = $resp["header"]["code"]; - // echo "从服务器接收到的数据: " . $response; + $resp = json_decode($response, true); + $code = $resp["header"]["code"] ?? 0; if(0 == $code){ $status = $resp["header"]["status"]; if($status != 2){ $content = $resp['payload']['choices']['text'][0]['content']; $answer .= $content; - print($answer); - ob_flush(); // 刷新输出缓冲区 - flush(); // 刷新系统输出缓冲区 }else{ $content = $resp['payload']['choices']['text'][0]['content']; $answer .= $content; - $total_tokens = $resp['payload']['usage']['text']['total_tokens']; - print("\n本次消耗token用量:\n"); - print($total_tokens); - ob_flush(); // 刷新输出缓冲区 - flush(); // 刷新系统输出缓冲区 break; } }else{ - return $this->fail( "服务返回报错".$response); + return $this->fail( "服务返回报错 " . $response); break; } } - ob_flush(); // 刷新输出缓冲区 - flush(); // 刷新系统输出缓冲区 - return $this->success('success'); - + return $this->data(['answer' => $answer]); } else { return $this->fail('无法连接到 WebSocket 服务器'); } } - //构造参数体 - function getBody($appid,$question){ - $header = array( - "app_id" => $appid, - "uid" => "1" - ); - - $parameter = array( - "chat" => array( - "domain" => "generalv2", - "temperature" => 0.5, - "max_tokens" => 1024 - ) - ); - - $payload = array( - "message" => array( - "text" => array( - array("role" => "user", "content" => $question) - ) - ) - ); - - $json_string = json_encode(array( - "header" => $header, - "parameter" => $parameter, - "payload" => $payload - )); - - return $json_string; - - } - //语音听写(流式版) public function iat() { @@ -147,7 +122,7 @@ class XunFeiController extends BaseApiController $last_file = substr($savename, -36); $copyFile = app()->getRootPath() . '/public/uploads/iat/' . $last_file; - // 临时方案 + // ********** 临时方案 ********** // copy($file, $copyFile); // $last_file = 'a1fcdd96c7967b48add17b52ab456368.mp3'; $curl_url = "https://dev.app.tword.cn/ffmpeg.php?file={$last_file}"; diff --git a/app/common/model/informationg/UserInformationg.php b/app/common/model/informationg/UserInformationg.php index d0879494a..fec977ef0 100644 --- a/app/common/model/informationg/UserInformationg.php +++ b/app/common/model/informationg/UserInformationg.php @@ -93,6 +93,7 @@ class UserInformationg extends BaseModel $category_child = $param['card_id']; $field_array = json_decode($category_info['data_field'], true); if (!empty($field_array) && is_array($field_array)) { + // 拼装词语 foreach($param['datas'] as $k => $v) { if (!empty($field_array[$k]['text'])) { $key = $field_array[$k]['text'];