File size: 6,223 Bytes
f54e377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f23b928
f54e377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f23b928
f54e377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f23b928
 
 
 
f54e377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import * as React from "react";
import {
  Table,
  TableHeader,
  TableBody,
  TableRow,
  TableHead,
  TableCell,
} from "@/components/ui/table";
import { MultiSelect } from "@/components/ui/multi-select";
import { FlattenedModel, Provider } from "@/App";

interface PricingTableProps {
  data: FlattenedModel[];
  providers: Provider[];
  selectedProviders: string[];
  selectedModels: string[];
  onProviderChange: (values: string[]) => void;
  onModelChange: (values: string[]) => void;
  comparisonModels: string[];
  inputTokens: number;
  outputTokens: number;
  tokenCalculation: string;
  requestSort: (key: keyof FlattenedModel) => void;
  sortConfig: {
    key: keyof FlattenedModel;
    direction: string;
  } | null;

}

export const PricingTable: React.FC<PricingTableProps> = ({
  data,
  providers,
  selectedProviders,
  selectedModels,
  onProviderChange,
  onModelChange,
  comparisonModels,
  inputTokens,
  outputTokens,
  tokenCalculation,
  requestSort,
  sortConfig,

}) => {
  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 => {
    if (comparisonPrice === 0) return "∞";
    return (((modelPrice - comparisonPrice) / comparisonPrice) * 100).toFixed(2);
  };

  const getModelsForSelectedProviders = () => {
    const flatModels = providers.flatMap((provider) =>
      provider.models.map((model) => ({
        label: model.name,
        value: model.name,
        provider: provider.provider,
      }))
    );

const filtered = flatModels.filter((m) =>
  !selectedProviders.length || selectedProviders.includes(m.provider)
);


    // Remove duplicates
    return Array.from(new Map(filtered.map((m) => [m.value, m])).values());
  };

  return (
    <Table>
      <TableHeader>
        <TableRow>
          <TableHead>
            <button onClick={() => requestSort("provider")}>
              Provider {sortConfig?.key === "provider" ? (sortConfig.direction === "ascending" ? "▲" : "▼") : null}
            </button>
          </TableHead>
          <TableHead>
            <button onClick={() => requestSort("name")}>
              Model {sortConfig?.key === "name" ? (sortConfig.direction === "ascending" ? "▲" : "▼") : null}
            </button>
          </TableHead>
          <TableHead>
            <button onClick={() => requestSort("inputPrice")}>
              Input Price (million tokens) {sortConfig?.key === "inputPrice" ? (sortConfig.direction === "ascending" ? "▲" : "▼") : null}
            </button>
          </TableHead>
          <TableHead>
            <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={providers.map((p) => ({ label: p.provider, value: p.provider }))}
              defaultValue={selectedProviders}
              onValueChange={onProviderChange}
            />
          </TableHead>
          <TableHead>
            <MultiSelect
              options={getModelsForSelectedProviders()}
              defaultValue={selectedModels}
              onValueChange={onModelChange}
            />
          </TableHead>
          <TableHead />
          <TableHead />
          <TableHead />
          {comparisonModels.flatMap((model) => [
            <TableHead key={`${model}-input`}>Input</TableHead>,
            <TableHead key={`${model}-output`}>Output</TableHead>,
          ])}
        </TableRow>
      </TableHeader>
      <TableBody>
        {data.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((model) => {
              const [comparisonProvider, comparisonModelName] = model.split(":");
              const comparisonModel = providers
                .find((p) => p.provider === comparisonProvider)
                ?.models.find((m) => m.name === comparisonModelName);

              if (!comparisonModel) return [<TableCell key="missing"></TableCell>, <TableCell key="missing2"></TableCell>];

              const inputDelta = calculateComparison(item.inputPrice, comparisonModel.inputPrice);
              const outputDelta = calculateComparison(item.outputPrice, comparisonModel.outputPrice);

              return [
                <TableCell key={`${model}-input`} className={parseFloat(inputDelta) < 0 ? "bg-green-100" : parseFloat(inputDelta) > 0 ? "bg-red-100" : ""}>
                  {`${item.provider}:${item.name}` === model ? "0.00%" : `${inputDelta}%`}
                </TableCell>,
                <TableCell key={`${model}-output`} className={parseFloat(outputDelta) < 0 ? "bg-green-100" : parseFloat(outputDelta) > 0 ? "bg-red-100" : ""}>
                  {`${item.provider}:${item.name}` === model ? "0.00%" : `${outputDelta}%`}
                </TableCell>,
              ];
            })}
          </TableRow>
        ))}
      </TableBody>
    </Table>
  );
};