222 lines
6.2 KiB
HTML
222 lines
6.2 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>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
|
background: #f5f5f5;
|
|
height: 100vh;
|
|
display: flex;
|
|
flex-direction: column;
|
|
}
|
|
|
|
.header {
|
|
background: #4a90d9;
|
|
color: white;
|
|
padding: 20px;
|
|
text-align: center;
|
|
font-size: 20px;
|
|
font-weight: bold;
|
|
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
|
}
|
|
|
|
.chat-container {
|
|
flex: 1;
|
|
overflow-y: auto;
|
|
padding: 20px;
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 15px;
|
|
}
|
|
|
|
.message {
|
|
max-width: 70%;
|
|
padding: 12px 16px;
|
|
border-radius: 12px;
|
|
line-height: 1.5;
|
|
word-wrap: break-word;
|
|
}
|
|
|
|
.user-message {
|
|
align-self: flex-end;
|
|
background: #4a90d9;
|
|
color: white;
|
|
border-bottom-right-radius: 4px;
|
|
}
|
|
|
|
.ai-message {
|
|
align-self: flex-start;
|
|
background: white;
|
|
color: #333;
|
|
border-bottom-left-radius: 4px;
|
|
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
|
}
|
|
|
|
.input-container {
|
|
background: white;
|
|
padding: 15px 20px;
|
|
box-shadow: 0 -2px 4px rgba(0,0,0,0.05);
|
|
display: flex;
|
|
gap: 10px;
|
|
}
|
|
|
|
#messageInput {
|
|
flex: 1;
|
|
padding: 12px 16px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 8px;
|
|
font-size: 14px;
|
|
outline: none;
|
|
transition: border-color 0.3s;
|
|
}
|
|
|
|
#messageInput:focus {
|
|
border-color: #4a90d9;
|
|
}
|
|
|
|
#sendBtn {
|
|
padding: 12px 24px;
|
|
background: #4a90d9;
|
|
color: white;
|
|
border: none;
|
|
border-radius: 8px;
|
|
font-size: 14px;
|
|
cursor: pointer;
|
|
transition: background 0.3s;
|
|
}
|
|
|
|
#sendBtn:hover {
|
|
background: #3a7bc8;
|
|
}
|
|
|
|
#sendBtn:disabled {
|
|
background: #ccc;
|
|
cursor: not-allowed;
|
|
}
|
|
|
|
.loading {
|
|
display: inline-block;
|
|
width: 20px;
|
|
height: 20px;
|
|
border: 3px solid #f3f3f3;
|
|
border-top: 3px solid #4a90d9;
|
|
border-radius: 50%;
|
|
animation: spin 1s linear infinite;
|
|
}
|
|
|
|
@keyframes spin {
|
|
0% { transform: rotate(0deg); }
|
|
100% { transform: rotate(360deg); }
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="header">AI 智能助手</div>
|
|
|
|
<div class="chat-container" id="chatContainer"></div>
|
|
|
|
<div class="input-container">
|
|
<input type="text" id="messageInput" placeholder="输入您的问题..." autofocus>
|
|
<button id="sendBtn">发送</button>
|
|
</div>
|
|
|
|
<script>
|
|
const chatContainer = document.getElementById('chatContainer');
|
|
const messageInput = document.getElementById('messageInput');
|
|
const sendBtn = document.getElementById('sendBtn');
|
|
|
|
// API 地址(根据你的实际运行地址修改)
|
|
const API_URL = 'http://localhost:8000/api/v1/chat-with-tools';
|
|
|
|
function addMessage(content, isUser = false) {
|
|
const messageDiv = document.createElement('div');
|
|
messageDiv.className = `message ${isUser ? 'user-message' : 'ai-message'}`;
|
|
messageDiv.textContent = content;
|
|
chatContainer.appendChild(messageDiv);
|
|
chatContainer.scrollTop = chatContainer.scrollHeight;
|
|
}
|
|
|
|
function addLoadingIndicator() {
|
|
const loadingDiv = document.createElement('div');
|
|
loadingDiv.className = 'message ai-message';
|
|
loadingDiv.id = 'loadingIndicator';
|
|
loadingDiv.innerHTML = '<div class="loading"></div>';
|
|
chatContainer.appendChild(loadingDiv);
|
|
chatContainer.scrollTop = chatContainer.scrollHeight;
|
|
}
|
|
|
|
function removeLoadingIndicator() {
|
|
const loading = document.getElementById('loadingIndicator');
|
|
if (loading) {
|
|
loading.remove();
|
|
}
|
|
}
|
|
|
|
async function sendMessage() {
|
|
const message = messageInput.value.trim();
|
|
if (!message) return;
|
|
|
|
// 禁用输入和按钮
|
|
messageInput.disabled = true;
|
|
sendBtn.disabled = true;
|
|
|
|
// 添加用户消息
|
|
addMessage(message, true);
|
|
messageInput.value = '';
|
|
|
|
// 添加加载指示器
|
|
addLoadingIndicator();
|
|
|
|
try {
|
|
const response = await fetch(API_URL, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
message: message
|
|
})
|
|
});
|
|
|
|
removeLoadingIndicator();
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json();
|
|
throw new Error(error.detail || '请求失败');
|
|
}
|
|
|
|
const data = await response.json();
|
|
addMessage(data.response);
|
|
|
|
} catch (error) {
|
|
removeLoadingIndicator();
|
|
addMessage(`错误: ${error.message}`);
|
|
} finally {
|
|
messageInput.disabled = false;
|
|
sendBtn.disabled = false;
|
|
messageInput.focus();
|
|
}
|
|
}
|
|
|
|
// 点击发送按钮
|
|
sendBtn.addEventListener('click', sendMessage);
|
|
|
|
// 回车发送
|
|
messageInput.addEventListener('keypress', (e) => {
|
|
if (e.key === 'Enter') {
|
|
sendMessage();
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|