-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathllm_diff_tool.html
More file actions
714 lines (630 loc) · 24.9 KB
/
llm_diff_tool.html
File metadata and controls
714 lines (630 loc) · 24.9 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
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LLM Model Response Diff Tool</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #0f0f23 0%, #1a1a2e 50%, #16213e 100%);
min-height: 100vh;
padding: 20px;
color: #e4e6ea;
}
.container {
max-width: 1400px;
margin: 0 auto;
background: rgba(26, 26, 46, 0.95);
border-radius: 20px;
padding: 30px;
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.4);
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.1);
}
h1 {
text-align: center;
color: #e4e6ea;
margin-bottom: 30px;
font-size: 2.5em;
background: linear-gradient(135deg, #64ffda, #3f51b5, #9c27b0);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
text-shadow: 0 0 30px rgba(100, 255, 218, 0.3);
}
.config-section {
margin-bottom: 30px;
padding: 20px;
background: rgba(30, 30, 51, 0.8);
border-radius: 15px;
border: 1px solid rgba(255, 255, 255, 0.1);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}
.config-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin-bottom: 15px;
}
.config-group {
display: flex;
flex-direction: column;
}
label {
font-weight: 600;
color: #64ffda;
margin-bottom: 8px;
font-size: 14px;
text-shadow: 0 0 10px rgba(100, 255, 218, 0.3);
}
input, select, textarea {
padding: 12px;
border: 2px solid rgba(255, 255, 255, 0.1);
border-radius: 8px;
font-size: 14px;
transition: all 0.3s ease;
background: rgba(15, 15, 35, 0.9);
color: #e4e6ea;
}
input:focus, select:focus, textarea:focus {
outline: none;
border-color: #64ffda;
box-shadow: 0 0 0 3px rgba(100, 255, 218, 0.2), 0 0 20px rgba(100, 255, 218, 0.1);
}
input::placeholder, textarea::placeholder {
color: #8892b0;
}
textarea {
resize: vertical;
min-height: 80px;
font-family: inherit;
}
.compare-btn {
background: linear-gradient(135deg, #64ffda, #3f51b5, #9c27b0);
color: #0f0f23;
border: none;
padding: 15px 40px;
font-size: 16px;
font-weight: 600;
border-radius: 25px;
cursor: pointer;
transition: all 0.3s ease;
display: block;
margin: 20px auto;
box-shadow: 0 5px 15px rgba(100, 255, 218, 0.3);
position: relative;
overflow: hidden;
}
.compare-btn:hover {
transform: translateY(-2px);
box-shadow: 0 8px 25px rgba(100, 255, 218, 0.4), 0 0 40px rgba(100, 255, 218, 0.2);
}
.compare-btn:disabled {
opacity: 0.6;
cursor: not-allowed;
transform: none;
}
.loading {
text-align: center;
padding: 20px;
color: #8892b0;
}
.spinner {
border: 3px solid rgba(255, 255, 255, 0.1);
border-top: 3px solid #64ffda;
border-radius: 50%;
width: 30px;
height: 30px;
animation: spin 1s linear infinite;
margin: 0 auto 10px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.results {
margin-top: 30px;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin-bottom: 30px;
}
.metric-card {
background: rgba(30, 30, 51, 0.8);
padding: 20px;
border-radius: 15px;
border: 1px solid rgba(255, 255, 255, 0.1);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}
.metric-title {
font-size: 18px;
font-weight: 600;
color: #64ffda;
margin-bottom: 15px;
text-align: center;
text-shadow: 0 0 10px rgba(100, 255, 218, 0.3);
}
.metric-item {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
padding: 8px 0;
border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}
.metric-item:last-child {
border-bottom: none;
}
.metric-label {
font-weight: 500;
color: #8892b0;
}
.metric-value {
font-weight: 600;
color: #e4e6ea;
text-shadow: 0 0 10px rgba(100, 255, 218, 0.2);
}
.responses {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
.response-card {
background: rgba(15, 15, 35, 0.9);
border-radius: 15px;
padding: 20px;
border: 1px solid rgba(255, 255, 255, 0.1);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}
.response-title {
font-size: 16px;
font-weight: 600;
color: #64ffda;
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 2px solid #64ffda;
text-shadow: 0 0 10px rgba(100, 255, 218, 0.3);
}
.response-content {
line-height: 1.6;
color: #e4e6ea;
white-space: pre-wrap;
max-height: 400px;
overflow-y: auto;
padding: 10px;
background: rgba(30, 30, 51, 0.5);
border-radius: 8px;
border: 1px solid rgba(255, 255, 255, 0.05);
}
.response-content::-webkit-scrollbar {
width: 8px;
}
.response-content::-webkit-scrollbar-track {
background: rgba(255, 255, 255, 0.05);
border-radius: 4px;
}
.response-content::-webkit-scrollbar-thumb {
background: rgba(100, 255, 218, 0.3);
border-radius: 4px;
}
.response-content::-webkit-scrollbar-thumb:hover {
background: rgba(100, 255, 218, 0.5);
}
.diff-added {
background-color: rgba(76, 175, 80, 0.3);
color: #81c784;
padding: 2px 4px;
border-radius: 3px;
box-shadow: 0 0 8px rgba(76, 175, 80, 0.2);
}
.diff-removed {
background-color: rgba(244, 67, 54, 0.3);
color: #e57373;
padding: 2px 4px;
border-radius: 3px;
box-shadow: 0 0 8px rgba(244, 67, 54, 0.2);
}
.error {
background: rgba(244, 67, 54, 0.2);
color: #ef5350;
padding: 15px;
border-radius: 8px;
border: 1px solid rgba(244, 67, 54, 0.3);
margin: 10px 0;
}
.toggle-container {
margin-top: 10px;
}
.toggle-label {
display: flex;
align-items: center;
cursor: pointer;
user-select: none;
gap: 12px;
}
.toggle-text {
color: #64ffda;
font-weight: 500;
text-shadow: 0 0 10px rgba(100, 255, 218, 0.3);
}
.toggle-switch {
position: relative;
width: 50px;
height: 24px;
background: rgba(255, 255, 255, 0.1);
border-radius: 12px;
transition: all 0.3s ease;
border: 1px solid rgba(255, 255, 255, 0.2);
}
.toggle-switch::before {
content: '';
position: absolute;
top: 2px;
left: 2px;
width: 18px;
height: 18px;
background: #8892b0;
border-radius: 50%;
transition: all 0.3s ease;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}
#highlightToggle {
display: none;
}
#highlightToggle:checked + .toggle-label .toggle-switch {
background: rgba(100, 255, 218, 0.3);
border-color: #64ffda;
box-shadow: 0 0 10px rgba(100, 255, 218, 0.3);
}
#highlightToggle:checked + .toggle-label .toggle-switch::before {
transform: translateX(26px);
background: #64ffda;
box-shadow: 0 0 8px rgba(100, 255, 218, 0.4);
}
@media (max-width: 768px) {
.config-row, .metrics, .responses {
grid-template-columns: 1fr;
}
.container {
padding: 20px;
}
h1 {
font-size: 2em;
}
}
</style>
</head>
<body>
<div class="container">
<h1>LLM Model Response Diff Tool</h1>
<div class="config-section">
<div class="config-row">
<div class="config-group">
<label for="endpoint1">Model 1 Endpoint:</label>
<input type="text" id="endpoint1" placeholder="https://api.openai.com/v1/chat/completions"
value="https://api.openai.com/v1/chat/completions">
</div>
<div class="config-group">
<label for="endpoint2">Model 2 Endpoint:</label>
<input type="text" id="endpoint2" placeholder="https://api.anthropic.com/v1/messages"
value="https://api.anthropic.com/v1/messages">
</div>
</div>
<div class="config-row">
<div class="config-group">
<label for="apikey1">Model 1 API Key:</label>
<input type="password" id="apikey1" placeholder="sk-...">
</div>
<div class="config-group">
<label for="apikey2">Model 2 API Key:</label>
<input type="password" id="apikey2" placeholder="sk-...">
</div>
</div>
<div class="config-row">
<div class="config-group">
<label for="model1">Model 1 Name:</label>
<input type="text" id="model1" placeholder="gpt-4" value="gpt-4">
</div>
<div class="config-group">
<label for="model2">Model 2 Name:</label>
<input type="text" id="model2" placeholder="claude-3-sonnet-20240229" value="claude-3-sonnet-20240229">
</div>
</div>
<div class="config-group">
<label for="prompt">Prompt:</label>
<textarea id="prompt" placeholder="Enter your prompt here...">Explain the concept of machine learning in simple terms.</textarea>
</div>
<div class="config-group">
<div class="toggle-container">
<input type="checkbox" id="highlightToggle" checked>
<label for="highlightToggle" class="toggle-label">
<span class="toggle-text">Enable Difference Highlighting</span>
<span class="toggle-switch"></span>
</label>
</div>
</div>
<button class="compare-btn" onclick="compareResponses()">Compare Responses</button>
</div>
<div id="loading" class="loading" style="display: none;">
<div class="spinner"></div>
<p>Comparing responses...</p>
</div>
<div id="results" class="results" style="display: none;">
<div class="metrics">
<div class="metric-card">
<div class="metric-title">Model 1 Metrics</div>
<div id="metrics1"></div>
</div>
<div class="metric-card">
<div class="metric-title">Model 2 Metrics</div>
<div id="metrics2"></div>
</div>
</div>
<div class="responses">
<div class="response-card">
<div class="response-title">Model 1 Response</div>
<div id="response1" class="response-content"></div>
</div>
<div class="response-card">
<div class="response-title">Model 2 Response</div>
<div id="response2" class="response-content"></div>
</div>
</div>
</div>
</div>
<script>
/**
* Main function to compare responses from two LLM models
* Validates input, makes API calls, and displays results
*/
async function compareResponses() {
// Get all form input values
const endpoint1 = document.getElementById('endpoint1').value;
const endpoint2 = document.getElementById('endpoint2').value;
const apikey1 = document.getElementById('apikey1').value;
const apikey2 = document.getElementById('apikey2').value;
const model1 = document.getElementById('model1').value;
const model2 = document.getElementById('model2').value;
const prompt = document.getElementById('prompt').value;
// Validate that all required fields are filled
if (!endpoint1 || !endpoint2 || !apikey1 || !apikey2 || !model1 || !model2 || !prompt) {
alert('Please fill in all fields');
return;
}
// Show loading state and hide previous results
document.getElementById('loading').style.display = 'block';
document.getElementById('results').style.display = 'none';
try {
// Make parallel API calls to both models for faster comparison
const [result1, result2] = await Promise.all([
callModel(endpoint1, apikey1, model1, prompt),
callModel(endpoint2, apikey2, model2, prompt)
]);
// Display the comparison results
displayResults(result1, result2, model1, model2);
} catch (error) {
console.error('Error comparing responses:', error);
displayError('Error comparing responses: ' + error.message);
} finally {
// Hide loading state regardless of success or failure
document.getElementById('loading').style.display = 'none';
}
}
/**
* Makes an API call to a specific LLM model
* @param {string} endpoint - The API endpoint URL
* @param {string} apiKey - The API key for authentication
* @param {string} model - The model identifier
* @param {string} prompt - The user prompt to send
* @returns {Promise<Object>} Response object containing content and metrics
*/
async function callModel(endpoint, apiKey, model, prompt) {
const startTime = Date.now();
// Prepare the request payload in OpenAI format
// This works for both OpenAI and Anthropic APIs with minor differences
const payload = {
model: model,
messages: [
{
role: "user",
content: prompt
}
],
max_tokens: 1000,
temperature: 0.7
};
// Make the API request
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify(payload)
});
const endTime = Date.now();
const responseTime = endTime - startTime;
// Check if the request was successful
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
// Initialize default values for response parsing
let content = '';
let promptTokens = 0;
let completionTokens = 0;
let totalTokens = 0;
// Parse response based on API format (OpenAI vs Anthropic)
if (data.choices && data.choices[0] && data.choices[0].message) {
// OpenAI format
content = data.choices[0].message.content;
if (data.usage) {
promptTokens = data.usage.prompt_tokens || 0;
completionTokens = data.usage.completion_tokens || 0;
totalTokens = data.usage.total_tokens || 0;
}
} else if (data.content && data.content[0] && data.content[0].text) {
// Anthropic format
content = data.content[0].text;
if (data.usage) {
promptTokens = data.usage.input_tokens || 0;
completionTokens = data.usage.output_tokens || 0;
totalTokens = promptTokens + completionTokens;
}
} else {
throw new Error('Unexpected response format');
}
// Return structured response object
return {
content,
responseTime,
promptTokens,
completionTokens,
totalTokens
};
}
/**
* Displays the comparison results in the UI
* @param {Object} result1 - Response data from model 1
* @param {Object} result2 - Response data from model 2
* @param {string} model1Name - Name of model 1
* @param {string} model2Name - Name of model 2
*/
function displayResults(result1, result2, model1Name, model2Name) {
// Generate and display metrics for model 1
document.getElementById('metrics1').innerHTML = `
<div class="metric-item">
<span class="metric-label">Model:</span>
<span class="metric-value">${model1Name}</span>
</div>
<div class="metric-item">
<span class="metric-label">Response Time:</span>
<span class="metric-value">${result1.responseTime}ms</span>
</div>
<div class="metric-item">
<span class="metric-label">Prompt Tokens:</span>
<span class="metric-value">${result1.promptTokens}</span>
</div>
<div class="metric-item">
<span class="metric-label">Completion Tokens:</span>
<span class="metric-value">${result1.completionTokens}</span>
</div>
<div class="metric-item">
<span class="metric-label">Total Tokens:</span>
<span class="metric-value">${result1.totalTokens}</span>
</div>
`;
// Generate and display metrics for model 2
document.getElementById('metrics2').innerHTML = `
<div class="metric-item">
<span class="metric-label">Model:</span>
<span class="metric-value">${model2Name}</span>
</div>
<div class="metric-item">
<span class="metric-label">Response Time:</span>
<span class="metric-value">${result2.responseTime}ms</span>
</div>
<div class="metric-item">
<span class="metric-label">Prompt Tokens:</span>
<span class="metric-value">${result2.promptTokens}</span>
</div>
<div class="metric-item">
<span class="metric-label">Completion Tokens:</span>
<span class="metric-value">${result2.completionTokens}</span>
</div>
<div class="metric-item">
<span class="metric-label">Total Tokens:</span>
<span class="metric-value">${result2.totalTokens}</span>
</div>
`;
// Check if difference highlighting is enabled
const highlightEnabled = document.getElementById('highlightToggle').checked;
// Display responses with or without highlighting based on toggle state
if (highlightEnabled) {
// Apply difference highlighting
const [highlighted1, highlighted2] = highlightDifferences(result1.content, result2.content);
document.getElementById('response1').innerHTML = highlighted1;
document.getElementById('response2').innerHTML = highlighted2;
} else {
// Display plain text without highlighting
document.getElementById('response1').textContent = result1.content;
document.getElementById('response2').textContent = result2.content;
}
// Show the results section
document.getElementById('results').style.display = 'block';
}
/**
* Highlights differences between two text strings by comparing words
* @param {string} text1 - First text to compare
* @param {string} text2 - Second text to compare
* @returns {Array<string>} Array containing highlighted HTML for both texts
*/
function highlightDifferences(text1, text2) {
// Split texts into words and whitespace tokens to preserve spacing
const words1 = text1.split(/(\s+)/);
const words2 = text2.split(/(\s+)/);
// Use the longer array length to handle texts of different lengths
const maxLength = Math.max(words1.length, words2.length);
let highlighted1 = '';
let highlighted2 = '';
// Compare each word position
for (let i = 0; i < maxLength; i++) {
const word1 = words1[i] || '';
const word2 = words2[i] || '';
// If words are different, highlight them
if (word1 !== word2) {
if (word1) {
// Mark as removed (red highlighting)
highlighted1 += `<span class="diff-removed">${escapeHtml(word1)}</span>`;
}
if (word2) {
// Mark as added (green highlighting)
highlighted2 += `<span class="diff-added">${escapeHtml(word2)}</span>`;
}
} else {
// Words are the same, no highlighting needed
highlighted1 += escapeHtml(word1);
highlighted2 += escapeHtml(word2);
}
}
return [highlighted1, highlighted2];
}
/**
* Escapes HTML characters to prevent XSS attacks
* @param {string} text - Text to escape
* @returns {string} HTML-escaped text
*/
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
/**
* Displays an error message in the results area
* @param {string} message - Error message to display
*/
function displayError(message) {
// Clear existing results but maintain the structure
document.getElementById('metrics1').innerHTML = '';
document.getElementById('metrics2').innerHTML = '';
document.getElementById('response1').innerHTML = '';
document.getElementById('response2').innerHTML = '';
// Display error message in the first response area
document.getElementById('response1').innerHTML = `
<div class="error">
<strong>Error:</strong> ${message}
</div>
`;
// Show the results section to display the error
document.getElementById('results').style.display = 'block';
}
</script>
</body>
</html>