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

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

<?php

namespace app\extend;

use think\facade\Env;
use think\facade\Log;
use Wrench\Client;

class Xfyun
{



    private $host = "spark-api.xf-yun.com";
    private $version = "/v1.1/chat";

    private $header = [];
    private $query = [];

    function __construct(){
        $this->host = Env::get("xfyun.HOST");
    }

    private  function makeSign(){
        $date = gmdate('D, d M Y H:i:s') . ' GMT';
//        $date ="Fri, 05 May 2023 10:43:39 GMT";
        $tmp = "host: " .$this->host. "\n";
        $tmp .= "date: " .$date. "\n";
        $tmp .= "GET " .$this->version. " HTTP/1.1";
//var_dump( $tmp);
        $this->header = explode("\n", $tmp);
        $signature = hash_hmac('sha256', $tmp, Env::get("xfyun.APISECRET"), true);

        $tmpSha = base64_encode($signature);

        return [$date , $tmpSha];
    }


    function makeUrl(){
        list($date, $sign) = $this->makeSign();

        $authorization = "api_key=\"".Env::get("xfyun.apikey")."\", algorithm=\"hmac-sha256\", headers=\"host date request-line\", signature=\"".$sign."\"";
        $authorization = base64_encode($authorization);

        $query = [
            "authorization" => $authorization,
            "date"=>$date,
            "host" => $this->host,
        ];
        $this->query = $query;
        return sprintf("ws://%s%s?%s", $this->host, $this->version, http_build_query($query));
    }


    function send($question , $deviceId){
        $data = array(
            "header" => array(
                "app_id" => Env::get("xfyun.APPID"),
                "uid" => $deviceId
            ),
            "parameter" => array(
                "chat" => array(
                    "domain" => "general",
                    "random_threshold" => 0.5,
                    "max_tokens" => 2048,
                    "auditing" => "default"
                )
            ),
            "payload" => array(
                "message" => array(
                    "text" => array(
                        array("role" => "user", "content" => $question)
                    )
                )
            )
        );
        $url = $this->makeUrl();

        try{
            $client = new Client($url , "ws://".$this->host , []);
//            var_dump($url);
            $client->connect();
            $client->sendData(json_encode($data));
//$response = $client->receive();
            $result = [];
            //最多获取50段
            for($i = 0; $i< 50; $i++) {
//                $response = $client->receive()[0]->getPayload();
                $res = $client->receive();
                if(!isset($res[$i])){
                    break;
                }
                $response = $res[$i]->getPayload();
                if(!$response){
                    break;
                }
                $response = json_decode($response, true);

                Log::write($response, "ws");
                $result[] = $response['payload']['choices']['text'][0]['content'];
                if($response['header']['status'] == 2){
                    //结束了
                    break;
                }
            }
            $client->disconnect();

            return join("", $result);
        }
        catch (\Exception $e){
            return $e->getMessage();
        }


    }

}

调用方法如下:

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值