Repository navigation
Expand file tree
/
Copy pathtest-ai-chat.html
More file actions
243 lines (216 loc) · 8.41 KB
/
Copy pathtest-ai-chat.html
File metadata and controls
243 lines (216 loc) · 8.41 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
<!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>
body {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #f5f5f5;
}
.container {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input, textarea {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
textarea {
height: 80px;
resize: vertical;
}
button {
background-color: #28a745;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
margin-right: 10px;
}
button:hover {
background-color: #218838;
}
button:disabled {
background-color: #6c757d;
cursor: not-allowed;
}
.response-area {
margin-top: 20px;
padding: 15px;
background-color: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 4px;
min-height: 200px;
white-space: pre-wrap;
font-family: 'Courier New', monospace;
overflow-y: auto;
max-height: 400px;
}
.status {
margin-top: 10px;
padding: 10px;
border-radius: 4px;
font-weight: bold;
}
.status.success {
background-color: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
.status.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.status.info {
background-color: #d1ecf1;
color: #0c5460;
border: 1px solid #bee5eb;
}
h1 {
color: #333;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<h1>🤖 AI聊天流式输出测试</h1>
<div class="form-group">
<label for="message">消息内容:</label>
<textarea id="message" placeholder="请输入你想问的问题...">你好,查询一下教师列表</textarea>
</div>
<div class="form-group">
<label for="apikey">API Key:</label>
<input type="password" id="apikey" value="sk-kuvykaxnntxkcqipwyleneauabfndimvyylryphezdevcjak" placeholder="请输入你的API Key">
</div>
<div class="form-group">
<label for="baseurl">Base URL:</label>
<input type="text" id="baseurl" value="https://api.siliconflow.cn/" placeholder="API基础URL">
</div>
<div class="form-group">
<label for="modelName">模型名称:</label>
<input type="text" id="modelName" value="deepseek-ai/DeepSeek-R1" placeholder="模型名称">
</div>
<div class="form-group">
<label for="systemPrompt">系统提示:</label>
<textarea id="systemPrompt" placeholder="系统角色设定...">你是一个有用的AI助手</textarea>
</div>
<button id="sendBtn" onclick="sendChatRequest()">💬 发送消息</button>
<button id="clearBtn" onclick="clearResponse()">🗑️ 清空响应</button>
<div id="status"></div>
<div id="response" class="response-area"></div>
</div>
<script>
async function sendChatRequest() {
const sendBtn = document.getElementById('sendBtn');
const responseDiv = document.getElementById('response');
const statusDiv = document.getElementById('status');
// 禁用按钮
sendBtn.disabled = true;
sendBtn.textContent = '发送中...';
// 清空之前的响应
responseDiv.textContent = '';
// 构建请求数据
const requestData = {
message: document.getElementById('message').value,
apikey: document.getElementById('apikey').value,
baseurl: document.getElementById('baseurl').value,
modelName: document.getElementById('modelName').value,
systemPrompt: document.getElementById('systemPrompt').value,
mcpUrls: ["http://127.0.0.1:3001","http://127.0.0.1:3002"]
};
// 验证必填字段
if (!requestData.message.trim()) {
showStatus('请输入消息内容', 'error');
sendBtn.disabled = false;
sendBtn.textContent = '💬 发送消息';
return;
}
if (!requestData.apikey.trim()) {
showStatus('请输入API Key', 'error');
sendBtn.disabled = false;
sendBtn.textContent = '💬 发送消息';
return;
}
try {
showStatus('正在连接AI服务...', 'info');
const response = await fetch('http://127.0.0.1:8080/ai/chat', {
method: 'post',
headers: {
'Content-Type': 'application/json',
'Accept': 'text/event-stream'
},
body: JSON.stringify(requestData)
});
if (!response.ok) {
throw new Error(`HTTP错误! 状态: ${response.status} - ${response.statusText}`);
}
showStatus('连接成功,正在接收AI响应...', 'success');
// 处理流式响应
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) {
showStatus('AI响应完成 ✅', 'success');
break;
}
// 解码数据并添加到响应区域
const chunk = decoder.decode(value, { stream: true });
responseDiv.textContent += chunk;
// 自动滚动到底部
responseDiv.scrollTop = responseDiv.scrollHeight;
}
} catch (error) {
console.error('请求失败:', error);
showStatus(`❌ 请求失败: ${error.message}`, 'error');
responseDiv.textContent = `错误详情:\n${error.message}\n\n请检查:\n1. 服务器是否正在运行 (http://127.0.0.1:8080)\n2. API Key是否正确\n3. 网络连接是否正常`;
} finally {
// 恢复按钮
sendBtn.disabled = false;
sendBtn.textContent = '💬 发送消息';
}
}
function clearResponse() {
document.getElementById('response').textContent = '';
document.getElementById('status').innerHTML = '';
}
function showStatus(message, type) {
const statusDiv = document.getElementById('status');
statusDiv.textContent = message;
statusDiv.className = `status ${type}`;
}
// 快捷键支持
document.getElementById('message').addEventListener('keydown', function(e) {
if (e.ctrlKey && e.key === 'Enter') {
sendChatRequest();
}
});
// 页面加载完成后的提示
window.addEventListener('load', function() {
showStatus('准备就绪,请填写必要信息后发送消息 (Ctrl+Enter快速发送)', 'info');
});
</script>
</body>
</html>