-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatgpt.html
More file actions
256 lines (222 loc) · 7.65 KB
/
chatgpt.html
File metadata and controls
256 lines (222 loc) · 7.65 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<!-- 设置网页的字符编码为 UTF-8,确保中文显示正常 -->
<meta charset="UTF-8">
<!-- 设置页面视口,以适配移动端设备 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="refresh" content="0; url=https://qujiaweb.top/ai">
<!-- 设置网页标题 -->
<title>ChatGPT 接口调用示例</title>
<style>
/* 设置整个网页的字体样式和布局 */
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: linear-gradient(135deg, #e0eafc, #cfdef3);
overflow: hidden;
}
/* 聊天容器样式 */
.chat-container {
width: 90%;
max-width: 600px;
height: 90vh;
display: flex;
flex-direction: column;
background: #fff;
border-radius: 12px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
padding: 10px;
box-sizing: border-box;
}
/* 聊天记录框样式 */
.chat-box {
flex: 1;
overflow-y: auto;
padding: 15px;
background: #f9f9f9;
border-radius: 8px;
box-shadow: inset 0 4px 6px rgba(0, 0, 0, 0.1);
margin-bottom: 10px;
}
/* 聊天消息的通用样式 */
.message {
margin: 10px 0;
font-size: 16px;
line-height: 1.5;
white-space: pre-wrap;
position: relative;
}
/* 用户消息样式 */
.user-message {
text-align: right;
color: #007bff;
font-weight: bold;
}
/* AI 消息样式 */
.assistant-message {
text-align: left;
color: #333;
font-style: italic;
}
/* 删除按钮样式 */
.delete-button {
position: absolute;
top: 50%;
right: 10px;
transform: translateY(-50%);
background-color: #ff4d4d;
color: white;
border: none;
padding: 5px 10px;
font-size: 12px;
border-radius: 5px;
cursor: pointer;
display: none;
}
/* 删除按钮的悬停效果 */
.delete-button:hover {
background-color: #cc0000;
}
/* 输入框容器样式 */
.input-container {
display: flex;
align-items: center;
padding: 10px;
background: #f0f0f5;
border-radius: 8px;
}
/* 输入框样式 */
input[type="text"] {
flex: 1;
padding: 10px;
font-size: 16px;
border-radius: 20px;
border: 1px solid #ddd;
margin-right: 10px;
outline: none;
box-sizing: border-box;
}
/* 按钮样式 */
button {
padding: 10px 20px;
font-size: 16px;
border: none;
border-radius: 20px;
background-color: #007bff;
color: #fff;
cursor: pointer;
white-space: nowrap;
transition: background-color 0.3s;
}
/* 按钮的悬停效果 */
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<!-- 主容器 -->
<div class="chat-container">
<!-- 聊天记录框 -->
<div class="chat-box" id="chatBox"></div>
<!-- 输入框容器 -->
<div class="input-container">
<!-- 用户输入框 -->
<input type="text" id="userInput" placeholder="请输入您的问题..." />
<!-- 发送按钮 -->
<button onclick="sendMessage()">发送</button>
</div>
</div>
<script>
// 从 Local Storage 中获取历史记录
function getHistoryFromLocalStorage() {
const history = localStorage.getItem('chatHistory');
return history ? JSON.parse(history) : [];
}
// 将历史记录保存到 Local Storage
function saveHistoryToLocalStorage(history) {
localStorage.setItem('chatHistory', JSON.stringify(history));
}
// 加载历史聊天记录并显示到聊天框
function loadHistory() {
const chatBox = document.getElementById('chatBox');
const history = getHistoryFromLocalStorage();
history.forEach(({ role, content }) => {
addMessage(role, content);
});
}
// 添加一条聊天消息到聊天框
function addMessage(role, content) {
const chatBox = document.getElementById('chatBox');
const message = document.createElement('div');
message.className = `message ${role}-message`;
message.textContent = content;
// 创建删除按钮
const deleteButton = document.createElement('button');
deleteButton.className = 'delete-button';
deleteButton.textContent = '删除本条';
deleteButton.onclick = () => deleteMessage(message, content);
message.appendChild(deleteButton);
// 点击消息显示删除按钮
message.onclick = () => {
deleteButton.style.display = deleteButton.style.display === 'inline-block' ? 'none' : 'inline-block';
};
chatBox.appendChild(message);
}
// 删除指定消息并更新历史记录
function deleteMessage(element, content) {
const chatBox = document.getElementById('chatBox');
chatBox.removeChild(element);
const history = getHistoryFromLocalStorage();
const updatedHistory = history.filter(item => item.content !== content);
saveHistoryToLocalStorage(updatedHistory);
}
// 发送消息到接口
async function sendMessage() {
const chatBox = document.getElementById('chatBox');
const userInput = document.getElementById('userInput').value;
if (userInput.trim() === '') return;
addMessage('user', userInput);
document.getElementById('userInput').value = '';
const loadingMessage = document.createElement('div');
loadingMessage.className = 'message assistant-message';
loadingMessage.textContent = '加载中,较长文字等待时间较长';
chatBox.appendChild(loadingMessage);
const payload = {
model: "gpt-4o-mini",
messages: [{ role: "user", content: userInput }],
stream: false
};
try {
const response = await fetch("https://apiserver.alcex.cn/v1/chat/completions", {
method: "POST",
headers: {
"User-Agent": "Apifox/1.0.0 (https://apifox.com)",
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
const result = await response.json();
const content = result.choices[0].message.content;
loadingMessage.remove();
addMessage('assistant', content);
const history = getHistoryFromLocalStorage();
history.push({ role: 'user', content: userInput });
history.push({ role: 'assistant', content });
saveHistoryToLocalStorage(history);
} catch (error) {
console.error("Error:", error);
loadingMessage.textContent = '请求失败,请稍后再试。';
}
chatBox.scrollTop = chatBox.scrollHeight;
}
// 页面加载时自动加载历史记录
loadHistory();
</script>
</body>
</html>