-
Notifications
You must be signed in to change notification settings - Fork 446
Expand file tree
/
Copy pathmodels-with-context.ts
More file actions
193 lines (160 loc) · 4.57 KB
/
models-with-context.ts
File metadata and controls
193 lines (160 loc) · 4.57 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
import "server-only";
import { gateway } from "ai";
import { filterDisabledModels } from "./model-availability";
import type { AvailableModel, AvailableModelCost } from "./models";
const MODELS_DEV_URL = "https://models.dev/api.json";
const MODELS_DEV_TIMEOUT_MS = 750;
type GatewayModel = Awaited<
ReturnType<typeof gateway.getAvailableModels>
>["models"][number];
interface ModelsDevMetadata {
contextWindow?: number;
cost?: AvailableModelCost;
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null;
}
function toOptionalNumber(value: unknown): number | undefined {
return typeof value === "number" && Number.isFinite(value)
? value
: undefined;
}
function getModelsDevCostTier(
value: unknown,
): AvailableModelCost | AvailableModelCost["context_over_200k"] | undefined {
if (!isRecord(value)) {
return undefined;
}
const input = toOptionalNumber(value.input);
const output = toOptionalNumber(value.output);
const cacheRead = toOptionalNumber(value.cache_read);
if (
typeof input !== "number" &&
typeof output !== "number" &&
typeof cacheRead !== "number"
) {
return undefined;
}
return {
input,
output,
cache_read: cacheRead,
};
}
function getModelsDevCost(value: unknown): AvailableModelCost | undefined {
if (!isRecord(value)) {
return undefined;
}
const baseCost = getModelsDevCostTier(value);
const contextOver200k = getModelsDevCostTier(value.context_over_200k);
if (!baseCost && !contextOver200k) {
return undefined;
}
return {
...baseCost,
...(contextOver200k ? { context_over_200k: contextOver200k } : {}),
};
}
function getModelsDevMetadataMap(
data: unknown,
): Map<string, ModelsDevMetadata> {
const metadataMap = new Map<string, ModelsDevMetadata>();
if (!isRecord(data)) {
return metadataMap;
}
for (const [providerKey, providerValue] of Object.entries(data)) {
if (!isRecord(providerValue)) {
continue;
}
const modelsValue = providerValue.models;
if (!isRecord(modelsValue)) {
continue;
}
for (const [modelKey, modelValue] of Object.entries(modelsValue)) {
if (!isRecord(modelValue)) {
continue;
}
const idValue = modelValue.id;
const rawId = typeof idValue === "string" ? idValue : modelKey;
const modelId = rawId.includes("/") ? rawId : `${providerKey}/${rawId}`;
const limitValue = modelValue.limit;
const contextWindow = isRecord(limitValue)
? toOptionalNumber(limitValue.context)
: undefined;
const cost = getModelsDevCost(modelValue.cost);
if (
(typeof contextWindow !== "number" || contextWindow <= 0) &&
cost === undefined
) {
continue;
}
metadataMap.set(modelId, {
contextWindow:
typeof contextWindow === "number" && contextWindow > 0
? contextWindow
: undefined,
cost,
});
}
}
return metadataMap;
}
async function fetchModelsDevMetadataMap(): Promise<
Map<string, ModelsDevMetadata>
> {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), MODELS_DEV_TIMEOUT_MS);
try {
const response = await fetch(MODELS_DEV_URL, {
signal: controller.signal,
});
if (!response.ok) {
return new Map();
}
const data: unknown = await response.json();
return getModelsDevMetadataMap(data);
} catch {
return new Map();
} finally {
clearTimeout(timeoutId);
}
}
function addModelsDevMetadata(
model: GatewayModel,
metadataMap: Map<string, ModelsDevMetadata>,
): AvailableModel {
const metadata = metadataMap.get(model.id);
if (!metadata) {
return model;
}
const nextModel: AvailableModel = { ...model };
if (
typeof metadata.contextWindow === "number" &&
metadata.contextWindow > 0
) {
nextModel.context_window = metadata.contextWindow;
}
if (metadata.cost) {
nextModel.cost = metadata.cost;
}
return nextModel;
}
export async function fetchAvailableLanguageModels(): Promise<
AvailableModel[]
> {
const { models } = await gateway.getAvailableModels();
return filterDisabledModels(
models.filter((model) => model.modelType === "language"),
);
}
export async function fetchAvailableLanguageModelsWithContext(): Promise<
AvailableModel[]
> {
const [models, modelsDevMetadataMap] = await Promise.all([
fetchAvailableLanguageModels(),
fetchModelsDevMetadataMap(),
]);
return models.map((model) =>
addModelsDevMetadata(model, modelsDevMetadataMap),
);
}