-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi-handlers.js
More file actions
337 lines (285 loc) · 11.7 KB
/
Copy pathapi-handlers.js
File metadata and controls
337 lines (285 loc) · 11.7 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// API handlers for different AI providers
class APIHandler {
constructor(protocol, apiUrl, apiKey) {
this.protocol = protocol;
this.apiUrl = apiUrl;
this.apiKey = apiKey;
}
async testModel(model, prompt, onProgress) {
const startTime = Date.now();
let firstTokenTime = null;
let totalTokens = 0;
let result = '';
let error = null;
try {
if (this.protocol === 'openai') {
return await this.testOpenAI(model, prompt, startTime, onProgress);
} else if (this.protocol === 'gemini') {
return await this.testGemini(model, prompt, startTime, onProgress);
} else {
throw new Error('Unsupported protocol');
}
} catch (err) {
const errorResult = {
firstTokenTime: null,
outputSpeed: 0,
result: '',
error: err.message,
status: 'failed'
};
// Call onProgress to immediately update the UI with error
if (onProgress) {
onProgress(errorResult);
}
return errorResult;
}
}
async testOpenAI(model, prompt, startTime, onProgress) {
const response = await fetch(this.apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
},
body: JSON.stringify({
model: model,
messages: [{
role: 'user',
content: prompt
}],
stream: true,
max_tokens: 1000
})
});
if (!response.ok) {
const errorData = await response.text();
throw new Error(`HTTP ${response.status}: ${errorData}`);
}
return await this.processStreamResponse(response, startTime, onProgress);
}
async testGemini(model, prompt, startTime, onProgress) {
// Gemini API endpoint construction
let baseUrl = this.apiUrl;
if (baseUrl.endsWith('/')) {
baseUrl = baseUrl.slice(0, -1);
}
// Smart URL handling:
// 1. If URL contains '/models', extract the base part before it
// 2. If URL doesn't have version (v1/v1beta), add v1beta default
let endpointBase = baseUrl;
if (baseUrl.includes('/models')) {
endpointBase = baseUrl.substring(0, baseUrl.indexOf('/models'));
} else if (!baseUrl.match(/\/v1(beta)?$/)) {
endpointBase = `${baseUrl}/v1beta`;
}
const url = `${endpointBase}/models/${model}:streamGenerateContent?alt=sse`;
const headers = {
'Content-Type': 'application/json',
'x-goog-api-key': this.apiKey
};
const bodyData = {
contents: [{
parts: [{
text: prompt || ''
}],
role: 'user'
}],
generationConfig: {
maxOutputTokens: 1000
}
};
console.log('[Gemini Test] Request:', { url, headers, body: bodyData });
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify(bodyData)
});
if (!response.ok) {
const errorData = await response.text();
throw new Error(`HTTP ${response.status}: ${errorData}`);
}
return await this.processGeminiStreamResponse(response, startTime, onProgress);
}
async processStreamResponse(response, startTime, onProgress) {
const reader = response.body.getReader();
const decoder = new TextDecoder();
let firstTokenTime = null;
let totalTokens = 0;
let result = '';
let buffer = '';
let finishReason = null;
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (line.startsWith('data: ')) {
const data = line.slice(6);
if (data === '[DONE]') continue;
try {
const parsed = JSON.parse(data);
const content = parsed.choices?.[0]?.delta?.content;
const currentFinishReason = parsed.choices?.[0]?.finish_reason;
// 记录finish_reason
if (currentFinishReason) {
finishReason = currentFinishReason;
}
if (content) {
if (firstTokenTime === null) {
firstTokenTime = Date.now() - startTime;
}
result += content;
totalTokens += this.estimateTokens(content);
const outputSpeed = totalTokens / ((Date.now() - startTime) / 1000);
onProgress({
firstTokenTime,
outputSpeed: outputSpeed.toFixed(2),
result,
error: null,
status: 'testing'
});
}
} catch (e) {
// Skip invalid JSON lines
continue;
}
}
}
}
const totalTime = (Date.now() - startTime) / 1000;
const finalOutputSpeed = totalTokens / totalTime;
// 检查finish_reason,'stop'和'length'都视为正常完成
const validFinishReasons = ['stop', 'length'];
const isSuccess = validFinishReasons.includes(finishReason);
const status = isSuccess ? 'completed' : 'failed';
const error = isSuccess ? null : `Invalid finish_reason: ${finishReason || 'missing'}`;
return {
firstTokenTime,
outputSpeed: finalOutputSpeed.toFixed(2),
result,
error,
status
};
} finally {
reader.releaseLock();
}
}
async processGeminiStreamResponse(response, startTime, onProgress) {
const reader = response.body.getReader();
const decoder = new TextDecoder();
let firstTokenTime = null;
let totalTokens = 0;
let result = '';
let buffer = '';
let finishReason = null;
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (line.trim()) {
let data = line.trim();
// Handle SSE format
if (data.startsWith('data: ')) {
data = data.slice(6);
}
// Skip empty or invalid lines
if (!data || data === '[DONE]') continue;
try {
const parsed = JSON.parse(data);
const content = parsed.candidates?.[0]?.content?.parts?.[0]?.text;
const currentFinishReason = parsed.candidates?.[0]?.finishReason;
// 记录finish_reason
if (currentFinishReason) {
finishReason = currentFinishReason;
}
if (content) {
if (firstTokenTime === null) {
firstTokenTime = Date.now() - startTime;
}
result += content;
totalTokens += this.estimateTokens(content);
const outputSpeed = totalTokens / ((Date.now() - startTime) / 1000);
onProgress({
firstTokenTime,
outputSpeed: outputSpeed.toFixed(2),
result,
error: null,
status: 'testing'
});
}
} catch (e) {
// Skip invalid JSON lines
continue;
}
}
}
}
const totalTime = (Date.now() - startTime) / 1000;
const finalOutputSpeed = totalTokens / totalTime;
// 检查finish_reason,Gemini的正常完成状态包括'STOP'和'MAX_TOKENS'
const validFinishReasons = ['STOP', 'MAX_TOKENS'];
const isSuccess = validFinishReasons.includes(finishReason);
const status = isSuccess ? 'completed' : 'failed';
const error = isSuccess ? null : `Invalid finish_reason: ${finishReason || 'missing'}`;
return {
firstTokenTime,
outputSpeed: finalOutputSpeed.toFixed(2),
result,
error,
status
};
} finally {
reader.releaseLock();
}
}
// Simple token estimation (rough approximation)
estimateTokens(text) {
// Rough estimation: 1 token ≈ 4 characters for English
// This is a simplified approach, real tokenization is more complex
return Math.ceil(text.length / 4);
}
// Validate API configuration
static validateConfig(protocol, apiUrl, apiKey, models, prompts) {
const errors = [];
if (!protocol) {
errors.push('Protocol is required');
}
if (!apiUrl || !this.isValidUrl(apiUrl)) {
errors.push('Valid API URL is required');
}
if (!apiKey || apiKey.trim().length === 0) {
errors.push('API Key is required');
}
if (!models || models.length === 0) {
errors.push('At least one model is required');
}
if (!prompts || prompts.length === 0) {
errors.push('At least one prompt is required');
}
// No limit on number of prompts
return errors;
}
static isValidUrl(string) {
try {
new URL(string);
return true;
} catch (_) {
return false;
}
}
// Get default API URLs for different protocols
static getDefaultApiUrl(protocol) {
const defaults = {
'openai': 'https://api.openai.com/v1/chat/completions',
'gemini': 'https://generativelanguage.googleapis.com'
};
return defaults[protocol] || '';
}
}