1月 17, 2023 - laravel, php开发    php调用ChatGPT示例已关闭评论

php调用ChatGPT示例

用Laravel调用示例,官方文档链接:

https://platform.openai.com/docs/api-reference/completions/create

让ChatGPT做首诗:
$open_ai_key = env('OPENAI_API_KEY', 'the open api key');
$client = new Client(['verify' => false]);
$query = [
    'model' => 'text-davinci-003',
    'prompt' => '写一首诗',
    'temperature' => 0.5,
    'max_tokens' => 1000,
    'frequency_penalty' => 0,
    'presence_penalty' => 0.6,
    'stop' => ["input:"],
];
$data = $client->request('post', 'https://api.openai.com/v1/completions', [
    'headers' => [
        'Content-Type' => 'application/json',
        'Authorization' => "Bearer $open_ai_key",
    ],
    'body' => json_encode($query),
    'timeout' => 60,
    'read_timeout' => 60,
    'connect_timeout' => 60,
])->getBody()->getContents();
$data = json_decode($data, true);
dd($data);

 

结果:

 

评论被关闭。