Skip to content

Commit ba58253

Browse files
authored
Merge branch 'stackblitz-labs:main' into FEAT_BoltDYI_NEW_SETTINGS_UI_V3
2 parents b509673 + 3be18e3 commit ba58253

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

app/lib/modules/llm/providers/openai.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,47 @@ export default class OpenAIProvider extends BaseProvider {
2020
{ name: 'gpt-3.5-turbo', label: 'GPT-3.5 Turbo', provider: 'OpenAI', maxTokenAllowed: 8000 },
2121
];
2222

23+
async getDynamicModels(
24+
apiKeys?: Record<string, string>,
25+
settings?: IProviderSetting,
26+
serverEnv?: Record<string, string>,
27+
): Promise<ModelInfo[]> {
28+
const { apiKey } = this.getProviderBaseUrlAndKey({
29+
apiKeys,
30+
providerSettings: settings,
31+
serverEnv: serverEnv as any,
32+
defaultBaseUrlKey: '',
33+
defaultApiTokenKey: 'OPENAI_API_KEY',
34+
});
35+
36+
if (!apiKey) {
37+
throw `Missing Api Key configuration for ${this.name} provider`;
38+
}
39+
40+
const response = await fetch(`https://api.openai.com/v1/models`, {
41+
headers: {
42+
Authorization: `Bearer ${apiKey}`,
43+
},
44+
});
45+
46+
const res = (await response.json()) as any;
47+
const staticModelIds = this.staticModels.map((m) => m.name);
48+
49+
const data = res.data.filter(
50+
(model: any) =>
51+
model.object === 'model' &&
52+
(model.id.startsWith('gpt-') || model.id.startsWith('o') || model.id.startsWith('chatgpt-')) &&
53+
!staticModelIds.includes(model.id),
54+
);
55+
56+
return data.map((m: any) => ({
57+
name: m.id,
58+
label: `${m.id}`,
59+
provider: this.name,
60+
maxTokenAllowed: m.context_window || 32000,
61+
}));
62+
}
63+
2364
getModelInstance(options: {
2465
model: string;
2566
serverEnv: Env;

0 commit comments

Comments
 (0)