-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-models.js
More file actions
148 lines (122 loc) · 4.48 KB
/
test-models.js
File metadata and controls
148 lines (122 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// 測試不同 Gemini 模型的可用性
// 在瀏覽器 Console 中運行
console.log('🔍 開始測試 Gemini 模型可用性...');
// 請在這裡輸入您的 API Key
const API_KEY = 'YOUR_API_KEY_HERE';
const models = [
{ name: 'gemini-1.5-flash', description: '最穩定' },
{ name: 'gemini-1.5-pro', description: '功能強大' },
{ name: 'gemini-2.5-flash', description: '最新版本' },
{ name: 'gemini-2.5-pro', description: '最高級版本' },
{ name: 'gemini-pro', description: '舊版本' }
];
async function testModel(modelName, apiKey) {
const url = `https://generativelanguage.googleapis.com/v1beta/models/${modelName}:generateContent?key=${apiKey}`;
const requestBody = {
contents: [{
parts: [{
text: "請用繁體中文回覆「測試成功」"
}]
}],
generationConfig: {
temperature: 0.7,
maxOutputTokens: 50
}
};
try {
console.log(`\n🧪 測試模型: ${modelName}`);
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
});
console.log(`📊 狀態碼: ${response.status}`);
if (response.ok) {
const data = await response.json();
if (data.candidates && data.candidates[0] && data.candidates[0].content) {
const reply = data.candidates[0].content.parts[0].text;
console.log(`✅ ${modelName} 成功! 回覆: ${reply}`);
return { success: true, reply, model: modelName };
} else {
console.log(`❌ ${modelName} 回覆格式異常:`, data);
return { success: false, error: '回覆格式異常', model: modelName };
}
} else {
const errorText = await response.text();
console.log(`❌ ${modelName} 失敗 (${response.status}):`, errorText);
let errorType = '未知錯誤';
if (response.status === 404) {
errorType = '模型不存在或無權限';
} else if (response.status === 403) {
errorType = 'API Key 權限不足';
} else if (response.status === 400) {
errorType = '請求格式錯誤';
}
return { success: false, error: errorType, model: modelName };
}
} catch (error) {
console.log(`❌ ${modelName} 網路錯誤:`, error.message);
return { success: false, error: `網路錯誤: ${error.message}`, model: modelName };
}
}
async function testAllModels() {
if (API_KEY === 'YOUR_API_KEY_HERE' || !API_KEY) {
console.log('❌ 請先設定您的 API Key');
console.log('請修改腳本開頭的 API_KEY 變數');
return;
}
console.log('🚀 開始測試所有模型...\n');
const results = [];
for (const model of models) {
const result = await testModel(model.name, API_KEY);
results.push({ ...result, description: model.description });
// 避免請求過於頻繁
await new Promise(resolve => setTimeout(resolve, 1000));
}
console.log('\n📊 測試結果總結:');
console.log('=' .repeat(50));
const successful = results.filter(r => r.success);
const failed = results.filter(r => !r.success);
if (successful.length > 0) {
console.log(`\n✅ 可用模型 (${successful.length}):`);
successful.forEach(r => {
console.log(` 🟢 ${r.model} - ${r.description}`);
console.log(` 回覆: "${r.reply}"`);
});
}
if (failed.length > 0) {
console.log(`\n❌ 不可用模型 (${failed.length}):`);
failed.forEach(r => {
console.log(` 🔴 ${r.model} - ${r.description}`);
console.log(` 錯誤: ${r.error}`);
});
}
console.log('\n💡 建議:');
if (successful.length > 0) {
const recommended = successful[0];
console.log(`✅ 建議使用: ${recommended.model} (${recommended.description})`);
} else {
console.log('❌ 沒有可用的模型,請檢查:');
console.log(' 1. API Key 是否正確');
console.log(' 2. Google Cloud Console 中是否啟用了 Generative Language API');
console.log(' 3. 網路連接是否正常');
}
return results;
}
// 自動開始測試
if (API_KEY !== 'YOUR_API_KEY_HERE' && API_KEY) {
testAllModels();
} else {
console.log('⚠️ 請設定 API Key 後執行 testAllModels()');
}
// 導出函數供手動使用
window.geminiModelTest = {
testAllModels,
testModel,
models
};
console.log('\n📋 測試工具已載入!');
console.log('💡 設定 API Key 後會自動開始測試');
console.log('💡 或手動執行: geminiModelTest.testAllModels()');