Index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 97
  5. */
  6. namespace app\tytw\controller;
  7. use app\BaseController;
  8. use Darabonba\OpenApi\OpenApiClient;
  9. use AlibabaCloud\OpenApiUtil\OpenApiUtilClient;
  10. use Darabonba\OpenApi\Models\Config;
  11. use Darabonba\OpenApi\Models\Params;
  12. use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
  13. use Darabonba\OpenApi\Models\OpenApiRequest;
  14. use think\facade\Env;
  15. class Index extends BaseController{
  16. //创建任务 1
  17. public function addtaskone(){
  18. $token = $_GET['token'];
  19. if($token != md5('TeasxTyts@sg'.date('Y-m-d'))){
  20. $res = array('error' => 1, 'errortip' => '验证失效', 'result' => array());
  21. exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  22. }
  23. $overview = $_GET['overview'];
  24. $audiourl = $_GET['audiourl'];
  25. $taskKey = $_GET['taskKey'];
  26. // if(!$overview){
  27. // $res = array('error' => 1, 'errortip' => '请传入音频解析方向的描述', 'result' => array());
  28. // exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  29. // }
  30. if(!$audiourl){
  31. $res = array('error' => 1, 'errortip' => '请传入音频文件链接', 'result' => array());
  32. exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  33. }
  34. $taskone = $this->createTaskOneInfo($audiourl,$overview,$taskKey);
  35. // print_r($taskone);
  36. //判断返回输出结果
  37. if($taskone['body']['Code'] == '0' && $taskone['body']['Message'] == 'success'){
  38. $result = array();
  39. $result['TaskId'] = $taskone['body']['Data']['TaskId'];
  40. $result['TaskKey'] = $taskone['body']['Data']['TaskKey'];
  41. $result['TaskStatus'] = $taskone['body']['Data']['TaskStatus'];
  42. $res = array('error' => 0, 'errortip' => '任务创建成功', 'result' => $result);
  43. exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  44. }else{
  45. $result = array();
  46. $res = array('error' => 1, 'errortip' => '任务创建失败', 'result' => $result);
  47. exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  48. }
  49. // print_r($result);
  50. die;
  51. ajax_return(array('error' => $this->error, 'errortip' => $this->errortip, 'result' => array()));
  52. }
  53. //解析任务-
  54. public function gettaskinfo(){
  55. $token = $_GET['token'];
  56. if($token != md5('TeasxTyts@sg'.date('Y-m-d'))){
  57. $res = array('error' => 1, 'errortip' => '验证失效', 'result' => array());
  58. exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  59. }
  60. $taskid = $_GET['taskid'];
  61. if(!$taskid){
  62. $res = array('error' => 1, 'errortip' => '请传入听悟任务的ID', 'result' => array());
  63. exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  64. }
  65. $taskone = $this->getTaskOneInfo($taskid);
  66. // print_r($taskone);
  67. //判断返回输出结果
  68. if($taskone['body']['Code'] == '0' && $taskone['body']['Message'] == 'success'){
  69. if($taskone['body']['Data']['TaskStatus'] == 'COMPLETED'){//任务完成 0 成功
  70. $result = array();
  71. $result['TaskId'] = $taskone['body']['Data']['TaskId'];
  72. $result['TaskKey'] = $taskone['body']['Data']['TaskKey'];
  73. $result['TaskStatus'] = $taskone['body']['Data']['TaskStatus'];
  74. $result['Result']['Transcription'] = $taskone['body']['Data']['Result']['Transcription']?$taskone['body']['Data']['Result']['Transcription']:'';
  75. $result['Result']['CustomPrompt'] = $taskone['body']['Data']['Result']['CustomPrompt']?$taskone['body']['Data']['Result']['CustomPrompt']:'';
  76. $res = array('error' => 0, 'errortip' => '任务解析成功', 'result' => $result);
  77. exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  78. }elseif($taskone['body']['Data']['TaskStatus'] == 'ONGOING'){//任务进行中 1
  79. $res = array('error' => 1, 'errortip' => '任务解析进行中', 'result' => array());
  80. exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  81. }else{
  82. $result = array();
  83. $res = array('error' => 2, 'errortip' => '任务解析失败', 'result' => $result);
  84. exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  85. }
  86. // elseif($taskone['body']['Data']['Result']['ONGOING']){//任务进行中
  87. //
  88. // $res = array('error' => 1, 'errortip' => '任务解析失败', 'result' => array());
  89. // exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  90. // }elseif($taskone['body']['Data']['Result']['FAILED']){//任务失败
  91. //
  92. // $res = array('error' => 1, 'errortip' => '任务解析失败', 'result' => array());
  93. // exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  94. // }elseif($taskone['body']['Data']['Result']['INVALID']){//无效任务
  95. //
  96. // $res = array('error' => 1, 'errortip' => '任务解析失败', 'result' => array());
  97. // exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  98. // }
  99. }else{
  100. $result = array();
  101. $res = array('error' => 1, 'errortip' => '任务解析失败', 'result' => $result);
  102. exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  103. }
  104. // print_r($result);
  105. die;
  106. ajax_return(array('error' => $this->error, 'errortip' => $this->errortip, 'result' => array()));
  107. }
  108. /**
  109. * 使用AK&SK初始化账号Client
  110. * @return OpenApiClient Client
  111. */
  112. public static function createClient(){
  113. // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
  114. // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/311677.html。
  115. $config = new Config([
  116. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
  117. "accessKeyId" => Env::get("AliyunTingwu.AccessKeyId"),
  118. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
  119. "accessKeySecret" => Env::get("AliyunTingwu.AccessKeySecret")
  120. ]);
  121. // Endpoint 请参考 https://api.aliyun.com/product/tingwu
  122. $config->endpoint = "tingwu.cn-beijing.aliyuncs.com";
  123. return new OpenApiClient($config);
  124. }
  125. /**
  126. * API 相关
  127. * @return Params OpenApi.Params
  128. */
  129. public static function createTaskApiInfo(){
  130. $params = new Params([
  131. // 接口名称
  132. "action" => "CreateTask",
  133. // 接口版本
  134. "version" => "2023-09-30",
  135. // 接口协议
  136. "protocol" => "HTTPS",
  137. // 接口 HTTP 方法
  138. "method" => "PUT",
  139. "authType" => "AK",
  140. "style" => "ROA",
  141. // 接口 PATH
  142. "pathname" => "/openapi/tingwu/v2/tasks",
  143. // 接口请求体内容格式
  144. "reqBodyType" => "json",
  145. // 接口响应体内容格式
  146. "bodyType" => "json"
  147. ]);
  148. return $params;
  149. }
  150. /**
  151. * @param string[] $args
  152. * @return void
  153. */
  154. public static function createTaskOneInfo($audiourl='',$overview='',$taskKey=''){
  155. $client = self::createClient();
  156. $params = self::createTaskApiInfo();
  157. // query params
  158. $queries = [];
  159. $queries["type"] = "offline";
  160. // body params
  161. // $body = [
  162. // "AppKey" => "jB3lTWgWwCCmO12g",
  163. // "Input" => [
  164. // "SourceLanguage" => "cn",
  165. // "FileUrl" => "https://crmapi.kedingdang.com/Intentionclient/getHuijieCallidRecordingFile?callid=9903ca31-b0c5-4d9b-aad1-cd6f97f87c43"
  166. // ]
  167. // ];
  168. // --- 之前模拟的数据
  169. //设置数据项 - - 第五层
  170. $contents = array();
  171. $contents['Name'] = "test";
  172. $contents['Prompt'] = $overview?($overview.":\n {Transcription}"):"对话中客户表达的完整内容输出出来,并且请帮我将下面的对话进行总结,分析对话中客户是否有下单意愿以及商品是否感兴趣:\n {Transcription}";
  173. $contents['Model'] = "tingwu-turbo";
  174. $contents['TransType'] = "chat";
  175. //设置数据项 - - 第四层
  176. $customPrompt = array();
  177. $customPrompt['Contents'] = $contents;
  178. //设置数据项 - - 第三层
  179. $parameters = array();
  180. $parameters['CustomPromptEnabled'] = 'True';
  181. $parameters['CustomPrompt'] = $customPrompt;
  182. $inputone = array();
  183. $inputone['SourceLanguage'] = 'cn';
  184. $inputone['TaskKey'] = $taskKey?$taskKey:'task'.time();
  185. $inputone['FileUrl'] = $audiourl?$audiourl:'https://crmapi.kedingdang.com/Intentionclient/getHuijieCallidRecordingFile?callid=9903ca31-b0c5-4d9b-aad1-cd6f97f87c43';
  186. //设置数据项 - - 第二层
  187. $body = array();
  188. $body['AppKey'] = 'jB3lTWgWwCCmO12g';
  189. $body['Input'] = $inputone;
  190. $body['Parameters'] = $parameters;
  191. // runtime options
  192. $runtime = new RuntimeOptions([]);
  193. $request = new OpenApiRequest([
  194. "query" => OpenApiUtilClient::query($queries),
  195. "body" => $body
  196. ]);
  197. // 复制代码运行请自行打印 API 的返回值
  198. // 返回值实际为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
  199. $result = $client->callApi($params, $request, $runtime);
  200. return $result;
  201. }
  202. /**
  203. * API 相关
  204. * @param string $TaskId
  205. * @return Params OpenApi.Params
  206. */
  207. public static function getTaskApiInfo($TaskId){
  208. $params = new Params([
  209. // 接口名称
  210. "action" => "GetTaskInfo",
  211. // 接口版本
  212. "version" => "2023-09-30",
  213. // 接口协议
  214. "protocol" => "HTTPS",
  215. // 接口 HTTP 方法
  216. "method" => "GET",
  217. "authType" => "AK",
  218. "style" => "ROA",
  219. // 接口 PATH
  220. "pathname" => "/openapi/tingwu/v2/tasks/" . $TaskId . "",
  221. // 接口请求体内容格式
  222. "reqBodyType" => "json",
  223. // 接口响应体内容格式
  224. "bodyType" => "json"
  225. ]);
  226. return $params;
  227. }
  228. /**
  229. * @param string[] $args
  230. * @return void
  231. */
  232. public static function getTaskOneInfo($args){
  233. $client = self::createClient();
  234. $params = self::getTaskApiInfo($args);//"5125c43149db4e01a2bf92bb6d34128b"
  235. // runtime options
  236. $runtime = new RuntimeOptions([]);
  237. $request = new OpenApiRequest([]);
  238. // 复制代码运行请自行打印 API 的返回值
  239. // 返回值实际为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
  240. $result = $client->callApi($params, $request, $runtime);
  241. return $result;
  242. }
  243. }