File size: 16,678 Bytes
83de502
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
343b64b
7663767
 
 
 
 
343b64b
83de502
 
 
343b64b
 
 
83de502
 
 
343b64b
 
 
83de502
 
 
 
 
 
 
2798716
 
83de502
 
 
 
343b64b
 
83de502
 
343b64b
 
2798716
 
 
 
 
 
 
 
 
83de502
343b64b
2798716
 
83de502
 
 
 
 
 
a7865fc
7663767
83de502
 
7663767
 
83de502
7663767
 
83de502
7663767
67dfb22
83de502
 
 
 
 
67dfb22
 
83de502
 
 
 
67dfb22
83de502
67dfb22
a7865fc
7663767
35a1853
a7865fc
83de502
 
 
 
 
 
 
 
 
 
 
 
 
a7865fc
 
35a1853
 
 
 
83de502
35a1853
83de502
 
 
 
 
35a1853
 
 
 
 
fd6e72e
83de502
 
 
 
 
fd6e72e
c297c27
343b64b
 
 
73a4307
343b64b
 
6108f36
4f69107
 
 
343b64b
6108f36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83de502
 
 
 
343b64b
c297c27
83de502
 
 
343b64b
 
83de502
 
 
 
343b64b
c297c27
83de502
 
 
 
343b64b
 
 
c297c27
 
 
 
6108f36
343b64b
 
c297c27
6108f36
 
 
c297c27
6108f36
c297c27
6108f36
 
83de502
 
 
 
2798716
 
6108f36
 
 
 
 
af1b4ff
6108f36
 
 
 
 
83de502
 
 
 
2798716
6108f36
 
 
 
 
af1b4ff
6108f36
 
 
 
2798716
 
 
 
 
 
 
 
 
 
 
3ffa40a
2798716
 
 
 
 
 
 
 
 
343b64b
6108f36
2798716
6108f36
83de502
 
6108f36
 
 
 
 
35a1853
 
83de502
 
 
 
 
 
 
 
35a1853
 
 
a7865fc
83de502
 
 
 
 
 
 
 
35a1853
 
eda87ee
35a1853
 
2798716
83de502
 
 
 
 
 
 
35a1853
 
 
 
2798716
83de502
 
 
 
 
 
 
35a1853
 
eda87ee
2798716
 
6108f36
 
 
 
 
 
 
1ae14ee
 
83de502
 
 
 
 
 
1ae14ee
 
 
 
 
 
 
 
 
69ff6ba
83de502
 
 
 
 
 
 
 
 
 
 
 
1ae14ee
 
 
 
 
6108f36
 
 
 
 
 
 
 
 
 
83de502
 
 
 
 
 
 
 
 
4be5ad5
83de502
 
eda87ee
83de502
 
 
 
 
 
 
eda87ee
 
83de502
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73a4307
 
 
 
 
 
 
 
 
83de502
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73a4307
 
 
 
 
 
 
 
 
83de502
 
 
 
 
 
 
 
 
 
 
 
 
6108f36
 
343b64b
 
83de502
 
343b64b
83de502
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
import React, { useState, useEffect } from 'react';
import {
  Card,
  CardContent,
  CardHeader,
  CardTitle,
} from '@/components/ui/card';
import { Checkbox } from '@/components/ui/checkbox';
import { Input } from '@/components/ui/input';
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from '@/components/ui/table';
import { MultiSelect } from '@/components/ui/multi-select';
import {
  Collapsible,
  CollapsibleContent,
  CollapsibleTrigger,
} from '@/components/ui/collapsible';
import { Button } from '@/components/ui/button';
import { ChevronDown, ChevronRight } from 'lucide-react';
import { mockData } from './lib/data'; // Assuming you have this file for mock data

interface FlattenedModel extends Model {
  provider: string;
  uri: string;
}

export interface Model {
  name: string;
  inputPrice: number;
  outputPrice: number;
}

export interface Provider {
  provider: string;
  uri: string;
  models: Model[];
}

