ChatGPT网页版PHP接口源码

原创 admin  2023-04-01 10:39  阅读 77 次

ChatGPT网页版是一款基于GPT-3技术的聊天机器人,它可以帮助用户轻松地与机器人进行聊天,获得有用的信息和建议。它可以根据用户的输入,自动生成准确的回答,从而提高用户的体验。此外,ChatGPT网页版还提供了一个PHP接口,可以让开发者轻松地将ChatGPT网页版集成到他们的网站中。 预览

体验网站:https://tt.dadaidc.com/​/

PHP后端接口源码

<?php
// 设置请求URL
$url = 'https://api.openai.com/v1/completions';

// 设置ChatGPT API密钥
$api_key = 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

//获取问题和key密钥(key密钥可以不要,看需求自定义)
$prompt = $_POST['question'];
$key = $_POST['key'];

// 设置请求参数
$data = array(
	// 使用ChatGPT的三号模型
    "model" => "text-davinci-003",
    "prompt" => $prompt,
    "temperature" => 0,
    "max_tokens" => 1000,
    "top_p" => 1,
    "frequency_penalty" => 0.0,
    "presence_penalty" => 0.0
);

// 设置请求头部
$headers = array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $api_key,
);

// 发送POST请求
if ($key === '123456') {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $response = curl_exec($ch);
    curl_close($ch);

    // 处理响应结果
    if ($response === false) {
        echo json_encode(array('status' => '405', 'msg' => "错误: 请求异常,请联系管理员检测ChatGPT是否正常!   ".$result));
    } else {
        $result = json_decode($response, true);
        if (isset($result['choices'][0]['text'])) {
            echo json_encode(array('status' => '200', 'msg' => $result['choices'][0]['text']));
        } else {
            echo json_encode(array('status' => '404', 'msg' => "错误: 请求异常,请联系管理员检测ChatGPT是否正常!   ".$result));
        }
    }
} else {
    echo json_encode(array('status' => '201', 'msg' => "错误: 异常非法请求!!!"));
}

Github项目地址:https://github.com/genius-kim/ChatGPT_PHP_API

来源:https://www.huanp.com/idc/139070.html
声明:欢迎分享本文,转载请保留出处!

发表评论


表情