mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-05-10 06:50:53 +00:00
feat: added dynamic model support for openAI provider (#1241)
This commit is contained in:
parent
137e268943
commit
3be18e3f9d
@ -20,6 +20,47 @@ export default class OpenAIProvider extends BaseProvider {
|
|||||||
{ name: 'gpt-3.5-turbo', label: 'GPT-3.5 Turbo', provider: 'OpenAI', maxTokenAllowed: 8000 },
|
{ name: 'gpt-3.5-turbo', label: 'GPT-3.5 Turbo', provider: 'OpenAI', maxTokenAllowed: 8000 },
|
||||||
];
|
];
|
||||||
|
|
||||||
|
async getDynamicModels(
|
||||||
|
apiKeys?: Record<string, string>,
|
||||||
|
settings?: IProviderSetting,
|
||||||
|
serverEnv?: Record<string, string>,
|
||||||
|
): Promise<ModelInfo[]> {
|
||||||
|
const { apiKey } = this.getProviderBaseUrlAndKey({
|
||||||
|
apiKeys,
|
||||||
|
providerSettings: settings,
|
||||||
|
serverEnv: serverEnv as any,
|
||||||
|
defaultBaseUrlKey: '',
|
||||||
|
defaultApiTokenKey: 'OPENAI_API_KEY',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!apiKey) {
|
||||||
|
throw `Missing Api Key configuration for ${this.name} provider`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(`https://api.openai.com/v1/models`, {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${apiKey}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = (await response.json()) as any;
|
||||||
|
const staticModelIds = this.staticModels.map((m) => m.name);
|
||||||
|
|
||||||
|
const data = res.data.filter(
|
||||||
|
(model: any) =>
|
||||||
|
model.object === 'model' &&
|
||||||
|
(model.id.startsWith('gpt-') || model.id.startsWith('o') || model.id.startsWith('chatgpt-')) &&
|
||||||
|
!staticModelIds.includes(model.id),
|
||||||
|
);
|
||||||
|
|
||||||
|
return data.map((m: any) => ({
|
||||||
|
name: m.id,
|
||||||
|
label: `${m.id}`,
|
||||||
|
provider: this.name,
|
||||||
|
maxTokenAllowed: m.context_window || 32000,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
getModelInstance(options: {
|
getModelInstance(options: {
|
||||||
model: string;
|
model: string;
|
||||||
serverEnv: Env;
|
serverEnv: Env;
|
||||||
|
Loading…
Reference in New Issue
Block a user