当前位置:   article > 正文

PHP版本的科大讯飞ai星火认知大模型对接示例代码_星火ai接入

星火ai接入

用了wrench/wrench 组件  compose require wrench/wrench

  1. <?php
  2. namespace app\extend;
  3. use think\facade\Env;
  4. use think\facade\Log;
  5. use Wrench\Client;
  6. class Xfyun
  7. {
  8. private $host = "spark-api.xf-yun.com";
  9. private $version = "/v1.1/chat";
  10. private $header = [];
  11. private $query = [];
  12. function __construct(){
  13. $this->host = Env::get("xfyun.HOST");
  14. }
  15. private function makeSign(){
  16. $date = gmdate('D, d M Y H:i:s') . ' GMT';
  17. // $date ="Fri, 05 May 2023 10:43:39 GMT";
  18. $tmp = "host: " .$this->host. "\n";
  19. $tmp .= "date: " .$date. "\n";
  20. $tmp .= "GET " .$this->version. " HTTP/1.1";
  21. //var_dump( $tmp);
  22. $this->header = explode("\n", $tmp);
  23. $signature = hash_hmac('sha256', $tmp, Env::get("xfyun.APISECRET"), true);
  24. $tmpSha = base64_encode($signature);
  25. return [$date , $tmpSha];
  26. }
  27. function makeUrl(){
  28. list($date, $sign) = $this->makeSign();
  29. $authorization = "api_key=\"".Env::get("xfyun.apikey")."\", algorithm=\"hmac-sha256\", headers=\"host date request-line\", signature=\"".$sign."\"";
  30. $authorization = base64_encode($authorization);
  31. $query = [
  32. "authorization" => $authorization,
  33. "date"=>$date,
  34. "host" => $this->host,
  35. ];
  36. $this->query = $query;
  37. return sprintf("ws://%s%s?%s", $this->host, $this->version, http_build_query($query));
  38. }
  39. function send($question , $deviceId){
  40. $data = array(
  41. "header" => array(
  42. "app_id" => Env::get("xfyun.APPID"),
  43. "uid" => $deviceId
  44. ),
  45. "parameter" => array(
  46. "chat" => array(
  47. "domain" => "general",
  48. "random_threshold" => 0.5,
  49. "max_tokens" => 2048,
  50. "auditing" => "default"
  51. )
  52. ),
  53. "payload" => array(
  54. "message" => array(
  55. "text" => array(
  56. array("role" => "user", "content" => $question)
  57. )
  58. )
  59. )
  60. );
  61. $url = $this->makeUrl();
  62. try{
  63. $client = new Client($url , "ws://".$this->host , []);
  64. // var_dump($url);
  65. $client->connect();
  66. $client->sendData(json_encode($data));
  67. //$response = $client->receive();
  68. $result = [];
  69. //最多获取50段
  70. for($i = 0; $i< 50; $i++) {
  71. // $response = $client->receive()[0]->getPayload();
  72. $res = $client->receive();
  73. if(!isset($res[$i])){
  74. break;
  75. }
  76. $response = $res[$i]->getPayload();
  77. if(!$response){
  78. break;
  79. }
  80. $response = json_decode($response, true);
  81. Log::write($response, "ws");
  82. $result[] = $response['payload']['choices']['text'][0]['content'];
  83. if($response['header']['status'] == 2){
  84. //结束了
  85. break;
  86. }
  87. }
  88. $client->disconnect();
  89. return join("", $result);
  90. }
  91. catch (\Exception $e){
  92. return $e->getMessage();
  93. }
  94. }
  95. }

调用方法如下:

  1. $Xfyun = new Xfyun();
  2. $res = $Xfyun->send($qustion, '你能为我做什么');

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/827004
推荐阅读
相关标签
  

闽ICP备14008679号