const App: React.FC = () => {
  const [data, setData] = useState<Provider[]>([]);
  const [comparisonModels, setComparisonModels] = useState<string[]>([]);
  const [inputTokens, setInputTokens] = useState<number>(1);
  const [outputTokens, setOutputTokens] = useState<number>(1);
  const [selectedProviders, setSelectedProviders] = useState<string[]>([]);
  const [selectedModels, setSelectedModels] = useState<string[]>([]);
  const [expandedProviders, setExpandedProviders] = useState<string[]>([]);
  const [tokenCalculation, setTokenCalculation] = useState<string>('million');

  const [sortConfig, setSortConfig] = useState<{
    key: keyof FlattenedModel;
    direction: string;
  } | null>(null);

  useEffect(() => {
    setData(mockData);
  }, []);

  const calculatePrice = (price: number, tokens: number): number => {
    let multiplier = 1;
    if (tokenCalculation === 'thousand') {
      multiplier = 1e-3;
    } else if (tokenCalculation === 'unit') {
      multiplier = 1e-6;
    } else if (tokenCalculation === 'billion') {
      multiplier = 1e3;
    }
    return price * tokens * multiplier;
  };



  const calculateComparison = (
    modelPrice: number,
    comparisonPrice: number
  ): string => {
    return (((modelPrice - comparisonPrice) / comparisonPrice) * 100).toFixed(2);
  };

  const flattenData = (data: Provider[]) => {
    return data.flatMap((provider) =>
      provider.models.map((model) => ({
        provider: provider.provider,
        uri: provider.uri,
        ...model,
      }))
    );
  };

  const filteredData = data
    .filter(
      (provider) =>
        selectedProviders.length === 0 ||
        selectedProviders.includes(provider.provider)
    )
    .map((provider) => ({
      ...provider,
      models: provider.models.filter(
        (model) =>
          selectedModels.length === 0 || selectedModels.includes(model.name)
      ),
    }))
    .filter((provider) => provider.models.length > 0);

  const sortedFlattenedData = React.useMemo(() => {
    let sortableData: FlattenedModel[] = flattenData(filteredData);
    if (sortConfig !== null) {
      sortableData.sort((a, b) => {
        const aValue = a[sortConfig.key];
        const bValue = b[sortConfig.key];

        if (typeof aValue === 'string' && typeof bValue === 'string') {
          return sortConfig.direction === 'ascending'
            ? aValue.localeCompare(bValue)
            : bValue.localeCompare(aValue);
        } else if (typeof aValue === 'number' && typeof bValue === 'number') {
          return sortConfig.direction === 'ascending'
            ? aValue - bValue
            : bValue - aValue;
        } else {
          return 0;
        }
      });
    }
    return sortableData;
  }, [filteredData, sortConfig]);

  const requestSort = (key: keyof FlattenedModel) => {
    let direction = 'ascending';
    if (
      sortConfig &&
      sortConfig.key === key &&
      sortConfig.direction === 'ascending'
    ) {
      direction = 'descending';
    }
    setSortConfig({ key, direction });
  };

  const toggleProviderExpansion = (provider: string) => {
    setExpandedProviders((prev) =>
      prev.includes(provider)
        ? prev.filter((p) => p !== provider)
        : [...prev, provider]
    );
  };

  return (
    <Card className="w-full max-w-6xl mx-auto">
      <CardHeader>
        <CardTitle>LLM Pricing Calculator</CardTitle>
      </CardHeader>
      <CardContent>
        <div className="mb-4">
          <p className="italic text-sm text-muted-foreground mb-4">
            <a href="https://huggingface.co/spaces/philschmid/llm-pricing" className='underline'>This is a fork of philschmid tool: philschmid/llm-pricing</a>
          </p>
          <h3 className="text-lg font-semibold mb-2">Select Comparison Models</h3>
          <div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
            {data.map((provider) => (
              <Collapsible
                key={provider.provider}
                open={expandedProviders.includes(provider.provider)}
                onOpenChange={() => toggleProviderExpansion(provider.provider)}
              >
                <CollapsibleTrigger asChild>
                  <Button variant="outline" className="w-full justify-between">
                    {provider.provider}
                    {expandedProviders.includes(provider.provider) ? (
                      <ChevronDown className="h-4 w-4" />
                    ) : (
                      <ChevronRight className="h-4 w-4" />
                    )}
                  </Button>
                </CollapsibleTrigger>
                <CollapsibleContent className="mt-2">
                  {provider.models.map((model) => (
                    <div
                      key={`${provider.provider}:${model.name}`}
                      className="flex items-center space-x-2 mb-1"
                    >
                      <Checkbox
                        id={`${provider.provider}:${model.name}`}
                        checked={comparisonModels.includes(
                          `${provider.provider}:${model.name}`
                        )}
                        onCheckedChange={(checked) => {
                          if (checked) {
                            setComparisonModels((prev) => [
                              ...prev,
                              `${provider.provider}:${model.name}`,
                            ]);
                          } else {
                            setComparisonModels((prev) =>
                              prev.filter(
                                (m) => m !== `${provider.provider}:${model.name}`
                              )
                            );
                          }
                        }}
                      />
                      <label
                        htmlFor={`${provider.provider}:${model.name}`}
                        className="text-sm font-medium text-gray-700"
                      >
                        {model.name}
                      </label>
                    </div>
                  ))}
                </CollapsibleContent>
              </Collapsible>
            ))}
          </div>
        </div>

        <div className="flex gap-4 mb-4">
          <div className="flex-1">
            <label
              htmlFor="inputTokens"
              className="block text-sm font-medium text-gray-700"
            >

              Input Tokens ({tokenCalculation})
            </label>
            <Input
              id="inputTokens"
              type="number"
              value={inputTokens}
              min={1}
              onChange={(e) => setInputTokens(Number(e.target.value))}
              className="mt-1"
            />
          </div>
          <div className="flex-1">
            <label
              htmlFor="outputTokens"
              className="block text-sm font-medium text-gray-700"
            >
              Output Tokens ({tokenCalculation})
            </label>
            <Input
              id="outputTokens"
              type="number"
              value={outputTokens}
              min={1}
              onChange={(e) => setOutputTokens(Number(e.target.value))}
              className="mt-1"
            />
          </div>
          <div className="flex-1">
            <label
              htmlFor="tokenCalculation"
              className="block text-sm font-medium text-gray-700"
            >
              Token Calculation
            </label>
            <select
              id="tokenCalculation"
              value={tokenCalculation}
              onChange={(e) => setTokenCalculation(e.target.value)}
              className="mt-1 block w-full pl-3 pr-10 py-2 text-base bg-white border focus:outline-none focus:ring-indigo-500  sm:text-sm rounded-md"
            >
              <option value="billion">Billion Tokens</option>
              <option value="million">Million Tokens</option>
              <option value="thousand">Thousand Tokens</option>
              <option value="unit">Unit Tokens</option>
            </select>

          </div>

        </div>


        <p className="italic text-sm text-muted-foreground mb-4">
          Note: If you use Amazon Bedrock or Azure prices for Anthropic, Cohere
          or OpenAI should be the same.
        </p>

        <Table>
          <TableHeader>
            <TableRow>
              <TableHead>
                <button type="button" onClick={() => requestSort('provider')}>
                  Provider{' '}
                  {sortConfig?.key === 'provider' ? (
                    sortConfig.direction === 'ascending' ? (
                      '▲'
                    ) : (
                      '▼'
                    )
                  ) : null}
                </button>
              </TableHead>
              <TableHead>
                <button type="button" onClick={() => requestSort('name')}>
                  Model{' '}
                  {sortConfig?.key === 'name' ? (
                    sortConfig.direction === 'ascending' ? (
                      '▲'
                    ) : (
                      '▼'
                    )
                  ) : null}
                </button>
              </TableHead>

              <TableHead>
                <button type="button" onClick={() => requestSort('inputPrice')}>
                  Input Price (million tokens)
                  {sortConfig?.key === 'inputPrice' ? (
                    sortConfig.direction === 'ascending' ? (
                      '▲'
                    ) : (
                      '▼'
                    )
                  ) : null}
                </button>
              </TableHead>
              <TableHead>
                <button type="button" onClick={() => requestSort('outputPrice')}>
                  Output Price (million tokens)
                  {sortConfig?.key === 'outputPrice' ? (
                    sortConfig.direction === 'ascending' ? (
                      '▲'
                    ) : (
                      '▼'
                    )
                  ) : null}
                </button>
              </TableHead>


              <TableHead>Total Price (per {tokenCalculation} tokens){' '}</TableHead>
              {comparisonModels.map((model) => (
                <TableHead key={model} colSpan={2}>
                  Compared to {model}
                </TableHead>
              ))}
            </TableRow>
            <TableRow>
              <TableHead>
                <MultiSelect
                  options={
                    data.map((provider) => ({
                      label: provider.provider,
                      value: provider.provider,
                    })) || []
                  }
                  onValueChange={setSelectedProviders}
                  defaultValue={selectedProviders}
                />
              </TableHead>
              <TableHead>
                <MultiSelect
                  options={
                    data
                      .flatMap((provider) => provider.models)
                      .map((model) => ({ label: model.name, value: model.name }))
                      .reduce(
                        (
                          acc: { label: string; value: string }[],
                          curr: { label: string; value: string }
                        ) => {
                          if (!acc.find((m) => m.value === curr.value)) {
                            acc.push(curr);
                          }
                          return acc;
                        },
                        []
                      ) || []
                  }
                  defaultValue={selectedModels}
                  onValueChange={setSelectedModels}
                />
              </TableHead>
              <TableHead />
              <TableHead />
              <TableHead />
              {comparisonModels.flatMap((model) => [
                <TableHead key={`${model}-input`}>Input</TableHead>,
                <TableHead key={`${model}-output`}>Output</TableHead>,
              ])}
            </TableRow>
          </TableHeader>
          <TableBody>
            {sortedFlattenedData.map((item) => (
              <TableRow key={`${item.provider}-${item.name}`}>
                <TableCell>
                  {' '}
                  <a href={item.uri} className="underline">
                    {item.provider}
                  </a>
                </TableCell>
                <TableCell>{item.name}</TableCell>

                <TableCell>{item.inputPrice.toFixed(2)}</TableCell>
                <TableCell>{item.outputPrice.toFixed(2)}</TableCell>

                <TableCell className="font-bold">
                  $
                  {(
                    calculatePrice(item.inputPrice, inputTokens) +
                    calculatePrice(item.outputPrice, outputTokens)
                  ).toFixed(2)}
                </TableCell>


                {comparisonModels.flatMap((comparisonModel) => {
                  const [comparisonProvider, comparisonModelName] =
                    comparisonModel.split(':');
                  const comparisonModelData = data
                    .find((p) => p.provider === comparisonProvider)
                    ?.models.find((m) => m.name === comparisonModelName)!;
                  return [
                    <TableCell
                      key={`${comparisonModel}-input`}
                      className={`${parseFloat(
                        calculateComparison(
                          item.inputPrice,
                          comparisonModelData.inputPrice
                        )
                      ) < 0
                        ? 'bg-green-100'
                        : parseFloat(
                          calculateComparison(
                            item.inputPrice,
                            comparisonModelData.inputPrice
                          )
                        ) > 0
                          ? 'bg-red-100'
                          : ''
                        }`}
                    >
                      {`${item.provider}:${item.name}` === comparisonModel
                        ? '0.00%'
                        : `${calculateComparison(
                          item.inputPrice,
                          comparisonModelData.inputPrice
                        )}%`}
                    </TableCell>,
                    <TableCell
                      key={`${comparisonModel}-output`}
                      className={`${parseFloat(
                        calculateComparison(
                          item.outputPrice,
                          comparisonModelData.outputPrice
                        )
                      ) < 0
                        ? 'bg-green-100'
                        : parseFloat(
                          calculateComparison(
                            item.outputPrice,
                            comparisonModelData.outputPrice
                          )
                        ) > 0
                          ? 'bg-red-100'
                          : ''
                        }`}
                    >
                      {`${item.provider}:${item.name}` === comparisonModel
                        ? '0.00%'
                        : `${calculateComparison(
                          item.outputPrice,
                          comparisonModelData.outputPrice
                        )}%`}
                    </TableCell>,
                  ];
                })}
              </TableRow>
            ))}
          </TableBody>
        </Table>
      </CardContent>
    </Card>
  );
};

export default App;