基于科大讯飞AIUI平台自定义语义库的开发

说明:我写这篇文章的主要目的是因为我在做这块的时候遇到过一些坑,也是希望后来者能少走一些弯路。

科大讯飞AIUI开放平台地址:https://aiui.xfyun.cn

科大讯飞AIUI开放平台后处理地址:https://aiui.xfyun.cn/myApp/config/5bf22b4e



1. 科大讯飞接口服务类:

<?php
namespace service;

/**
 * 科大讯飞AIUI服务
 * Class AIUIService
 * @package service
 */
class AIUIService
{
    const APP_ID = '*****';                    //讯飞AIUI开放平台注册申请应用的应用ID(appid)
    const API_KEY = '****';                    //接口密钥,由讯飞AIUI开放平台提供,调用方管理
    const TOKEN = '*****';                     //后处理token
    const AES_KEY = '*****';                   //加密AES KEY

    /**
     * @title 构造函数
     * @param string $key 密钥
     * @param string $method 加密方式
     * @param string $iv iv向量
     * @param mixed $options 还不是很清楚
     */
    public function __construct()
    {
        $this->token = self::TOKEN;

        // key是必须要设置的
        $this->secret_key = self::AES_KEY;
        $this->method = "AES-128-CBC";
        $this->iv = self::AES_KEY;
        $this->options = OPENSSL_RAW_DATA;
    }

    /**
     * @title 签名验证
     * @param $token token
     * @param $timestamp 时间戳
     * @param $rand 随机数
     * @param $aesKey $aesKey
     * @param $sign 客户端请求接口sign参数值
     * @return INT
     */
    public function checkAuth($sign,$timestamp,$rand,$key='')
    {
        //按规则拼接为字符串
        $str = self::createSignature($this->token,$timestamp,$rand,$key);
        ///校验签名字符串:0为一致、-1为不一致
        if ($str !== $sign) {
            return -1;
        }
        return 0;
    }

    /**
     * @title 生成签名
     * @param $token
     * @param $timestamp
     * @param $rand
     * @param string $aesKey
     * @return string
     */
    private static function createSignature($token,$timestamp,$rand,$key='')
    {
        //组装要排序的数组
        $arr = [$timestamp,$token,$rand];
        //字典序排序
        sort($arr);
        //拼接为一个字符串
        $str = implode('',$arr);
        //sha1加密
        return sha1($str);
    }

    /**
     * @title 加密
     * @param $plaintext string 要加密的字符串
     * @return string
     */
    public function encrypt($plaintext){
        //加密采用AES的CBC加密方式,秘钥为16字节(128bit),初始化向量复用秘钥,填充方式为PKCS7Padding。
        //返回的消息要以同样的方式进行加密。
        //加密过程:padding->CBC加密->base64编码
        //$option 以下标记的按位或: OPENSSL_RAW_DATA 原生数据,对应数字1,不进行 base64 编码。OPENSSL_ZERO_PADDING 数据进行 base64 编码再返回,对应数字0。
        return openssl_encrypt($plaintext, $this->method, $this->secret_key,$this->options, $this->iv);
    }

    /**
     * @title 解密
     * @param $ciphertext string 要解密的字符串
     * @return string
     */
    public function decrypt($ciphertext){
        //解密过程:base64解码->CBC解密->unpadding
        return openssl_decrypt($ciphertext, $this->method, $this->secret_key, $this->options, $this->iv);
    }
}

2. 控制器处理:

接口调用示例:



注: 其他具体接入过程可参见文档:https://aiui.xfyun.cn/docs/access_docs

<?php
namespace app\api\controller;

use service\AIUIService;
use service\SimilarityMatch;
use think\Db;

/**
 * @title 科大讯飞自定义语义库
 * @class AiUi
 * @auth 邹柯
 * @date 2018-11-19
 */
