当前位置:   article > 正文

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

科大讯飞自定义知识库

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

科大讯飞AIUI开放平台地址
科大讯飞AIUI开放平台后处理地址
AIUI后处理开放平台协议

db726aa3d62bc482864aa6d71b5afb1b.PNG
1c5b3baa3359ccc59119436d831da0ae.PNG
6601015f46e8b5a8c39fa8bef6f564be.jpg

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

  1. <?php
  2. namespace service;
  3. /**
  4. * 科大讯飞AIUI服务
  5. * Class AIUIService
  6. * @package service
  7. */
  8. class AIUIService
  9. {
  10. const APP_ID = '*****'; //讯飞AIUI开放平台注册申请应用的应用ID(appid)
  11. const API_KEY = '****'; //接口密钥,由讯飞AIUI开放平台提供,调用方管理
  12. const TOKEN = '*****'; //后处理token
  13. const AES_KEY = '*****'; //加密AES KEY
  14. /**
  15. * @title 构造函数
  16. * @param string $key 密钥
  17. * @param string $method 加密方式
  18. * @param string $iv iv向量
  19. * @param mixed $options 还不是很清楚
  20. */
  21. public function __construct()
  22. {
  23. $this->token = self::TOKEN;
  24. // key是必须要设置的
  25. $this->secret_key = self::AES_KEY;
  26. $this->method = "AES-128-CBC";
  27. $this->iv = self::AES_KEY;
  28. $this->options = OPENSSL_RAW_DATA;
  29. }
  30. /**
  31. * @title 签名验证
  32. * @param $token token
  33. * @param $timestamp 时间戳
  34. * @param $rand 随机数
  35. * @param $aesKey $aesKey
  36. * @param $sign 客户端请求接口sign参数值
  37. * @return INT
  38. */
  39. public function checkAuth($sign,$timestamp,$rand,$key='')
  40. {
  41. //按规则拼接为字符串
  42. $str = self::createSignature($this->token,$timestamp,$rand,$key);
  43. ///校验签名字符串:0为一致、-1为不一致
  44. if ($str !== $sign) {
  45. return -1;
  46. }
  47. return 0;
  48. }
  49. /**
  50. * @title 生成签名
  51. * @param $token
  52. * @param $timestamp
  53. * @param $rand
  54. * @param string $aesKey
  55. * @return string
  56. */
  57. private static function createSignature($token,$timestamp,$rand,$key='')
  58. {
  59. //组装要排序的数组
  60. $arr = [$timestamp,$token,$rand];
  61. //字典序排序
  62. sort($arr);
  63. //拼接为一个字符串
  64. $str = implode('',$arr);
  65. //sha1加密
  66. return sha1($str);
  67. }
  68. /**
  69. * @title 加密
  70. * @param $plaintext string 要加密的字符串
  71. * @return string
  72. */
  73. public function encrypt($plaintext){
  74. //加密采用AES的CBC加密方式,秘钥为16字节(128bit),初始化向量复用秘钥,填充方式为PKCS7Padding。
  75. //返回的消息要以同样的方式进行加密。
  76. //加密过程:padding->CBC加密->base64编码
  77. //$option 以下标记的按位或: OPENSSL_RAW_DATA 原生数据,对应数字1,不进行 base64 编码。OPENSSL_ZERO_PADDING 数据进行 base64 编码再返回,对应数字0
  78. return openssl_encrypt($plaintext, $this->method, $this->secret_key,$this->options, $this->iv);
  79. }
  80. /**
  81. * @title 解密
  82. * @param $ciphertext string 要解密的字符串
  83. * @return string
  84. */
  85. public function decrypt($ciphertext){
  86. //解密过程:base64解码->CBC解密->unpadding
  87. return openssl_decrypt($ciphertext, $this->method, $this->secret_key, $this->options, $this->iv);
  88. }
  89. }

2. 控制器处理:

接口调用示例:

10763004612d1c4f230c39ec9f92f49d.PNG
61354d6012a67891f831bdbdf35098c9.PNG
e1d8fa31963bf8d06f799d36c4ec3bd6.PNG

注: 其他具体接入过程可参见文档:https://aiui.xfyun.cn/docs/access_docs
  1. <?php
  2. namespace app\api\controller;
  3. use service\AIUIService;
  4. use service\SimilarityMatch;
  5. use think\Db;
  6. /**
  7. * @title 科大讯飞自定义语义库
  8. * @class AiUi
  9. * @auth 邹柯
  10. * @date 2018-11-19
  11. */
  12. class AiUi
  13. {
  14. /**
  15. * @title 科大讯飞自定义语义库
  16. * @return json
  17. */
  18. public function accessVerification()
  19. {
  20. //接收参数
  21. $param = request()->param(false);
  22. //判断接口访问方式:POST/GET
  23. $res = isset($_SERVER['REQUEST_METHOD']) && !strcasecmp($_SERVER['REQUEST_METHOD'],'POST');
  24. if($res) { //POST
  25. //是否加密:encrypttype=aes-加密、raw-不加密
  26. $encrypttype = $param[ 'encrypttype'];
  27. //解密
  28. if($encrypttype == "aes"){
  29. $DeMsgContent = (new AIUIService())->decrypt(file_get_contents("php://input"));
  30. $DeMsgContent = json_decode($DeMsgContent,true);
  31. }else{
  32. //消息内容
  33. $MsgContent = $param['Msg']['Content'];
  34. $DeMsgContent = json_decode(base64_decode($MsgContent),true);
  35. }
  36. //这个地方是个坑,为空时必须返回'{"intent":{}}' 不能是'{"intent":[]}' 否则会报错,切记
  37. if(empty($DeMsgContent["intent"])){
  38. return '{"intent":{}}';
  39. }
  40. //组装要返回的问题答案
  41. //应答码(response code),0-操作成功、4-文本没有匹配的技能场景,技能不理解或不能处理该文本
  42. if(!empty($DeMsgContent["intent"]) && $DeMsgContent["intent"]["rc"] == 4){
  43. $msg = $this->getMsg($DeMsgContent["intent"]["text"]);
  44. if(empty($msg)){
  45. $msg_text = $DeMsgContent["intent"]["text"];
  46. $keywords = ["返回"];
  47. if(!in_array($msg_text,$keywords)){
  48. $msg_text = "这个技能我还没学会呢";
  49. $DeMsgContent["intent"]["service"] = "openQA";
  50. $DeMsgContent["intent"]["operation"] = "ANSWER";
  51. //入库未学会的问题
  52. $this->insertUndefineQuestion($DeMsgContent["intent"]['text']);
  53. $DeMsgContent["intent"]["semantic"] = [
  54. 'slots'=>[
  55. 'name'=>$DeMsgContent["intent"]["text"],
  56. 'operation'=>'ANSWER'
  57. ],
  58. ];
  59. }else{
  60. $DeMsgContent["intent"]["service"] = "app";
  61. $DeMsgContent["intent"]["operation"] = "EXIT";
  62. $DeMsgContent["intent"]["semantic"] = [
  63. 'slots'=>[
  64. 'name'=>$msg_text,
  65. 'operation'=>'EXIT'
  66. ],
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/312604
推荐阅读
相关标签
  

闽ICP备14008679号