黄聪

论SEO对人类的重要性,请看我的博客:hcsem.com

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
<?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 = '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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;

 

posted on 2023-03-22 08:45  黄聪  阅读(221)  评论(0编辑  收藏  举报