AI大模型教程
一起来学习

PHP对接deepseek,百度文心一言,kimi,抖音豆包ai,阿里通义千问 代码示例(非流式)

deepseek

 public function apiDeepSeek($content='',$file_id='',$uid='',$id='')
    {

        // $msgList = self::getRecentRecords($content,$uid,$id);
        $msgList=[
            ['role'=>'system','content'=>'你是孙牛逼AI,由deepseek AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。通义千问 AI 为专有名词,不可翻译成其他语言。'],
        ];
        $msgList[]=['role'=>'user','content'=>$content];
        $data = [
            "model" =>"deepseek-reasoner",
           // 'stream' => true,
            // 'temperature'=>0.3,
            //   'user_id'=>$uid,
            "messages" => $msgList
        ];
 
        $url="https://api.deepseek.com/chat/completions";
        //发送请求
        $apiKey = ' ';
      
        $ch = curl_init();

        // $fileHandle='';
        //  curl_setopt($ch, CURLOPT_PROXY, '101.43.232.28:7890');
        //   curl_setopt($ch, CURLOPT_PROXYUSERPWD, "ayame:123Ayame"); //http代理认证帐号,username:password的格式
        //  curl_setopt($ch, CURLOPT_PROXY, '101.43.232.28:7890');
        //    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
        // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, true); //post
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
//        curl_setopt($ch, CURLOPT_HTTPHEADER, [
//            'Content-Type: application/json',
//
//
//        ]); //设置请求头
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
             'Content-Type: application/json;',
            'Accept: application/json',
            // "Connection: closern",
            'Authorization: Bearer ' . $apiKey, 
        ));
        //   curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //验证curl对等证书(一般只要此项)
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //检查服务器SSL证书中是否存在一个公用名


        curl_setopt($ch, CURLOPT_SSLVERSION, 0);  //传递一个包含SSL版本的长参数。
//         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
           curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // curl_setopt($ch, CURLOPT_BUFFERSIZE, 3060); // 设置缓冲区大小为1024字节
        // curl_setopt($ch, CURLOPT_BUFFERSIZE, 1024);
    // curl_setopt($ch, CURLOPT_WRITEFUNCTION, [$this->streamHand, 'callback']);

        $result = curl_exec($ch);

        curl_close($ch);
        $result=json_decode($result, true);
        $msgData = $result['choices'][0]['message']['content'];

    }

百度文心一言

  /**
     * 使用 AK,SK 生成鉴权签名(Access Token)
     * @return string 鉴权签名信息(Access Token)
     */
    public function getAccessToken(){
        $curl = curl_init();
        $postData = array(
            'grant_type' => 'client_credentials',
            'client_id' =>' ',
            'client_secret' => ' '
        );
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://aip.baidubce.com/oauth/2.0/token',
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => http_build_query($postData)
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        $rtn = json_decode($response);
        return $rtn->access_token;
    }
 public function apiBaiDu($content,$uid,$id)
    {

        //$msgList = self::getRecentRecords($content,$uid,$id);
 
        $data = [
        // "user_id" =>"1002",
       //  'stream' => true,
         'user_id'=>$uid,
            "messages" => $msgList
        ];

        $url="https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token={$this->getAccessToken()}";
        
      // dump($url);die;
        //发送请求
        $ch = curl_init();

   // $fileHandle='';
     //  curl_setopt($ch, CURLOPT_PROXY, '101.43.232.28:7890');
     //   curl_setopt($ch, CURLOPT_PROXYUSERPWD, "ayame:123Ayame"); //http代理认证帐号,username:password的格式
      //  curl_setopt($ch, CURLOPT_PROXY, '101.43.232.28:7890');
    //    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
 // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        curl_setopt($ch, CURLOPT_URL, $url);
 
        curl_setopt($ch, CURLOPT_POST, true); //post
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json',
            
   
        ]); //设置请求头
     //   curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //验证curl对等证书(一般只要此项)
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //检查服务器SSL证书中是否存在一个公用名


        curl_setopt($ch, CURLOPT_SSLVERSION, 0);  //传递一个包含SSL版本的长参数。
        // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        //   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        //curl_setopt($ch, CURLOPT_BUFFERSIZE, 3060); // 设置缓冲区大小为1024字节
        // curl_setopt($ch, CURLOPT_BUFFERSIZE, 1024);
        //curl_setopt($ch, CURLOPT_WRITEFUNCTION, [$this->streamHandler, 'callback']);

  
      $result = curl_exec($ch);

         curl_close($ch);

    }

