codacus commited on
Commit
3be18e3
·
unverified ·
1 Parent(s): 137e268

feat: added dynamic model support for openAI provider (#1241)

Browse files
app/lib/modules/llm/providers/openai.ts CHANGED
@@ -20,6 +20,47 @@ export default class OpenAIProvider extends BaseProvider {
20
  { name: 'gpt-3.5-turbo', label: 'GPT-3.5 Turbo', provider: 'OpenAI', maxTokenAllowed: 8000 },
21
  ];
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  getModelInstance(options: {
24
  model: string;
25
  serverEnv: Env;
 
20
  { name: 'gpt-3.5-turbo', label: 'GPT-3.5 Turbo', provider: 'OpenAI', maxTokenAllowed: 8000 },
21
  ];
22
 
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
+
64
  getModelInstance(options: {
65
  model: string;
66
  serverEnv: Env;