123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- /**
- * Created by PhpStorm.
- * User: 97
- */
- namespace app\tytw\controller;
- use app\BaseController;
- use Darabonba\OpenApi\OpenApiClient;
- use AlibabaCloud\OpenApiUtil\OpenApiUtilClient;
- use Darabonba\OpenApi\Models\Config;
- use Darabonba\OpenApi\Models\Params;
- use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
- use Darabonba\OpenApi\Models\OpenApiRequest;
- use think\facade\Env;
- class Index extends BaseController{
- //创建任务 1
- public function addtaskone(){
- $token = $_GET['token'];
- if($token != md5('TeasxTyts@sg'.date('Y-m-d'))){
- $res = array('error' => 1, 'errortip' => '验证失效', 'result' => array());
- exit(json_encode($res,JSON_UNESCAPED_UNICODE));
- }
- $overview = $_GET['overview'];
- $audiourl = $_GET['audiourl'];
- $taskKey = $_GET['taskKey'];
- // if(!$overview){
- // $res = array('error' => 1, 'errortip' => '请传入音频解析方向的描述', 'result' => array());
- // exit(json_encode($res,JSON_UNESCAPED_UNICODE));
- // }
- if(!$audiourl){
- $res = array('error' => 1, 'errortip' => '请传入音频文件链接', 'result' => array());
- exit(json_encode($res,JSON_UNESCAPED_UNICODE));
- }
- $taskone = $this->createTaskOneInfo($audiourl,$overview,$taskKey);
- // print_r($taskone);
- //判断返回输出结果
- if($taskone['body']['Code'] == '0' && $taskone['body']['Message'] == 'success'){
- $result = array();
- $result['TaskId'] = $taskone['body']['Data']['TaskId'];
- $result['TaskKey'] = $taskone['body']['Data']['TaskKey'];
- $result['TaskStatus'] = $taskone['body']['Data']['TaskStatus'];
- $res = array('error' => 0, 'errortip' => '任务创建成功', 'result' => $result);
- exit(json_encode($res,JSON_UNESCAPED_UNICODE));
- }else{
- $result = array();
- $res = array('error' => 1, 'errortip' => '任务创建失败', 'result' => $result);
- exit(json_encode($res,JSON_UNESCAPED_UNICODE));
- }
- print_r($result);
- die;
- ajax_return(array('error' => $this->error, 'errortip' => $this->errortip, 'result' => array()));
- }
- //解析任务-
- public function gettaskinfo(){
- $token = $_GET['token'];
- if($token != md5('TeasxTyts@sg'.date('Y-m-d'))){
- $res = array('error' => 1, 'errortip' => '验证失效', 'result' => array());
- exit(json_encode($res,JSON_UNESCAPED_UNICODE));
- }
- $taskid = $_GET['taskid'];
- if(!$taskid){
- $res = array('error' => 1, 'errortip' => '请传入听悟任务的ID', 'result' => array());
- exit(json_encode($res,JSON_UNESCAPED_UNICODE));
- }
- $taskone = $this->getTaskOneInfo($taskid);
- // print_r($taskone);
- //判断返回输出结果
- if($taskone['body']['Code'] == '0' && $taskone['body']['Message'] == 'success'){
- $result = array();
- $result['TaskId'] = $taskone['body']['Data']['TaskId'];
- $result['TaskKey'] = $taskone['body']['Data']['TaskKey'];
- $result['TaskStatus'] = $taskone['body']['Data']['TaskStatus'];
- $result['Result']['Transcription'] = $taskone['body']['Data']['Result']['Transcription']?$taskone['body']['Data']['Result']['Transcription']:'';
- $result['Result']['CustomPrompt'] = $taskone['body']['Data']['Result']['CustomPrompt']?$taskone['body']['Data']['Result']['CustomPrompt']:'';
- $res = array('error' => 0, 'errortip' => '任务解析成功', 'result' => $result);
- exit(json_encode($res,JSON_UNESCAPED_UNICODE));
- }else{
- $result = array();
- $res = array('error' => 1, 'errortip' => '任务解析失败', 'result' => $result);
- exit(json_encode($res,JSON_UNESCAPED_UNICODE));
- }
- print_r($result);
- die;
- ajax_return(array('error' => $this->error, 'errortip' => $this->errortip, 'result' => array()));
- }
- /**
- * 使用AK&SK初始化账号Client
- * @return OpenApiClient Client
- */
- public static function createClient(){
- // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
- // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/311677.html。
- $config = new Config([
- // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
- "accessKeyId" => Env::get("AliyunTingwu.AccessKeyId"),
- // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
- "accessKeySecret" => Env::get("AliyunTingwu.AccessKeySecret")
- ]);
- // Endpoint 请参考 https://api.aliyun.com/product/tingwu
- $config->endpoint = "tingwu.cn-beijing.aliyuncs.com";
- return new OpenApiClient($config);
- }
- /**
- * API 相关
- * @return Params OpenApi.Params
- */
- public static function createTaskApiInfo(){
- $params = new Params([
- // 接口名称
- "action" => "CreateTask",
- // 接口版本
- "version" => "2023-09-30",
- // 接口协议
- "protocol" => "HTTPS",
- // 接口 HTTP 方法
- "method" => "PUT",
- "authType" => "AK",
- "style" => "ROA",
- // 接口 PATH
- "pathname" => "/openapi/tingwu/v2/tasks",
- // 接口请求体内容格式
- "reqBodyType" => "json",
- // 接口响应体内容格式
- "bodyType" => "json"
- ]);
- return $params;
- }
- /**
- * @param string[] $args
- * @return void
- */
- public static function createTaskOneInfo($audiourl='',$overview='',$taskKey=''){
- $client = self::createClient();
- $params = self::createTaskApiInfo();
- // query params
- $queries = [];
- $queries["type"] = "offline";
- // body params
- // $body = [
- // "AppKey" => "jB3lTWgWwCCmO12g",
- // "Input" => [
- // "SourceLanguage" => "cn",
- // "FileUrl" => "https://crmapi.kedingdang.com/Intentionclient/getHuijieCallidRecordingFile?callid=9903ca31-b0c5-4d9b-aad1-cd6f97f87c43"
- // ]
- // ];
- // --- 之前模拟的数据
- //设置数据项 - - 第五层
- $contents = array();
- $contents['Name'] = "test";
- $contents['Prompt'] = $overview?($overview.":\n {Transcription}"):"对话中客户表达的完整内容输出出来,并且请帮我将下面的对话进行总结,分析对话中客户是否有下单意愿以及商品是否感兴趣:\n {Transcription}";
- $contents['Model'] = "tingwu-turbo";
- $contents['TransType'] = "chat";
- //设置数据项 - - 第四层
- $customPrompt = array();
- $customPrompt['Contents'] = $contents;
- //设置数据项 - - 第三层
- $parameters = array();
- $parameters['CustomPromptEnabled'] = 'True';
- $parameters['CustomPrompt'] = $customPrompt;
- $inputone = array();
- $inputone['SourceLanguage'] = 'cn';
- $inputone['TaskKey'] = $taskKey?$taskKey:'task'.time();
- $inputone['FileUrl'] = $audiourl?$audiourl:'https://crmapi.kedingdang.com/Intentionclient/getHuijieCallidRecordingFile?callid=9903ca31-b0c5-4d9b-aad1-cd6f97f87c43';
- //设置数据项 - - 第二层
- $body = array();
- $body['AppKey'] = 'jB3lTWgWwCCmO12g';
- $body['Input'] = $inputone;
- $body['Parameters'] = $parameters;
- // runtime options
- $runtime = new RuntimeOptions([]);
- $request = new OpenApiRequest([
- "query" => OpenApiUtilClient::query($queries),
- "body" => $body
- ]);
- // 复制代码运行请自行打印 API 的返回值
- // 返回值实际为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
- $result = $client->callApi($params, $request, $runtime);
- return $result;
- }
- /**
- * API 相关
- * @param string $TaskId
- * @return Params OpenApi.Params
- */
- public static function getTaskApiInfo($TaskId){
- $params = new Params([
- // 接口名称
- "action" => "GetTaskInfo",
- // 接口版本
- "version" => "2023-09-30",
- // 接口协议
- "protocol" => "HTTPS",
- // 接口 HTTP 方法
- "method" => "GET",
- "authType" => "AK",
- "style" => "ROA",
- // 接口 PATH
- "pathname" => "/openapi/tingwu/v2/tasks/" . $TaskId . "",
- // 接口请求体内容格式
- "reqBodyType" => "json",
- // 接口响应体内容格式
- "bodyType" => "json"
- ]);
- return $params;
- }
- /**
- * @param string[] $args
- * @return void
- */
- public static function getTaskOneInfo($args){
- $client = self::createClient();
- $params = self::getTaskApiInfo($args);//"5125c43149db4e01a2bf92bb6d34128b"
- // runtime options
- $runtime = new RuntimeOptions([]);
- $request = new OpenApiRequest([]);
- // 复制代码运行请自行打印 API 的返回值
- // 返回值实际为 Map 类型,可从 Map 中获得三类数据:响应体 body、响应头 headers、HTTP 返回的状态码 statusCode。
- $result = $client->callApi($params, $request, $runtime);
- return $result;
- }
- }
|