(baseUrl?: string, apiKey?: string, openAiHeaders?: Record<string, string>)
| 539 | } |
| 540 | |
| 541 | export async function getOpenAiModels(baseUrl?: string, apiKey?: string, openAiHeaders?: Record<string, string>) { |
| 542 | try { |
| 543 | if (!baseUrl) { |
| 544 | return [] |
| 545 | } |
| 546 | |
| 547 | // Trim whitespace from baseUrl to handle cases where users accidentally include spaces |
| 548 | const trimmedBaseUrl = baseUrl.trim() |
| 549 | |
| 550 | if (!URL.canParse(trimmedBaseUrl)) { |
| 551 | return [] |
| 552 | } |
| 553 | |
| 554 | const config: Record<string, any> = {} |
| 555 | const headers: Record<string, string> = { |
| 556 | ...DEFAULT_HEADERS, |
| 557 | ...(openAiHeaders || {}), |
| 558 | } |
| 559 | |
| 560 | if (apiKey) { |
| 561 | headers["Authorization"] = `Bearer ${apiKey}` |
| 562 | } |
| 563 | |
| 564 | if (Object.keys(headers).length > 0) { |
| 565 | config["headers"] = headers |
| 566 | } |
| 567 | |
| 568 | const response = await axios.get(`${trimmedBaseUrl}/models`, config) |
| 569 | const modelsArray = response.data?.data?.map((model: any) => model.id) || [] |
| 570 | return [...new Set<string>(modelsArray)] |
| 571 | } catch (error) { |
| 572 | return [] |
| 573 | } |
| 574 | } |
no test coverage detected