PHP对接chatgpt使用方法

PHP对接chatgpt使用方法,教大家如何对接chatgpt 使用curl请求官方api

PHP对接chatgpt使用方法

本章教大家如何对接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'];//该数组输出最后回答的内容

 

Intoep小程序

微信扫一扫,打开小程序浏览更便捷

转载作品,原作者:夏柔,文章来源:https://www.wpon.cn/28438.html

发表回复

登录后才能评论