使用PHP请求ChatGPT聊天接口
Posted 黄聪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用PHP请求ChatGPT聊天接口相关的知识,希望对你有一定的参考价值。
<?php (0); // ChatGPT API endpoint // $url = \'https://api.openai.com/v1/engines/davinci-codex/completions\'; $url = \'https://api.openai.com/v1/chat/completions\'; //聊天接口 // $url = \'https://api.openai.com/v1/completions\'; // Your API key $api_key = \'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\'; // Request headers $headers = array( \'Content-Type: application/json\', \'Authorization: Bearer \' . $api_key, ); // Request data $data = array( // \'model\' => \'text-davinci-003\', \'model\' => \'gpt-3.5-turbo\', //聊天模型 // \'model\' => \'text-curie-001\', \'temperature\' => 0.8, // \'prompt\' => \'如何用php使用chatgpt的聊天接口\', //聊天不用 \'max_tokens\' => 3000, \'messages\' => [ ["role" => "user", "content" => "Hello!"], ["role" => "assistant","content" => "\\n\\n您好!有什么可以帮助您的今天?"], ["role" => "user", "content" => "历史上的今天发生过什么事情?"], ] ); // Send request $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); $response = curl_exec($ch); curl_close($ch); // Print response echo $response;
作者:黄聪
出处:http://www.cnblogs.com/huangcong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/huangcong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
以上是关于使用PHP请求ChatGPT聊天接口的主要内容,如果未能解决你的问题,请参考以下文章
基于ChatGPT+SpringBoot打造智能聊天AI机器人接口并上线至服务器
基于ChatGPT+SpringBoot打造智能聊天AI机器人接口并上线至服务器