55 lines
1.5 KiB
TypeScript
55 lines
1.5 KiB
TypeScript
|
|
/**
|
||
|
|
* 列出可用的 Gemini 模型
|
||
|
|
* 呼叫 Google API 取得實際可用的模型列表
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { getGeminiToken } from '~/utils/storage'
|
||
|
|
|
||
|
|
const GEMINI_API_BASE = 'https://generativelanguage.googleapis.com/v1'
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 取得所有可用的模型列表
|
||
|
|
*/
|
||
|
|
export async function listAvailableModels(): Promise<Array<{
|
||
|
|
name: string
|
||
|
|
displayName: string
|
||
|
|
description: string
|
||
|
|
supportedGenerationMethods: string[]
|
||
|
|
}>> {
|
||
|
|
const token = getGeminiToken()
|
||
|
|
if (!token) {
|
||
|
|
throw new Error('Gemini API Token 未設定,請至設定頁面輸入')
|
||
|
|
}
|
||
|
|
|
||
|
|
const url = `${GEMINI_API_BASE}/models?key=${token}`
|
||
|
|
|
||
|
|
try {
|
||
|
|
const response = await fetch(url)
|
||
|
|
|
||
|
|
if (!response.ok) {
|
||
|
|
const error = await response.json().catch(() => ({ error: { message: 'Unknown error' } }))
|
||
|
|
throw new Error(error.error?.message || `API 錯誤: ${response.status}`)
|
||
|
|
}
|
||
|
|
|
||
|
|
const data = await response.json()
|
||
|
|
|
||
|
|
// 過濾出支援 generateContent 的模型
|
||
|
|
const models = (data.models || [])
|
||
|
|
.filter((model: any) =>
|
||
|
|
model.supportedGenerationMethods?.includes('generateContent')
|
||
|
|
)
|
||
|
|
.map((model: any) => ({
|
||
|
|
name: model.name.replace('models/', ''),
|
||
|
|
displayName: model.displayName || model.name,
|
||
|
|
description: model.description || '',
|
||
|
|
supportedGenerationMethods: model.supportedGenerationMethods || []
|
||
|
|
}))
|
||
|
|
|
||
|
|
return models
|
||
|
|
} catch (error) {
|
||
|
|
console.error('取得模型列表失敗:', error)
|
||
|
|
throw error
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|