AI 生成: 基于 Flask 的 AI 免费对话应用,后端集成豆包(Doubao)和 DeepSeek,通过 SSE 实时推送消息,前端提供简洁的聊天界面。
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
|
||||
"Microsoft YaHei", sans-serif;
|
||||
background: #f5f6fa;
|
||||
color: #1f2937;
|
||||
height: 100vh;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.app {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100vh;
|
||||
max-width: 900px;
|
||||
margin: 0 auto;
|
||||
background: #fff;
|
||||
box-shadow: 0 0 24px rgba(0, 0, 0, 0.06);
|
||||
}
|
||||
|
||||
.header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 14px 20px;
|
||||
border-bottom: 1px solid #e5e7eb;
|
||||
background: linear-gradient(90deg, #6366f1, #8b5cf6);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.header h1 {
|
||||
font-size: 18px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.provider-switch {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.provider-switch label {
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.chat {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 20px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 14px;
|
||||
}
|
||||
|
||||
.message {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.message.user {
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.bubble {
|
||||
max-width: 78%;
|
||||
padding: 10px 14px;
|
||||
border-radius: 14px;
|
||||
line-height: 1.6;
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
.message.user .bubble {
|
||||
background: #6366f1;
|
||||
color: #fff;
|
||||
border-bottom-right-radius: 4px;
|
||||
}
|
||||
|
||||
.message.assistant .bubble {
|
||||
background: #f1f3f8;
|
||||
color: #1f2937;
|
||||
border-bottom-left-radius: 4px;
|
||||
}
|
||||
|
||||
.composer {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
padding: 12px 16px;
|
||||
border-top: 1px solid #e5e7eb;
|
||||
background: #fafafa;
|
||||
}
|
||||
|
||||
.composer textarea {
|
||||
flex: 1;
|
||||
resize: none;
|
||||
height: 48px;
|
||||
padding: 12px 14px;
|
||||
border: 1px solid #d1d5db;
|
||||
border-radius: 10px;
|
||||
font-size: 15px;
|
||||
font-family: inherit;
|
||||
outline: none;
|
||||
transition: border-color 0.2s;
|
||||
}
|
||||
|
||||
.composer textarea:focus {
|
||||
border-color: #6366f1;
|
||||
}
|
||||
|
||||
.composer button {
|
||||
padding: 0 22px;
|
||||
border: none;
|
||||
border-radius: 10px;
|
||||
background: #6366f1;
|
||||
color: #fff;
|
||||
font-size: 15px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
.composer button:hover {
|
||||
background: #4f46e5;
|
||||
}
|
||||
|
||||
.composer button:disabled {
|
||||
background: #a5b4fc;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.cursor::after {
|
||||
content: "▋";
|
||||
animation: blink 1s steps(2, start) infinite;
|
||||
margin-left: 2px;
|
||||
color: #6366f1;
|
||||
}
|
||||
|
||||
@keyframes blink {
|
||||
to {
|
||||
visibility: hidden;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
// AI 免费对话 - 前端逻辑
|
||||
(function () {
|
||||
const chatEl = document.getElementById("chat");
|
||||
const inputEl = document.getElementById("input");
|
||||
const sendBtn = document.getElementById("send");
|
||||
|
||||
// 保存对话历史(用于多轮上下文)
|
||||
const history = [];
|
||||
let sending = false;
|
||||
|
||||
/** 获取当前选择的 provider */
|
||||
function getProvider() {
|
||||
const el = document.querySelector('input[name="provider"]:checked');
|
||||
return el ? el.value : "deepseek";
|
||||
}
|
||||
|
||||
/** 追加一条消息到界面 */
|
||||
function appendMessage(role, content) {
|
||||
const wrapper = document.createElement("div");
|
||||
wrapper.className = `message ${role}`;
|
||||
const bubble = document.createElement("div");
|
||||
bubble.className = "bubble";
|
||||
bubble.textContent = content;
|
||||
wrapper.appendChild(bubble);
|
||||
chatEl.appendChild(wrapper);
|
||||
chatEl.scrollTop = chatEl.scrollHeight;
|
||||
return bubble;
|
||||
}
|
||||
|
||||
/** 发送消息并接收 SSE 流 */
|
||||
async function send() {
|
||||
if (sending) return;
|
||||
const text = inputEl.value.trim();
|
||||
if (!text) return;
|
||||
|
||||
sending = true;
|
||||
sendBtn.disabled = true;
|
||||
inputEl.value = "";
|
||||
|
||||
// 展示用户消息
|
||||
appendMessage("user", text);
|
||||
history.push({ role: "user", content: text });
|
||||
|
||||
// 展示助手占位
|
||||
const bubble = appendMessage("assistant", "");
|
||||
bubble.classList.add("cursor");
|
||||
|
||||
let assistantText = "";
|
||||
|
||||
try {
|
||||
const resp = await fetch("api/chat", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
provider: getProvider(),
|
||||
messages: history,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!resp.ok || !resp.body) {
|
||||
throw new Error("网络请求失败");
|
||||
}
|
||||
|
||||
const reader = resp.body.getReader();
|
||||
const decoder = new TextDecoder("utf-8");
|
||||
let buffer = "";
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
|
||||
// 按 SSE 事件分隔(\n\n)
|
||||
const parts = buffer.split("\n\n");
|
||||
buffer = parts.pop(); // 保留最后不完整片段
|
||||
|
||||
for (const part of parts) {
|
||||
const line = part.trim();
|
||||
if (!line.startsWith("data:")) continue;
|
||||
const data = line.slice(5).trim();
|
||||
if (data === "[DONE]") continue;
|
||||
try {
|
||||
const obj = JSON.parse(data);
|
||||
if (obj.error) {
|
||||
assistantText += `\n[错误] ${obj.error}`;
|
||||
bubble.textContent = assistantText;
|
||||
} else if (obj.content) {
|
||||
assistantText += obj.content;
|
||||
bubble.textContent = assistantText;
|
||||
chatEl.scrollTop = chatEl.scrollHeight;
|
||||
}
|
||||
} catch (e) {
|
||||
// 忽略解析错误
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
assistantText += `\n[错误] ${err.message}`;
|
||||
bubble.textContent = assistantText;
|
||||
} finally {
|
||||
bubble.classList.remove("cursor");
|
||||
if (assistantText) {
|
||||
history.push({ role: "assistant", content: assistantText });
|
||||
}
|
||||
sending = false;
|
||||
sendBtn.disabled = false;
|
||||
inputEl.focus();
|
||||
}
|
||||
}
|
||||
|
||||
// 事件绑定
|
||||
sendBtn.addEventListener("click", send);
|
||||
inputEl.addEventListener("keydown", function (e) {
|
||||
if (e.key === "Enter" && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
send();
|
||||
}
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user