( provider: string, apiKey?: string, apiBase?: string, )
| 216 | } |
| 217 | |
| 218 | async function fetchProviderModelsViaListModels( |
| 219 | provider: string, |
| 220 | apiKey?: string, |
| 221 | apiBase?: string, |
| 222 | ): Promise<FetchedModel[]> { |
| 223 | try { |
| 224 | const cls = LLMClasses.find((llm) => llm.providerName === provider); |
| 225 | const defaultApiBase = cls?.defaultOptions?.apiBase; |
| 226 | |
| 227 | const llm = llmFromProviderAndOptions(provider, { |
| 228 | apiKey, |
| 229 | apiBase: apiBase || defaultApiBase, |
| 230 | model: "", |
| 231 | }); |
| 232 | const modelIds = await llm.listModels(); |
| 233 | return modelIds.map((id) => ({ name: id })); |
| 234 | } catch (error: any) { |
| 235 | throw new Error( |
| 236 | `Failed to fetch models for ${provider}: ${error?.message ?? error}`, |
| 237 | ); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | export async function fetchModels( |
| 242 | provider: string, |
no test coverage detected