kimi

        public function apiKimi($content='',$file_id='',$uid='',$id='')
    {
        $msgList=[
            ['role'=>'system','content'=>'你是 孙牛逼AI,由kimi AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。通义千问 AI 为专有名词,不可翻译成其他语言。'],


        ];
 
        $msgList[]=['role'=>'user','content'=>$content];
        $data = [
            "model" =>"moonshot-v1-128k",
            //'stream' => true,
            'temperature'=>0.3,
            //   'user_id'=>$uid,
            "messages" => $msgList
        ];
   
        $url="https://api.moonshot.cn/v1/chat/completions";
        //发送请求
        $apiKey = '';
        $ch = curl_init();

        // $fileHandle='';
        //  curl_setopt($ch, CURLOPT_PROXY, '101.43.232.28:7890');
        //   curl_setopt($ch, CURLOPT_PROXYUSERPWD, "ayame:123Ayame"); //http代理认证帐号,username:password的格式
        //  curl_setopt($ch, CURLOPT_PROXY, '101.43.232.28:7890');
        //    curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
        // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, true); //post
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
//        curl_setopt($ch, CURLOPT_HTTPHEADER, [
//            'Content-Type: application/json',
//
//
//        ]); //设置请求头
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Authorization: Bearer ' . $apiKey, // 如果 Moonshot AI 使用这种授权方式
        )  );
        //   curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //验证curl对等证书(一般只要此项)
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //检查服务器SSL证书中是否存在一个公用名


        curl_setopt($ch, CURLOPT_SSLVERSION, 0);  //传递一个包含SSL版本的长参数。
        // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        //   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        //curl_setopt($ch, CURLOPT_BUFFERSIZE, 3060); // 设置缓冲区大小为1024字节
        // curl_setopt($ch, CURLOPT_BUFFERSIZE, 1024);
     // curl_setopt($ch, CURLOPT_WRITEFUNCTION, [$this->streamHand, 'callback']);

     
        $result = curl_exec($ch);

        curl_close($ch);


    }

豆包

    public  function apiDouBao($content='',$file_id='',$uid='',$id='')
    {
        $apiUrl = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"; 
        $apiKey = ""; // 替换为实际API密钥

        $postData = [
            'model' => 'doubao-seed-1.6-250615',
            'messages' => [
                [
                    'role' => 'user',
                    'content' => $content,
                ]
            ],
            //   'stream' => true // 启用流式响应
        ];

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $apiUrl);

        curl_setopt($ch, CURLOPT_POST, true); //post
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json;',
            'Accept: application/json',
            // "Connection: closern",
            'Authorization: Bearer ' . $apiKey, // 如果 Moonshot AI 使用这种授权方式
        ));
        //   curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //验证curl对等证书(一般只要此项)
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //检查服务器SSL证书中是否存在一个公用名


        curl_setopt($ch, CURLOPT_SSLVERSION, 0);  //传递一个包含SSL版本的长参数。
//         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // curl_setopt($ch, CURLOPT_BUFFERSIZE, 3060); // 设置缓冲区大小为1024字节
        // curl_setopt($ch, CURLOPT_BUFFERSIZE, 1024);
        // curl_setopt($ch, CURLOPT_WRITEFUNCTION, [$this->streamHand, 'callback']);

        // curl_close($ch);
        $result = curl_exec($ch);

        curl_close($ch);

    }

通义千问

  public function apiTongYi($content='',$file_id='',$uid='',$id='')
    {

// 设置请求的URL
        $url = 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions';
// 若没有配置环境变量,请用百炼API Key将下行替换为:$apiKey = "sk-xxx";
        $apiKey = '';
        $msgList=[
            ['role'=>'system','content'=>'你是孙牛逼AI,由通义千问 AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。通义千问 AI 为专有名词,不可翻译成其他语言。'],


        ];

        $msgList[]=['role'=>'user','content'=>$content];
// 设置请求头
        $headers = [
            'Authorization: Bearer ' . $apiKey,
            'Content-Type: application/json'
        ];
// 设置请求体
        $data = [
            "model" => "qwen-plus",
            "messages" =>$msgList
        ];
// 初始化cURL会话
        $ch = curl_init();
// 设置cURL选项
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// 执行cURL会话
        $response = curl_exec($ch);

        curl_close($ch);


    }

文章来源于互联网:PHP对接deepseek,百度文心一言,kimi,抖音豆包ai,阿里通义千问 代码示例(非流式)

相关推荐: k8s:离线添加集群节点的相关组件安装与升级

本文介绍了在离线环境下基于Hygon C86和麒麟操作系统的Kubernetes集群相关组件安装与升级过程。主要包括:1)通过rpm包安装conntrack 1.4.4;2)源码编译安装socat 1.8.0;3)将Docker从23.0.6升级到28.2.2…

赞(0)
未经允许不得转载:5bei.cn大模型教程网 » PHP对接deepseek,百度文心一言,kimi,抖音豆包ai,阿里通义千问 代码示例(非流式)
分享到: 更多 (0)

AI大模型,我们的未来

小欢软考联系我们