dirk1983 / chatgpt

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

Output is still not just right... #74

Closed oleteacher closed 1 year ago

oleteacher commented 1 year ago

This is example when the script breaks outside the code box and destroys the response:

echo '<script>
        setTimeout(function() {
            document.getElementById("popup").classList.add("fade-out");
        }, 5000);
      </script>';
} ?>

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Token Generator</title> <!-- Bootstrap CSS --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"> <style> .text-center { text-align: center; } .popup { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); padding: 20px; background-color: black; color: white; } @keyframes fadeOut { from {opacity: 1;} to {opacity: 0;} } .popup.fade-out { animation-name: fadeOut; animation-duration: 5s; animation-fill-mode: forwards; } </style> </head> <body> <div class="container my-5"> <!-- Form to generate a new token --> <form action="<?php echo htmlspecialchars($_SERVER[“PHP_SELF”]); ?>" method="post"> <div class="form-group"> <label for="email">Email:</label> <input type="email" id="email" name="email" class="form-control"> </div> <div class="form-group"> <label for="name">Recipient Name:</label> <input type="text" id="name" name="name" class="form-control"> </div> <div class="form-group"> <label for="uses">Number of uses:</label> <input type="number" id="uses" name="uses" min="1" value="1" class="form-control"> </div> <input type="submit" name="submit" value="Generate Token" class="btn btn-primary mt-3"> </form>

复制    <!-- Display the generated URL -->
    <?php if (isset($url)): ?>
        <div class="input-group my-3">
            <input type="text" class="form-control" value="<?php echo htmlspecialchars($url); ?>" readonly>
            <button class="btn btn-outline-secondary" type="button" onclick="copyToClipboard(this)">Copy</button>
        </div>
    <?php endif; ?>

    <!-- Form to resend the email -->
    <?php if (isset($token)): ?>
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
            <input type="hidden" name="email" value="<?php echo htmlspecialchars($email); ?>">
            <input type="hidden" name="name" value="<?php echo htmlspecialchars($name); ?>">
            <input type="hidden" name="token" value="<?php echo htmlspecialchars($token); ?>">
            <input type="submit" name="resend" value="Resend Email" class="btn btn-secondary mt-3">
        </form>
    <?php endif; ?>
</div>

<!-- Script to copy the URL to the clipboard -->
<script>
function copyToClipboard(element) {
    var input = element.previousElementSibling;
    input.select();
    document.execCommand("copy");
}
</script>

<!-- Bootstrap Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body> </html>

image

Not positive how to fix, spent hours looking into with no success. Above my tiny mind, need a god:)

oleteacher commented 1 year ago

Any input on this issue greatly appreciated.

gzz100 commented 1 year ago

This issue usually occurs when letting AI write HTML, where the AI does not respond with descriptive language but directly provides code. Therefore, adding the following code to line 301 of chat.js can solve this problem:

if (alltext == "") {
alltext = json.choices[0].delta.content.replace(/^\n+/, '');
if(alltext.indexOf("```") == -1 && (alltext.indexOf("<") != -1 || alltext.indexOf(">") != -1)){
                            alltext = "```\n" + alltext;
                        }
oleteacher commented 1 year ago

Thank you @gzz100

Your code seems to fix the issue. Appreciate you taking the time to provide input.

oleteacher commented 1 year ago

I spoke too soon :(

A student typed in the following prompt:

write example php code for flat-file texted based cms for sales pages

This was the output (screenshot):

image

oleteacher commented 1 year ago

Very strange results. Just tried the code on another workstation and it works fine. Moved to another workstation and does not work correctly, output is broken as shown above.

Have cleared cache and even renamed chatjs in index.