国产清纯在线一区二区,日本强伦片中文字幕免费看,结衣波多野番号,亚洲欧美日韩精品色xxx,开心色怡人综合网站

返回上一頁 財(cái)富領(lǐng)航小程序定制開發(fā)解決方案 網(wǎng)站建設(shè)案例 自定義開發(fā)富文本編輯器(Javascript實(shí)現(xiàn)點(diǎn)擊插入內(nèi)容到textarea光標(biāo)處)

當(dāng)前位置:首頁 > 解決方案 > 網(wǎng)絡(luò)方案 > 詳細(xì)內(nèi)容

使用PHP請求ChatGPT聊天接口(gpt-3.5-turbo)

時(shí)間:2023-03-21 瀏覽:2332次 + 打印

最近c(diǎn)hatGPT和百度文心一言火的不得了,今天藤設(shè)計(jì)也整好在使用ChatGPT開發(fā)應(yīng)用項(xiàng)目,給大家分享一段PHP請求api的代碼,希望對開發(fā)人員有用。
注:該php文件需部署在海外空間或服務(wù)器上使用,不能在國內(nèi)服務(wù)器上運(yùn)行。


方案一
<?php
define('OPENAI_API_KEY', '這里輸入您的key秘鑰');
$prompt = $_GET['prompt'];
$response = sendPostRequest($prompt);
echo $response;

function sendPostRequest($prompt) {
    $curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://api.openai.com/v1/completions",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => json_encode(array(
          "prompt" => $prompt,
          "max_tokens" => 2048,
          "model" => "text-davinci-003"
      )),
      CURLOPT_HTTPHEADER => array(
        "Content-Type: application/json",
        "Authorization: Bearer " . OPENAI_API_KEY
      ),
    ));

    $response = curl_exec($curl);

    curl_close($curl);
    return $response;
}
?>

以上代碼保存為index.php存放到您海外空間或服務(wù)器里即可運(yùn)行

在瀏覽器里輸入index.php?prompt=請問有外星人嗎?

你就可以看到openai回答的內(nèi)容了!

方案二

<?php
set_time_limit(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 = '這里輸入您的key秘鑰';
 
// 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" => "歷史上的今天發(fā)生過什么事情?"],
    ]
     
);
 
// 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;
?>


網(wǎng)站建設(shè)公司項(xiàng)目經(jīng)理

掃二維碼與項(xiàng)目經(jīng)理溝通

我們在微信上24小時(shí)期待你的聲音
解答:網(wǎng)站優(yōu)化、網(wǎng)站建設(shè)、APP開發(fā)、小程序開發(fā)

藤設(shè)計(jì)是一家以提供網(wǎng)站建設(shè)、網(wǎng)站優(yōu)化APP開發(fā)、小程序開發(fā)、網(wǎng)絡(luò)營銷推廣為主的互聯(lián)網(wǎng)開發(fā)公司。以客戶需求為導(dǎo)向,客戶利益為出發(fā)點(diǎn),結(jié)合自身設(shè)計(jì)及專業(yè)建站優(yōu)勢,為客戶提供從基礎(chǔ)建設(shè)到營銷推廣的一整套解決方案,探索并實(shí)現(xiàn)客戶商業(yè)價(jià)值較大化,為所有謀求長遠(yuǎn)發(fā)展的企業(yè)貢獻(xiàn)全部力量。

Learn more

Our Service 上海網(wǎng)站建設(shè)
QQ客服 微信客服 返回頂部
網(wǎng)站制作
掃二維碼與項(xiàng)目經(jīng)理溝通
×