纯php实现的最新的chatGPT接口模型(gpt-3.5-turbo)的实现过程

目前最新接口模型(gpt-3.5-turbo),费用更低,速度更快,大家一起测试一下吧。
以下为文本的实现方式

function completions($API_KEY,$TEXT,$success) {
    $header = array(
        'Authorization: Bearer '.$API_KEY,
        'Content-type: application/json',
    );

    $params = json_encode(array(
        'messages'=>$TEXT,
        //'messages'=>$TEXT,
        'model' => 'gpt-3.5-turbo',

        ));
    $curl = curl_init('https://api.openai.com/v1/chat/completions');
    $options = array(
        CURLOPT_POST => true,
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_POSTFIELDS => $params,
        CURLOPT_RETURNTRANSFER => true,
    );
    curl_setopt_array($curl, $options);
    $response = curl_exec($curl);
    //var_dump($response);
    $httpcode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
    $text = "服务器连接错误,请稍后再试!";

    if (200 == $httpcode || 429 == $httpcode || 401 == $httpcode || 400 == $httpcode) {
        $json_array = json_decode($response, true);
        if (isset($json_array['choices'][0]['message']['content'])) {
            $text = str_replace("\\n", "\n", $json_array['choices'][0]['message']['content']);
            if ($success == true) {
                success();
            }
        } elseif (isset($json_array['error']['message'])) {
            $text = $json_array['error']['message'];
        } else {
            $text = "对不起,我不知道该怎么回答。";
        }
    }
    return $text;
}

调用方法:

$text = completions($open_ai_key,$prompt,$_POST['key'] == '');

以下为图片实现方法

function imges($API_KEY,$TEXT,$success) {
    $header = array(
        'Authorization: Bearer '.$API_KEY,
        'Content-type: application/json',
    );
    $params = json_encode(array(
        'prompt' => $TEXT,
        "n" => 1,
        "size" => "1024x1024",
    ));
    $curl = curl_init('https://api.openai.com/v1/images/generations');
    $options = array(
        CURLOPT_POST => true,
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_POSTFIELDS => $params,
        CURLOPT_RETURNTRANSFER => true,
    );
    curl_setopt_array($curl, $options);
    $response = curl_exec($curl);

    $httpcode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
    $text['text'] = "服务器连接错误,请稍后再试!";
    $text['status'] = 0;

    if (200 == $httpcode || 429 == $httpcode || 401 == $httpcode || 400 == $httpcode) {
        $json_array = json_decode($response, true);
        if (isset($json_array['data'][0]['url'])) {
            $text['status'] = 1;
            $text['text'] = str_replace("\\n", "\n", $json_array['data'][0]['url']);
            if ($success == true) {
                success();
            }
        } elseif (isset($json_array['error']['message'])) {
            $text['status'] = 0;
            $text['text'] = $json_array['error']['message'];
        } else {
            $text['status'] = 0;
            $text['text'] = "出现一点小问题,可能是网络问题,也可能是您的关键字违规。";
        }
    }

    return $text;
}

调用方法:

$mapping = imges($open_ai_key,$_POST['message'],$_POST['key'] == '');

你可能感兴趣的:(笔记,php,chatgpt,gpt-3)