class AiUi
{
    /**
     * @title 科大讯飞自定义语义库
     * @return json
     */
    public function accessVerification()
    {
        //接收参数
        $param = request()->param(false);
        //判断接口访问方式:POST/GET
        $res = isset($_SERVER['REQUEST_METHOD']) && !strcasecmp($_SERVER['REQUEST_METHOD'],'POST');
        if($res) { //POST
            //是否加密:encrypttype=aes-加密、raw-不加密
            $encrypttype = $param[ 'encrypttype'];

            //解密
            if($encrypttype == "aes"){
                $DeMsgContent = (new AIUIService())->decrypt(file_get_contents("php://input"));
                $DeMsgContent = json_decode($DeMsgContent,true);
            }else{
                //消息内容
                $MsgContent = $param['Msg']['Content'];
                $DeMsgContent = json_decode(base64_decode($MsgContent),true);
                //调试代码
                p($DeMsgContent);
            }
            //组装要返回的问题答案
            //应答码(response code),0-操作成功、4-文本没有匹配的技能场景,技能不理解或不能处理该文本
            if(!empty($DeMsgContent["intent"]) && $DeMsgContent["intent"]["rc"] == 4){
                $msg = $this->getMsg($DeMsgContent["intent"]["text"]);
                if(empty($msg)){
                    $msg_text = "这个技能还没学会--测试";
                }else{
                    $msg_text = $msg[0]['answer'];
                }
                $DeMsgContent["intent"]["answer"] =[
                    //通用的文字显示,属于text数据
                    "text"=> $msg_text,
                    //显示的类型,通过这个类型,可以确定数据的返回内容和客户端的显示内容,默认值为 T 。
                    //T:text数据 U:url数据 TU:text+url数据 IT:image+text数据 ITU:image+text+url数据
                    "type"=>"T",
                    //回答的情绪,取值参见附录的情感标签对照表
                    "emotion"=>"default",
                    "answerType"=>"openQA"
                ];
                $DeMsgContent["intent"]["operation"] = "ANSWER";
                $DeMsgContent["intent"]["score"] = 1;
                $DeMsgContent["intent"]["service"] = "openQA";
                $DeMsgContent["intent"]["status"] = 0;
                $DeMsgContent["intent"]["array_index"] = 0;
                $DeMsgContent["intent"]["engine_time"] = 0.001;
            }
            //加密
            if($encrypttype == "aes"){
                //...
                return (new AIUIService())->encrypt(json_encode($DeMsgContent));
            }else{
                //调试代码
                p(json_encode($DeMsgContent)); 
                echo json_encode($DeMsgContent);
            }
        } else { //GET
            //接收验证签名参数
            $timestamp = $param['timestamp'];
            $rand = $param['rand'];
            $sign = $param['signature'];

            $AIUIService =  new AIUIService();
            $res = $AIUIService->checkAuth($sign,$timestamp,$rand);
            if($res == -1){
                return null;
            }
            //回传后处理 token,token 在官网上有
            echo sha1("sdjdjsodml");
            //调试代码
            /**********************************
            foreach ($_GET as $key=>$value){
                file_put_contents("log.txt", date('H:i:s')." "."_GET: Key: $key; Value: $value"."\r\n", FILE_APPEND);
            }
            **********************************/
        }
    }

