Repository navigation
Expand file tree
/
Copy pathtest-stream.html
More file actions
230 lines (204 loc) · 7.68 KB
/
Copy pathtest-stream.html
File metadata and controls
230 lines (204 loc) · 7.68 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
<!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: 800px;
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: 100px;
resize: vertical;
}
button {
background-color: #007bff;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background-color: #0056b3;
}
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: monospace;
}
.status {
margin-top: 10px;
padding: 10px;
border-radius: 4px;
}
.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;
}
</style>
</head>
<body>
<div class="container">
<h1>AI流式输出测试</h1>
<div class="form-group">
<label for="userId">用户ID:</label>
<input type="text" id="userId" value="test-user-001">
</div>
<div class="form-group">
<label for="sessionName">会话名称:</label>
<input type="text" id="sessionName" value="test-session">
</div>
<div class="form-group">
<label for="userMessage">用户消息:</label>
<textarea id="userMessage" placeholder="请输入你的消息...">你好,请介绍一下你自己</textarea>
</div>
<div class="form-group">
<label for="apikey">API Key:</label>
<input type="text" id="apikey" value="your-api-key-here">
</div>
<div class="form-group">
<label for="baseurl">Base URL:</label>
<input type="text" id="baseurl" value="https://api.openai.com/v1">
</div>
<div class="form-group">
<label for="modelName">模型名称:</label>
<input type="text" id="modelName" value="gpt-3.5-turbo">
</div>
<div class="form-group">
<label for="systemPrompt">系统提示:</label>
<textarea id="systemPrompt">你是一个有用的AI助手</textarea>
</div>
<button id="sendBtn" onclick="sendRequest()">发送请求</button>
<button id="clearBtn" onclick="clearResponse()">清空响应</button>
<div id="status"></div>
<div id="response" class="response-area"></div>
</div>
<script>
async function sendRequest() {
const sendBtn = document.getElementById('sendBtn');
const responseDiv = document.getElementById('response');
const statusDiv = document.getElementById('status');
// 禁用按钮
sendBtn.disabled = true;
sendBtn.textContent = '发送中...';
// 清空之前的响应
responseDiv.textContent = '';
// 构建请求数据
const requestData = {
userId: document.getElementById('userId').value,
sessionName: document.getElementById('sessionName').value,
parentId: "-1",
userMessage: document.getElementById('userMessage').value,
contextStartIdx: "0",
contextEndIdx: "10",
message: document.getElementById('userMessage').value,
apikey: document.getElementById('apikey').value,
baseurl: document.getElementById('baseurl').value,
modelName: document.getElementById('modelName').value,
systemPrompt: document.getElementById('systemPrompt').value
};
try {
showStatus('正在连接服务器...', 'info');
const response = await fetch('http://127.0.0.1:8080/user/conversation/add', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'text/event-stream'
},
body: JSON.stringify(requestData)
});
if (!response.ok) {
throw new Error(`HTTP错误! 状态: ${response.status}`);
}
showStatus('连接成功,正在接收数据...', 'success');
// 处理流式响应
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const { done, value } = await reader.read();
if (done) {
showStatus('数据接收完成', '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 = `错误: ${error.message}`;
} finally {
// 恢复按钮
sendBtn.disabled = false;
sendBtn.textContent = '发送请求';
}
}
function clearResponse() {
document.getElementById('response').textContent = '';
document.getElementById('status').textContent = '';
}
function showStatus(message, type) {
const statusDiv = document.getElementById('status');
statusDiv.textContent = message;
statusDiv.className = `status ${type}`;
}
// 回车键发送
document.getElementById('userMessage').addEventListener('keydown', function(e) {
if (e.ctrlKey && e.key === 'Enter') {
sendRequest();
}
});
</script>
</body>
</html>