-
Notifications
You must be signed in to change notification settings - Fork 2
/
calculate_tokens.js
391 lines (323 loc) · 13.3 KB
/
calculate_tokens.js
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Import required modules
const dotenv = require('dotenv');
const fs = require('fs');
const path = require('path');
// Load environment variables
dotenv.config();
// Read API Provider configurations from JSON file
const API_PROVIDER_OG = JSON.parse(fs.readFileSync(path.join(__dirname, 'api_providers.json'), 'utf8'));
// Dynamically import makeApiCall functions
for (const provider in API_PROVIDER_OG) {
const makeApiCallFunctionName = API_PROVIDER_OG[provider].makeApiCall;
API_PROVIDER_OG[provider].makeApiCall = eval(makeApiCallFunctionName);
}
// Replace environment variable placeholders with actual values
for (const provider in API_PROVIDER_OG) {
if (API_PROVIDER_OG[provider].apiKey.startsWith('process.env.')) {
const envVarName = API_PROVIDER_OG[provider].apiKey.replace('process.env.', '');
API_PROVIDER_OG[provider].apiKey = process.env[envVarName];
}
}
// Create a copy of the original API providers
let API_PROVIDERS = { ...API_PROVIDER_OG };
// Function to update active providers based on checkbox state
function updateActiveProviders() {
const activeProviders = Array.from(document.querySelectorAll('input[name="provider"]:checked')).map(checkbox => checkbox.value);
console.log('Active providers:', activeProviders);
// Reset API_PROVIDERS to the original state
API_PROVIDERS = { ...API_PROVIDER_OG };
// Remove providers that are not active
for (const provider in API_PROVIDERS) {
if (!activeProviders.includes(provider)) {
console.log(`Removing provider: ${provider}`);
delete API_PROVIDERS[provider];
} else {
console.log(`Keeping provider: ${provider}`);
}
}
console.log('Updated API_PROVIDERS:', Object.keys(API_PROVIDERS));
}
// Generic function to make API calls
async function makeApiCall(provider, prompt) {
const headers = {
'Authorization': `Bearer ${provider.apiKey}`,
'Content-Type': 'application/json'
};
const payload = {
model: provider.selectedModel,
messages: [
{ role: 'system', content: 'You are a helpful assistant' },
{ role: 'user', content: prompt }
],
stream: true
};
const response = await fetch(provider.apiUrl, {
method: 'POST',
headers: headers,
body: JSON.stringify(payload)
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.body.getReader();
}
// Specific API call functions
async function makeGroqApiCall(prompt) {
return makeApiCall(API_PROVIDERS.GROQ, prompt);
}
async function makeSambanovaApiCall(prompt) {
return makeApiCall(API_PROVIDERS.SAMBANOVA, prompt);
}
async function makeCustomApiCall(prompt) {
return makeApiCall(API_PROVIDERS.CUSTOM, prompt);
}
async function makeNvidiaApiCall(prompt) {
const headers = {
'Authorization': `Bearer ${API_PROVIDERS.NVIDIA.apiKey}`,
'Content-Type': 'application/json'
};
const payload = {
model: API_PROVIDERS.NVIDIA.selectedModel,
messages: [
{ role: 'system', content: 'You are a helpful assistant' },
{ role: 'user', content: prompt }
],
stream: true,
max_tokens: 1024,
presence_penalty: 0,
frequency_penalty: 0,
top_p: 0.7,
temperature: 0.2
};
const response = await fetch(API_PROVIDERS.NVIDIA.apiUrl, {
method: 'POST',
headers: headers,
body: JSON.stringify(payload)
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.body.getReader();
}
async function makeTogetherAIApiCall(prompt) {
const headers = {
'Authorization': `Bearer ${API_PROVIDERS.TOGETHERAI.apiKey}`,
'Content-Type': 'application/json'
};
const payload = {
model: API_PROVIDERS.TOGETHERAI.selectedModel,
messages: [
{ role: 'system', content: 'You are a helpful assistant' },
{ role: 'user', content: prompt }
],
stream: true
};
const response = await fetch(API_PROVIDERS.TOGETHERAI.apiUrl, {
method: 'POST',
headers: headers,
body: JSON.stringify(payload)
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.body.getReader();
}
async function makeFireworksAIApiCall(prompt) {
const headers = {
'Authorization': `Bearer ${API_PROVIDERS.FIREWORKS.apiKey}`,
'Content-Type': 'application/json'
};
const payload = {
model: API_PROVIDERS.FIREWORKS.selectedModel,
messages: [
{ role: 'user', content: prompt }
],
stream: true,
temperature: 1,
top_p: 1,
top_k: 50
};
const response = await fetch(API_PROVIDERS.FIREWORKS.apiUrl, {
method: 'POST',
headers: headers,
body: JSON.stringify(payload)
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.body.getReader();
}
// Function to stream tokens per second
async function* streamTokensPerSecond(provider, prompt) {
try {
const reader = await provider.makeApiCall(prompt);
const decoder = new TextDecoder();
let startTime = Date.now();
let tokenCount = 0;
let buffer = '';
let lastYieldTime = startTime;
const yieldInterval = 33; // Yield every 33ms
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).trim();
if (data === '[DONE]') break;
try {
const jsonData = JSON.parse(data);
const content = jsonData.choices[0]?.delta?.content || '';
if (content) {
tokenCount += content.trim().split(/\s+/).filter(word => word.length > 0).length;
const currentTime = Date.now();
const elapsedTime = (currentTime - startTime) / 1000;
if (currentTime - lastYieldTime >= yieldInterval) {
const tokensPerSecond = tokenCount / elapsedTime;
console.log('tokens:', tokenCount, 'time:', elapsedTime.toFixed(3), 't/s:', tokensPerSecond.toFixed(2));
provider.tokensPerSecondList.push(tokensPerSecond);
yield tokensPerSecond;
lastYieldTime = currentTime;
// Update the UI with the current tokens per second
updateTokensPerSecondUI(provider.name, tokensPerSecond);
}
}
} catch (parseError) {
console.warn('Error parsing JSON:', parseError);
}
}
}
}
} catch (error) {
console.error('Error:', error);
yield 0;
} finally {
// Update the UI one last time with isDone set to true
updateTokensPerSecondUI(provider.name, provider.tokensPerSecondList[provider.tokensPerSecondList.length - 1], true);
}
}
// Function to update the UI with tokens per second
function updateTokensPerSecondUI(providerName, tokensPerSecond, isDone = false) {
const event = new CustomEvent('updateTokensPerSecond', {
detail: { providerName, tokensPerSecond, isDone }
});
window.dispatchEvent(event);
}
// Function to stream responses from all API providers concurrently
async function streamAllProviders(prompt) {
const currentPrompt = getCurrentPrompt();
console.log(`Using prompt: ${currentPrompt}`);
const providerStreams = Object.values(API_PROVIDERS).map(provider => ({
name: provider.name,
stream: streamTokensPerSecond(provider, currentPrompt)
}));
const results = {};
const startTime = Date.now();
const handleStream = async ({ name, stream }) => {
let totalTokens = 0;
let lastUpdateTime = startTime;
console.log(`Starting to handle stream for ${name}`);
for await (const value of stream) {
if (value > 0) {
const provider = Object.values(API_PROVIDERS).find(p => p.name === name);
if (provider) {
provider.tokensPerSecondList.push(value);
} else {
console.warn(`Provider ${name} not found in API_PROVIDERS`);
}
}
}
console.log(`Stream handling completed for ${name}`);
const provider = Object.values(API_PROVIDERS).find(p => p.name === name);
if (provider && provider.tokensPerSecondList.length > 0) {
const avgTokensPerSecond = provider.tokensPerSecondList.reduce((acc, curr) => acc + curr, 0) / provider.tokensPerSecondList.length;
console.log(`Average tokens per second for ${name}: ${avgTokensPerSecond.toFixed(2)}`);
results[name] = { totalTokens, avgTokensPerSecond };
} else {
console.warn(`No tokens per second data available for ${name}`);
}
};
await Promise.all(providerStreams.map(handleStream));
for (const provider of Object.values(API_PROVIDERS)) {
console.log(`Tokens per second list for ${provider.name}:`, provider.tokensPerSecondList);
}
return results;
}
// Example usage of the new function
async function runSimultaneousComparison() {
const currentPrompt = getCurrentPrompt();
console.log('Starting simultaneous comparison for all providers');
console.log(`Using prompt: ${currentPrompt}`);
const results = await streamAllProviders(currentPrompt);
for (const [providerName, result] of Object.entries(results)) {
console.log(`Results for ${providerName}:`, result);
}
console.log('Finished simultaneous comparison');
return results;
}
// Function to update tokens per second for a given provider
async function updateTokensPerSecond(provider, prompt) {
const currentPrompt = getCurrentPrompt();
console.log(`Updating tokens per second for ${provider.name}`);
console.log(`Using prompt: ${currentPrompt}`);
const tokenStream = streamTokensPerSecond(provider, currentPrompt);
let totalTokensPerSecond = 0;
let count = 0;
for await (const tokensPerSecond of tokenStream) {
if (tokensPerSecond > 0) {
console.log(`Current tokens per second (${provider.name}): ${tokensPerSecond.toFixed(2)}`);
totalTokensPerSecond += tokensPerSecond;
count++;
}
}
if (count === 0) {
console.warn(`No valid tokens received for ${provider.name}. Check the API response.`);
return 0;
}
console.log(`Tokens per second list for ${provider.name}:`, provider.tokensPerSecondList);
return totalTokensPerSecond / count;
}
// Example usage
async function runTokensPerSecondComparison() {
const currentPrompt = getCurrentPrompt();
console.log(`Using prompt: ${currentPrompt}`);
const results = {};
for (const provider of Object.values(API_PROVIDERS)) {
console.log(`Starting comparison for ${provider.name}`);
results[provider.name] = await updateTokensPerSecond(provider, currentPrompt);
console.log(`Finished comparison for ${provider.name}. Result: ${results[provider.name]}`);
}
for (const [providerName, avgTokensPerSecond] of Object.entries(results)) {
console.log(`Debug - Raw value for ${providerName}:`, avgTokensPerSecond);
console.log(`Debug - Is finite: ${isFinite(avgTokensPerSecond)}, Is NaN: ${isNaN(avgTokensPerSecond)}`);
if (isFinite(avgTokensPerSecond) && !isNaN(avgTokensPerSecond) && avgTokensPerSecond > 0) {
console.log(`Average tokens per second for ${providerName}: ${avgTokensPerSecond.toFixed(2)}`);
} else {
console.log(`Average tokens per second for ${providerName}: Unable to calculate (possibly no valid tokens received)`);
}
}
return results;
}
// Function to get tokens per second list for a specific provider
function getTokensPerSecondList(providerName) {
const provider = API_PROVIDERS[providerName.toUpperCase()];
if (provider) {
return provider.tokensPerSecondList;
} else {
console.warn(`Provider ${providerName} not found.`);
return null;
}
}
// Add this new function to get the current prompt
function getCurrentPrompt() {
const promptInput = document.getElementById('promptInput');
return promptInput.value || 'Tell me about the Milky Way galaxy in 200 words';
}
// Expose necessary functions to the global scope
global.API_PROVIDERS = API_PROVIDERS;
global.getTokensPerSecondList = getTokensPerSecondList;
global.updateActiveProviders = updateActiveProviders;
global.runSimultaneousComparison = runSimultaneousComparison;
global.getCurrentPrompt = getCurrentPrompt;