Index.php 8.6 KB

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