78 lines
2.2 KiB
HTML
78 lines
2.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>Chat API</title>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
margin: 20px;
|
|
}
|
|
.container {
|
|
max-width: 800px;
|
|
margin: 0 auto;
|
|
}
|
|
textarea {
|
|
width: 100%;
|
|
height: 100px;
|
|
margin-bottom: 10px;
|
|
}
|
|
button {
|
|
padding: 10px 20px;
|
|
background-color: #007bff;
|
|
color: white;
|
|
border: none;
|
|
cursor: pointer;
|
|
}
|
|
.response {
|
|
margin-top: 20px;
|
|
padding: 15px;
|
|
background-color: #f5f5f5;
|
|
border-radius: 5px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<h1>Chat with AI</h1>
|
|
<textarea id="messageInput" placeholder="请输入您的问题..."></textarea>
|
|
<button onclick="sendMessage()">发送</button>
|
|
<div class="response" id="responseArea"></div>
|
|
</div>
|
|
|
|
<script>
|
|
async function sendMessage() {
|
|
const message = document.getElementById('messageInput').value;
|
|
const responseArea = document.getElementById('responseArea');
|
|
|
|
if (!message) {
|
|
alert('请输入消息');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch('/api/chat', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ message })
|
|
});
|
|
|
|
const data = await response.json();
|
|
if (data.status === 'success') {
|
|
responseArea.innerHTML = `<strong>回复:</strong><br>${data.response.output.text}`;
|
|
} else {
|
|
responseArea.innerHTML = '请求失败,请稍后重试';
|
|
}
|
|
} catch (error) {
|
|
console.error(error);
|
|
responseArea.innerHTML = '网络错误,请检查连接';
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|
|
|