2023-09-12 12:36:32 +08:00
|
|
|
|
<?php
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
|
|
|
|
// | 开源版本可自由商用,可去除界面版权logo
|
|
|
|
|
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
|
|
|
|
// | github下载:https://github.com/likeshop-github/likeadmin
|
|
|
|
|
// | 访问官网:https://www.likeadmin.cn
|
|
|
|
|
// | likeadmin团队 版权所有 拥有最终解释权
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | author: likeadminTeam
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
namespace app\api\controller;
|
|
|
|
|
|
2023-09-12 15:20:31 +08:00
|
|
|
|
use IFlytek\Xfyun\Speech\ChatClient;
|
2023-10-09 22:39:21 +08:00
|
|
|
|
use IFlytek\Xfyun\Speech\IatClient;
|
2023-09-12 15:20:31 +08:00
|
|
|
|
use WebSocket\Client;
|
2023-09-12 12:36:32 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 讯飞
|
|
|
|
|
* Class WechatController
|
|
|
|
|
*/
|
|
|
|
|
class XunFeiController extends BaseApiController
|
|
|
|
|
{
|
2023-10-09 20:00:58 +08:00
|
|
|
|
public array $notNeedLogin = ['chat', 'iat'];
|
2023-09-12 12:36:32 +08:00
|
|
|
|
|
|
|
|
|
private $app_id='2eda6c2e';
|
|
|
|
|
|
2023-09-12 15:20:31 +08:00
|
|
|
|
private $api_key='12ec1f9d113932575fc4b114a2f60ffd';
|
2023-09-12 12:36:32 +08:00
|
|
|
|
|
2023-09-12 15:20:31 +08:00
|
|
|
|
private $api_secret='MDEyMzE5YTc5YmQ5NjMwOTU1MWY4N2Y2';
|
2023-09-12 12:36:32 +08:00
|
|
|
|
|
2023-09-12 15:20:31 +08:00
|
|
|
|
public function chat()
|
2023-09-12 12:36:32 +08:00
|
|
|
|
{
|
2023-09-12 16:44:15 +08:00
|
|
|
|
header('X-Accel-Buffering: no');
|
2023-09-12 15:22:32 +08:00
|
|
|
|
$parmas=$this->request->param('content');
|
|
|
|
|
if(empty($parmas)){
|
|
|
|
|
return $this->success('success');
|
|
|
|
|
}
|
2023-09-12 15:20:31 +08:00
|
|
|
|
$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 服务器
|
2023-09-12 15:22:32 +08:00
|
|
|
|
$data = $this->getBody($this->app_id,$parmas);
|
2023-09-12 15:20:31 +08:00
|
|
|
|
|
|
|
|
|
$client->send($data);
|
|
|
|
|
// 从 WebSocket 服务器接收数据
|
|
|
|
|
$answer = "";
|
|
|
|
|
while(true){
|
|
|
|
|
$response = $client->receive();
|
|
|
|
|
$resp = json_decode($response,true);
|
|
|
|
|
$code = $resp["header"]["code"];
|
2023-09-12 16:56:36 +08:00
|
|
|
|
// echo "从服务器接收到的数据: " . $response;
|
2023-09-12 15:20:31 +08:00
|
|
|
|
if(0 == $code){
|
|
|
|
|
$status = $resp["header"]["status"];
|
|
|
|
|
if($status != 2){
|
|
|
|
|
$content = $resp['payload']['choices']['text'][0]['content'];
|
|
|
|
|
$answer .= $content;
|
2023-09-12 16:44:15 +08:00
|
|
|
|
print($answer);
|
|
|
|
|
ob_flush(); // 刷新输出缓冲区
|
|
|
|
|
flush(); // 刷新系统输出缓冲区
|
2023-09-12 15:20:31 +08:00
|
|
|
|
}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);
|
2023-09-12 16:44:15 +08:00
|
|
|
|
ob_flush(); // 刷新输出缓冲区
|
|
|
|
|
flush(); // 刷新系统输出缓冲区
|
2023-09-12 15:20:31 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}else{
|
2023-09-12 16:56:36 +08:00
|
|
|
|
return $this->fail( "服务返回报错".$response);
|
2023-09-12 15:20:31 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-12 16:44:15 +08:00
|
|
|
|
ob_flush(); // 刷新输出缓冲区
|
|
|
|
|
flush(); // 刷新系统输出缓冲区
|
|
|
|
|
return $this->success('success');
|
2023-09-12 15:20:31 +08:00
|
|
|
|
|
|
|
|
|
} else {
|
2023-09-12 16:44:15 +08:00
|
|
|
|
return $this->fail('无法连接到 WebSocket 服务器');
|
2023-09-12 15:20:31 +08:00
|
|
|
|
}
|
2023-09-12 12:36:32 +08:00
|
|
|
|
}
|
2023-09-12 15:20:31 +08:00
|
|
|
|
|
2023-10-09 20:00:58 +08:00
|
|
|
|
//构造参数体
|
|
|
|
|
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()
|
|
|
|
|
{
|
2023-10-09 22:39:21 +08:00
|
|
|
|
header('X-Accel-Buffering: no');
|
2023-10-09 23:40:07 +08:00
|
|
|
|
$st = microtime(true);
|
2023-10-09 22:39:21 +08:00
|
|
|
|
|
2023-10-09 23:40:07 +08:00
|
|
|
|
// $this->app_id = "fa185cd6";
|
|
|
|
|
// $this->api_secret = "1474397d85f34828194622aab80f1e51";
|
|
|
|
|
// $this->api_key = "ZjQOYjhjMmE2NmMzYzhiMjU30GE1NjJl";
|
2023-10-09 20:00:58 +08:00
|
|
|
|
|
2023-10-09 22:39:21 +08:00
|
|
|
|
$hostUrl = "wss://iat-api.xfyun.cn/v2/iat";
|
2023-10-09 20:00:58 +08:00
|
|
|
|
|
2023-10-09 22:39:21 +08:00
|
|
|
|
// $iat=new IatClient($appid,$apiKey,$apiSecret);
|
2023-10-09 23:40:07 +08:00
|
|
|
|
$iat = new IatClient($this->app_id,$this->api_key,$this->api_secret);
|
|
|
|
|
// halt($iat->assembleAuthUrl($hostUrl));
|
2023-10-09 22:39:21 +08:00
|
|
|
|
$client = new Client($iat->assembleAuthUrl($hostUrl));
|
2023-10-09 23:40:07 +08:00
|
|
|
|
if ($client) {
|
|
|
|
|
// $file = app()->getRuntimePath() . 'iat_mp3_16k.mp3';
|
|
|
|
|
$file = "https://lihai001.oss-cn-chengdu.aliyuncs.com/media/iat_mp3_16k.mp3";
|
|
|
|
|
$audioFile = fopen($file, 'rb');
|
|
|
|
|
$frameSize = 1280; //每一帧的音频大小
|
|
|
|
|
$intervel = 40 * 1000; //发送音频间隔
|
|
|
|
|
$status = 0;
|
|
|
|
|
while (true) {
|
|
|
|
|
$len = fread($audioFile, $frameSize);
|
|
|
|
|
if ($len === false) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if ($len === '') { //文件读取完了
|
|
|
|
|
$status = 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch ($status) {
|
|
|
|
|
case 0: //发送第一帧音频,带business 参数
|
|
|
|
|
$frameData = array(
|
|
|
|
|
'common' => array(
|
|
|
|
|
'app_id' => $this->app_id //appid 必须带上,只需第一帧发送
|
|
|
|
|
),
|
|
|
|
|
'business' => array( //business 参数,只需一帧发送
|
|
|
|
|
'language' => 'zh_cn',
|
|
|
|
|
'domain' => 'iat',
|
|
|
|
|
'accent' => 'mandarin'
|
|
|
|
|
),
|
|
|
|
|
'data' => array(
|
|
|
|
|
'status' => 0,
|
|
|
|
|
'format' => 'audio/L16;rate=16000',
|
|
|
|
|
'audio' => base64_encode($len),
|
|
|
|
|
'encoding' => 'lame'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
$client->send(json_encode($frameData));
|
|
|
|
|
$status = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
$frameData = array(
|
|
|
|
|
'data' => array(
|
|
|
|
|
'status' => 1,
|
|
|
|
|
'format' => 'audio/L16;rate=16000',
|
|
|
|
|
'audio' => base64_encode($len),
|
|
|
|
|
'encoding' => 'raw'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
$client->send(json_encode($frameData));
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
$frameData = array(
|
|
|
|
|
'data' => array(
|
|
|
|
|
'status' => 2,
|
|
|
|
|
'format' => 'audio/L16;rate=16000',
|
|
|
|
|
'audio' => base64_encode($len),
|
|
|
|
|
'encoding' => 'raw'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
$client->send(json_encode($frameData));
|
|
|
|
|
break 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//模拟音频采样间隔
|
|
|
|
|
usleep($intervel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
$response = $client->receive();
|
|
|
|
|
if ($response === null) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
$resp = json_decode($response,true);
|
|
|
|
|
$code = $resp['code'];
|
|
|
|
|
$message = $resp['message'];
|
|
|
|
|
$data = $resp['data'];
|
|
|
|
|
$result = $data['result'];
|
|
|
|
|
$status = $data['status'];
|
|
|
|
|
$sid = $resp['sid'];
|
|
|
|
|
|
|
|
|
|
if ($code != 0) {
|
2023-10-09 23:44:38 +08:00
|
|
|
|
// echo sprintf('%d %s %f', $code, $message, microtime(true) - $st);
|
2023-10-09 23:40:07 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-09 23:42:58 +08:00
|
|
|
|
echo json_encode($result);
|
2023-10-09 23:40:07 +08:00
|
|
|
|
if ($status === 2) {
|
2023-10-09 23:44:38 +08:00
|
|
|
|
// echo sprintf('%d %s %f', $code, $message, microtime(true) - $st);
|
2023-10-09 23:40:07 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return $this->fail('无法连接到 WebSocket 服务器');
|
|
|
|
|
}
|
|
|
|
|
fclose($audioFile);
|
2023-10-09 20:00:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 15:20:31 +08:00
|
|
|
|
|
2023-09-12 12:36:32 +08:00
|
|
|
|
}
|