Index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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'){//任务完成
  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. }else{
  79. $result = array();
  80. $res = array('error' => 1, 'errortip' => '任务解析失败', 'result' => $result);
  81. exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  82. }
  83. // elseif($taskone['body']['Data']['Result']['ONGOING']){//任务进行中
  84. //
  85. // $res = array('error' => 1, 'errortip' => '任务解析失败', 'result' => array());
  86. // exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  87. // }elseif($taskone['body']['Data']['Result']['FAILED']){//任务失败
  88. //
  89. // $res = array('error' => 1, 'errortip' => '任务解析失败', 'result' => array());
  90. // exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  91. // }elseif($taskone['body']['Data']['Result']['INVALID']){//无效任务
  92. //
  93. // $res = array('error' => 1, 'errortip' => '任务解析失败', 'result' => array());
  94. // exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  95. // }
  96. }else{
  97. $result = array();
  98. $res = array('error' => 1, 'errortip' => '任务解析失败', 'result' => $result);
  99. exit(json_encode($res,JSON_UNESCAPED_UNICODE));
  100. }
  101. // print_r($result);
  102. die;
  103. ajax_return(array('error' => $this->error, 'errortip' => $this->errortip, 'result' => array()));
  104. }
  105. /**
  106. * 使用AK&SK初始化账号Client
  107. * @return OpenApiClient Client
  108. */
  109. public static function createClient(){
  110. // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
  111. // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/311677.html。
  112. $config = new Config([
  113. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
  114. "accessKeyId" => Env::get("AliyunTingwu.AccessKeyId"),
  115. // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
  116. "accessKeySecret" => Env::get("AliyunTingwu.AccessKeySecret")
  117. ]);
  118. // Endpoint 请参考 https://api.aliyun.com/product/tingwu
  119. $config->endpoint = "tingwu.cn-beijing.aliyuncs.com";
  120. return new OpenApiClient($config);
  121. }
  122. /**
  123. * API 相关
  124. * @return Params OpenApi.Params
  125. */
  126. public static function createTaskApiInfo(){
  127. $params = new Params([
  128. // 接口名称
  129. "action" => "CreateTask",
  130. // 接口版本
  131. "version" => "2023-09-30",
  132. // 接口协议
  133. "protocol" => "HTTPS",
  134. // 接口 HTTP 方法
  135. "method" => "PUT",
  136. "authType" => "AK",
  137. "style" => "ROA",
  138. // 接口 PATH
  139. "pathname" => "/openapi/tingwu/v2/tasks",
  140. // 接口请求体内容格式
  141. "reqBodyType" => "json",
  142. // 接口响应体内容格式
  143. "bodyType" => "json"
  144. ]);
  145. return $params;
  146. }
  147. /**
  148. * @param string[] $args
  149. * @return void
  150. */
  151. public static function createTaskOneInfo($audiourl='',$overview='',$taskKey=''){
  152. $client = self::createClient();
  153. $params = self::createTaskApiInfo();
  154. // query params
  155. $queries = [];
  156. $queries["type"] = "offline";
  157. // body params
  158. // $body = [
  159. // "AppKey" => "jB3lTWgWwCCmO12g",
  160. // "Input" => [
  161. // "SourceLanguage" => "cn",
  162. // "FileUrl" => "https://crmapi.kedingdang.com/Intentionclient/getHuijieCallidRecordingFile?callid=9903ca31-b0c5-4d9b-aad1-cd6f97f87c43"
  163. // ]
  164. // ];
  165. // --- 之前模拟的数据
  166. //设置数据项 - - 第五层
  167. $contents = array();
  168. $contents['Name'] = "test";
  169. $contents['Prompt'] = $overview?($overview.":\n {Transcription}"):"对话中客户表达的完整内容输出出来,并且请帮我将下面的对话进行总结,分析对话中客户是否有下单意愿以及商品是否感兴趣:\n {Transcription}";
  170. $contents['Model'] = "tingwu-turbo";
  171. $contents['TransType'] = "chat";
  172. //设置数据项 - - 第四层
  173. $customPrompt = array();
  174. $customPrompt['Contents'] = $contents;
  175. //设置数据项 - - 第三层
  176. $parameters = array();
  177. $parameters['CustomPromptEnabled'] = 'True';
  178. $parameters['CustomPrompt'] = $customPrompt;
  179. $inputone = array();
  180. $inputone['SourceLanguage'] = 'cn';
  181. $inputone['TaskKey'] = $taskKey?$taskKey:'task'.time();
  182. $inputone['FileUrl'] = $audiourl?$audiourl:'https://crmapi.kedingdang.com/Intentionclient/getHuijieCallidRecordingFile?callid=9903ca31-b0c5-4d9b-aad1-cd6f97f87c43';
  183. //设置数据项 - - 第二层
  184. $body = array();
  185. $body['AppKey'] = 'jB3lTWgWwCCmO12g';
  186. $body['Input'] = $inputone;
  187. $body['Parameters'] = $parameters;
  188. // runtime options
  189. $runtime = new RuntimeOptions([]);
  190. $request = new OpenApiRequest([
  191. "query" => OpenApiUtilClient::query($queries),
  192. "body" => $body
  193. ]);
  194. // 复制代码运行请自行打印 API 的返回值
  195. // 返回值实际为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
  196. $result = $client->callApi($params, $request, $runtime);
  197. return $result;
  198. }
  199. /**
  200. * API 相关
  201. * @param string $TaskId
  202. * @return Params OpenApi.Params
  203. */
  204. public static function getTaskApiInfo($TaskId){
  205. $params = new Params([
  206. // 接口名称
  207. "action" => "GetTaskInfo",
  208. // 接口版本
  209. "version" => "2023-09-30",
  210. // 接口协议
  211. "protocol" => "HTTPS",
  212. // 接口 HTTP 方法
  213. "method" => "GET",
  214. "authType" => "AK",
  215. "style" => "ROA",
  216. // 接口 PATH
  217. "pathname" => "/openapi/tingwu/v2/tasks/" . $TaskId . "",
  218. // 接口请求体内容格式
  219. "reqBodyType" => "json",
  220. // 接口响应体内容格式
  221. "bodyType" => "json"
  222. ]);
  223. return $params;
  224. }
  225. /**
  226. * @param string[] $args
  227. * @return void
  228. */
  229. public static function getTaskOneInfo($args){
  230. $client = self::createClient();
  231. $params = self::getTaskApiInfo($args);//"5125c43149db4e01a2bf92bb6d34128b"
  232. // runtime options
  233. $runtime = new RuntimeOptions([]);
  234. $request = new OpenApiRequest([]);
  235. // 复制代码运行请自行打印 API 的返回值
  236. // 返回值实际为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
  237. $result = $client->callApi($params, $request, $runtime);
  238. return $result;
  239. }
  240. }