381 行
9.9 KiB
HTML
381 行
9.9 KiB
HTML
<!doctype html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="UTF-8" />
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||
<title>中文 AI 对话工作台</title>
|
||
<style>
|
||
:root {
|
||
--bg: #0f172a;
|
||
--panel: #111827;
|
||
--panel-2: #1f2937;
|
||
--text: #e5e7eb;
|
||
--muted: #9ca3af;
|
||
--primary: #22c55e;
|
||
--primary-hover: #16a34a;
|
||
--danger: #ef4444;
|
||
--border: #374151;
|
||
}
|
||
|
||
* {
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
body {
|
||
margin: 0;
|
||
font-family: "Noto Sans SC", "Microsoft YaHei", "PingFang SC", sans-serif;
|
||
background: radial-gradient(circle at top right, #1e293b 0%, var(--bg) 55%);
|
||
color: var(--text);
|
||
min-height: 100vh;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
padding: 16px;
|
||
}
|
||
|
||
.app {
|
||
width: min(980px, 100%);
|
||
height: min(92vh, 860px);
|
||
border: 1px solid var(--border);
|
||
border-radius: 16px;
|
||
background: linear-gradient(180deg, #0b1220 0%, #0d1424 100%);
|
||
display: grid;
|
||
grid-template-rows: auto 1fr auto;
|
||
overflow: hidden;
|
||
}
|
||
|
||
.header {
|
||
padding: 14px 16px;
|
||
border-bottom: 1px solid var(--border);
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 12px;
|
||
}
|
||
|
||
.title {
|
||
font-size: 18px;
|
||
font-weight: 700;
|
||
letter-spacing: 0.2px;
|
||
}
|
||
|
||
.subtitle {
|
||
margin-top: 2px;
|
||
color: var(--muted);
|
||
font-size: 12px;
|
||
}
|
||
|
||
.header-actions {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 12px;
|
||
}
|
||
|
||
.switch {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
gap: 6px;
|
||
color: var(--muted);
|
||
font-size: 13px;
|
||
}
|
||
|
||
.btn {
|
||
border: none;
|
||
border-radius: 10px;
|
||
padding: 8px 12px;
|
||
font-size: 13px;
|
||
font-weight: 600;
|
||
cursor: pointer;
|
||
}
|
||
|
||
.btn-clear {
|
||
color: #fff;
|
||
background: #374151;
|
||
}
|
||
|
||
.btn-clear:hover {
|
||
background: #4b5563;
|
||
}
|
||
|
||
.chat {
|
||
padding: 18px;
|
||
overflow: auto;
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 12px;
|
||
}
|
||
|
||
.msg {
|
||
max-width: 82%;
|
||
padding: 12px 14px;
|
||
border-radius: 12px;
|
||
border: 1px solid var(--border);
|
||
line-height: 1.6;
|
||
white-space: pre-wrap;
|
||
word-break: break-word;
|
||
}
|
||
|
||
.msg.user {
|
||
align-self: flex-end;
|
||
background: #0f2a1f;
|
||
border-color: #1d4f3c;
|
||
}
|
||
|
||
.msg.assistant {
|
||
align-self: flex-start;
|
||
background: var(--panel);
|
||
}
|
||
|
||
.msg.system {
|
||
align-self: center;
|
||
max-width: 92%;
|
||
background: #172033;
|
||
border-style: dashed;
|
||
color: #cbd5e1;
|
||
font-size: 13px;
|
||
}
|
||
|
||
.composer {
|
||
border-top: 1px solid var(--border);
|
||
padding: 14px;
|
||
display: grid;
|
||
gap: 10px;
|
||
}
|
||
|
||
textarea {
|
||
width: 100%;
|
||
min-height: 96px;
|
||
resize: vertical;
|
||
border-radius: 12px;
|
||
border: 1px solid var(--border);
|
||
background: var(--panel-2);
|
||
color: var(--text);
|
||
padding: 12px;
|
||
font-size: 14px;
|
||
line-height: 1.5;
|
||
}
|
||
|
||
textarea::placeholder {
|
||
color: var(--muted);
|
||
}
|
||
|
||
.actions {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 12px;
|
||
}
|
||
|
||
.hint {
|
||
color: var(--muted);
|
||
font-size: 12px;
|
||
}
|
||
|
||
.btn-send {
|
||
color: #08120a;
|
||
background: var(--primary);
|
||
}
|
||
|
||
.btn-send:hover {
|
||
background: var(--primary-hover);
|
||
}
|
||
|
||
.btn-send:disabled {
|
||
cursor: not-allowed;
|
||
background: #14532d;
|
||
color: #86efac;
|
||
}
|
||
|
||
.status {
|
||
min-height: 18px;
|
||
color: #93c5fd;
|
||
font-size: 12px;
|
||
}
|
||
|
||
@media (max-width: 720px) {
|
||
.app {
|
||
height: 100vh;
|
||
border-radius: 0;
|
||
}
|
||
|
||
.msg {
|
||
max-width: 92%;
|
||
}
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<main class="app">
|
||
<header class="header">
|
||
<div>
|
||
<div class="title">中文 AI 对话工作台</div>
|
||
<div class="subtitle">默认通过 /api/llm/chat 调用,回答语言优先简体中文</div>
|
||
</div>
|
||
<div class="header-actions">
|
||
<label class="switch">
|
||
<input id="streamSwitch" type="checkbox" checked />
|
||
流式返回
|
||
</label>
|
||
<button id="clearBtn" class="btn btn-clear" type="button">清空对话</button>
|
||
</div>
|
||
</header>
|
||
|
||
<section id="chatBox" class="chat" aria-live="polite"></section>
|
||
|
||
<section class="composer">
|
||
<textarea id="inputBox" placeholder="请输入问题(示例:请总结 APK 动态调试的关键步骤)"></textarea>
|
||
<div class="actions">
|
||
<div>
|
||
<div id="status" class="status"></div>
|
||
<div class="hint">回车发送,Shift+回车换行</div>
|
||
</div>
|
||
<button id="sendBtn" class="btn btn-send" type="button">发送</button>
|
||
</div>
|
||
</section>
|
||
</main>
|
||
|
||
<script>
|
||
const chatBox = document.getElementById("chatBox");
|
||
const inputBox = document.getElementById("inputBox");
|
||
const sendBtn = document.getElementById("sendBtn");
|
||
const clearBtn = document.getElementById("clearBtn");
|
||
const statusEl = document.getElementById("status");
|
||
const streamSwitch = document.getElementById("streamSwitch");
|
||
|
||
const apiPath = "/api/llm/chat";
|
||
const conversation = [];
|
||
let sending = false;
|
||
|
||
function setStatus(text) {
|
||
statusEl.textContent = text || "";
|
||
}
|
||
|
||
function addMessage(role, content) {
|
||
const el = document.createElement("div");
|
||
el.className = `msg ${role}`;
|
||
el.textContent = content;
|
||
chatBox.appendChild(el);
|
||
chatBox.scrollTop = chatBox.scrollHeight;
|
||
return el;
|
||
}
|
||
|
||
function resetConversation() {
|
||
conversation.length = 0;
|
||
chatBox.innerHTML = "";
|
||
addMessage(
|
||
"system",
|
||
"系统提示:本页面用于中文对话。若你明确要求其他语言,模型会按你的要求切换。"
|
||
);
|
||
setStatus("");
|
||
}
|
||
|
||
function extractTextFromChunk(json) {
|
||
const choice = json && Array.isArray(json.choices) ? json.choices[0] : null;
|
||
if (!choice) return "";
|
||
if (choice.delta && typeof choice.delta.content === "string") return choice.delta.content;
|
||
if (choice.message && typeof choice.message.content === "string") return choice.message.content;
|
||
return "";
|
||
}
|
||
|
||
async function sendMessage() {
|
||
if (sending) return;
|
||
const text = inputBox.value.trim();
|
||
if (!text) return;
|
||
|
||
sending = true;
|
||
sendBtn.disabled = true;
|
||
setStatus("正在请求模型...");
|
||
|
||
addMessage("user", text);
|
||
conversation.push({ role: "user", content: text });
|
||
inputBox.value = "";
|
||
|
||
const stream = !!streamSwitch.checked;
|
||
|
||
try {
|
||
const res = await fetch(apiPath, {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({
|
||
messages: conversation,
|
||
stream,
|
||
}),
|
||
});
|
||
|
||
if (!res.ok) {
|
||
const errText = await res.text();
|
||
addMessage("system", `请求失败(${res.status}):${errText || "未知错误"}`);
|
||
return;
|
||
}
|
||
|
||
if (!stream) {
|
||
const data = await res.json();
|
||
const content =
|
||
data && data.choices && data.choices[0] && data.choices[0].message
|
||
? data.choices[0].message.content || ""
|
||
: "";
|
||
addMessage("assistant", content || "(空响应)");
|
||
conversation.push({ role: "assistant", content: content || "" });
|
||
setStatus("完成");
|
||
return;
|
||
}
|
||
|
||
const aiEl = addMessage("assistant", "");
|
||
const reader = res.body.getReader();
|
||
const decoder = new TextDecoder("utf-8");
|
||
let buffer = "";
|
||
let full = "";
|
||
|
||
while (true) {
|
||
const { value, done } = await reader.read();
|
||
if (done) break;
|
||
|
||
buffer += decoder.decode(value, { stream: true });
|
||
const lines = buffer.split("\n");
|
||
buffer = lines.pop() || "";
|
||
|
||
for (const rawLine of lines) {
|
||
const line = rawLine.trim();
|
||
if (!line.startsWith("data:")) continue;
|
||
|
||
const payload = line.slice(5).trim();
|
||
if (!payload || payload === "[DONE]") continue;
|
||
|
||
try {
|
||
const json = JSON.parse(payload);
|
||
const piece = extractTextFromChunk(json);
|
||
if (!piece) continue;
|
||
full += piece;
|
||
aiEl.textContent = full;
|
||
chatBox.scrollTop = chatBox.scrollHeight;
|
||
} catch (_) {
|
||
}
|
||
}
|
||
}
|
||
|
||
conversation.push({ role: "assistant", content: full });
|
||
setStatus("完成(流式)");
|
||
} catch (error) {
|
||
addMessage("system", `网络异常:${error && error.message ? error.message : "未知错误"}`);
|
||
} finally {
|
||
sending = false;
|
||
sendBtn.disabled = false;
|
||
}
|
||
}
|
||
|
||
sendBtn.addEventListener("click", sendMessage);
|
||
|
||
inputBox.addEventListener("keydown", (event) => {
|
||
if (event.key === "Enter" && !event.shiftKey) {
|
||
event.preventDefault();
|
||
sendMessage();
|
||
}
|
||
});
|
||
|
||
clearBtn.addEventListener("click", resetConversation);
|
||
|
||
resetConversation();
|
||
</script>
|
||
</body>
|
||
</html>
|