    /**
     * @title 根据语义返回要查询问题的答案
     * @param $question 问题
     * @return array
     */
    private function getMsg($question){
         //匹配主关键词
         $keywords_info = Db::query("select * from cr_aiui_keywords where status=1 and LOCATE(keywords,'{$question}') > 0");
         if(!empty($keywords_info)){
             $keywords_str =implode(",",array_unique(array_column($keywords_info,"keywords")));
             $keywords_str2 = "'".str_replace(",","','",$keywords_str)."'";

             //组装查询条件
             $where = "status=1 and keywords in ($keywords_str2)";
             $result = Db::name("cr_aiui_semantic_library")->field("id,second_keywords,answer")->where($where)->select();

             //计算不同匹配结果的匹配次数
             $count = [];
             foreach($result as $k=>$v){
                 $count[$v['id']]['count'] = 0;
                 $second_keywords_array = explode(",",$v['second_keywords']);
                 foreach($second_keywords_array  as $k2=>$v2){
                     $res = strpos($question, $v2);
                     $count[$v['id']]['answer'] = $v['answer'];
                     if($res <> false){
                         $count[$v['id']]['count'] += 1;
                     }
                 }
             }

             $count = array_values($count);

             //多维数组排序
             // 取得列的列表
             foreach ($count as $key => $row){
                 $volume[$key]  = $row['count'];
             }
             array_multisort($volume, SORT_DESC,$count);

             //获取匹配度最大的数组的次数
             $max_count = $count[0]['count'];

             //组装匹配次数最大的结果集
             foreach($count as $k=>$v){
                 if($v['count'] <> $max_count ){
                     unset($count[$k]);
                 }
             }
             $count =  array_values($count);
         }else{
             return null;
         }

         return $count;
    }

下面是测试过程中的测试数据

(1) 测试数据1

接收post过来的参数:
tp5开启调试,查看日志记录:/home/wwwroot/default/CrAdmin/runtime/log/201811/23.log

      array (
            //随机字符串
            'rand' => '04d1L6G2',
            //是否加密:encrypttype=aes加密/raw不加密
            'encrypttype' => 'raw',
            //签名
            'msgsignature' => 'ef64f8ef4ec53a1f7ad30f5c8d312d316e1bd54d',
            'timestamp' => '2018-11-19 17:47:23 +0800 CST',
            //消息id,字符串类型
            'MsgId' => 'cida160a452@dx00a00f4dd2a70100ac1',
            //消息创建时间,整型
            'CreateTime' => 1542620843,
            //开发者应用Id,字符串类型
            'AppId' => '5bf22b4e',
            //AIUI唯一用户标注,字符串类型
            'UserId' => 'd10601259780',
            //本次会话交互参数,Base64格式字符串,解码后为json格式
            'SessionParams' => 'eyJjb250ZXh0Ijp7InNka19zdXBwb3J0IjpbInR0cyIsImlhdCIsIm5scCJdfSwiZGV2X2xhbmciOiJjcHAiLCJkc3JjIjoic2RrIiwiZHR5cGUiOiJhdWRpbyIsImludGVyYWN0X21vZGUiOiJjb250aW51b3VzIiwibXNjLmxhdCI6IiIsIm1zYy5sbmciOiIiLCJvc19zeXMiOiJBbmRyb2lkIiwicHJvdF90eXBlIjoicGIiLCJzY2VuZSI6Im1haW4iLCJzY2l0eSI6ImNoIiwic2RrX3ZlciI6IjUuNS4xMDQxLjAwMDAiLCJzaWQiOiJjaWRhMTYwYTQ1MkBkeDAwYTAwZjRkZDJhNzAxMDBhYyIsInN0bWlkIjoiYXVkaW8tMTcyIiwidmVyX3R5cGUiOiJpbnRlbGxpZ2VudF9oZHciLCJ3YWtlX2lkIjoiMTU0MjYyMDgxMDg2MGU2Y2ZhYWJhNzMwNTg2N2RjNGVmM2Q2OSJ9',
            //开发者自定义参数,通过客户端的userparams参数上传,Base64格式字符串
            'UserParams' => '',
            //游业务类型,目前包括两种(iat:听写结果,kc:语义结果),字符串类型
            'FromSub' => 'kc',
            //消息内容,json object参考Msg消息内容格式
            'Msg' =>
                array (
                    'ContentType' => 'json',
                    'Type' => 'text',
                    'Content' => 'eyJpbnRlbnQiOnsiYW5zd2VyIjp7InRleHQiOiLkuLrmgqjmib7liLDku6XkuIvnu5PmnpwifSwiZGF0YSI6eyJyZXN1bHQiOlt7ImFsaWFzTmFtZXMiOlsiS2FtZW4gUmlkZXIgS3V1Z2EgdnMuIHRoZSBTdHJvbmcgTW9uc3RlciBHby1KaWluby1EYSIsIuiSmemdoui2heS6uuWPpOi/pnZz5Yia5Yqb5oCq5Lq6R2/Ct0ppaW5vwrdEYSzku67pnaLjg6njgqTjg4Djg7zjgq/jgqbjgqxWU+WImuWKm+aAquS6uuOCtOODu+OCuOOCpOODjuODu+ODgCIsIuS7rumdouODqeOCpOODgOODvOOCr+OCpuOCrFZT5Yia5Yqb5oCq5Lq644K044O744K444Kk44OO44O744OAIiwi5YGH6Z2i6aqR5aOr56m65oiRdnPliJrlipvmgKrkurpHb8K3Smlpbm/Ct0RhIiwi6JKZ6Z2i6LaF5Lq65Y+k6L+mdnPliJrlipvmgKrkurpHb8K3Smlpbm/Ct0RhIl0sImRpcmVjdG9ycyI6W3siaWQiOj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IsInNlcnZpY2VUeXBlIjoicHJldmVudGl2ZSIsInNpZCI6ImNpZGExNjBhNDUyQGR4MDBhMDBmNGRkMmE3MDEwMGFjIiwic3RhdGUiOnsiZmc6OnZpZGVvX3NlYXJjaDo6ZGVmYXVsdDo6ZGVmYXVsdCI6e319LCJ0ZXh0Ijoi5oiR5Yia5Y+I6K+05LqG5LiA5Y+l5LuW6K+05LiL6Z2i5piv5Zyo5pCc57Si57uT5p6cIiwidXVpZCI6ImNpZGExNjBhNDUyQGR4MDBhMDBmNGRkMmE3MDEwMGFjIiwiY2lkIjoiY2lkYTE2MGE0NTJAZHgwMGEwMGY0ZGNmYmIwMDAwMDAifX0=',
                ),
        );

(2) 测试数据2

