Skip to content

Commit

Permalink
feat: added dynamic model support for openAI provider (#1241)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodacus authored Feb 1, 2025
1 parent 137e268 commit 3be18e3
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions app/lib/modules/llm/providers/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3be18e3

Please sign in to comment.