![cover](https://img-blog.csdnimg.cn/268d81d356d34607b01b27cc5e7f23bd.png)
php实现通过api实现chatgpt
php实现通过api实现chatgpt。
·
<?php
$text='php你知道咋样区分不同的两台电脑吗通过程序';
$headers[] = "Content-Type: application/json";
$headers[] = "Authorization: Bearer sk-pmnyMNsajmyQowmzVZFDT3BlbkFJym66WY5eZlCIh23N";//换成自己key
$url="https://api.openai.com/v1/completions";
$data=[
"model"=>"text-davinci-003",
"prompt"=> "'.$text.'",
"max_tokens"=> 1024,
"temperature"=>0
];
$result=curl($url,$data,$headers);
var_dump(json_decode($result,true));exit;
function curl($url,$data,$headers){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //停止验证证书(一般只需要设置此项为false即可)
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
更多推荐
所有评论(0)