-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
479 lines (428 loc) · 22.4 KB
/
index.html
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=0.95">
<title>VectorVelocity</title>
<link rel="stylesheet" href="style.css">
<style>
/* Add this new style for the tokens per second display */
#tokensPerSecondDisplay {
position: fixed;
top: 60%; /* Move it below the loading indicator */
left: 50%;
transform: translate(-50%, -50%);
background-color: rgba(0, 0, 0, 0.8);
color: white;
padding: 20px;
border-radius: 10px;
z-index: 9999; /* Set it below the loading indicator */
display: none;
font-family: 'Roboto', sans-serif;
max-height: 30vh; /* Limit the height */
overflow-y: auto; /* Add scrollbar if needed */
}
</style>
</head>
<body>
<div class="heading-container">
<h1>Vector Velocity</h1>
<p>Race the APIs against each other using the same prompt and measure their speed in words (~tokens) per second.</p>
<div class="instructions">
<ol>
<p>Add your API key(s) in the .env file</p>
<p>Select your desired model(s) and provider(s) below</p>
<p>Enter a prompt in the input field ( ~150 words recommended )</p>
<p>Click "Start Race" and watch the APIs compete! ( store and race mode may take a while to start )</p>
</ol>
</div>
<p>Compare which AI API is better suited for your needs based on speed.</p>
</div>
<div class="container">
<div id="raceTrack">
<!-- Will be generated by update cars -->
</div>
<div id="modelNames">
<!-- Provider checkboxes and model selects will be dynamically added here -->
<div id="race-controls">
<div class="finish-line-control">
<label for="finishLineInput" style="font-family: 'Montserrat', sans-serif; font-weight: 600;">FINISH DISTANCE (scaled Token/s):</label>
<br><br>
<input type="number" id="finishLineInput" placeholder="Finish line distance" value="4000" min="1000" step="1000" style="font-family: 'Roboto', sans-serif;">
<div id="streamModeContainer" style="font-family: 'Open Sans', sans-serif;">
<h3 style="font-family: 'Poppins', sans-serif; font-weight: 700;">STREAM MODE</h3>
<div>
<input type="radio" id="streamModeDirect" name="streamMode" value="direct" disabled>
<label for="streamModeDirect" style="font-family: 'Lato', sans-serif; color: #999;">INSTANT: Generate and race simultaneously (Coming Soon)</label>
</div>
<div>
<input type="radio" id="streamModeSave" name="streamMode" value="save" checked>
<label for="streamModeSave" style="font-family: 'Lato', sans-serif;">STORE AND RACE: Generate response for all providers first, then race</label>
</div>
</div>
</div>
</div>
<div id="providers_models" style="font-family: 'Roboto', sans-serif;">
</div>
</div>
<div id="promptInputContainer">
<input type="text" id="promptInput" placeholder="Enter prompt (Default: Tell me about the Milky Way galaxy in 200 words')">
<div id="raceButtons">
<button id="startButton">Start Race</button>
<button id="stopButton">Stop Race</button>
</div>
</div>
</div>
<div id="loadingIndicator" class="loading-indicator">
<div class="spinner"></div>
<p>Calculating tokens...</p>
</div>
<!-- Add this new div for displaying tokens per second -->
<div id="tokensPerSecondDisplay"></div>
<script src="calculate_tokens.js"></script>
<script>
const raceTrack = document.getElementById('raceTrack');
const startButton = document.getElementById('startButton');
const stopButton = document.getElementById('stopButton');
const winnerDisplay = document.getElementById('winner');
const streamModeDirect = document.getElementById('streamModeDirect');
const streamModeSave = document.getElementById('streamModeSave');
const providers = Object.keys(API_PROVIDERS);
const cars = {};
// const finishLine = 7000;
const updateInterval = 100; // Update every 100ms
let raceActive = false;
let finishedProviders = new Set();
const finishLineInput = document.getElementById('finishLineInput');
let finishLine = parseInt(finishLineInput.value, 10); // Initialize with the default value
finishLineInput.addEventListener('change', (event) => {
finishLine = parseInt(event.target.value, 10);
console.log(`Finish line distance updated to: ${finishLine}`);
});
const imageFiles = [ 'racing-car-296772_1920.png']; // Add all available image files here
function updateCars()
{
raceTrack.innerHTML = ''; // Clear existing lanes
console.log('Providers:', providers);
console.log('API_PROVIDERS:', API_PROVIDERS);
const activeCars = providers.filter(provider => {
console.log(`Checking provider: ${provider}`);
console.log(`API_PROVIDERS[${provider}]:`, API_PROVIDERS[provider]);
console.log(`API_PROVIDERS[${provider}].active:`, API_PROVIDERS[provider] ? API_PROVIDERS[provider].active : 'undefined');
return API_PROVIDERS[provider] && API_PROVIDERS[provider].active !== false;
});
console.log('Active cars:', activeCars);
const laneHeight = 100 / activeCars.length; // Calculate lane height based on number of active cars
activeCars.forEach((provider, index) => {
console.log(`Creating car for provider: ${provider}`);
const lane = document.createElement('div');
lane.className = 'lane';
lane.style.top = `${index * laneHeight}%`;
lane.style.height = `${laneHeight}%`;
raceTrack.appendChild(lane);
const car = document.createElement('div');
car.className = 'car';
const topPosition = (index / (activeCars.length)) * 100 + 7.5; // Evenly space cars from 0 to 100
car.style.top = `${topPosition}%`;
const randomImage = imageFiles[Math.floor(Math.random() * imageFiles.length)];
car.innerHTML = `
<img src="images/${randomImage}" alt="${provider}">
<div class="car-info">
<div class="car-name">${API_PROVIDERS[provider].name}</div>
<div class="car-speed">0 t/s</div>
</div>
`;
raceTrack.appendChild(car);
cars[provider] = { element: car, position: 0, tokensPerSecond: 0, index: 0, outOfTokens: false, lanePosition: topPosition };
// Debug: Log car name, image path, and lane position
console.log(`Car created for ${provider}:`);
console.log(` Name: ${API_PROVIDERS[provider].name}`);
console.log(` Image: images/${randomImage}`);
console.log(` Lane position: ${topPosition}%`);
});
raceTrack.appendChild(document.createElement('div')).id = 'winner';
console.log('Final cars object:', cars);
// Debug: Log the number of active cars and their providers
console.log('Number of active cars:', activeCars.length);
console.log('Active providers:', activeCars);
console.log('All providers:', providers);
console.log('API_PROVIDERS:', API_PROVIDERS);
logCarLanePositions();
}
function logCarLanePositions() {
console.log('Current car lane positions:');
for (const [provider, car] of Object.entries(cars)) {
if (API_PROVIDERS[provider] && API_PROVIDERS[provider].name) {
console.log(`${API_PROVIDERS[provider].name}: ${car.lanePosition}%`);
} else {
console.log(`Unknown provider (${provider}): ${car.lanePosition}%`);
}
}
}
async function updateCarPosition(provider) {
if (cars[provider] && raceActive && !finishedProviders.has(provider) && !cars[provider].outOfTokens) {
let tokensPerSecond;
if (streamModeDirect.checked) {
tokensPerSecond = await updateTokensPerSecond(API_PROVIDERS[provider], getCurrentPrompt());
} else {
const tokensPerSecondList = getTokensPerSecondList(provider);
if (tokensPerSecondList && cars[provider].index < tokensPerSecondList.length) {
tokensPerSecond = tokensPerSecondList[cars[provider].index];
cars[provider].index++;
} else {
// Provider has run out of tokens
finishProvider(provider, true);
return;
}
}
if (tokensPerSecond) {
cars[provider].tokensPerSecond = tokensPerSecond;
cars[provider].element.querySelector('.car-speed').textContent = `${tokensPerSecond.toFixed(1)} t/s`;
if (!finishedProviders.has(provider)) {
cars[provider].element.style.transform = `rotate(${Math.random() * 10 - 5}deg)`; // Add a slight rotation for effect
}
}
setTimeout(() => updateCarPosition(provider), updateInterval);
}
}
function moveCards() {
if (raceActive) {
const trackWidth = raceTrack.offsetWidth;
for (const [provider, car] of Object.entries(cars)) {
if (car.tokensPerSecond && !finishedProviders.has(provider) && !car.outOfTokens) {
car.position += (car.tokensPerSecond / 10);
const scaledPosition = (car.position / finishLine) * (trackWidth - car.element.offsetWidth);
car.element.style.left = `${Math.min(scaledPosition, trackWidth - car.element.offsetWidth)}px`;
if (car.position > finishLine) {
console.log(`${API_PROVIDERS[provider].name} finished the race!`);
finishProvider(provider);
}
}
}
requestAnimationFrame(moveCards);
}
}
async function startRace() {
// Reset tokensPerSecondList for each provider
for (const provider in API_PROVIDERS) {
if (API_PROVIDERS[provider].tokensPerSecondList) {
API_PROVIDERS[provider].tokensPerSecondList = [];
}
}
updateCars();
Object.values(cars).forEach(car => {
car.element.style.left = '0px';
car.position = 0;
car.tokensPerSecond = 0;
car.index = 0;
car.outOfTokens = false;
});
raceActive = true;
finishedProviders.clear();
const currentPrompt = getCurrentPrompt();
console.log(`Starting race with prompt: ${currentPrompt}`);
if (streamModeSave.checked) {
showLoadingIndicator(); // Show loading indicator
await runSimultaneousComparison();
hideLoadingIndicator(); // Hide loading indicator
}
for (const provider of providers) {
if (API_PROVIDERS[provider] && API_PROVIDERS[provider].active !== false) {
updateCarPosition(provider);
}
}
moveCards();
}
function showLoadingIndicator() {
document.getElementById('loadingIndicator').style.display = 'flex';
document.getElementById('tokensPerSecondDisplay').innerHTML = ''; // Clear previous results
document.getElementById('tokensPerSecondDisplay').style.display = 'block';
}
function hideLoadingIndicator() {
document.getElementById('loadingIndicator').style.display = 'none';
document.getElementById('tokensPerSecondDisplay').style.display = 'none';
}
function finishProvider(provider, outOfTokens = false)
{
console.log(`finishProvider called for ${provider}. Out of tokens: ${outOfTokens}`);
// Check if the provider is active before adding to finishedProviders
if (API_PROVIDERS[provider] && API_PROVIDERS[provider].active !== false && !outOfTokens) {
finishedProviders.add(provider);
console.log(`Current finishedProviders: ${Array.from(finishedProviders)}`);
} else {
console.log(`Provider ${provider} is disabled, not found, or out of tokens. Not adding to finishedProviders.`);
}
if (cars[provider]) {
console.log(`Stopping animation for ${provider}'s car`);
cars[provider].element.style.animation = 'none'; // Stop the jiggling animation
if (outOfTokens) {
console.log(`${provider} is out of tokens`);
cars[provider].element.querySelector('.car-speed').textContent = 'Out of tokens';
cars[provider].outOfTokens = true;
}
} else {
console.warn(`Car not found for provider: ${provider}`);
}
// Check if this is the first provider to finish and announce the winner
if (finishedProviders.size === 1 && API_PROVIDERS[provider] && !outOfTokens)
{
console.log(`First provider to finish: ${provider}`);
const winnerDisplay = document.getElementById('winner');
winnerDisplay.textContent = `${API_PROVIDERS[provider].name} wins!`;
winnerDisplay.style.display = 'block';
winnerDisplay.style.position = 'absolute';
winnerDisplay.style.top = '10px';
winnerDisplay.style.left = '50%';
winnerDisplay.style.transform = 'translateX(-50%)';
winnerDisplay.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
winnerDisplay.style.color = '#FFD700';
winnerDisplay.style.padding = '15px';
winnerDisplay.style.border = '3px solid #FFD700';
winnerDisplay.style.borderRadius = '10px';
winnerDisplay.style.fontSize = '24px';
winnerDisplay.style.fontWeight = 'bold';
winnerDisplay.style.textShadow = '2px 2px 4px #000000';
winnerDisplay.style.boxShadow = '0 0 20px #FFD700';
winnerDisplay.style.zIndex = '1000';
winnerDisplay.style.animation = 'winnerPulse 1.5s infinite';
// Add keyframe animation
const style = document.createElement('style');
style.textContent = `
@keyframes winnerPulse {
0% { transform: translateX(-50%) scale(1); }
50% { transform: translateX(-50%) scale(1.1); }
100% { transform: translateX(-50%) scale(1); }
}
`;
document.head.appendChild(style);
console.log(`Winner announced: ${API_PROVIDERS[provider].name}`);
} else
{
console.log(`Not announcing winner. Finished providers: ${finishedProviders.size}`);
}
const activeProviders = providers.filter(p => API_PROVIDERS[p] && API_PROVIDERS[p].active !== false);
const finishedActiveProviders = Array.from(finishedProviders).filter(p => activeProviders.includes(p));
if (finishedActiveProviders.length === activeProviders.length) {
console.log('All active providers have finished. Ending race.');
endRace();
} else {
console.log(`Race continues. Finished: ${finishedActiveProviders.length}/${activeProviders.length}`);
}
}
function endRace() {
raceActive = false;
}
function stopRace() {
location.reload(); // Reload the page
}
startButton.addEventListener('click', startRace);
stopButton.addEventListener('click', stopRace);
window.addEventListener('resize', () => {
if (!raceActive) {
Object.values(cars).forEach(car => {
car.element.style.left = '0px';
car.position = 0;
});
}
});
function updateProviderNames() {
const modelNamesContainer = document.getElementById('providers_models');
modelNamesContainer.style.display = 'flex';
modelNamesContainer.style.flexDirection = 'row';
modelNamesContainer.style.flexWrap = 'wrap';
modelNamesContainer.style.gap = '10px';
// Find the maximum label width
let maxLabelWidth = 0;
Object.keys(API_PROVIDERS).forEach(providerKey => {
const provider = API_PROVIDERS[providerKey];
const tempLabel = document.createElement('label');
tempLabel.textContent = provider.name;
document.body.appendChild(tempLabel);
const labelWidth = tempLabel.offsetWidth;
document.body.removeChild(tempLabel);
maxLabelWidth = Math.max(maxLabelWidth, labelWidth);
});
Object.keys(API_PROVIDERS).forEach(providerKey => {
const provider = API_PROVIDERS[providerKey];
const providerDiv = document.createElement('div');
providerDiv.className = 'model-name';
providerDiv.style.display = 'grid';
providerDiv.style.gridTemplateColumns = `auto ${100}px auto`;
providerDiv.style.alignItems = 'center';
providerDiv.style.gap = '10px';
providerDiv.style.marginRight = '20px';
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = providerKey;
checkbox.name = 'provider';
checkbox.value = providerKey;
checkbox.checked = (providerKey !== 'TOGETHERAI' && providerKey !== 'FIREWORKS');
checkbox.addEventListener('change', (event) => {
console.log(`Checkbox for ${providerKey} changed. Checked: ${event.target.checked}`);
updateActiveProviders();
updateCars();
});
const label = document.createElement('label');
label.htmlFor = providerKey;
label.textContent = provider.name;
label.style.whiteSpace = 'nowrap';
label.style.overflow = 'hidden';
label.style.textOverflow = 'ellipsis';
const modelSelect = document.createElement('select');
modelSelect.id = `${providerKey}-model`;
modelSelect.name = `${providerKey}-model`;
provider.models.forEach(model => {
const option = document.createElement('option');
option.value = model;
option.textContent = model;
if (model === provider.selectedModel) {
option.selected = true;
}
modelSelect.appendChild(option);
});
modelSelect.addEventListener('change', (event) => {
provider.selectedModel = event.target.value;
console.log(`Selected model for ${provider.name}: ${provider.selectedModel}`);
});
providerDiv.appendChild(checkbox);
providerDiv.appendChild(label);
providerDiv.appendChild(modelSelect);
modelNamesContainer.appendChild(providerDiv);
});
}
// Add this function to update the tokens per second display
function updateTokensPerSecondDisplay(providerName, tokensPerSecond, isDone = false) {
const display = document.getElementById('tokensPerSecondDisplay');
let providerDiv = document.getElementById(`${providerName}-tps`);
if (!providerDiv) {
providerDiv = document.createElement('div');
providerDiv.id = `${providerName}-tps`;
display.appendChild(providerDiv);
}
providerDiv.textContent = `${providerName}: current t/s: ${tokensPerSecond.toFixed(2)}`;
providerDiv.style.color = isDone ? 'lime' : 'white'; // Change color to green if done
display.style.display = 'block';
}
// Add this event listener to handle the custom event
window.addEventListener('updateTokensPerSecond', (event) => {
const { providerName, tokensPerSecond, isDone } = event.detail;
updateTokensPerSecondDisplay(providerName, tokensPerSecond, isDone);
});
// Add this function to handle the prompt input
function handlePromptInput() {
const promptInput = document.getElementById('promptInput');
promptInput.addEventListener('change', (event) => {
const prompt = event.target.value;
console.log('New prompt:', prompt);
});
}
// Call updateProviderNames when the page loads
document.addEventListener('DOMContentLoaded', () => {
updateProviderNames();
updateActiveProviders();
handlePromptInput();
updateCars();
});
</script>
</body>
</html>