|
1
|
+ |
defmodule OpenAgentsWeb.ModelCatalogLive do
|
|
2
|
+ |
@moduledoc """
|
|
3
|
+ |
`/models`: what each lane costs, and whether the figure can be trusted,
|
|
4
|
+ |
before anything spends against it (#200, METER-001).
|
|
5
|
+ |
|
|
6
|
+ |
The contract asks that per-model pricing be readable by the CLI **and** by
|
|
7
|
+ |
the web before any spend. `GET /api/v1/models` answered the first half; a
|
|
8
|
+ |
person could not read it. This page is that endpoint's other reader: it
|
|
9
|
+ |
renders `OpenAgents.Inference.Models.catalog/0` directly, so the page and the
|
|
10
|
+ |
endpoint cannot come to disagree about what this deployment serves or what it
|
|
11
|
+ |
charges.
|
|
12
|
+ |
|
|
13
|
+ |
Same principal as the endpoint, deliberately. `GET /api/v1/models` sits in
|
|
14
|
+ |
the thread scope because the catalog names what a grant can be minted for, so
|
|
15
|
+ |
the caller who can open a thread is the caller who reads it — and the reader
|
|
16
|
+ |
of this page is the person who can open one. "Before spend" still holds:
|
|
17
|
+ |
spending here requires an account.
|
|
18
|
+ |
|
|
19
|
+ |
Two rules govern every cell. A lane with no declared rates shows the word
|
|
20
|
+ |
`Unpriced`, never `$0.00` — the deployment not knowing what a call cost is a
|
|
21
|
+ |
different fact from the call having cost nothing. And a rate this deployment
|
|
22
|
+ |
wrote to make itself run is labelled `provisional`, so nobody reads a working
|
|
23
|
+ |
figure as a price. Today that is every rate in the catalog, and the page says
|
|
24
|
+ |
so at the top rather than leaving a reader to infer it column by column.
|
|
25
|
+ |
|
|
26
|
+ |
Availability is `Models.availability/1`, the same word the endpoint
|
|
27
|
+ |
publishes, refreshed on a slow tick: a page about what a lane costs that
|
|
28
|
+ |
showed a dead lane as available would be lying about the more urgent half.
|
|
29
|
+ |
"""
|
|
30
|
+ |
|
|
31
|
+ |
use OpenAgentsWeb, :live_view
|
|
32
|
+ |
|
|
33
|
+ |
alias OpenAgents.Inference.Models
|
|
34
|
+ |
|
|
35
|
+ |
@tick_ms 15_000
|
|
36
|
+ |
|
|
37
|
+ |
@impl true
|
|
38
|
+ |
def mount(_params, _session, socket) do
|
|
39
|
+ |
if connected?(socket), do: Process.send_after(self(), :tick, @tick_ms)
|
|
40
|
+ |
|
|
41
|
+ |
{:ok,
|
|
42
|
+ |
socket
|
|
43
|
+ |
|> assign(:page_title, "Models · OpenAgents")
|
|
44
|
+ |
|> assign_catalog()}
|
|
45
|
+ |
end
|
|
46
|
+ |
|
|
47
|
+ |
@impl true
|
|
48
|
+ |
def handle_info(:tick, socket) do
|
|
49
|
+ |
Process.send_after(self(), :tick, @tick_ms)
|
|
50
|
+ |
{:noreply, assign_catalog(socket)}
|
|
51
|
+ |
end
|
|
52
|
+ |
|
|
53
|
+ |
defp assign_catalog(socket) do
|
|
54
|
+ |
models = Models.catalog()
|
|
55
|
+ |
|
|
56
|
+ |
socket
|
|
57
|
+ |
|> assign(:models, models)
|
|
58
|
+ |
|> assign(:default_id, Models.default_id())
|
|
59
|
+ |
|> assign(:billable?, Enum.any?(models, &(&1["pricing_basis"] == "declared")))
|
|
60
|
+ |
end
|
|
61
|
+ |
|
|
62
|
+ |
@impl true
|
|
63
|
+ |
def render(assigns) do
|
|
64
|
+ |
~H"""
|
|
65
|
+ |
<Layouts.app
|
|
66
|
+ |
flash={@flash}
|
|
67
|
+ |
sidebar_sections={assigns[:sidebar_sections]}
|
|
68
|
+ |
current_scope={@current_scope}
|
|
69
|
+ |
>
|
|
70
|
+ |
<main id="model-catalog" class="mx-auto w-full max-w-5xl space-y-6 px-4 py-10">
|
|
71
|
+ |
<.header>
|
|
72
|
+ |
Models
|
|
73
|
+ |
<:subtitle>
|
|
74
|
+ |
Every model this deployment serves, what each lane charges, and on whose
|
|
75
|
+ |
authority. The same catalog
|
|
76
|
+ |
<.kbd>GET /api/v1/models</.kbd>
|
|
77
|
+ |
publishes.
|
|
78
|
+ |
</:subtitle>
|
|
79
|
+ |
</.header>
|
|
80
|
+ |
|
|
81
|
+ |
<.alert
|
|
82
|
+ |
:if={!@billable?}
|
|
83
|
+ |
id="model-catalog-not-billable"
|
|
84
|
+ |
variant={:warning}
|
|
85
|
+ |
label="Nothing here is billable"
|
|
86
|
+ |
>
|
|
87
|
+ |
No lane on this deployment carries rates read off a provider's price page, so
|
|
88
|
+ |
every figure below is a working number and nothing bills from any of them.
|
|
89
|
+ |
A lane marked <strong>Unpriced</strong>
|
|
90
|
+ |
has no rates at all: its calls record no cost, and the sessions that touch it
|
|
91
|
+ |
report no total rather than a zero.
|
|
92
|
+ |
</.alert>
|
|
93
|
+ |
|
|
94
|
+ |
<.table id="model-catalog-table" rows={@models} row_id={&"model-#{&1["id"]}"}>
|
|
95
|
+ |
<:col :let={model} label="Model">
|
|
96
|
+ |
<span class="font-mono text-xs">{model["id"]}</span>
|
|
97
|
+ |
<.badge :if={model["id"] == @default_id} class="ml-2" variant={:dim}>default</.badge>
|
|
98
|
+ |
<p class="mt-0.5 text-xs text-muted-foreground">{model["provider"]} lane</p>
|
|
99
|
+ |
</:col>
|
|
100
|
+ |
|
|
101
|
+ |
<:col :let={model} label="Availability">
|
|
102
|
+ |
<.badge
|
|
103
|
+ |
id={"model-availability-#{model["id"]}"}
|
|
104
|
+ |
variant={availability_variant(model["availability"])}
|
|
105
|
+ |
>
|
|
106
|
+ |
{model["availability"]}
|
|
107
|
+ |
</.badge>
|
|
108
|
+ |
</:col>
|
|
109
|
+ |
|
|
110
|
+ |
<:col :let={model} label="Input">
|
|
111
|
+ |
<span
|
|
112
|
+ |
id={"model-input-rate-#{model["id"]}"}
|
|
113
|
+ |
class="tabular-nums"
|
|
114
|
+ |
>{rate(model, "input_per_million_tokens")}</span>
|
|
115
|
+ |
</:col>
|
|
116
|
+ |
|
|
117
|
+ |
<:col :let={model} label="Cached input">
|
|
118
|
+ |
<span class="tabular-nums">{cached_rate(model)}</span>
|
|
119
|
+ |
</:col>
|
|
120
|
+ |
|
|
121
|
+ |
<:col :let={model} label="Output">
|
|
122
|
+ |
<span
|
|
123
|
+ |
id={"model-output-rate-#{model["id"]}"}
|
|
124
|
+ |
class="tabular-nums"
|
|
125
|
+ |
>{rate(model, "output_per_million_tokens")}</span>
|
|
126
|
+ |
</:col>
|
|
127
|
+ |
|
|
128
|
+ |
<:col :let={model} label="Basis">
|
|
129
|
+ |
<.badge
|
|
130
|
+ |
id={"model-basis-#{model["id"]}"}
|
|
131
|
+ |
variant={basis_variant(model["pricing_basis"])}
|
|
132
|
+ |
data-basis={model["pricing_basis"]}
|
|
133
|
+ |
>
|
|
134
|
+ |
{model["pricing_basis"]}
|
|
135
|
+ |
</.badge>
|
|
136
|
+ |
<p
|
|
137
|
+ |
id={"model-pricing-id-#{model["id"]}"}
|
|
138
|
+ |
class="mt-0.5 font-mono text-xs text-muted-foreground"
|
|
139
|
+ |
>
|
|
140
|
+ |
{get_in(model, ["pricing", "id"]) || "unpriced"}
|
|
141
|
+ |
</p>
|
|
142
|
+ |
</:col>
|
|
143
|
+ |
|
|
144
|
+ |
<:col :let={model} label="Context">
|
|
145
|
+ |
<span class="tabular-nums">{tokens(model["context_window"])}</span>
|
|
146
|
+ |
<p class="mt-0.5 text-xs text-muted-foreground">
|
|
147
|
+ |
{tokens(model["max_output"])} out
|
|
148
|
+ |
</p>
|
|
149
|
+ |
</:col>
|
|
150
|
+ |
</.table>
|
|
151
|
+ |
|
|
152
|
+ |
<p class="text-xs text-muted-foreground">
|
|
153
|
+ |
Rates are US dollars per million tokens. Cached reads are charged at the cached
|
|
154
|
+ |
rate where a lane declares one and at the input rate where it does not, because
|
|
155
|
+ |
a lane that has not said otherwise has not offered a discount.
|
|
156
|
+ |
</p>
|
|
157
|
+ |
|
|
158
|
+ |
<section id="model-catalog-bases" class="space-y-2">
|
|
159
|
+ |
<h2 class="text-sm font-semibold">What the basis means</h2>
|
|
160
|
+ |
<.list>
|
|
161
|
+ |
<:item title="declared">
|
|
162
|
+ |
The operator entered the provider's published rates. This is the only basis
|
|
163
|
+ |
anything may bill from.
|
|
164
|
+ |
</:item>
|
|
165
|
+ |
<:item title="provisional">
|
|
166
|
+ |
Rates this deployment wrote to make itself run, or rates whose table it can
|
|
167
|
+ |
no longer resolve. A figure, not a bill.
|
|
168
|
+ |
</:item>
|
|
169
|
+ |
<:item title="unpriced">
|
|
170
|
+ |
No rates. Calls on this lane record no cost, and every surface that reads
|
|
171
|
+ |
them shows the word rather than a zero.
|
|
172
|
+ |
</:item>
|
|
173
|
+ |
</.list>
|
|
174
|
+ |
</section>
|
|
175
|
+ |
</main>
|
|
176
|
+ |
</Layouts.app>
|
|
177
|
+ |
"""
|
|
178
|
+ |
end
|
|
179
|
+ |
|
|
180
|
+ |
defp availability_variant("available"), do: :success
|
|
181
|
+ |
defp availability_variant("degraded"), do: :warning
|
|
182
|
+ |
defp availability_variant(_unavailable), do: :danger
|
|
183
|
+ |
|
|
184
|
+ |
defp basis_variant("declared"), do: :success
|
|
185
|
+ |
defp basis_variant("provisional"), do: :warning
|
|
186
|
+ |
defp basis_variant(_unpriced), do: :dim
|
|
187
|
+ |
|
|
188
|
+ |
# A lane with no rates gets the word, never a figure. `$0.00` here would be
|
|
189
|
+ |
# the most confident wrong number on the page.
|
|
190
|
+ |
defp rate(%{"pricing" => %{} = pricing}, key), do: dollars(Map.get(pricing, key))
|
|
191
|
+ |
defp rate(_model, _key), do: "Unpriced"
|
|
192
|
+ |
|
|
193
|
+ |
# A priced lane that declares no cached rate is not offering free cache
|
|
194
|
+ |
# reads: `Pricing.price/2` charges them at the input rate, so that is what
|
|
195
|
+ |
# this cell says.
|
|
196
|
+ |
defp cached_rate(%{"pricing" => %{} = pricing}) do
|
|
197
|
+ |
case Map.fetch(pricing, "cached_input_per_million_tokens") do
|
|
198
|
+ |
{:ok, value} -> dollars(value)
|
|
199
|
+ |
:error -> "at input rate"
|
|
200
|
+ |
end
|
|
201
|
+ |
end
|
|
202
|
+ |
|
|
203
|
+ |
defp cached_rate(_model), do: "Unpriced"
|
|
204
|
+ |
|
|
205
|
+ |
defp dollars(nil), do: "Unpriced"
|
|
206
|
+ |
|
|
207
|
+ |
defp dollars(microusd) when is_integer(microusd) do
|
|
208
|
+ |
"$" <> :erlang.float_to_binary(microusd / 1_000_000, decimals: 2)
|
|
209
|
+ |
end
|
|
210
|
+ |
|
|
211
|
+ |
defp tokens(count) when is_integer(count) do
|
|
212
|
+ |
count
|
|
213
|
+ |
|> Integer.to_string()
|
|
214
|
+ |
|> String.reverse()
|
|
215
|
+ |
|> String.replace(~r/(\d{3})(?=\d)/, "\\1,")
|
|
216
|
+ |
|> String.reverse()
|
|
217
|
+ |
end
|
|
218
|
+ |
|
|
219
|
+ |
defp tokens(_count), do: "—"
|
|
220
|
+ |
end
|