Files
page-assist/src/libs/openai.ts

41 lines
751 B
TypeScript
Raw Normal View History

type Model = {
id: string
name?: string
2024-10-12 18:28:29 +05:30
display_name?: string
type: string
}
export const getAllOpenAIModels = async (baseUrl: string, apiKey?: string) => {
2024-10-12 18:28:29 +05:30
try {
const url = `${baseUrl}/models`
const headers = apiKey
? {
Authorization: `Bearer ${apiKey}`
2024-10-12 18:28:29 +05:30
}
: {}
2024-10-12 18:28:29 +05:30
const res = await fetch(url, {
headers
})
2024-10-12 18:28:29 +05:30
if (!res.ok) {
return []
}
2024-10-12 18:28:29 +05:30
if (baseUrl === "https://api.together.xyz/v1") {
const data = (await res.json()) as Model[]
return data.map(model => ({
id: model.id,
name: model.display_name,
}))
}
2024-10-12 18:28:29 +05:30
const data = (await res.json()) as { data: Model[] }
return data.data
} catch (e) {
console.log(e)
return []
}
}