本章教大家如何对接chatgpt 使用curl请求官方api
$api_key='填写你的key值';
$message="问题内容";
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.openai.com/v1/completions',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
],
CURLOPT_POSTFIELDS => json_encode([
'prompt' => $message,//你的内容
'model' => 'text-davinci-003',//机器人3代
'max_tokens' => 4000,//最大字符串
'temperature' => 0.5//回答精准度
])
]);
// 请求结束
$domain = curl_exec($curl);
curl_close($curl);
//处理请求的数据
$domain_array = json_decode($domain, true);
$data = $domain_array['choices'][0]['text'];//该数组输出最后回答的内容