dirk1983 / chatgpt

演示站现在可以免费使用ChatGPT对话和画图了。全网最易部署,响应速度最快的ChatGPT环境。PHP版调用OpenAI接口进行问答和画图,采用Stream流模式通信,一边生成一边输出。前端采用EventSource,支持Markdown格式解析,支持公式显示,代码有着色处理,支持画图。页面UI简洁,支持上下文连续会话。源码只有几个文件,没用任何框架,支持所有PHP版本,全部开源,极易二开。保姆级教程,账号等周边资源,欢迎进群交流,一切全免费。

Home Page:https://mm1.ltd

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

More options in postData

oleteacher opened this issue · comments

commented

Hello again. Looking forward to trying out your code in the classroom. Going to teach students proper way to use AI:)

Is there any reason the code below would not work:

$postData = [
    "model" => "gpt-3.5-turbo",
    "messages" => [],
    'max_tokens' => 1000,
    'temperature' => 0.7,
    'top_p' => 1,
    'frequency_penalty' => 0,
    'presence_penalty' => 0,
];

I have not yet educated myself on gpt-3.5-turbo so maybe above not needed like past models?

Also, will you be releasing the code you have at http://mm1.ltd/? Like to setup same for some classroom tests.

The new code has been released, and you can try adding these parameters to it. The new code has error prompts, so you can check the error reasons. The specific error information can be viewed from the "errmsg" cookie.

commented

Thank you for reply.

Can you help me understand where to place the above code in new stream.php?

Was clear when using message.php not with stream.php.

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: text/event-stream");
session_start();
$Postscript = $_SESSION['data'];
$ch = curl_init();
$OPENAI_API_KEY = "en-PXQ0A35RLCQaImgLujPST3BlbkFJ2d7Kaa9aJjUqzvYwwkqd";
if ((isset($_SESSION['key'])) && (!empty($_POST['key']))) {
    $OPENAI_API_KEY = $_SESSION['key'];
}
$headers  = [
    'Accept: application/json',
    'Content-Type: application/json',
    'Authorization: Bearer ' . $OPENAI_API_KEY
];

setcookie("errcode", ""); EventSource cannot get the error message, passed through the cookie
setcookie("errmsg", "");

$callback = function ($ch, $data) {
    $complete = json_decode($data);
    if (isset($complete->error)) {
        setcookie("errcode", $complete->error->code);
        setcookie("errmsg", $data);
        if (strpos($complete->error->message, "Rate limit reached") === 0) { The code returned by the access frequency overrun error is empty, so special handling is given
            setcookie("errcode", "rate_limit_reached");
        }
    } else {
        echo $data;
    }
    return strlen($data);
};

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $Postscript);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback);
//curl_setopt($ch, CURLOPT_PROXY, "http://127.0.0.1:1081");

curl_exec($ch);
curl_close($ch);
commented

OK, I open my eyes after morning coffee:) I think now the postdata is handled in setsession.php?