php调用chatgpd接口输出

admin2年前php465
<?php
$url = 'https://api.openai.com/v1/completions';
$data = array("model"=> "text-davinci-003",
          "prompt"=> "帮我用C#开发一个生成二维码的程序",
          "temperature"=> 0.7,
          "max_tokens"=> 256,
          "top_p"=> 1,
          "frequency_penalty"=> 0,
          "presence_penalty"=> 0);
$data_string = json_encode($data);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: Bearer apikey',
    'Content-Length: ' . strlen($data_string))
);

$result = json_decode(curl_exec($ch),true);

curl_close($ch);

print_r($result);
 
echo  $result["choices"][0]["text"];


 ?>


相关文章

mysql批量添加数据

$conn=new sqlhelper;for ($i = 0; $i <1000 ; $i++) {$sql="insert into system_user(user_shenfe...

PHP生成json,javascript解析json

生成json<?php $arr=array();  //定义空数组 $one="111"; $two="222"; arra...

php的function函数内部只能有一个return

php的function函数内部只能有一个return,之后的代码就都全部运行不正常了...

php设置跨站访问

  php的open_basedir设置多个路径(目录),分隔符是“:”冒号open_basedir=/www/wwwroot/www.ceshi.com/:/tmp/:/www/...

PHP保存数组到数据库

数组是 PHP 开发中使用最多的数据类型之一,对于结构化的数据尤为重要。很多时候我们需要把数组保存到数据库中,实现对结构化数据的直接存储和读取。其中一个案例就是,对于 Form 提交的多选 check...

php post undefined index,PHP 中提示undefined index的解决方案

声明为空isset($_POST['user_email']),或者,empty($_POST['user_email'])在php.ini中设置error_repor...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。