php调用ChatGPT代码

2023-02-15 11:45

admin
261
来源:未知

直接问的 ChatGPT: $url = 'https://api.openai.com/v1/engines/davinci-codex/completions'; $method = 'POST'; $data = '{"prompt": "Hello, ChatGPT!", "max_tokens": 5}'; $api_key = 'YOUR_API_KEY'; $ch = curl_init(); curl_setopt($ch, CURLOPT

直接问的ChatGPT:


$url = 'https://api.openai.com/v1/engines/davinci-codex/completions';
$method = 'POST';
$data = '{"prompt": "Hello, ChatGPT!", "max_tokens": 5}';
$api_key = 'YOUR_API_KEY';


$ch = curl_init();


curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Authorization: Bearer '.$api_key
));


$response = curl_exec($ch);


curl_close($ch);


if ($response === false) {
    echo 'Error: '.curl_error($ch);
} else {
    $response_data = json_decode($response, true);
    if (isset($response_data['choices']) && count($response_data['choices']) > 0) {
        $text = $response_data['choices'][0]['text'];
        echo 'Generated text: '.$text;
    } else {
        echo 'Error: Invalid response format';
    }
}



上一篇

上一篇:某咕最新ID抓取方法