feat: added dynamic model support for openAI provider ()

This commit is contained in:
Anirban Kar 2025-02-01 15:29:54 +05:30 committed by GitHub
parent 137e268943
commit 3be18e3f9d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -20,6 +20,47 @@ export default class OpenAIProvider extends BaseProvider {
{ 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: {
model: string;
serverEnv: Env;