260 lines
6.9 KiB
HTML
260 lines
6.9 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<title>问·想</title>
|
|
<script src="dist/Pandora.min.js"></script>
|
|
<script>
|
|
$("html").AutoSize();
|
|
</script>
|
|
<style>
|
|
:root {
|
|
--alertFontSize: 0.15rem;
|
|
--alertBg: rgba(0, 0, 0, 0);
|
|
--confirmFontSize: 0.15rem;
|
|
--confirmBg: rgba(0, 0, 0, 0);
|
|
}
|
|
body {
|
|
margin: 0;
|
|
padding: 0;
|
|
font-family: "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif;
|
|
background: #edede9;
|
|
overflow: hidden;
|
|
}
|
|
/* 自定义滚动条样式 */
|
|
::-webkit-scrollbar {
|
|
width: 5px;
|
|
height: 5px;
|
|
}
|
|
::-webkit-scrollbar-track {
|
|
background: #fff;
|
|
}
|
|
::-webkit-scrollbar-thumb {
|
|
background: #ccc;
|
|
border-radius: 5px;
|
|
}
|
|
::-webkit-scrollbar-thumb:hover {
|
|
background: #aaa;
|
|
}
|
|
section {
|
|
width: 90%;
|
|
height: 100vh;
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
margin: 0 auto;
|
|
font-size: 0.4rem;
|
|
}
|
|
section article {
|
|
width: 100%;
|
|
height: 70%;
|
|
overflow: auto;
|
|
padding: 20px;
|
|
box-shadow: 0 0 10px 5px rgba(0, 0, 0, 0.08);
|
|
border-radius: 15px;
|
|
margin: 2vh auto;
|
|
box-sizing: border-box;
|
|
flex-shrink: 0;
|
|
background: #e5e5e5;
|
|
}
|
|
section article .left,
|
|
section article .right {
|
|
margin: 10px 0;
|
|
line-height: 1.5;
|
|
color: white;
|
|
max-width: 60%;
|
|
padding: 10px 20px;
|
|
box-sizing: border-box;
|
|
clear: both;
|
|
border-radius: 15px;
|
|
font-size: 0.45em;
|
|
font-family: "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif;
|
|
}
|
|
section article .left {
|
|
float: left;
|
|
background: #57cc99;
|
|
border-top-left-radius: 0;
|
|
}
|
|
section article .right {
|
|
float: right;
|
|
background: #3a86ff;
|
|
color: white;
|
|
border-bottom-right-radius: 0;
|
|
}
|
|
section textarea {
|
|
width: 100%;
|
|
height: 4.5em;
|
|
resize: none;
|
|
padding: 10px;
|
|
box-sizing: border-box;
|
|
flex-shrink: 0;
|
|
margin: 2vh auto;
|
|
border: 0;
|
|
font-size: 0.5em;
|
|
font-family: "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif;
|
|
background: #e5e5e5;
|
|
box-shadow: 0 0 10px 5px rgba(0, 0, 0, 0.08);
|
|
border-radius: 15px;
|
|
}
|
|
section textarea:focus {
|
|
outline: #40a9ff solid 1px;
|
|
}
|
|
section button {
|
|
border: none;
|
|
outline: none;
|
|
background: #000;
|
|
color: #fff;
|
|
cursor: pointer;
|
|
padding: 10px;
|
|
box-shadow: 0 0 5px 5px rgba(0, 0, 0, 0.08);
|
|
}
|
|
section .buttons {
|
|
display: flex;
|
|
justify-content: center;
|
|
flex-shrink: 0;
|
|
}
|
|
section .buttons button {
|
|
border-radius: 15px;
|
|
font-size: 0.5em;
|
|
margin: 0 0.5em;
|
|
padding: 10px 30px;
|
|
}
|
|
section .buttons #reload {
|
|
background: #ff4d4f;
|
|
}
|
|
section .buttons #submit {
|
|
background: #40a9ff;
|
|
}
|
|
#Pd_loader {
|
|
font-size: 0.2rem !important;
|
|
}
|
|
section article pre,
|
|
section article p {
|
|
margin: 0;
|
|
white-space: break-spaces;
|
|
word-break: break-all;
|
|
}
|
|
section article pre {
|
|
background: #fafafa;
|
|
padding: 1em;
|
|
border-radius: 15px;
|
|
margin: 0.5em 0;
|
|
}
|
|
section article a {
|
|
color: #f6bd60;
|
|
}
|
|
</style>
|
|
<link rel="stylesheet" href="dist/atom-one-light.css" />
|
|
</head>
|
|
|
|
<body>
|
|
<section>
|
|
<article></article>
|
|
<textarea placeholder="请在这里输入你的提问..."></textarea>
|
|
<div class="buttons"><button id="reload">重新发问</button><button id="submit">提问</button></div>
|
|
</section>
|
|
</body>
|
|
<script src="dist/markdown-it.min.js"></script>
|
|
<script src="dist/highlight.min.js"></script>
|
|
<script>
|
|
const md = window.markdownit();
|
|
md.set({ linkify: true, typographer: true, breaks: true });
|
|
const messages = [];
|
|
let timer = null;
|
|
|
|
confirm({
|
|
content: `欢迎使用问·想`,
|
|
showCancel: false,
|
|
confirmText: "进入",
|
|
});
|
|
|
|
// 提交问题
|
|
function questionSubmit() {
|
|
const question = $("textarea").val().trim();
|
|
if (question == "") {
|
|
alert("请输入内容");
|
|
return;
|
|
}
|
|
|
|
$("textarea").blur();
|
|
|
|
let loadTime = 0;
|
|
window.showLoading(`正在思考中... ${loadTime++}s`);
|
|
timer = setInterval(() => {
|
|
window.showLoading(`正在思考中... ${loadTime++}s`);
|
|
}, 1000);
|
|
$("article").append(`<p class="right">${question.toString()}</p>`);
|
|
|
|
//延时滚动到底部
|
|
$("article").get.scrollTo({ top: $("article").get.scrollHeight, behavior: "smooth" });
|
|
messages.splice(0, 0, { role: "user", content: question.toString() });
|
|
|
|
$()
|
|
.ajax({
|
|
url: "https://api.pandorastudio.cn/common/chat",
|
|
type: "POST",
|
|
data: { messages: JSON.stringify(messages.reverse()), model: "gpt-3.5-turbo" },
|
|
async: true,
|
|
})
|
|
.then(res => {
|
|
const { code, data } = res;
|
|
if (code == 200) {
|
|
// 生成时间戳id
|
|
const id = new Date().getTime();
|
|
$("article").append(`<div id="ass_${id}" class="left">${data[0].message.content}</div>`);
|
|
// 解析markdown
|
|
$(`#ass_${id}`).html(md.render($(`#ass_${id}`).html()));
|
|
// 高亮html
|
|
$(`#ass_${id}`)
|
|
.get.querySelectorAll("code")
|
|
.forEach(el => {
|
|
// then highlight each
|
|
hljs.highlightElement(el);
|
|
});
|
|
|
|
messages.splice(0, 0, { role: "assistant", content: data[0].message.content });
|
|
|
|
// 清空输入框所有内容,删除换行符
|
|
$("textarea").val("");
|
|
} else {
|
|
const { message } = JSON.parse(res.err).error;
|
|
alert(message);
|
|
}
|
|
})
|
|
.catch(err => {
|
|
confirm({
|
|
content: `发生错误:${JSON.stringify(err)}`,
|
|
confirmText: "重新发送",
|
|
}).then(questionSubmit);
|
|
})
|
|
.finally(() => {
|
|
clearInterval(timer);
|
|
window.hideLoading();
|
|
|
|
//延时滚动到底部
|
|
$("article").get.scrollTo({
|
|
top: $("article").get.scrollHeight,
|
|
behavior: "smooth",
|
|
});
|
|
});
|
|
}
|
|
|
|
// 点击提交按钮
|
|
$("#submit").click(questionSubmit);
|
|
// 监听回车键
|
|
$("textarea").bind("keydown", e => {
|
|
if (e.keyCode == 13) {
|
|
e.preventDefault();
|
|
questionSubmit();
|
|
}
|
|
});
|
|
|
|
// 重新发问
|
|
$("#reload").click(() => {
|
|
$("article").empty();
|
|
messages.splice(0, messages.length);
|
|
});
|
|
</script>
|
|
</html>
|