You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
6.6 KiB
HTML

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SQL生成聊天界面</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.chat-container {
background-color: white;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
padding: 20px;
margin-bottom: 20px;
height: 500px;
overflow-y: auto;
}
.message {
margin-bottom: 15px;
padding: 10px;
border-radius: 8px;
max-width: 80%;
}
.user-message {
background-color: #dcf8c6;
margin-left: auto;
}
.bot-message {
background-color: #e5e5ea;
margin-right: auto;
}
.input-container {
display: flex;
gap: 10px;
}
#user-input {
flex: 1;
padding: 12px;
border: 1px solid #ddd;
border-radius: 20px;
outline: none;
}
#send-button {
padding: 12px 24px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 20px;
cursor: pointer;
}
#send-button:disabled {
background-color: #cccccc;
cursor: not-allowed;
}
.loading {
text-align: center;
color: #666;
font-style: italic;
}
.sql-result {
font-family: monospace;
background-color: #f8f8f8;
padding: 10px;
border-left: 4px solid #4CAF50;
white-space: pre-wrap;
overflow-x: auto;
}
.error-message {
color: #f44336;
background-color: #ffebee;
padding: 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<h1>SQL生成聊天助手</h1>
<div class="chat-container" id="chat-container">
<div class="message bot-message">
您好请描述您需要生成SQL的需求我会为您生成相应的SQL语句。
</div>
</div>
<div class="input-container">
<input type="text" id="user-input" placeholder="请输入您的问题..." />
<button id="send-button">发送</button>
</div>
<script>
const chatContainer = document.getElementById('chat-container');
const userInput = document.getElementById('user-input');
const sendButton = document.getElementById('send-button');
// 添加消息到聊天界面
function addMessage(text, isUser = false, isSQL = false, isError = false) {
const messageDiv = document.createElement('div');
messageDiv.className = `message ${isUser ? 'user-message' : 'bot-message'}`;
if (isSQL) {
const sqlDiv = document.createElement('div');
sqlDiv.className = 'sql-result';
sqlDiv.textContent = text;
messageDiv.appendChild(sqlDiv);
} else if (isError) {
messageDiv.className = 'error-message';
messageDiv.textContent = text;
} else {
messageDiv.textContent = text;
}
chatContainer.appendChild(messageDiv);
chatContainer.scrollTop = chatContainer.scrollHeight;
}
// 显示加载状态
function showLoading() {
const loadingDiv = document.createElement('div');
loadingDiv.className = 'message bot-message loading';
loadingDiv.id = 'loading-message';
loadingDiv.textContent = '正在生成SQL请稍候...';
chatContainer.appendChild(loadingDiv);
chatContainer.scrollTop = chatContainer.scrollHeight;
// 禁用输入和发送按钮
userInput.disabled = true;
sendButton.disabled = true;
sendButton.textContent = '处理中...';
}
// 隐藏加载状态
function hideLoading() {
const loadingMessage = document.getElementById('loading-message');
if (loadingMessage) {
loadingMessage.remove();
}
// 启用输入和发送按钮
userInput.disabled = false;
sendButton.disabled = false;
sendButton.textContent = '发送';
}
// 调用后端API生成SQL
async function generateSQL(query) {
try {
const response = await fetch('http://localhost:8000/rule/generate_sql', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ query: query })
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || '请求失败');
}
const data = await response.json();
return data.sql;
} catch (error) {
throw new Error(`API调用失败: ${error.message}`);
}
}
// 处理用户发送消息
async function handleSendMessage() {
const query = userInput.value.trim();
if (!query) return;
// 添加用户消息
addMessage(query, true);
userInput.value = '';
// 显示加载状态
showLoading();
try {
// 调用后端生成SQL
const sqlResult = await generateSQL(query);
// 隐藏加载状态
hideLoading();
// 显示SQL结果
addMessage(sqlResult, false, true);
} catch (error) {
// 隐藏加载状态
hideLoading();
// 显示错误信息
addMessage(`错误: ${error.message}`, false, false, true);
}
}
// 绑定发送按钮点击事件
sendButton.addEventListener('click', handleSendMessage);
// 绑定回车键发送事件
userInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
handleSendMessage();
}
});
</script>
</body>
</html>