OpenAI(ChatGPT)对接的php代码

openai windows 文章 2022-12-07 11:04 4129 0 全屏看文

AI助手支持GPT4.0

密钥申请参考:

http://www.sanshu.cn/a/75825.html

代码如下:

$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_URL => 'https://api.openai.com/v1/engines/davinci/completions',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'Authorization: Bearer ' . $api_key
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'prompt' => 'Hello, how are you today?',
        'model' => 'text-davinci-002',
        'max_tokens' => 128,
        'temperature' => 0.5
    ])
]);
$response = curl_exec($curl);
curl_close($curl);


-EOF-

AI助手支持GPT4.0