-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_ui.html
178 lines (151 loc) · 6.1 KB
/
chat_ui.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat Interface</title>
<style>
/* 样式可以根据需要进行修改 */
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
.container {
max-width: 66%;
margin: 0 auto;
display: flex;
}
.chat-history {
height: 600px;
overflow-y: scroll;
border: 1px solid #ccc;
padding: 10px;
flex: 2;
}
.user-message {
margin-bottom: 10px;
}
.btn-container {
margin-top: 20px;
display: flex;
flex-direction: column;
justify-content: flex-start;
}
.btn {
display: block;
margin-bottom: 12px;
padding: 10px 20px;
padding-left: 10px;
background-color: #007bff;
color: #fff;
border: none;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<div class="chat-history" id="chat-history"></div>
<div class="btn-container">
<button class="btn" id="Download-button">Download</button>
<button class="btn" id="Upload-button">Upload</button>
<button class="btn" id="Clear-button">Clear</button>
</div>
</div>
<div style="display: flex; flex-direction: row;padding-left: 300px;padding-top: 20px">
<input type="text" id="message-input" placeholder="Type your message..." size="80px">
<button class="btn" id="send-button">Send message</button>
</div>
<script>
/* 获取发送按钮点击事件*/
var msg_history="{"
document.getElementById('send-button').addEventListener('click', function (event) {
event.preventDefault();
var message = document.getElementById('message-input').value;
var chatHistory = document.getElementById('chat-history');
/* 将用户输入的消息添加到聊天历史记录中*/
var userMessage = document.createElement('div');
userMessage.classList.add('user-message');
userMessage.textContent = "user:" + message;
chatHistory.appendChild(userMessage);
msg_history = msg_history + " \"user\": \""+message+"\""
/*向服务器发送一个请求*/
const url = "http://localhost:1181";
var data = {
role: "user",
msg: message
}
fetch(url,
{
method: "POST",
headers: {
'Content-Type': 'text/plain'
},
body: msg_history + "}\0"
})
.then(response => response.text())
.then(text => {
console.log("respoens get")
console.log("Response from server:", text);
message = text;
userMessage = document.createElement('div');
userMessage.classList.add('user-message');
userMessage.textContent = "assistant:" + message;
chatHistory.appendChild(userMessage);
message= message.substr(0, message.length - 2)
msg_history = msg_history + ", \"assistant\":\"" + message + "\","
// 在这里处理来自服务器的文本响应
})
.catch(error => {
console.error("myError:" + error);
// 在这里处理错误情况
});
/* 清空消息输入框*/
document.getElementById('message-input').value = '';
/* 滚动到最底部*/
chatHistory.scrollTop = chatHistory.scrollHeight;
});
// Download-button点击事件
document.getElementById('Download-button').addEventListener('click', function () {
// 处理Download-button的逻辑
var chatHistory = document.getElementById('chat-history');
// 在聊天历史记录中添加Download-button的消息
var button1Message = document.createElement('div');
button1Message.classList.add('user-message');
button1Message.textContent = 'Download';
chatHistory.appendChild(button1Message);
// 滚动到最底部
chatHistory.scrollTop = chatHistory.scrollHeight;
});
// Upload-button点击事件
document.getElementById('Upload-button').addEventListener('click', function () {
// 处理Upload-button的逻辑
var chatHistory = document.getElementById('chat-history');
// 在聊天历史记录中添加Upload-button的消息
var button2Message = document.createElement('div');
button2Message.classList.add('user-message');
button2Message.textContent = 'Upload';
chatHistory.appendChild(button2Message);
// 滚动到最底部
chatHistory.scrollTop = chatHistory.scrollHeight;
});
// Clear-button点击事件
document.getElementById('Clear-button').addEventListener('click', function () {
// 处理unregistered-button的逻辑
var chatHistory = document.getElementById('chat-history');
// 在聊天历史记录中添加Clear-button的消息
var unregisteredButtonMessage = document.createElement('div');
unregisteredButtonMessage.classList.add('Clear-button');
msg_history = "{"
while (chatHistory.firstChild) {
chatHistory.removeChild(chatHistory.firstChild);
}
unregisteredButtonMessage.textContent = 'Clear';
chatHistory.appendChild(unregisteredButtonMessage);
// 滚动到最底部
chatHistory.scrollTop = chatHistory.scrollHeight;
});
</script>
</body>
</html>