  • base64_encode($SessionParams);
{
    "context":{
        "sdk_support":[
            "tts",
            "iat",
            "nlp"
        ]
    },
    "dev_lang":"cpp",
    "dsrc":"sdk",
    "dtype":"audio",
    "interact_mode":"continuous",
    "msc.lat":"",
    "msc.lng":"",
    "os_sys":"Android",
    "prot_type":"pb",
    "scene":"main",
    "scity":"ch",
    "sdk_ver":"5.5.1041.0000",
    "sid":"cida160a452@dx00a00f4dd2a70100ac",
    "stmid":"audio-172",
    "ver_type":"intelligent_hdw",
    "wake_id":"1542620810860e6cfaaba7305867dc4ef3d69"
}

(3) 测试数据3

  • base64_encode($param[‘Msg’][‘Content’]);

    “`json
    “intent”:{
    “array_index”:0,
    “data”:{
    “inherit”:0,
    “isCached”:0,
    “priority”:0,
    “result”:[
    {
    “albumname”:”答案就是你”,
    “audiopath”:”http://vbox.hf.openstorage.cn/ctimusic/128/2016-01-20/%E5%88%98%E5%BE%B7%E5%8D%8E/%E7%AD%94%E6%A1%88%E5%B0%B1%E6%98%AF%E4%BD%A0/%E6%9A%97%E9%87%8C%E7%9D%80%E8%BF%B71453250743.mp3″,
    “itemid”:”45106597″,
    “movienames”:[],
    “neatsongname”:[
    “暗里着迷”
    ],
    “pictures”:[
    {
    “level”:2,
    “path”:”http://vbox.hfdn.openstorage.cn/ctipicture/2/20160303/580ef3c814d9e33871dfada810f88f3d/09d7de7eb33cc3b65f4e19b43f8bc81e_z.jpg”,
    “size”:”420×420″
    },
    {
    “level”:1,
    “path”:”http://vbox.hfdn.openstorage.cn/ctipicture/2/20160303/580ef3c814d9e33871dfada810f88f3d/09d7de7eb33cc3b65f4e19b43f8bc81e_small.jpg”,
    “size”:”300×300″
    }
    ],
    “programname”:””,
    “publishtime”:752083200,
    “singeraliasnames”:[
    “AndyLau”,
    “刘主席”,
    “华Dee”,
    “华仔”,
    “华哥”,
    “华神”
    ],
    “singerids”:[
    “25000”
    ],
    “singernames”:[
    “刘德华”
    ],
    “songname”:”暗里着迷”,
    “source”:”iflytek”,
    “tagnames”:[
    “经典”,
    “下午茶”,
    “华语”,
    “工作”,
    “”
    ]
    },
    ],
    “sem_score”:{
    “artist”:{
    “lcs”:1,
    “pos”:”ps”,
    “txt”:”刘德华”
    },
    “song”:{
    “lcs”:1,
    “pos”:”ps”,
    “txt”:”暗里着迷”
    },
    “top”:0
    }
    },
    “demand_semantic”:{
    “artist”:”刘德华”,
    “service”:”musicX”,
    “song”:”暗里着迷”
    },
    “engine_time”:41.273,
    “operation”:”PLAY”,
    “orig_semantic”:{
    “slots”:{
    “artist”:”刘德华”,
    “song”:”暗里着迷”
    }
    },
    “rc”:0,
    “score”:0,
    “search_semantic”:{
    “artist”:”刘德华”,
    “operation”:”PLAY”,
    “service”:”musicX”,
    “song”:”暗里着迷”
    },
    “semantic”:{
    “slots”:{
    “artist”:”刘德华”,
    “operation”:”PLAY”,
    “song”:”暗里着迷”
    }
    },
    “service”:”musicX”,
    “uuid”:”cida160a452@dx00a00f4dd17f01009a”,
    “text”:”那你竖起耳朵听刘德华的暗里着迷”,
    “state”:{
    “fg::musicX::default::playing”:{
    “state”:”playing”
    }
    },
    “used_state”:{
    “state”:”playing”,
    “state_key”:”fg::musicX::default::playing”
    },
    “answer”:{
    “text”:”刘德华 暗里着迷”
    },
    “dialog_stat”:”DataValid”,
    “save_history”:true,
    “sid”:”cida160a452@dx00a00f4dd17f01009a”,
    “cid”:”cida160a452@dx00a00f4dcfbb000000″
    }

3. 自定义问答库管理后台的开发






  • 注:管理后台的代码我就不贴了,主要是原理,如有需要请联系我。

4. 数据库表结构的设计

    CREATE TABLE `cr_aiui_keywords` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `keywords` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '关键词',
      `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态(0:禁用 1:启用)',
      `create_time` datetime NOT NULL COMMENT '创建时间',
      `update_time` datetime DEFAULT NULL COMMENT '修改时间',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='关键词库';
CREATE TABLE `cr_aiui_semantic_library` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `question` text COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '问题',
  `keywords` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '关键词',
  `second_keywords` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '副关键词',
  `type` tinyint(1) NOT NULL COMMENT '消息类型:1-文字、2-音频、3-图文、4-视频、5-图片',
  `answer` text COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '消息内容或标题',
  `url` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '视频、音乐或图片地址',
  `image_url` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '视频或音乐封面图片地址',
  `desc` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '视频、音乐描述',
  `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态(0:禁用 1:启用)',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '修改时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='基于科大讯飞AIUI平台对的自定义语义库';

参考文章:

资源经验分享 — AIUI第三方接入的问题
AIUI语义后处理历险记
PHP用openssl_encrypt代替mcrypt_encrypt